0c6a790af6331308a88c13d927a916eef6372b23
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / errors / JsonStreamWriterWithDisabledValidation.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 com.google.gson.stream.JsonWriter;
11 import java.io.IOException;
12 import java.io.OutputStreamWriter;
13 import org.opendaylight.restconf.server.api.DatabindContext;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.Errors;
15 import org.opendaylight.yangtools.yang.common.XMLNamespace;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
18 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
19 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
20
21 /**
22  * JSON stream-writer with disabled leaf-type validation for specified QName.
23  */
24 final class JsonStreamWriterWithDisabledValidation extends StreamWriterWithDisabledValidation {
25     private static final int DEFAULT_INDENT_SPACES_NUM = 2;
26     private static final XMLNamespace IETF_RESTCONF_URI = Errors.QNAME.getModule().getNamespace();
27
28     private final JsonWriter jsonWriter;
29     private final NormalizedNodeStreamWriter jsonNodeStreamWriter;
30
31     /**
32      * Creation of the custom JSON stream-writer.
33      *
34      * @param databindContext {@link DatabindContext} to use
35      * @param outputWriter    Output stream that is used for creation of JSON writers.
36      */
37     JsonStreamWriterWithDisabledValidation(final DatabindContext databindContext,
38             final OutputStreamWriter outputWriter) {
39         jsonWriter = JsonWriterFactory.createJsonWriter(outputWriter, DEFAULT_INDENT_SPACES_NUM);
40         final var inference = errorsContainerInference(databindContext);
41         jsonNodeStreamWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter(databindContext.jsonCodecs(),
42             inference, IETF_RESTCONF_URI, jsonWriter);
43     }
44
45     @Override
46     protected NormalizedNodeStreamWriter delegate() {
47         return jsonNodeStreamWriter;
48     }
49
50     @Override
51     void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException {
52         jsonWriter.name(nodeIdentifier.getNodeType().getLocalName());
53     }
54
55     @Override
56     void scalarValueWithDisabledValidation(final Object value) throws IOException {
57         jsonWriter.value(value.toString());
58     }
59
60     @Override
61     void endNodeWithDisabledValidation() {
62         // nope
63     }
64 }