From ae721eb9a2727c43752faac41e540bbfc62d7f6d Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 10 Nov 2022 18:40:23 +0100 Subject: [PATCH] Refactor StreamWriterWithDisabledValidation We are unnecessarily passing things to each instance, where these are static knowledge. Reduce the footprint of each instance by moving these constants around and adjusting instantiation accordingly. Change-Id: I72fe28699fdd55325126b5930b77e151293582eb Signed-off-by: Robert Varga --- ...JsonStreamWriterWithDisabledValidation.java | 18 ++++++------------ .../RestconfDocumentedExceptionMapper.java | 12 +++++------- .../StreamWriterWithDisabledValidation.java | 15 +-------------- .../XmlStreamWriterWithDisabledValidation.java | 12 +++--------- 4 files changed, 15 insertions(+), 42 deletions(-) diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/JsonStreamWriterWithDisabledValidation.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/JsonStreamWriterWithDisabledValidation.java index d84415d11b..982ff3abc9 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/JsonStreamWriterWithDisabledValidation.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/JsonStreamWriterWithDisabledValidation.java @@ -11,10 +11,9 @@ import com.google.gson.stream.JsonWriter; import java.io.IOException; import java.io.OutputStreamWriter; import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler; -import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.Errors; import org.opendaylight.yangtools.yang.common.XMLNamespace; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier; import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter; @@ -24,8 +23,8 @@ import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory; * JSON stream-writer with disabled leaf-type validation for specified QName. */ final class JsonStreamWriterWithDisabledValidation extends StreamWriterWithDisabledValidation { - private static final int DEFAULT_INDENT_SPACES_NUM = 2; + private static final XMLNamespace IETF_RESTCONF_URI = Errors.QNAME.getModule().getNamespace(); private final JsonWriter jsonWriter; private final NormalizedNodeStreamWriter jsonNodeStreamWriter; @@ -33,21 +32,16 @@ final class JsonStreamWriterWithDisabledValidation extends StreamWriterWithDisab /** * Creation of the custom JSON stream-writer. * - * @param excludedQName QName of the element that is excluded from type-check. - * @param outputWriter Output stream that is used for creation of JSON writers. - * @param schemaPath Schema-path of the {@link NormalizedNode} to be written. - * @param initialNs Initial namespace derived from schema node of the data that are serialized. * @param schemaContextHandler Handler that holds actual schema context. + * @param outputWriter Output stream that is used for creation of JSON writers. */ - JsonStreamWriterWithDisabledValidation(final QName excludedQName, final OutputStreamWriter outputWriter, - final XMLNamespace initialNs, final SchemaContextHandler schemaContextHandler) { - super(excludedQName); + JsonStreamWriterWithDisabledValidation(final SchemaContextHandler schemaContextHandler, + final OutputStreamWriter outputWriter) { jsonWriter = JsonWriterFactory.createJsonWriter(outputWriter, DEFAULT_INDENT_SPACES_NUM); - final var inference = errorsContainerInference(schemaContextHandler); jsonNodeStreamWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter( JSONCodecFactorySupplier.RFC7951.getShared(inference.getEffectiveModelContext()), - inference, initialNs, jsonWriter); + inference, IETF_RESTCONF_URI, jsonWriter); } @Override diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/RestconfDocumentedExceptionMapper.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/RestconfDocumentedExceptionMapper.java index f82a501287..5823da3cdb 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/RestconfDocumentedExceptionMapper.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/RestconfDocumentedExceptionMapper.java @@ -37,7 +37,6 @@ import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.errors.Errors; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.errors.errors.Error; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.common.XMLNamespace; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; @@ -61,9 +60,8 @@ public final class RestconfDocumentedExceptionMapper implements ExceptionMapper< private static final QName ERROR_TAG_QNAME = qnameOf("error-tag"); private static final QName ERROR_APP_TAG_QNAME = qnameOf("error-app-tag"); private static final QName ERROR_MESSAGE_QNAME = qnameOf("error-message"); - private static final QName ERROR_INFO_QNAME = qnameOf("error-info"); private static final QName ERROR_PATH_QNAME = qnameOf("error-path"); - private static final XMLNamespace IETF_RESTCONF_URI = Errors.QNAME.getModule().getNamespace(); + static final QName ERROR_INFO_QNAME = qnameOf("error-info"); @Context private HttpHeaders headers; @@ -171,8 +169,8 @@ public final class RestconfDocumentedExceptionMapper implements ExceptionMapper< try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); OutputStreamWriter streamStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); ) { - return writeNormalizedNode(errorsContainer, outputStream, new JsonStreamWriterWithDisabledValidation( - ERROR_INFO_QNAME, streamStreamWriter, IETF_RESTCONF_URI, schemaContextHandler)); + return writeNormalizedNode(errorsContainer, outputStream, + new JsonStreamWriterWithDisabledValidation(schemaContextHandler, streamStreamWriter)); } catch (IOException e) { throw new IllegalStateException("Cannot close some of the output JSON writers", e); } @@ -186,8 +184,8 @@ public final class RestconfDocumentedExceptionMapper implements ExceptionMapper< */ private String serializeErrorsContainerToXml(final ContainerNode errorsContainer) { try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { - return writeNormalizedNode(errorsContainer, outputStream, new XmlStreamWriterWithDisabledValidation( - ERROR_INFO_QNAME, outputStream, schemaContextHandler)); + return writeNormalizedNode(errorsContainer, outputStream, + new XmlStreamWriterWithDisabledValidation(schemaContextHandler, outputStream)); } catch (IOException e) { throw new IllegalStateException("Cannot close some of the output XML writers", e); } diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/StreamWriterWithDisabledValidation.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/StreamWriterWithDisabledValidation.java index 8a8579bfa3..652a83cb78 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/StreamWriterWithDisabledValidation.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/StreamWriterWithDisabledValidation.java @@ -10,9 +10,7 @@ package org.opendaylight.restconf.nb.rfc8040.jersey.providers.errors; import java.io.IOException; import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.Errors; -import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.stream.ForwardingNormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack; import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference; @@ -24,22 +22,11 @@ import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference * we override the leafNode behavior for error-info. */ abstract class StreamWriterWithDisabledValidation extends ForwardingNormalizedNodeStreamWriter { - private final QName excludedQName; - private boolean inOurLeaf; - /** - * Creation of the {@link NormalizedNode} stream-writer with {@link QName} that is excluded from type-check. - * - * @param excludedQName QName of the element that is excluded from type-check. - */ - StreamWriterWithDisabledValidation(final QName excludedQName) { - this.excludedQName = excludedQName; - } - @Override public final void startLeafNode(final NodeIdentifier name) throws IOException { - if (name.getNodeType().equals(excludedQName)) { + if (RestconfDocumentedExceptionMapper.ERROR_INFO_QNAME.equals(name.getNodeType())) { inOurLeaf = true; startLeafNodeWithDisabledValidation(name); } else { diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/XmlStreamWriterWithDisabledValidation.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/XmlStreamWriterWithDisabledValidation.java index 720e5203b3..fbf2a09a88 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/XmlStreamWriterWithDisabledValidation.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/XmlStreamWriterWithDisabledValidation.java @@ -16,9 +16,7 @@ import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler; -import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter; @@ -39,15 +37,11 @@ final class XmlStreamWriterWithDisabledValidation extends StreamWriterWithDisabl /** * Creation of the custom XML stream-writer. * - * @param excludedQName QName of the element that is excluded from type-check. - * @param outputStream Output stream that is used for creation of JSON writers. - * @param schemaPath Schema-path of the {@link NormalizedNode} to be written. * @param schemaContextHandler Handler that holds actual schema context. + * @param outputStream Output stream that is used for creation of JSON writers. */ - XmlStreamWriterWithDisabledValidation(final QName excludedQName, final OutputStream outputStream, - final SchemaContextHandler schemaContextHandler) { - super(excludedQName); - + XmlStreamWriterWithDisabledValidation(final SchemaContextHandler schemaContextHandler, + final OutputStream outputStream) { try { xmlWriter = XML_FACTORY.createXMLStreamWriter(outputStream, StandardCharsets.UTF_8.name()); } catch (final XMLStreamException | FactoryConfigurationError e) { -- 2.36.6