Merge "Complete implementation of DataChangeListenerProxy"
[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.TransformerConfigurationException;
25 import javax.xml.transform.TransformerException;
26 import javax.xml.transform.TransformerFactory;
27 import javax.xml.transform.dom.DOMSource;
28 import javax.xml.transform.stream.StreamResult;
29
30 import org.opendaylight.controller.sal.rest.api.Draft02;
31 import org.opendaylight.controller.sal.rest.api.RestconfService;
32 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
33 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
34 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
35 import org.opendaylight.controller.sal.restconf.impl.StructuredData;
36 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
37 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.w3c.dom.Document;
41
42 @Provider
43 @Produces({ Draft02.MediaTypes.API + RestconfService.XML, Draft02.MediaTypes.DATA + RestconfService.XML,
44     Draft02.MediaTypes.OPERATION + RestconfService.XML, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
45 public enum StructuredDataToXmlProvider implements MessageBodyWriter<StructuredData> {
46     INSTANCE;
47
48     private static final Logger LOG = LoggerFactory.getLogger(StructuredDataToXmlProvider.class);
49     private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
50     private static final ThreadLocal<Transformer> TRANSFORMER = new ThreadLocal<Transformer>() {
51         @Override
52         protected Transformer initialValue() {
53             final Transformer ret;
54             try {
55                 ret = FACTORY.newTransformer();
56             } catch (TransformerConfigurationException e) {
57                 LOG.error("Failed to instantiate XML transformer", e);
58                 throw new IllegalStateException("XML encoding currently unavailable", e);
59             }
60
61             ret.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
62             ret.setOutputProperty(OutputKeys.METHOD, "xml");
63             ret.setOutputProperty(OutputKeys.INDENT, "yes");
64             ret.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
65             ret.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
66
67             return ret;
68         }
69     };
70
71     @Override
72     public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
73         return type.equals( StructuredData.class );
74     }
75
76     @Override
77     public long getSize(final StructuredData t, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
78         return -1;
79     }
80
81     @Override
82     public void writeTo(final StructuredData t, final Class<?> type, final Type genericType, final Annotation[] annotations,
83             final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
84                     throws IOException, WebApplicationException {
85         CompositeNode data = t.getData();
86         if (data == null) {
87             throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
88         }
89
90         final Transformer trans;
91         try {
92             trans = TRANSFORMER.get();
93         } catch (RuntimeException e) {
94             throw new RestconfDocumentedException(e.getMessage(), ErrorType.TRANSPORT,
95                     ErrorTag.OPERATION_FAILED);
96         }
97
98         // FIXME: BUG-1281: eliminate the intermediate Document
99         final Document domTree = new XmlMapper().write(data, (DataNodeContainer) t.getSchema());
100         try {
101             trans.transform(new DOMSource(domTree), new StreamResult(entityStream));
102         } catch (TransformerException e) {
103             LOG.error("Error during translation of Document to OutputStream", e);
104             throw new RestconfDocumentedException(e.getMessage(), ErrorType.TRANSPORT,
105                     ErrorTag.OPERATION_FAILED);
106         }
107     }
108
109 }