Split out operation output serialization
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / api / FormattableBody.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.server.api;
9
10 import static java.util.Objects.requireNonNull;
11
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;
31
32 /**
33  * A response counterpart to {@link RequestBody}. It is inherently immutable and exposes methods to write the content
34  * to an {@link OutputStream}.
35  */
36 @NonNullByDefault
37 public abstract class FormattableBody implements Immutable {
38     private static final XMLOutputFactory XML_FACTORY = XMLOutputFactory.newFactory();
39     private static final String PRETTY_PRINT_INDENT = "  ";
40
41     private final FormatParameters format;
42
43     FormattableBody(final FormatParameters format) {
44         this.format = requireNonNull(format);
45     }
46
47     /**
48      * Write the content of this body as a JSON document.
49      *
50      * @param out output stream
51      * @throws IOException if an IO error occurs.
52      */
53     public final void formatToJSON(final OutputStream out) throws IOException {
54         formatToJSON(requireNonNull(out), format);
55     }
56
57     abstract void formatToJSON(OutputStream out, FormatParameters format) throws IOException;
58
59     /**
60      * Write the content of this body as an XML document.
61      *
62      * @param out output stream
63      * @throws IOException if an IO error occurs.
64      */
65     public final void formatToXML(final OutputStream out) throws IOException {
66         formatToXML(requireNonNull(out), format);
67     }
68
69     abstract void formatToXML(OutputStream out, FormatParameters format) throws IOException;
70
71     @Override
72     public final String toString() {
73         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
74     }
75
76     ToStringHelper addToStringAttributes(final ToStringHelper helper) {
77         return helper.add("prettyPrint", format.prettyPrint().value());
78     }
79
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 : "");
83         return ret;
84     }
85
86     public static final XMLStreamWriter createXmlWriter(final OutputStream out, final FormatParameters format)
87             throws IOException {
88         final var xmlWriter = createXmlWriter(out);
89         return format.prettyPrint().value() ? new IndentingXMLStreamWriter(xmlWriter) : xmlWriter;
90     }
91
92     private static XMLStreamWriter createXmlWriter(final OutputStream out) throws IOException {
93         try {
94             return XML_FACTORY.createXMLStreamWriter(out, StandardCharsets.UTF_8.name());
95         } catch (XMLStreamException | FactoryConfigurationError e) {
96             throw new IOException(e);
97         }
98     }
99
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();
104         try {
105             xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, nodeType.getLocalName(), namespace);
106             xmlWriter.writeDefaultNamespace(namespace);
107             for (var child : data.body()) {
108                 nnWriter.write(child);
109             }
110             nnWriter.flush();
111             xmlWriter.writeEndElement();
112             xmlWriter.flush();
113         } catch (final XMLStreamException e) {
114             throw new IOException("Failed to write elements", e);
115         }
116     }
117 }