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;
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;
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;
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> {
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>() {
52 protected Transformer initialValue() {
53 final Transformer ret;
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);
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");
72 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
73 return type.equals( StructuredData.class );
77 public long getSize(final StructuredData t, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
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();
87 throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
90 final Transformer trans;
92 trans = TRANSFORMER.get();
93 } catch (RuntimeException e) {
94 throw new RestconfDocumentedException(e.getMessage(), ErrorType.TRANSPORT,
95 ErrorTag.OPERATION_FAILED);
98 // FIXME: BUG-1281: eliminate the intermediate Document
99 final Document domTree = new XmlMapper().write(data, (DataNodeContainer) t.getSchema());
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);