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
9 package org.opendaylight.netconf.sal.rest.impl;
11 import java.io.OutputStream;
12 import java.lang.annotation.Annotation;
13 import java.lang.reflect.Type;
14 import java.nio.charset.StandardCharsets;
15 import java.util.List;
16 import javax.ws.rs.Produces;
17 import javax.ws.rs.WebApplicationException;
18 import javax.ws.rs.core.MediaType;
19 import javax.ws.rs.core.MultivaluedMap;
20 import javax.ws.rs.ext.MessageBodyWriter;
21 import javax.ws.rs.ext.Provider;
22 import javax.xml.stream.FactoryConfigurationError;
23 import javax.xml.stream.XMLOutputFactory;
24 import javax.xml.stream.XMLStreamException;
25 import javax.xml.stream.XMLStreamWriter;
26 import org.opendaylight.netconf.sal.rest.api.Draft02;
27 import org.opendaylight.netconf.sal.rest.api.RestconfService;
28 import org.opendaylight.restconf.common.errors.RestconfError;
29 import org.opendaylight.restconf.common.patch.PatchStatusContext;
30 import org.opendaylight.restconf.common.patch.PatchStatusEntity;
33 @Produces({ Draft02.MediaTypes.PATCH_STATUS + RestconfService.XML })
34 public class PatchXmlBodyWriter implements MessageBodyWriter<PatchStatusContext> {
36 private static final XMLOutputFactory XML_FACTORY;
39 XML_FACTORY = XMLOutputFactory.newFactory();
40 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
44 public boolean isWriteable(final Class<?> type, final Type genericType,
45 final Annotation[] annotations, final MediaType mediaType) {
46 return type.equals(PatchStatusContext.class);
50 public long getSize(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
51 final Annotation[] annotations, final MediaType mediaType) {
56 public void writeTo(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
57 final Annotation[] annotations, final MediaType mediaType,
58 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
59 throws WebApplicationException {
62 final XMLStreamWriter xmlWriter =
63 XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name());
64 writeDocument(xmlWriter, patchStatusContext);
65 } catch (final XMLStreamException | FactoryConfigurationError e) {
66 throw new IllegalStateException(e);
70 private static void writeDocument(final XMLStreamWriter writer, final PatchStatusContext context)
71 throws XMLStreamException {
72 writer.writeStartElement("", "yang-patch-status", "urn:ietf:params:xml:ns:yang:ietf-yang-patch");
73 writer.writeStartElement("patch-id");
74 writer.writeCharacters(context.getPatchId());
75 writer.writeEndElement();
78 writer.writeEmptyElement("ok");
80 if (context.getGlobalErrors() != null) {
81 reportErrors(context.getGlobalErrors(), writer);
83 writer.writeStartElement("edit-status");
84 for (final PatchStatusEntity patchStatusEntity : context.getEditCollection()) {
85 writer.writeStartElement("edit");
86 writer.writeStartElement("edit-id");
87 writer.writeCharacters(patchStatusEntity.getEditId());
88 writer.writeEndElement();
89 if (patchStatusEntity.getEditErrors() != null) {
90 reportErrors(patchStatusEntity.getEditErrors(), writer);
92 if (patchStatusEntity.isOk()) {
93 writer.writeEmptyElement("ok");
96 writer.writeEndElement();
98 writer.writeEndElement();
101 writer.writeEndElement();
106 private static void reportErrors(final List<RestconfError> errors, final XMLStreamWriter writer)
107 throws XMLStreamException {
108 writer.writeStartElement("errors");
110 for (final RestconfError restconfError : errors) {
111 writer.writeStartElement("error-type");
112 writer.writeCharacters(restconfError.getErrorType().getErrorTypeTag());
113 writer.writeEndElement();
115 writer.writeStartElement("error-tag");
116 writer.writeCharacters(restconfError.getErrorTag().getTagValue());
117 writer.writeEndElement();
120 if (restconfError.getErrorPath() != null) {
121 writer.writeStartElement("error-path");
122 writer.writeCharacters(restconfError.getErrorPath().toString());
123 writer.writeEndElement();
127 if (restconfError.getErrorMessage() != null) {
128 writer.writeStartElement("error-message");
129 writer.writeCharacters(restconfError.getErrorMessage());
130 writer.writeEndElement();
134 if (restconfError.getErrorInfo() != null) {
135 writer.writeStartElement("error-info");
136 writer.writeCharacters(restconfError.getErrorInfo());
137 writer.writeEndElement();
141 writer.writeEndElement();