From 24036f99195213badad52a133718673ccc1fb44b Mon Sep 17 00:00:00 2001 From: Ivan Hrasko Date: Tue, 28 Nov 2023 15:29:17 +0100 Subject: [PATCH] Remove not validating JSON/XML stream writters We have now customized logic to write JSON/XML errors in RestconfDocumentedExceptionMapper. Thus we no longer need Json/XmlStreamWriterWithDisabledValidation classes. JIRA: NETCONF-1130 Change-Id: I2249004c58070d8ad46a1f9dae78fbb5222f5230 Signed-off-by: Ivan Hrasko Signed-off-by: Yaroslav Lastivka --- ...sonStreamWriterWithDisabledValidation.java | 65 ------------ .../RestconfDocumentedExceptionMapper.java | 3 +- .../StreamWriterWithDisabledValidation.java | 85 ---------------- ...XmlStreamWriterWithDisabledValidation.java | 98 ------------------- 4 files changed, 1 insertion(+), 250 deletions(-) delete mode 100644 restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/JsonStreamWriterWithDisabledValidation.java delete mode 100644 restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/StreamWriterWithDisabledValidation.java delete mode 100644 restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/XmlStreamWriterWithDisabledValidation.java 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 deleted file mode 100644 index a2472f235f..0000000000 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/JsonStreamWriterWithDisabledValidation.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright © 2019 FRINX s.r.o. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.restconf.nb.rfc8040.jersey.providers.errors; - -import com.google.gson.stream.JsonWriter; -import java.io.IOException; -import java.io.OutputStreamWriter; -import org.opendaylight.restconf.server.api.DatabindContext; -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.stream.NormalizedNodeStreamWriter; -import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter; -import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory; - -/** - * JSON stream-writer with disabled leaf-type validation for specified QName. - */ -// FIXME remove this class -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; - - /** - * Creation of the custom JSON stream-writer. - * - * @param databindContext {@link DatabindContext} to use - * @param outputWriter Output stream that is used for creation of JSON writers. - */ - JsonStreamWriterWithDisabledValidation(final DatabindContext databindContext, - final OutputStreamWriter outputWriter) { - jsonWriter = JsonWriterFactory.createJsonWriter(outputWriter, DEFAULT_INDENT_SPACES_NUM); - final var inference = errorsContainerInference(databindContext); - jsonNodeStreamWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter(databindContext.jsonCodecs(), - inference, IETF_RESTCONF_URI, jsonWriter); - } - - @Override - protected NormalizedNodeStreamWriter delegate() { - return jsonNodeStreamWriter; - } - - @Override - void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException { - jsonWriter.name(nodeIdentifier.getNodeType().getLocalName()); - } - - @Override - void scalarValueWithDisabledValidation(final Object value) throws IOException { - jsonWriter.value(value.toString()); - } - - @Override - void endNodeWithDisabledValidation() { - // nope - } -} \ No newline at end of file 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 52db8ff6ea..276ad51b2b 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 @@ -69,8 +69,7 @@ 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"); - // FIXME make this private - static final QName ERROR_INFO_QNAME = qnameOf("error-info"); + private static final QName ERROR_INFO_QNAME = qnameOf("error-info"); private static final QName ERROR_PATH_QNAME = qnameOf("error-path"); private static final int DEFAULT_INDENT_SPACES_NUM = 2; private static final XMLOutputFactory XML_OUTPUT_FACTORY; 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 deleted file mode 100644 index e81b39517f..0000000000 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/StreamWriterWithDisabledValidation.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright © 2019 FRINX s.r.o. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.restconf.nb.rfc8040.jersey.providers.errors; - -import java.io.IOException; -import org.opendaylight.restconf.server.api.DatabindContext; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.Errors; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; -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; - -/** - * Created delegating writer to special-case error-info as error-info is defined as an empty container in the restconf - * yang schema but we create a leaf node so we can output it. The delegate stream writer validates the node type against - * the schema and thus will expect a LeafSchemaNode but the schema has a ContainerSchemaNode so, to avoid an error, - * we override the leafNode behavior for error-info. - */ -// FIXME remove this class -abstract class StreamWriterWithDisabledValidation extends ForwardingNormalizedNodeStreamWriter { - private boolean inOurLeaf; - - @Override - public final void startLeafNode(final NodeIdentifier name) throws IOException { - if (RestconfDocumentedExceptionMapper.ERROR_INFO_QNAME.equals(name.getNodeType())) { - inOurLeaf = true; - startLeafNodeWithDisabledValidation(name); - } else { - super.startLeafNode(name); - } - } - - /** - * Writing of the excluded leaf to the output stream. - * - * @param nodeIdentifier Node identifier of the leaf to be written to output stream. - * @throws IOException Writing of the leaf to output stream failed. - */ - abstract void startLeafNodeWithDisabledValidation(NodeIdentifier nodeIdentifier) throws IOException; - - @Override - public final void scalarValue(final Object value) throws IOException { - if (inOurLeaf) { - scalarValueWithDisabledValidation(value); - } else { - super.scalarValue(value); - } - } - - /** - * Writing of the value of the excluded leaf to the output stream. - * - * @param value Value of the excluded leaf. - * @throws IOException Writing of the leaf value to the output stream failed. - */ - abstract void scalarValueWithDisabledValidation(Object value) throws IOException; - - @Override - public final void endNode() throws IOException { - if (inOurLeaf) { - inOurLeaf = false; - endNodeWithDisabledValidation(); - } else { - super.endNode(); - } - } - - /** - * Writing of the end element with disabled validation. - * - * @throws IOException Writing of the end element to the output stream failed. - */ - abstract void endNodeWithDisabledValidation() throws IOException; - - static final Inference errorsContainerInference(final DatabindContext databindContext) { - final var stack = SchemaInferenceStack.of(databindContext.modelContext()); - stack.enterGrouping(Errors.QNAME); - return stack.toInference(); - } -} \ No newline at end of file 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 deleted file mode 100644 index 18ace4e71a..0000000000 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/XmlStreamWriterWithDisabledValidation.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright © 2019 FRINX s.r.o. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.restconf.nb.rfc8040.jersey.providers.errors; - -import java.io.IOException; -import java.io.OutputStream; -import java.nio.charset.StandardCharsets; -import javax.xml.XMLConstants; -import javax.xml.stream.FactoryConfigurationError; -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamWriter; -import org.opendaylight.restconf.server.api.DatabindContext; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter; -import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter; - -/** - * XML stream-writer with disabled leaf-type validation for specified QName. - */ -// FIXME remove this class -final class XmlStreamWriterWithDisabledValidation extends StreamWriterWithDisabledValidation { - private static final XMLOutputFactory XML_FACTORY; - - static { - XML_FACTORY = XMLOutputFactory.newFactory(); - XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true); - } - - private final XMLStreamWriter xmlWriter; - private final NormalizedNodeStreamWriter xmlNodeStreamWriter; - - /** - * Creation of the custom XML stream-writer. - * - * @param databindContext {@link DatabindContext} to use - * @param outputStream Output stream that is used for creation of JSON writers. - */ - XmlStreamWriterWithDisabledValidation(final DatabindContext databindContext, final OutputStream outputStream) { - try { - xmlWriter = XML_FACTORY.createXMLStreamWriter(outputStream, StandardCharsets.UTF_8.name()); - } catch (final XMLStreamException | FactoryConfigurationError e) { - throw new IllegalStateException("Cannot create XML writer", e); - } - - xmlNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, - errorsContainerInference(databindContext)); - } - - @Override - protected NormalizedNodeStreamWriter delegate() { - return xmlNodeStreamWriter; - } - - @Override - void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException { - final String namespace = nodeIdentifier.getNodeType().getNamespace().toString(); - try { - xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, - nodeIdentifier.getNodeType().getLocalName(), namespace); - } catch (XMLStreamException e) { - throw new IOException("Error writing leaf node", e); - } - } - - @Override - void scalarValueWithDisabledValidation(final Object value) throws IOException { - try { - xmlWriter.writeCharacters(value.toString()); - } catch (XMLStreamException e) { - throw new IOException("Error writing value", e); - } - } - - @Override - void endNodeWithDisabledValidation() throws IOException { - try { - xmlWriter.writeEndElement(); - } catch (XMLStreamException e) { - throw new IOException("Error writing end-node", e); - } - } - - @Override - public void close() throws IOException { - xmlNodeStreamWriter.close(); - try { - xmlWriter.close(); - } catch (XMLStreamException e) { - throw new IOException(e); - } - } -} \ No newline at end of file -- 2.36.6