Reduce exception guard
[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 static final int DEFAULT_INDENT_SPACES_NUM = 2;
30
31     private final JsonWriter jsonWriter;
32     private final NormalizedNodeStreamWriter jsonNodeStreamWriter;
33
34     /**
35      * Creation of the custom JSON stream-writer.
36      *
37      * @param excludedQName        QName of the element that is excluded from type-check.
38      * @param outputWriter         Output stream that is used for creation of JSON writers.
39      * @param schemaPath           Schema-path of the {@link NormalizedNode} to be written.
40      * @param initialNs            Initial namespace derived from schema node of the data that are serialized.
41      * @param schemaContextHandler Handler that holds actual schema context.
42      */
43     JsonStreamWriterWithDisabledValidation(final QName excludedQName, final OutputStreamWriter outputWriter,
44             final SchemaPath schemaPath, final XMLNamespace initialNs,
45             final SchemaContextHandler schemaContextHandler) {
46         super(excludedQName);
47         this.jsonWriter = JsonWriterFactory.createJsonWriter(outputWriter, DEFAULT_INDENT_SPACES_NUM);
48         this.jsonNodeStreamWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
49                 JSONCodecFactorySupplier.RFC7951.getShared(schemaContextHandler.get()),
50                 schemaPath, initialNs, jsonWriter);
51     }
52
53     @Override
54     protected NormalizedNodeStreamWriter delegate() {
55         return jsonNodeStreamWriter;
56     }
57
58     @Override
59     void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException {
60         jsonWriter.name(nodeIdentifier.getNodeType().getLocalName());
61     }
62
63     @Override
64     void scalarValueWithDisabledValidation(final Object value) throws IOException {
65         jsonWriter.value(value.toString());
66     }
67
68     @Override
69     void endNodeWithDisabledValidation() {
70         // nope
71     }
72 }