Merge "Fixes - config-registry registered to OSGi, used modules's factory bundle...
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / StructuredDataToXmlProvider.java
1 package org.opendaylight.controller.sal.rest.impl;
2
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.lang.annotation.Annotation;
6 import java.lang.reflect.Type;
7
8 import javax.ws.rs.Produces;
9 import javax.ws.rs.WebApplicationException;
10 import javax.ws.rs.core.MediaType;
11 import javax.ws.rs.core.MultivaluedMap;
12 import javax.ws.rs.core.Response;
13 import javax.ws.rs.ext.MessageBodyWriter;
14 import javax.ws.rs.ext.Provider;
15 import javax.xml.transform.OutputKeys;
16 import javax.xml.transform.Transformer;
17 import javax.xml.transform.TransformerException;
18 import javax.xml.transform.TransformerFactory;
19 import javax.xml.transform.dom.DOMSource;
20 import javax.xml.transform.stream.StreamResult;
21
22 import org.opendaylight.controller.sal.rest.api.Draft01;
23 import org.opendaylight.controller.sal.rest.api.Draft02;
24 import org.opendaylight.controller.sal.rest.api.RestconfService;
25 import org.opendaylight.controller.sal.restconf.impl.ResponseException;
26 import org.opendaylight.controller.sal.restconf.impl.StructuredData;
27 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
28 import org.opendaylight.yangtools.yang.data.impl.NodeUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.w3c.dom.Document;
32
33 @Provider
34 @Produces({ Draft01.MediaTypes.DATA + RestconfService.XML, Draft02.MediaTypes.DATA + RestconfService.XML,
35         MediaType.APPLICATION_XML, MediaType.TEXT_XML })
36 public enum StructuredDataToXmlProvider implements MessageBodyWriter<StructuredData> {
37     INSTANCE;
38
39     private final static Logger logger = LoggerFactory.getLogger(StructuredDataToXmlProvider.class);
40
41     @Override
42     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
43         return true;
44     }
45
46     @Override
47     public long getSize(StructuredData t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
48         return -1;
49     }
50
51     @Override
52     public void writeTo(StructuredData t, Class<?> type, Type genericType, Annotation[] annotations,
53             MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
54             throws IOException, WebApplicationException {
55         CompositeNode data = t.getData();
56         if (data == null) {
57             throw new ResponseException(Response.Status.NOT_FOUND, "No data exists.");
58         }
59
60         Document domTree = NodeUtils.buildShadowDomTree(data);
61         try {
62             TransformerFactory tf = TransformerFactory.newInstance();
63             Transformer transformer = tf.newTransformer();
64             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
65             transformer.setOutputProperty(OutputKeys.METHOD, "xml");
66             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
67             transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
68             transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
69             transformer.transform(new DOMSource(domTree), new StreamResult(entityStream));
70         } catch (TransformerException e) {
71             logger.error("Error during translation of Document to OutputStream", e);
72             throw new ResponseException(Response.Status.INTERNAL_SERVER_ERROR, e.getMessage());
73         }
74     }
75
76 }