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