2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.rest.impl;
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;
41 * @deprecated class will be removed in Lithium release
44 @Produces({ Draft02.MediaTypes.API + RestconfService.XML, Draft02.MediaTypes.DATA + RestconfService.XML,
45 Draft02.MediaTypes.OPERATION + RestconfService.XML, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
46 public enum StructuredDataToXmlProvider implements MessageBodyWriter<StructuredData> {
49 private static final Logger LOG = LoggerFactory.getLogger(StructuredDataToXmlProvider.class);
50 private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
51 private static final ThreadLocal<Transformer> TRANSFORMER = new ThreadLocal<Transformer>() {
53 protected Transformer initialValue() {
54 final Transformer ret;
56 ret = FACTORY.newTransformer();
57 } catch (final TransformerConfigurationException e) {
58 LOG.error("Failed to instantiate XML transformer", e);
59 throw new IllegalStateException("XML encoding currently unavailable", e);
62 ret.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
63 ret.setOutputProperty(OutputKeys.METHOD, "xml");
64 ret.setOutputProperty(OutputKeys.INDENT, "yes");
65 ret.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
66 ret.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
73 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
74 final MediaType mediaType) {
75 return type.equals(StructuredData.class);
79 public long getSize(final StructuredData t, final Class<?> type, final Type genericType,
80 final Annotation[] annotations, final MediaType mediaType) {
85 public void writeTo(final StructuredData t, final Class<?> type, final Type genericType,
86 final Annotation[] annotations, final MediaType mediaType,
87 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException,
88 WebApplicationException {
89 final CompositeNode data = t.getData();
91 throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
94 final Transformer trans;
96 trans = TRANSFORMER.get();
97 if (t.isPrettyPrintMode()) {
98 trans.setOutputProperty(OutputKeys.INDENT, "yes");
100 trans.setOutputProperty(OutputKeys.INDENT, "no");
102 } catch (final RuntimeException e) {
103 throw new RestconfDocumentedException(e.getMessage(), ErrorType.TRANSPORT, ErrorTag.OPERATION_FAILED);
106 // FIXME: BUG-1281: eliminate the intermediate Document
107 final Document domTree = new XmlMapper().write(data, (DataNodeContainer) t.getSchema());
109 trans.transform(new DOMSource(domTree), new StreamResult(entityStream));
110 } catch (final TransformerException e) {
111 LOG.error("Error during translation of Document to OutputStream", e);
112 throw new RestconfDocumentedException(e.getMessage(), ErrorType.TRANSPORT, ErrorTag.OPERATION_FAILED);