Remove unused exceptions
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / rest / impl / PatchXmlBodyWriter.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.sal.rest.impl;
10
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;
31
32 @Provider
33 @Produces({ Draft02.MediaTypes.PATCH_STATUS + RestconfService.XML })
34 public class PatchXmlBodyWriter implements MessageBodyWriter<PatchStatusContext> {
35
36     private static final XMLOutputFactory XML_FACTORY;
37
38     static {
39         XML_FACTORY = XMLOutputFactory.newFactory();
40         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
41     }
42
43     @Override
44     public boolean isWriteable(final Class<?> type, final Type genericType,
45                                final Annotation[] annotations, final MediaType mediaType) {
46         return type.equals(PatchStatusContext.class);
47     }
48
49     @Override
50     public long getSize(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
51                         final Annotation[] annotations, final MediaType mediaType) {
52         return -1;
53     }
54
55     @Override
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 {
60
61         try {
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);
69         }
70     }
71
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();
78
79         if (context.isOk()) {
80             writer.writeEmptyElement("ok");
81         } else {
82             if (context.getGlobalErrors() != null) {
83                 reportErrors(context.getGlobalErrors(), writer);
84             }
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);
93                 } else {
94                     if (patchStatusEntity.isOk()) {
95                         writer.writeEmptyElement("ok");
96                     }
97                 }
98                 writer.writeEndElement();
99             }
100             writer.writeEndElement();
101
102         }
103         writer.writeEndElement();
104
105         writer.flush();
106     }
107
108     private static void reportErrors(final List<RestconfError> errors, final XMLStreamWriter writer)
109             throws XMLStreamException {
110         writer.writeStartElement("errors");
111
112         for (final RestconfError restconfError : errors) {
113             writer.writeStartElement("error-type");
114             writer.writeCharacters(restconfError.getErrorType().getErrorTypeTag());
115             writer.writeEndElement();
116
117             writer.writeStartElement("error-tag");
118             writer.writeCharacters(restconfError.getErrorTag().getTagValue());
119             writer.writeEndElement();
120
121             // optional node
122             if (restconfError.getErrorPath() != null) {
123                 writer.writeStartElement("error-path");
124                 writer.writeCharacters(restconfError.getErrorPath().toString());
125                 writer.writeEndElement();
126             }
127
128             // optional node
129             if (restconfError.getErrorMessage() != null) {
130                 writer.writeStartElement("error-message");
131                 writer.writeCharacters(restconfError.getErrorMessage());
132                 writer.writeEndElement();
133             }
134
135             // optional node
136             if (restconfError.getErrorInfo() != null) {
137                 writer.writeStartElement("error-info");
138                 writer.writeCharacters(restconfError.getErrorInfo());
139                 writer.writeEndElement();
140             }
141         }
142
143         writer.writeEndElement();
144     }
145 }