b8303d1afd5ff16fe5296e61b0590d4bd88080f1
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / errors / XmlStreamWriterWithDisabledValidation.java
1 /*
2  * Copyright © 2019 FRINX s.r.o. 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.jersey.providers.errors;
9
10 import java.io.IOException;
11 import java.io.OutputStream;
12 import java.nio.charset.StandardCharsets;
13 import javax.xml.XMLConstants;
14 import javax.xml.stream.FactoryConfigurationError;
15 import javax.xml.stream.XMLOutputFactory;
16 import javax.xml.stream.XMLStreamException;
17 import javax.xml.stream.XMLStreamWriter;
18 import org.opendaylight.restconf.server.api.DatabindContext;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
21 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
22
23 /**
24  * XML stream-writer with disabled leaf-type validation for specified QName.
25  */
26 final class XmlStreamWriterWithDisabledValidation extends StreamWriterWithDisabledValidation {
27     private static final XMLOutputFactory XML_FACTORY;
28
29     static {
30         XML_FACTORY = XMLOutputFactory.newFactory();
31         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
32     }
33
34     private final XMLStreamWriter xmlWriter;
35     private final NormalizedNodeStreamWriter xmlNodeStreamWriter;
36
37     /**
38      * Creation of the custom XML stream-writer.
39      *
40      * @param databindContext {@link DatabindContext} to use
41      * @param outputStream    Output stream that is used for creation of JSON writers.
42      */
43     XmlStreamWriterWithDisabledValidation(final DatabindContext databindContext, final OutputStream outputStream) {
44         try {
45             xmlWriter = XML_FACTORY.createXMLStreamWriter(outputStream, StandardCharsets.UTF_8.name());
46         } catch (final XMLStreamException | FactoryConfigurationError e) {
47             throw new IllegalStateException("Cannot create XML writer", e);
48         }
49
50         xmlNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
51             errorsContainerInference(databindContext));
52     }
53
54     @Override
55     protected NormalizedNodeStreamWriter delegate() {
56         return xmlNodeStreamWriter;
57     }
58
59     @Override
60     void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException {
61         final String namespace = nodeIdentifier.getNodeType().getNamespace().toString();
62         try {
63             xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX,
64                     nodeIdentifier.getNodeType().getLocalName(), namespace);
65         } catch (XMLStreamException e) {
66             throw new IOException("Error writing leaf node", e);
67         }
68     }
69
70     @Override
71     void scalarValueWithDisabledValidation(final Object value) throws IOException {
72         try {
73             xmlWriter.writeCharacters(value.toString());
74         } catch (XMLStreamException e) {
75             throw new IOException("Error writing value", e);
76         }
77     }
78
79     @Override
80     void endNodeWithDisabledValidation() throws IOException {
81         try {
82             xmlWriter.writeEndElement();
83         } catch (XMLStreamException e) {
84             throw new IOException("Error writing end-node", e);
85         }
86     }
87
88     @Override
89     public void close() throws IOException {
90         xmlNodeStreamWriter.close();
91         try {
92             xmlWriter.close();
93         } catch (XMLStreamException e) {
94             throw new IOException(e);
95         }
96     }
97 }