982ff3abc9d052eea5b1296f866bef2200041ca8
[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.nb.rfc8040.handlers.SchemaContextHandler;
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.JSONCodecFactorySupplier;
19 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
20 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
21
22 /**
23  * JSON stream-writer with disabled leaf-type validation for specified QName.
24  */
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 schemaContextHandler Handler that holds actual schema context.
36      * @param outputWriter         Output stream that is used for creation of JSON writers.
37      */
38     JsonStreamWriterWithDisabledValidation(final SchemaContextHandler schemaContextHandler,
39             final OutputStreamWriter outputWriter) {
40         jsonWriter = JsonWriterFactory.createJsonWriter(outputWriter, DEFAULT_INDENT_SPACES_NUM);
41         final var inference = errorsContainerInference(schemaContextHandler);
42         jsonNodeStreamWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
43             JSONCodecFactorySupplier.RFC7951.getShared(inference.getEffectiveModelContext()),
44             inference, IETF_RESTCONF_URI, jsonWriter);
45     }
46
47     @Override
48     protected NormalizedNodeStreamWriter delegate() {
49         return jsonNodeStreamWriter;
50     }
51
52     @Override
53     void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException {
54         jsonWriter.name(nodeIdentifier.getNodeType().getLocalName());
55     }
56
57     @Override
58     void scalarValueWithDisabledValidation(final Object value) throws IOException {
59         jsonWriter.value(value.toString());
60     }
61
62     @Override
63     void endNodeWithDisabledValidation() {
64         // nope
65     }
66 }