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.IOException;
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.core.MediaType;
18 import javax.ws.rs.core.MultivaluedMap;
19 import javax.ws.rs.ext.Provider;
20 import javax.xml.stream.XMLOutputFactory;
21 import javax.xml.stream.XMLStreamException;
22 import javax.xml.stream.XMLStreamWriter;
23 import org.opendaylight.restconf.common.errors.RestconfError;
24 import org.opendaylight.restconf.common.patch.PatchStatusContext;
25 import org.opendaylight.restconf.common.patch.PatchStatusEntity;
26 import org.opendaylight.restconf.nb.rfc8040.MediaTypes;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.patch.rev170222.yang.patch.status.YangPatchStatus;
30 @Produces(MediaTypes.APPLICATION_YANG_DATA_XML)
31 public class XmlPatchStatusBodyWriter extends AbstractPatchStatusBodyWriter {
32 private static final String XML_NAMESPACE = YangPatchStatus.QNAME.getNamespace().toString();
33 private static final XMLOutputFactory XML_FACTORY;
36 XML_FACTORY = XMLOutputFactory.newFactory();
37 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
41 public void writeTo(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
42 final Annotation[] annotations, final MediaType mediaType,
43 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
46 final XMLStreamWriter xmlWriter =
47 XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name());
48 writeDocument(xmlWriter, patchStatusContext);
49 } catch (final XMLStreamException e) {
50 throw new IOException("Failed to write body", e);
54 private static void writeDocument(final XMLStreamWriter writer, final PatchStatusContext context)
55 throws XMLStreamException {
56 writer.writeStartElement("", "yang-patch-status", XML_NAMESPACE);
57 writer.writeStartElement("patch-id");
58 writer.writeCharacters(context.getPatchId());
59 writer.writeEndElement();
62 writer.writeEmptyElement("ok");
64 if (context.getGlobalErrors() != null) {
65 reportErrors(context.getGlobalErrors(), writer);
67 writer.writeStartElement("edit-status");
68 for (final PatchStatusEntity patchStatusEntity : context.getEditCollection()) {
69 writer.writeStartElement("edit");
70 writer.writeStartElement("edit-id");
71 writer.writeCharacters(patchStatusEntity.getEditId());
72 writer.writeEndElement();
73 if (patchStatusEntity.getEditErrors() != null) {
74 reportErrors(patchStatusEntity.getEditErrors(), writer);
76 if (patchStatusEntity.isOk()) {
77 writer.writeEmptyElement("ok");
80 writer.writeEndElement();
82 writer.writeEndElement();
85 writer.writeEndElement();
90 private static void reportErrors(final List<RestconfError> errors, final XMLStreamWriter writer)
91 throws XMLStreamException {
92 writer.writeStartElement("errors");
94 for (final RestconfError restconfError : errors) {
95 writer.writeStartElement("error-type");
96 writer.writeCharacters(restconfError.getErrorType().elementBody());
97 writer.writeEndElement();
99 writer.writeStartElement("error-tag");
100 writer.writeCharacters(restconfError.getErrorTag().getTagValue());
101 writer.writeEndElement();
104 if (restconfError.getErrorPath() != null) {
105 writer.writeStartElement("error-path");
106 writer.writeCharacters(restconfError.getErrorPath().toString());
107 writer.writeEndElement();
111 if (restconfError.getErrorMessage() != null) {
112 writer.writeStartElement("error-message");
113 writer.writeCharacters(restconfError.getErrorMessage());
114 writer.writeEndElement();
118 if (restconfError.getErrorInfo() != null) {
119 writer.writeStartElement("error-info");
120 writer.writeCharacters(restconfError.getErrorInfo());
121 writer.writeEndElement();
125 writer.writeEndElement();