From: Robert Varga Date: Fri, 5 Apr 2024 23:16:00 +0000 (+0200) Subject: Centralize AbstractPatchStatusBodyWriter.writeBody() X-Git-Tag: v7.0.5~64 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=321e23141ae822018acfea212d85583e13fad44e;p=netconf.git Centralize AbstractPatchStatusBodyWriter.writeBody() 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 --- diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/AbstractPatchStatusBodyWriter.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/AbstractPatchStatusBodyWriter.java index b2954ba3ef..bb2ceda3f9 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/AbstractPatchStatusBodyWriter.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/AbstractPatchStatusBodyWriter.java @@ -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 { @@ -19,4 +25,13 @@ abstract class AbstractPatchStatusBodyWriter implements MessageBodyWriter type, final Type genericType, + final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap httpHeaders, + final OutputStream entityStream) throws IOException { + writeTo(requireNonNull(body), requireNonNull(entityStream)); + } + + abstract void writeTo(@NonNull PatchStatusContext body, @NonNull OutputStream out) throws IOException; } diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/JsonPatchStatusBodyWriter.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/JsonPatchStatusBodyWriter.java index 5d2fd46ff6..8e800237a1 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/JsonPatchStatusBodyWriter.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/JsonPatchStatusBodyWriter.java @@ -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 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); } diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/XmlPatchStatusBodyWriter.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/XmlPatchStatusBodyWriter.java index 3e4ef118b8..533519b745 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/XmlPatchStatusBodyWriter.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/XmlPatchStatusBodyWriter.java @@ -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 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"); }