Bump MRI upstreams
[netconf.git] / restconf / restconf-nb-rfc8040 / 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.nb.rfc8040.handlers.SchemaContextHandler;
14 import org.opendaylight.yangtools.yang.common.QName;
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.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
19 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
20 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
21 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23
24 /**
25  * JSON stream-writer with disabled leaf-type validation for specified QName.
26  */
27 final class JsonStreamWriterWithDisabledValidation extends StreamWriterWithDisabledValidation {
28
29     private final JsonWriter jsonWriter;
30     private final NormalizedNodeStreamWriter jsonNodeStreamWriter;
31
32     /**
33      * Creation of the custom JSON stream-writer.
34      *
35      * @param excludedQName        QName of the element that is excluded from type-check.
36      * @param outputWriter         Output stream that is used for creation of JSON writers.
37      * @param schemaPath           Schema-path of the {@link NormalizedNode} to be written.
38      * @param initialNs            Initial namespace derived from schema node of the data that are serialized.
39      * @param schemaContextHandler Handler that holds actual schema context.
40      */
41     JsonStreamWriterWithDisabledValidation(final QName excludedQName, final OutputStreamWriter outputWriter,
42             final SchemaPath schemaPath, final XMLNamespace initialNs,
43             final SchemaContextHandler schemaContextHandler) {
44         super(excludedQName);
45         this.jsonWriter = JsonWriterFactory.createJsonWriter(outputWriter);
46         this.jsonNodeStreamWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
47                 JSONCodecFactorySupplier.RFC7951.getShared(schemaContextHandler.get()),
48                 schemaPath, initialNs, jsonWriter);
49     }
50
51     @Override
52     protected NormalizedNodeStreamWriter delegate() {
53         return jsonNodeStreamWriter;
54     }
55
56     @Override
57     void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException {
58         jsonWriter.name(nodeIdentifier.getNodeType().getLocalName());
59     }
60
61     @Override
62     void scalarValueWithDisabledValidation(final Object value) throws IOException {
63         jsonWriter.value(value.toString());
64     }
65
66     @Override
67     void endNodeWithDisabledValidation() {
68         // nope
69     }
70 }