2 * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.restconf.server.api;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.gson.stream.JsonWriter;
15 import java.io.IOException;
16 import java.io.OutputStream;
17 import java.io.OutputStreamWriter;
18 import java.nio.charset.StandardCharsets;
19 import javanet.staxutils.IndentingXMLStreamWriter;
20 import javax.xml.XMLConstants;
21 import javax.xml.stream.FactoryConfigurationError;
22 import javax.xml.stream.XMLOutputFactory;
23 import javax.xml.stream.XMLStreamException;
24 import javax.xml.stream.XMLStreamWriter;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.api.RestconfNormalizedNodeWriter;
27 import org.opendaylight.yangtools.concepts.Immutable;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
33 * A response counterpart to {@link RequestBody}. It is inherently immutable and exposes methods to write the content
34 * to an {@link OutputStream}.
37 public abstract class ReplyBody implements Immutable {
38 private static final XMLOutputFactory XML_FACTORY = XMLOutputFactory.newFactory();
39 private static final String PRETTY_PRINT_INDENT = " ";
41 private final boolean prettyPrint;
43 ReplyBody(final boolean prettyPrint) {
44 this.prettyPrint = prettyPrint;
48 * Write the content of this body as a JSON document.
50 * @param out output stream
51 * @throws IOException if an IO error occurs.
53 public final void writeJSON(final OutputStream out) throws IOException {
54 writeJSON(requireNonNull(out), prettyPrint);
57 abstract void writeJSON(OutputStream out, boolean prettyPrint) throws IOException;
60 * Write the content of this body as an XML document.
62 * @param out output stream
63 * @throws IOException if an IO error occurs.
65 public final void writeXML(final OutputStream out) throws IOException {
66 writeXML(requireNonNull(out), prettyPrint);
69 abstract void writeXML(OutputStream out, boolean prettyPrint) throws IOException;
72 public final String toString() {
73 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
76 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
77 return helper.add("prettyPrint", prettyPrint);
80 static final JsonWriter createJsonWriter(final OutputStream out, final boolean prettyPrint) {
81 final var ret = JsonWriterFactory.createJsonWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
82 ret.setIndent(prettyPrint ? PRETTY_PRINT_INDENT : "");
86 static final XMLStreamWriter createXmlWriter(final OutputStream out, final boolean prettyPrint) throws IOException {
87 final var xmlWriter = createXmlWriter(out);
88 return prettyPrint ? new IndentingXMLStreamWriter(xmlWriter) : xmlWriter;
91 private static XMLStreamWriter createXmlWriter(final OutputStream out) throws IOException {
93 return XML_FACTORY.createXMLStreamWriter(out, StandardCharsets.UTF_8.name());
94 } catch (XMLStreamException | FactoryConfigurationError e) {
95 throw new IOException(e);
99 static final void writeElements(final XMLStreamWriter xmlWriter, final RestconfNormalizedNodeWriter nnWriter,
100 final ContainerNode data) throws IOException {
101 final QName nodeType = data.name().getNodeType();
102 final String namespace = nodeType.getNamespace().toString();
104 xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, nodeType.getLocalName(), namespace);
105 xmlWriter.writeDefaultNamespace(namespace);
106 for (var child : data.body()) {
107 nnWriter.write(child);
110 xmlWriter.writeEndElement();
112 } catch (final XMLStreamException e) {
113 throw new IOException("Failed to write elements", e);