JSON: Resolve 500 response from device exception
[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 // FIXME remove this class
25 final class JsonStreamWriterWithDisabledValidation extends StreamWriterWithDisabledValidation {
26     private static final int DEFAULT_INDENT_SPACES_NUM = 2;
27     private static final XMLNamespace IETF_RESTCONF_URI = Errors.QNAME.getModule().getNamespace();
28
29     private final JsonWriter jsonWriter;
30     private final NormalizedNodeStreamWriter jsonNodeStreamWriter;
31
32     /**
33      * Creation of the custom JSON stream-writer.
34      *
35      * @param databindContext {@link DatabindContext} to use
36      * @param outputWriter    Output stream that is used for creation of JSON writers.
37      */
38     JsonStreamWriterWithDisabledValidation(final DatabindContext databindContext,
39             final OutputStreamWriter outputWriter) {
40         jsonWriter = JsonWriterFactory.createJsonWriter(outputWriter, DEFAULT_INDENT_SPACES_NUM);
41         final var inference = errorsContainerInference(databindContext);
42         jsonNodeStreamWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter(databindContext.jsonCodecs(),
43             inference, IETF_RESTCONF_URI, jsonWriter);
44     }
45
46     @Override
47     protected NormalizedNodeStreamWriter delegate() {
48         return jsonNodeStreamWriter;
49     }
50
51     @Override
52     void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException {
53         jsonWriter.name(nodeIdentifier.getNodeType().getLocalName());
54     }
55
56     @Override
57     void scalarValueWithDisabledValidation(final Object value) throws IOException {
58         jsonWriter.value(value.toString());
59     }
60
61     @Override
62     void endNodeWithDisabledValidation() {
63         // nope
64     }
65 }