Correctly close NormalizedNodeStreamWriters
[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 java.net.URI;
14 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
15 import org.opendaylight.yangtools.yang.common.QName;
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 URI initialNs, final SchemaContextHandler schemaContextHandler) {
43         super(excludedQName);
44         this.jsonWriter = JsonWriterFactory.createJsonWriter(outputWriter);
45         this.jsonNodeStreamWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
46                 JSONCodecFactorySupplier.RFC7951.getShared(schemaContextHandler.get()),
47                 schemaPath, initialNs, jsonWriter);
48     }
49
50     @Override
51     protected NormalizedNodeStreamWriter delegate() {
52         return jsonNodeStreamWriter;
53     }
54
55     @Override
56     void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException {
57         jsonWriter.name(nodeIdentifier.getNodeType().getLocalName());
58     }
59
60     @Override
61     void scalarValueWithDisabledValidation(final Object value) throws IOException {
62         jsonWriter.value(value.toString());
63     }
64
65     @Override
66     void endNodeWithDisabledValidation() {
67         // nope
68     }
69 }