Merge "Add BGPCEP logging configuration"
[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, final MediaType mediaType) {
71         return type.equals(StructuredData.class);
72     }
73
74     @Override
75     public long getSize(final StructuredData t, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
76         return -1;
77     }
78
79     @Override
80     public void writeTo(final StructuredData t, final Class<?> type, final Type genericType, final Annotation[] annotations,
81             final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
82                     throws IOException, WebApplicationException {
83         CompositeNode data = t.getData();
84         if (data == null) {
85             throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
86         }
87
88         final Transformer trans;
89         try {
90             trans = TRANSFORMER.get();
91             if (t.isPrettyPrintMode()) {
92                 trans.setOutputProperty(OutputKeys.INDENT, "yes");
93             } else {
94                 trans.setOutputProperty(OutputKeys.INDENT, "no");
95             }
96         } catch (RuntimeException e) {
97             throw new RestconfDocumentedException(e.getMessage(), ErrorType.TRANSPORT,
98                     ErrorTag.OPERATION_FAILED);
99         }
100
101         // FIXME: BUG-1281: eliminate the intermediate Document
102         final Document domTree = new XmlMapper().write(data, (DataNodeContainer) t.getSchema());
103         try {
104             trans.transform(new DOMSource(domTree), new StreamResult(entityStream));
105         } catch (TransformerException e) {
106             LOG.error("Error during translation of Document to OutputStream", e);
107             throw new RestconfDocumentedException(e.getMessage(), ErrorType.TRANSPORT,
108                     ErrorTag.OPERATION_FAILED);
109         }
110     }
111
112 }