2 * Copyright © 2019 FRINX s.r.o. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.errors;
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;
22 * JSON stream-writer with disabled leaf-type validation for specified QName.
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();
29 private final JsonWriter jsonWriter;
30 private final NormalizedNodeStreamWriter jsonNodeStreamWriter;
33 * Creation of the custom JSON stream-writer.
35 * @param databindContext {@link DatabindContext} to use
36 * @param outputWriter Output stream that is used for creation of JSON writers.
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);
47 protected NormalizedNodeStreamWriter delegate() {
48 return jsonNodeStreamWriter;
52 void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException {
53 jsonWriter.name(nodeIdentifier.getNodeType().getLocalName());
57 void scalarValueWithDisabledValidation(final Object value) throws IOException {
58 jsonWriter.value(value.toString());
62 void endNodeWithDisabledValidation() {