Add AbstractPatchStatusBodyWriter 53/96853/3
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 9 Jul 2021 10:11:35 +0000 (12:11 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 9 Jul 2021 12:28:23 +0000 (14:28 +0200)
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 <robert.varga@pantheon.tech>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/AbstractRestconfApplication.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/AbstractPatchStatusBodyWriter.java [new file with mode: 0644]
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchStatusBodyWriter.java [moved from restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/PatchJsonBodyWriter.java with 90% similarity]
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchStatusBodyWriter.java [moved from restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/PatchXmlBodyWriter.java with 88% similarity]

index 8675f387ae561a26f2e88391d754a63862e47ef0..aa33926a0b299ef383744a22a0351ab1a8ad414a 100644 (file)
@@ -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 (file)
index 0000000..a09aeb1
--- /dev/null
@@ -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<PatchStatusContext> {
+    @Override
+    public final boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
+            final MediaType mediaType) {
+        return type.equals(PatchStatusContext.class);
+    }
+}
@@ -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<PatchStatusContext> {
-
-    @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<String, Object> httpHeaders, final OutputStream entityStream)
-            throws IOException, WebApplicationException {
+            throws IOException {
 
         final JsonWriter jsonWriter = createJsonWriter(entityStream);
         jsonWriter.beginObject().name("ietf-yang-patch:yang-patch-status");
@@ -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<PatchStatusContext> {
-
+public class XmlPatchStatusBodyWriter extends AbstractPatchStatusBodyWriter {
     private static final XMLOutputFactory XML_FACTORY;
 
     static {
@@ -38,24 +35,17 @@ public class PatchXmlBodyWriter implements MessageBodyWriter<PatchStatusContext>
         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<String, Object> 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);
         }
     }