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 e) {
66 throw new IllegalStateException(e);
67 } catch (final FactoryConfigurationError e) {
68 throw new IllegalStateException(e);
72 private static void writeDocument(final XMLStreamWriter writer, final PatchStatusContext context)
73 throws XMLStreamException {
74 writer.writeStartElement("", "yang-patch-status", "urn:ietf:params:xml:ns:yang:ietf-yang-patch");
75 writer.writeStartElement("patch-id");
76 writer.writeCharacters(context.getPatchId());
77 writer.writeEndElement();
80 writer.writeEmptyElement("ok");
82 if (context.getGlobalErrors() != null) {
83 reportErrors(context.getGlobalErrors(), writer);
85 writer.writeStartElement("edit-status");
86 for (final PatchStatusEntity patchStatusEntity : context.getEditCollection()) {
87 writer.writeStartElement("edit");
88 writer.writeStartElement("edit-id");
89 writer.writeCharacters(patchStatusEntity.getEditId());
90 writer.writeEndElement();
91 if (patchStatusEntity.getEditErrors() != null) {
92 reportErrors(patchStatusEntity.getEditErrors(), writer);
94 if (patchStatusEntity.isOk()) {
95 writer.writeEmptyElement("ok");
98 writer.writeEndElement();
100 writer.writeEndElement();
103 writer.writeEndElement();
108 private static void reportErrors(final List<RestconfError> errors, final XMLStreamWriter writer)
109 throws XMLStreamException {
110 writer.writeStartElement("errors");
112 for (final RestconfError restconfError : errors) {
113 writer.writeStartElement("error-type");
114 writer.writeCharacters(restconfError.getErrorType().getErrorTypeTag());
115 writer.writeEndElement();
117 writer.writeStartElement("error-tag");
118 writer.writeCharacters(restconfError.getErrorTag().getTagValue());
119 writer.writeEndElement();
122 if (restconfError.getErrorPath() != null) {
123 writer.writeStartElement("error-path");
124 writer.writeCharacters(restconfError.getErrorPath().toString());
125 writer.writeEndElement();
129 if (restconfError.getErrorMessage() != null) {
130 writer.writeStartElement("error-message");
131 writer.writeCharacters(restconfError.getErrorMessage());
132 writer.writeEndElement();
136 if (restconfError.getErrorInfo() != null) {
137 writer.writeStartElement("error-info");
138 writer.writeCharacters(restconfError.getErrorInfo());
139 writer.writeEndElement();
143 writer.writeEndElement();