Centralize AbstractPatchStatusBodyWriter.writeBody() 95/111295/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 5 Apr 2024 23:16:00 +0000 (01:16 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 5 Apr 2024 23:17:00 +0000 (01:17 +0200)
We are providing two distinct implementations, let's cut down the
verbosity by having a shared entrypoint into them.

JIRA: NETCONF-773
Change-Id: I86ac22d13f2049201d9833a60c81ee2b45aa0aa9
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/AbstractPatchStatusBodyWriter.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/JsonPatchStatusBodyWriter.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/XmlPatchStatusBodyWriter.java

index b2954ba3ef20ffd25b9da069b9200e9d34db22ad..bb2ceda3f99a5f4e8e220e31a237ecbb018837ab 100644 (file)
@@ -7,10 +7,16 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.jersey.providers;
 
+import static java.util.Objects.requireNonNull;
+
+import java.io.IOException;
+import java.io.OutputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.MessageBodyWriter;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.restconf.server.api.PatchStatusContext;
 
 abstract class AbstractPatchStatusBodyWriter implements MessageBodyWriter<PatchStatusContext> {
@@ -19,4 +25,13 @@ abstract class AbstractPatchStatusBodyWriter implements MessageBodyWriter<PatchS
             final MediaType mediaType) {
         return type.equals(PatchStatusContext.class);
     }
+
+    @Override
+    public final void writeTo(final PatchStatusContext body, final Class<?> type, final Type genericType,
+            final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders,
+            final OutputStream entityStream) throws IOException {
+        writeTo(requireNonNull(body), requireNonNull(entityStream));
+    }
+
+    abstract void writeTo(@NonNull PatchStatusContext body, @NonNull OutputStream out) throws IOException;
 }
index 5d2fd46ff68136f6a7f1af4858e57f7fc6344778..8e800237a1526ab1738f220efe10f7ec253c5485 100644 (file)
@@ -10,12 +10,8 @@ package org.opendaylight.restconf.nb.rfc8040.jersey.providers;
 import com.google.gson.stream.JsonWriter;
 import java.io.IOException;
 import java.io.OutputStream;
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Type;
 import java.util.List;
 import javax.ws.rs.Produces;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.Provider;
 import org.opendaylight.restconf.api.MediaTypes;
 import org.opendaylight.restconf.api.query.PrettyPrintParam;
@@ -28,28 +24,26 @@ import org.opendaylight.restconf.server.spi.FormattableBodySupport;
 @Produces(MediaTypes.APPLICATION_YANG_DATA_JSON)
 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 {
-        final var jsonWriter = FormattableBodySupport.createJsonWriter(entityStream, () -> PrettyPrintParam.FALSE);
+    void writeTo(final PatchStatusContext body, final OutputStream out) throws IOException {
+        final var jsonWriter = FormattableBodySupport.createJsonWriter(out, () -> PrettyPrintParam.FALSE);
         jsonWriter.beginObject().name("ietf-yang-patch:yang-patch-status")
-            .beginObject().name("patch-id").value(patchStatusContext.patchId());
+            .beginObject().name("patch-id").value(body.patchId());
 
-        if (patchStatusContext.ok()) {
+        if (body.ok()) {
             reportSuccess(jsonWriter);
         } else {
-            final var globalErrors = patchStatusContext.globalErrors();
+            final var globalErrors = body.globalErrors();
             if (globalErrors != null) {
-                reportErrors(patchStatusContext.databind(), globalErrors, jsonWriter);
+                reportErrors(body.databind(), globalErrors, jsonWriter);
             } else {
                 jsonWriter.name("edit-status").beginObject()
                     .name("edit").beginArray();
-                for (var editStatus : patchStatusContext.editCollection()) {
+                for (var editStatus : body.editCollection()) {
                     jsonWriter.beginObject().name("edit-id").value(editStatus.getEditId());
 
                     final var editErrors = editStatus.getEditErrors();
                     if (editErrors != null) {
-                        reportErrors(patchStatusContext.databind(), editErrors, jsonWriter);
+                        reportErrors(body.databind(), editErrors, jsonWriter);
                     } else if (editStatus.isOk()) {
                         reportSuccess(jsonWriter);
                     }
index 3e4ef118b861e77db808b3efb5dfcf370a732be0..533519b745dc48d6e2697d8dc8153588f864a1b7 100644 (file)
@@ -9,13 +9,9 @@ package org.opendaylight.restconf.nb.rfc8040.jersey.providers;
 
 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.core.MediaType;
-import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.Provider;
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
@@ -38,35 +34,31 @@ public class XmlPatchStatusBodyWriter 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 {
+    void writeTo(final PatchStatusContext body, final OutputStream out) throws IOException {
         try {
-            final XMLStreamWriter xmlWriter =
-                    XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name());
-            writeDocument(xmlWriter, patchStatusContext);
+            final var xmlWriter = XML_FACTORY.createXMLStreamWriter(out, StandardCharsets.UTF_8.name());
+            writeDocument(xmlWriter, body);
         } catch (final XMLStreamException e) {
             throw new IOException("Failed to write body", e);
         }
     }
 
-    private static void writeDocument(final XMLStreamWriter writer, final PatchStatusContext context)
+    private static void writeDocument(final XMLStreamWriter writer, final PatchStatusContext body)
             throws XMLStreamException {
         writer.writeStartElement("", "yang-patch-status", XML_NAMESPACE);
         writer.writeStartElement("patch-id");
-        writer.writeCharacters(context.patchId());
+        writer.writeCharacters(body.patchId());
         writer.writeEndElement();
 
-        if (context.ok()) {
+        if (body.ok()) {
             writer.writeEmptyElement("ok");
         } else {
-            final var globalErrors = context.globalErrors();
+            final var globalErrors = body.globalErrors();
             if (globalErrors != null) {
-                reportErrors(context.databind(), globalErrors, writer);
+                reportErrors(body.databind(), globalErrors, writer);
             } else {
                 writer.writeStartElement("edit-status");
-                for (var patchStatusEntity : context.editCollection()) {
+                for (var patchStatusEntity : body.editCollection()) {
                     writer.writeStartElement("edit");
                     writer.writeStartElement("edit-id");
                     writer.writeCharacters(patchStatusEntity.getEditId());
@@ -74,7 +66,7 @@ public class XmlPatchStatusBodyWriter extends AbstractPatchStatusBodyWriter {
 
                     final var editErrors = patchStatusEntity.getEditErrors();
                     if (editErrors != null) {
-                        reportErrors(context.databind(), editErrors, writer);
+                        reportErrors(body.databind(), editErrors, writer);
                     } else if (patchStatusEntity.isOk()) {
                         writer.writeEmptyElement("ok");
                     }