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.restconf.common.errors.RestconfError;
30 import org.opendaylight.restconf.common.patch.PatchStatusContext;
31 import org.opendaylight.restconf.common.patch.PatchStatusEntity;
34 @Produces({ Draft02.MediaTypes.PATCH_STATUS + RestconfService.XML })
35 public class PatchXmlBodyWriter implements MessageBodyWriter<PatchStatusContext> {
37 private static final XMLOutputFactory XML_FACTORY;
40 XML_FACTORY = XMLOutputFactory.newFactory();
41 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
45 public boolean isWriteable(final Class<?> type, final Type genericType,
46 final Annotation[] annotations, final MediaType mediaType) {
47 return type.equals(PatchStatusContext.class);
51 public long getSize(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
52 final Annotation[] annotations, final MediaType mediaType) {
57 public void writeTo(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
58 final Annotation[] annotations, final MediaType mediaType,
59 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
60 throws IOException, WebApplicationException {
63 final XMLStreamWriter xmlWriter =
64 XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name());
65 writeDocument(xmlWriter, patchStatusContext);
66 } catch (final XMLStreamException e) {
67 throw new IllegalStateException(e);
68 } catch (final FactoryConfigurationError e) {
69 throw new IllegalStateException(e);
73 private static void writeDocument(final XMLStreamWriter writer, final PatchStatusContext context)
74 throws XMLStreamException, IOException {
75 writer.writeStartElement("", "yang-patch-status", "urn:ietf:params:xml:ns:yang:ietf-yang-patch");
76 writer.writeStartElement("patch-id");
77 writer.writeCharacters(context.getPatchId());
78 writer.writeEndElement();
81 writer.writeEmptyElement("ok");
83 if (context.getGlobalErrors() != null) {
84 reportErrors(context.getGlobalErrors(), writer);
86 writer.writeStartElement("edit-status");
87 for (final PatchStatusEntity patchStatusEntity : context.getEditCollection()) {
88 writer.writeStartElement("edit");
89 writer.writeStartElement("edit-id");
90 writer.writeCharacters(patchStatusEntity.getEditId());
91 writer.writeEndElement();
92 if (patchStatusEntity.getEditErrors() != null) {
93 reportErrors(patchStatusEntity.getEditErrors(), writer);
95 if (patchStatusEntity.isOk()) {
96 writer.writeEmptyElement("ok");
99 writer.writeEndElement();
101 writer.writeEndElement();
104 writer.writeEndElement();
109 private static void reportErrors(final List<RestconfError> errors, final XMLStreamWriter writer)
110 throws IOException, XMLStreamException {
111 writer.writeStartElement("errors");
113 for (final RestconfError restconfError : errors) {
114 writer.writeStartElement("error-type");
115 writer.writeCharacters(restconfError.getErrorType().getErrorTypeTag());
116 writer.writeEndElement();
118 writer.writeStartElement("error-tag");
119 writer.writeCharacters(restconfError.getErrorTag().getTagValue());
120 writer.writeEndElement();
123 if (restconfError.getErrorPath() != null) {
124 writer.writeStartElement("error-path");
125 writer.writeCharacters(restconfError.getErrorPath().toString());
126 writer.writeEndElement();
130 if (restconfError.getErrorMessage() != null) {
131 writer.writeStartElement("error-message");
132 writer.writeCharacters(restconfError.getErrorMessage());
133 writer.writeEndElement();
137 if (restconfError.getErrorInfo() != null) {
138 writer.writeStartElement("error-info");
139 writer.writeCharacters(restconfError.getErrorInfo());
140 writer.writeEndElement();
144 writer.writeEndElement();