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.TransformerException;
25 import javax.xml.transform.TransformerFactory;
26 import javax.xml.transform.dom.DOMSource;
27 import javax.xml.transform.stream.StreamResult;
29 import org.opendaylight.controller.sal.rest.api.Draft02;
30 import org.opendaylight.controller.sal.rest.api.RestconfService;
31 import org.opendaylight.controller.sal.restconf.impl.ResponseException;
32 import org.opendaylight.controller.sal.restconf.impl.StructuredData;
33 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
34 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.w3c.dom.Document;
40 @Produces({ Draft02.MediaTypes.DATA + RestconfService.XML, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
41 public enum StructuredDataToXmlProvider implements MessageBodyWriter<StructuredData> {
44 private final static Logger logger = LoggerFactory.getLogger(StructuredDataToXmlProvider.class);
47 public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
52 public long getSize(StructuredData t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
57 public void writeTo(StructuredData t, Class<?> type, Type genericType, Annotation[] annotations,
58 MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
59 throws IOException, WebApplicationException {
60 CompositeNode data = t.getData();
62 throw new ResponseException(Response.Status.NOT_FOUND, "No data exists.");
65 XmlMapper xmlMapper = new XmlMapper();
66 Document domTree = xmlMapper.write(data, (DataNodeContainer) t.getSchema());
68 TransformerFactory tf = TransformerFactory.newInstance();
69 Transformer transformer = tf.newTransformer();
70 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
71 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
72 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
73 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
74 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
75 transformer.transform(new DOMSource(domTree), new StreamResult(entityStream));
76 } catch (TransformerException e) {
77 logger.error("Error during translation of Document to OutputStream", e);
78 throw new ResponseException(Response.Status.INTERNAL_SERVER_ERROR, e.getMessage());