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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.