Merge "Bug 1029: Remove dead code: sal-dom-it"
[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.RestconfDocumentedException;
32 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
33 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
34 import org.opendaylight.controller.sal.restconf.impl.StructuredData;
35 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
36 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.w3c.dom.Document;
40
41 @Provider
42 @Produces({ Draft02.MediaTypes.API + RestconfService.XML, Draft02.MediaTypes.DATA + RestconfService.XML,
43         Draft02.MediaTypes.OPERATION + RestconfService.XML, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
44 public enum StructuredDataToXmlProvider implements MessageBodyWriter<StructuredData> {
45     INSTANCE;
46
47     private final static Logger logger = LoggerFactory.getLogger(StructuredDataToXmlProvider.class);
48
49     @Override
50     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
51         return type.equals( StructuredData.class );
52     }
53
54     @Override
55     public long getSize(StructuredData t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
56         return -1;
57     }
58
59     @Override
60     public void writeTo(StructuredData t, Class<?> type, Type genericType, Annotation[] annotations,
61             MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
62             throws IOException, WebApplicationException {
63         CompositeNode data = t.getData();
64         if (data == null) {
65             throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
66         }
67
68         XmlMapper xmlMapper = new XmlMapper();
69         Document domTree = xmlMapper.write(data, (DataNodeContainer) t.getSchema());
70         try {
71             TransformerFactory tf = TransformerFactory.newInstance();
72             Transformer transformer = tf.newTransformer();
73             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
74             transformer.setOutputProperty(OutputKeys.METHOD, "xml");
75             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
76             transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
77             transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
78             transformer.transform(new DOMSource(domTree), new StreamResult(entityStream));
79         } catch (TransformerException e) {
80             logger.error("Error during translation of Document to OutputStream", e);
81             throw new RestconfDocumentedException( e.getMessage(), ErrorType.TRANSPORT,
82                                                    ErrorTag.OPERATION_FAILED );
83         }
84     }
85
86 }