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 FormattableBody implements Immutable {
38 private static final XMLOutputFactory XML_FACTORY = XMLOutputFactory.newFactory();
39 private static final String PRETTY_PRINT_INDENT = " ";
41 private final FormatParameters format;
43 FormattableBody(final FormatParameters format) {
44 this.format = requireNonNull(format);
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 formatToJSON(final OutputStream out) throws IOException {
54 formatToJSON(requireNonNull(out), format);
57 abstract void formatToJSON(OutputStream out, FormatParameters format) 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 formatToXML(final OutputStream out) throws IOException {
66 formatToXML(requireNonNull(out), format);
69 abstract void formatToXML(OutputStream out, FormatParameters format) 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", format.prettyPrint().value());
80 public static final JsonWriter createJsonWriter(final OutputStream out, final FormatParameters format) {
81 final var ret = JsonWriterFactory.createJsonWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
82 ret.setIndent(format.prettyPrint().value() ? PRETTY_PRINT_INDENT : "");
86 public static final XMLStreamWriter createXmlWriter(final OutputStream out, final FormatParameters format)
88 final var xmlWriter = createXmlWriter(out);
89 return format.prettyPrint().value() ? new IndentingXMLStreamWriter(xmlWriter) : xmlWriter;
92 private static XMLStreamWriter createXmlWriter(final OutputStream out) throws IOException {
94 return XML_FACTORY.createXMLStreamWriter(out, StandardCharsets.UTF_8.name());
95 } catch (XMLStreamException | FactoryConfigurationError e) {
96 throw new IOException(e);
100 static final void writeElements(final XMLStreamWriter xmlWriter, final RestconfNormalizedNodeWriter nnWriter,
101 final ContainerNode data) throws IOException {
102 final QName nodeType = data.name().getNodeType();
103 final String namespace = nodeType.getNamespace().toString();
105 xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, nodeType.getLocalName(), namespace);
106 xmlWriter.writeDefaultNamespace(namespace);
107 for (var child : data.body()) {
108 nnWriter.write(child);
111 xmlWriter.writeEndElement();
113 } catch (final XMLStreamException e) {
114 throw new IOException("Failed to write elements", e);