From: Robert Varga Date: Fri, 9 Jul 2021 10:11:35 +0000 (+0200) Subject: Add AbstractPatchStatusBodyWriter X-Git-Tag: v2.0.1~20 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=54f18ecf00633a6fe5a5728e774f4ad1157340c9;p=netconf.git Add AbstractPatchStatusBodyWriter We have two specializations of this writer, create a common superclass and share common code. Also improve error handling by mapping wrapping XMLStreamException into an IOException. JIRA: NETCONF-773 Change-Id: I000b9c7b333f59d000dc28ce2e75ee65477da193 Signed-off-by: Robert Varga --- diff --git a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/AbstractRestconfApplication.java b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/AbstractRestconfApplication.java index 8675f387ae..aa33926a0b 100644 --- a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/AbstractRestconfApplication.java +++ b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/AbstractRestconfApplication.java @@ -22,9 +22,9 @@ import org.opendaylight.restconf.nb.rfc8040.jersey.providers.XmlNormalizedNodeBo import org.opendaylight.restconf.nb.rfc8040.jersey.providers.YangSchemaExportBodyWriter; import org.opendaylight.restconf.nb.rfc8040.jersey.providers.YinSchemaExportBodyWriter; import org.opendaylight.restconf.nb.rfc8040.jersey.providers.errors.RestconfDocumentedExceptionMapper; +import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.JsonPatchStatusBodyWriter; import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.JsonToPatchBodyReader; -import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.PatchJsonBodyWriter; -import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.PatchXmlBodyWriter; +import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.XmlPatchStatusBodyWriter; import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.XmlToPatchBodyReader; /** @@ -47,7 +47,7 @@ abstract class AbstractRestconfApplication extends Application { return Set.of( JsonNormalizedNodeBodyWriter.class, XmlNormalizedNodeBodyWriter.class, YinSchemaExportBodyWriter.class, YangSchemaExportBodyWriter.class, - PatchJsonBodyWriter.class, PatchXmlBodyWriter.class); + JsonPatchStatusBodyWriter.class, XmlPatchStatusBodyWriter.class); } @Override diff --git a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/AbstractPatchStatusBodyWriter.java b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/AbstractPatchStatusBodyWriter.java new file mode 100644 index 0000000000..a09aeb1897 --- /dev/null +++ b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/AbstractPatchStatusBodyWriter.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. 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.patch; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.ext.MessageBodyWriter; +import org.opendaylight.restconf.common.patch.PatchStatusContext; + +abstract class AbstractPatchStatusBodyWriter implements MessageBodyWriter { + @Override + public final boolean isWriteable(final Class type, final Type genericType, final Annotation[] annotations, + final MediaType mediaType) { + return type.equals(PatchStatusContext.class); + } +} diff --git a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/PatchJsonBodyWriter.java b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchStatusBodyWriter.java similarity index 90% rename from restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/PatchJsonBodyWriter.java rename to restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchStatusBodyWriter.java index 00b0a9995c..8336798cdf 100644 --- a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/PatchJsonBodyWriter.java +++ b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchStatusBodyWriter.java @@ -16,10 +16,8 @@ import java.lang.reflect.Type; import java.nio.charset.StandardCharsets; import java.util.List; import javax.ws.rs.Produces; -import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; import org.opendaylight.restconf.common.errors.RestconfError; import org.opendaylight.restconf.common.patch.PatchStatusContext; @@ -29,19 +27,12 @@ import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory; @Provider @Produces(MediaTypes.APPLICATION_YANG_DATA_JSON) -public class PatchJsonBodyWriter implements MessageBodyWriter { - - @Override - public boolean isWriteable(final Class type, final Type genericType, - final Annotation[] annotations, final MediaType mediaType) { - return type.equals(PatchStatusContext.class); - } - +public class JsonPatchStatusBodyWriter extends AbstractPatchStatusBodyWriter { @Override public void writeTo(final PatchStatusContext patchStatusContext, final Class type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap httpHeaders, final OutputStream entityStream) - throws IOException, WebApplicationException { + throws IOException { final JsonWriter jsonWriter = createJsonWriter(entityStream); jsonWriter.beginObject().name("ietf-yang-patch:yang-patch-status"); diff --git a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/PatchXmlBodyWriter.java b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchStatusBodyWriter.java similarity index 88% rename from restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/PatchXmlBodyWriter.java rename to restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchStatusBodyWriter.java index 74cdb810c5..076b8b531f 100644 --- a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/PatchXmlBodyWriter.java +++ b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchStatusBodyWriter.java @@ -7,18 +7,16 @@ */ package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch; +import java.io.IOException; import java.io.OutputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.nio.charset.StandardCharsets; import java.util.List; import javax.ws.rs.Produces; -import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; -import javax.xml.stream.FactoryConfigurationError; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; @@ -29,8 +27,7 @@ import org.opendaylight.restconf.nb.rfc8040.MediaTypes; @Provider @Produces(MediaTypes.APPLICATION_YANG_DATA_XML) -public class PatchXmlBodyWriter implements MessageBodyWriter { - +public class XmlPatchStatusBodyWriter extends AbstractPatchStatusBodyWriter { private static final XMLOutputFactory XML_FACTORY; static { @@ -38,24 +35,17 @@ public class PatchXmlBodyWriter implements MessageBodyWriter XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true); } - @Override - public boolean isWriteable(final Class type, final Type genericType, - final Annotation[] annotations, final MediaType mediaType) { - return type.equals(PatchStatusContext.class); - } - @Override public void writeTo(final PatchStatusContext patchStatusContext, final Class type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap httpHeaders, final OutputStream entityStream) - throws WebApplicationException { - + throws IOException { try { final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name()); writeDocument(xmlWriter, patchStatusContext); - } catch (final XMLStreamException | FactoryConfigurationError e) { - throw new IllegalStateException(e); + } catch (final XMLStreamException e) { + throw new IOException("Failed to write body", e); } }