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.IOException;
12 import java.io.OutputStream;
13 import java.lang.annotation.Annotation;
14 import java.lang.reflect.Type;
15 import java.nio.charset.StandardCharsets;
16 import java.util.List;
17 import javax.ws.rs.Produces;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.MultivaluedMap;
21 import javax.ws.rs.ext.MessageBodyWriter;
22 import javax.ws.rs.ext.Provider;
23 import javax.xml.stream.FactoryConfigurationError;
24 import javax.xml.stream.XMLOutputFactory;
25 import javax.xml.stream.XMLStreamException;
26 import javax.xml.stream.XMLStreamWriter;
27 import org.opendaylight.netconf.sal.rest.api.Draft02;
28 import org.opendaylight.netconf.sal.rest.api.RestconfService;
29 import org.opendaylight.netconf.sal.restconf.impl.PatchStatusContext;
30 import org.opendaylight.netconf.sal.restconf.impl.PatchStatusEntity;
31 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
32 import org.opendaylight.restconf.Rfc8040;
33 import org.opendaylight.restconf.utils.RestconfConstants;
36 @Produces({Draft02.MediaTypes.PATCH_STATUS + RestconfService.XML,
37 Rfc8040.MediaTypes.PATCH_STATUS + RestconfConstants.XML})
38 public class PatchXmlBodyWriter implements MessageBodyWriter<PatchStatusContext> {
40 private static final XMLOutputFactory XML_FACTORY;
43 XML_FACTORY = XMLOutputFactory.newFactory();
44 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
48 public boolean isWriteable(final Class<?> type, final Type genericType,
49 final Annotation[] annotations, final MediaType mediaType) {
50 return type.equals(PatchStatusContext.class);
54 public long getSize(final PatchStatusContext patchStatusContext, Class<?> type, final Type genericType,
55 final Annotation[] annotations, final MediaType mediaType) {
60 public void writeTo(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
61 final Annotation[] annotations, final MediaType mediaType,
62 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
63 throws IOException, WebApplicationException {
66 final XMLStreamWriter xmlWriter =
67 XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name());
68 writeDocument(xmlWriter, patchStatusContext);
69 } catch (final XMLStreamException e) {
70 throw new IllegalStateException(e);
71 } catch (final FactoryConfigurationError e) {
72 throw new IllegalStateException(e);
76 private static void writeDocument(final XMLStreamWriter writer, final PatchStatusContext context)
77 throws XMLStreamException, IOException {
78 writer.writeStartElement("", "yang-patch-status", "urn:ietf:params:xml:ns:yang:ietf-yang-patch");
79 writer.writeStartElement("patch-id");
80 writer.writeCharacters(context.getPatchId());
81 writer.writeEndElement();
84 writer.writeEmptyElement("ok");
86 if (context.getGlobalErrors() != null) {
87 reportErrors(context.getGlobalErrors(), writer);
89 writer.writeStartElement("edit-status");
90 for (final PatchStatusEntity patchStatusEntity : context.getEditCollection()) {
91 writer.writeStartElement("edit");
92 writer.writeStartElement("edit-id");
93 writer.writeCharacters(patchStatusEntity.getEditId());
94 writer.writeEndElement();
95 if (patchStatusEntity.getEditErrors() != null) {
96 reportErrors(patchStatusEntity.getEditErrors(), writer);
98 if (patchStatusEntity.isOk()) {
99 writer.writeEmptyElement("ok");
102 writer.writeEndElement();
104 writer.writeEndElement();
107 writer.writeEndElement();
112 private static void reportErrors(final List<RestconfError> errors, final XMLStreamWriter writer)
113 throws IOException, XMLStreamException {
114 writer.writeStartElement("errors");
116 for (final RestconfError restconfError : errors) {
117 writer.writeStartElement("error-type");
118 writer.writeCharacters(restconfError.getErrorType().getErrorTypeTag());
119 writer.writeEndElement();
121 writer.writeStartElement("error-tag");
122 writer.writeCharacters(restconfError.getErrorTag().getTagValue());
123 writer.writeEndElement();
126 if (restconfError.getErrorPath() != null) {
127 writer.writeStartElement("error-path");
128 writer.writeCharacters(restconfError.getErrorPath().toString());
129 writer.writeEndElement();
133 if (restconfError.getErrorMessage() != null) {
134 writer.writeStartElement("error-message");
135 writer.writeCharacters(restconfError.getErrorMessage());
136 writer.writeEndElement();
140 if (restconfError.getErrorInfo() != null) {
141 writer.writeStartElement("error-info");
142 writer.writeCharacters(restconfError.getErrorInfo());
143 writer.writeEndElement();
147 writer.writeEndElement();