Expose streams with all supported encodings
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / XMLNotificationFormatter.java
1 /*
2  * Copyright (c) 2020 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.nb.rfc8040.streams;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import java.io.IOException;
12 import java.io.StringWriter;
13 import java.time.Instant;
14 import javax.xml.stream.XMLStreamException;
15 import javax.xml.xpath.XPathExpressionException;
16 import org.opendaylight.mdsal.dom.api.DOMNotification;
17 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
18 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20
21 final class XMLNotificationFormatter extends NotificationFormatter {
22     @VisibleForTesting
23     static final XMLNotificationFormatter EMPTY = new XMLNotificationFormatter(TextParameters.EMPTY);
24     static final NotificationFormatterFactory FACTORY = new NotificationFormatterFactory(EMPTY) {
25         @Override
26         public XMLNotificationFormatter newFormatter(final TextParameters textParams) {
27             return new XMLNotificationFormatter(textParams);
28         }
29
30         @Override
31         public XMLNotificationFormatter getFormatter(final TextParameters textParams, final String xpathFilter)
32                 throws XPathExpressionException {
33             return new XMLNotificationFormatter(textParams, xpathFilter);
34         }
35     };
36
37     XMLNotificationFormatter(final TextParameters textParams) {
38         super(textParams);
39     }
40
41     XMLNotificationFormatter(final TextParameters textParams, final String xpathFilter)
42             throws XPathExpressionException {
43         super(textParams, xpathFilter);
44     }
45
46     @Override
47     String createText(final TextParameters params, final EffectiveModelContext schemaContext,
48             final DOMNotification input, final Instant now) throws IOException {
49         final var writer = new StringWriter();
50
51         try {
52             final var xmlStreamWriter = NotificationFormatter.createStreamWriterWithNotification(writer, now);
53             try (var nnWriter = NormalizedNodeWriter.forStreamWriter(XMLStreamNormalizedNodeStreamWriter.create(
54                     xmlStreamWriter, schemaContext, input.getType()))) {
55                 nnWriter.write(input.getBody());
56                 nnWriter.flush();
57
58                 xmlStreamWriter.writeEndElement();
59                 xmlStreamWriter.writeEndDocument();
60                 xmlStreamWriter.flush();
61             }
62         } catch (XMLStreamException e) {
63             throw new IOException("Failed to write notification content", e);
64         }
65
66         return writer.toString();
67     }
68 }