2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
10 import java.io.OutputStream;
11 import java.lang.annotation.Annotation;
12 import java.lang.reflect.Type;
13 import java.nio.charset.StandardCharsets;
14 import java.util.List;
15 import javax.ws.rs.Produces;
16 import javax.ws.rs.WebApplicationException;
17 import javax.ws.rs.core.MediaType;
18 import javax.ws.rs.core.MultivaluedMap;
19 import javax.ws.rs.ext.MessageBodyWriter;
20 import javax.ws.rs.ext.Provider;
21 import javax.xml.stream.FactoryConfigurationError;
22 import javax.xml.stream.XMLOutputFactory;
23 import javax.xml.stream.XMLStreamException;
24 import javax.xml.stream.XMLStreamWriter;
25 import org.opendaylight.restconf.common.errors.RestconfError;
26 import org.opendaylight.restconf.common.patch.PatchStatusContext;
27 import org.opendaylight.restconf.common.patch.PatchStatusEntity;
28 import org.opendaylight.restconf.nb.rfc8040.MediaTypes;
31 @Produces(MediaTypes.APPLICATION_YANG_DATA_XML)
32 public class PatchXmlBodyWriter implements MessageBodyWriter<PatchStatusContext> {
34 private static final XMLOutputFactory XML_FACTORY;
37 XML_FACTORY = XMLOutputFactory.newFactory();
38 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
42 public boolean isWriteable(final Class<?> type, final Type genericType,
43 final Annotation[] annotations, final MediaType mediaType) {
44 return type.equals(PatchStatusContext.class);
48 public void writeTo(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
49 final Annotation[] annotations, final MediaType mediaType,
50 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
51 throws WebApplicationException {
54 final XMLStreamWriter xmlWriter =
55 XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name());
56 writeDocument(xmlWriter, patchStatusContext);
57 } catch (final XMLStreamException | FactoryConfigurationError e) {
58 throw new IllegalStateException(e);
62 private static void writeDocument(final XMLStreamWriter writer, final PatchStatusContext context)
63 throws XMLStreamException {
64 writer.writeStartElement("", "yang-patch-status", "urn:ietf:params:xml:ns:yang:ietf-yang-patch");
65 writer.writeStartElement("patch-id");
66 writer.writeCharacters(context.getPatchId());
67 writer.writeEndElement();
70 writer.writeEmptyElement("ok");
72 if (context.getGlobalErrors() != null) {
73 reportErrors(context.getGlobalErrors(), writer);
75 writer.writeStartElement("edit-status");
76 for (final PatchStatusEntity patchStatusEntity : context.getEditCollection()) {
77 writer.writeStartElement("edit");
78 writer.writeStartElement("edit-id");
79 writer.writeCharacters(patchStatusEntity.getEditId());
80 writer.writeEndElement();
81 if (patchStatusEntity.getEditErrors() != null) {
82 reportErrors(patchStatusEntity.getEditErrors(), writer);
84 if (patchStatusEntity.isOk()) {
85 writer.writeEmptyElement("ok");
88 writer.writeEndElement();
90 writer.writeEndElement();
93 writer.writeEndElement();
98 private static void reportErrors(final List<RestconfError> errors, final XMLStreamWriter writer)
99 throws XMLStreamException {
100 writer.writeStartElement("errors");
102 for (final RestconfError restconfError : errors) {
103 writer.writeStartElement("error-type");
104 writer.writeCharacters(restconfError.getErrorType().getErrorTypeTag());
105 writer.writeEndElement();
107 writer.writeStartElement("error-tag");
108 writer.writeCharacters(restconfError.getErrorTag().getTagValue());
109 writer.writeEndElement();
112 if (restconfError.getErrorPath() != null) {
113 writer.writeStartElement("error-path");
114 writer.writeCharacters(restconfError.getErrorPath().toString());
115 writer.writeEndElement();
119 if (restconfError.getErrorMessage() != null) {
120 writer.writeStartElement("error-message");
121 writer.writeCharacters(restconfError.getErrorMessage());
122 writer.writeEndElement();
126 if (restconfError.getErrorInfo() != null) {
127 writer.writeStartElement("error-info");
128 writer.writeCharacters(restconfError.getErrorInfo());
129 writer.writeEndElement();
133 writer.writeEndElement();