X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=restconf%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fsal%2Frest%2Fimpl%2FPATCHXmlBodyWriter.java;h=9a60cabc5e721e4d74468153c7e218defe8149af;hb=e4fb9cf5bc1fa2bda3d01ad60b37758f0b0df13c;hp=2d094a6439c8782d2cf72ccf1112a57fe64b812d;hpb=af130c5e03ebfae9f2682fe6c9bf2214d4419f9c;p=netconf.git diff --git a/restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/rest/impl/PATCHXmlBodyWriter.java b/restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/rest/impl/PATCHXmlBodyWriter.java index 2d094a6439..9a60cabc5e 100644 --- a/restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/rest/impl/PATCHXmlBodyWriter.java +++ b/restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/rest/impl/PATCHXmlBodyWriter.java @@ -5,12 +5,14 @@ * 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.netconf.sal.rest.impl; 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; @@ -22,14 +24,17 @@ import javax.xml.stream.FactoryConfigurationError; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; -import org.opendaylight.netconf.sal.rest.api.Draft02.MediaTypes; +import org.opendaylight.netconf.sal.rest.api.Draft02; import org.opendaylight.netconf.sal.rest.api.RestconfService; import org.opendaylight.netconf.sal.restconf.impl.PATCHStatusContext; import org.opendaylight.netconf.sal.restconf.impl.PATCHStatusEntity; import org.opendaylight.netconf.sal.restconf.impl.RestconfError; +import org.opendaylight.restconf.Rfc8040; +import org.opendaylight.restconf.utils.RestconfConstants; @Provider -@Produces({ MediaTypes.PATCH_STATUS + RestconfService.XML}) +@Produces({Draft02.MediaTypes.PATCH_STATUS + RestconfService.XML, + Rfc8040.MediaTypes.PATCH_STATUS + RestconfConstants.XML}) public class PATCHXmlBodyWriter implements MessageBodyWriter { private static final XMLOutputFactory XML_FACTORY; @@ -40,33 +45,35 @@ public class PATCHXmlBodyWriter implements MessageBodyWriter } @Override - public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + public boolean isWriteable(final Class type, final Type genericType, + final Annotation[] annotations, final MediaType mediaType) { return type.equals(PATCHStatusContext.class); } @Override - public long getSize(PATCHStatusContext patchStatusContext, Class type, Type genericType, Annotation[] - annotations, MediaType mediaType) { + public long getSize(final PATCHStatusContext patchStatusContext, Class type, final Type genericType, + final Annotation[] annotations, final MediaType mediaType) { return -1; } @Override - public void writeTo(PATCHStatusContext patchStatusContext, Class type, Type genericType, Annotation[] - annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) - throws IOException, WebApplicationException { + 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 { try { - XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream); + final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name()); writeDocument(xmlWriter, patchStatusContext); } catch (final XMLStreamException e) { throw new IllegalStateException(e); } catch (final FactoryConfigurationError e) { throw new IllegalStateException(e); } - } - private static void writeDocument(XMLStreamWriter writer, PATCHStatusContext context) throws XMLStreamException, IOException { + private static void writeDocument(final XMLStreamWriter writer, final PATCHStatusContext context) + throws XMLStreamException, IOException { writer.writeStartElement("", "yang-patch-status", "urn:ietf:params:xml:ns:yang:ietf-yang-patch"); writer.writeStartElement("patch-id"); writer.writeCharacters(context.getPatchId()); @@ -79,7 +86,7 @@ public class PATCHXmlBodyWriter implements MessageBodyWriter reportErrors(context.getGlobalErrors(), writer); } writer.writeStartElement("edit-status"); - for (PATCHStatusEntity patchStatusEntity : context.getEditCollection()) { + for (final PATCHStatusEntity patchStatusEntity : context.getEditCollection()) { writer.writeStartElement("edit"); writer.writeStartElement("edit-id"); writer.writeCharacters(patchStatusEntity.getEditId()); @@ -101,23 +108,32 @@ public class PATCHXmlBodyWriter implements MessageBodyWriter writer.flush(); } - private static void reportErrors(List errors, XMLStreamWriter writer) throws IOException, XMLStreamException { + private static void reportErrors(final List errors, final XMLStreamWriter writer) + throws IOException, XMLStreamException { writer.writeStartElement("errors"); - for (RestconfError restconfError : errors) { + for (final RestconfError restconfError : errors) { writer.writeStartElement("error-type"); writer.writeCharacters(restconfError.getErrorType().getErrorTypeTag()); writer.writeEndElement(); + writer.writeStartElement("error-tag"); writer.writeCharacters(restconfError.getErrorTag().getTagValue()); writer.writeEndElement(); - //TODO: fix error-path reporting (separate error-path from error-message) -// writer.writeStartElement("error-path"); -// writer.writeCharacters(restconfError.getErrorPath()); -// writer.writeEndElement(); - writer.writeStartElement("error-message"); - writer.writeCharacters(restconfError.getErrorMessage()); - writer.writeEndElement(); + + // optional node + if (restconfError.getErrorPath() != null) { + writer.writeStartElement("error-path"); + writer.writeCharacters(restconfError.getErrorPath().toString()); + writer.writeEndElement(); + } + + // optional node + if (restconfError.getErrorMessage() != null) { + writer.writeStartElement("error-message"); + writer.writeCharacters(restconfError.getErrorMessage()); + writer.writeEndElement(); + } } writer.writeEndElement();