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.API + RestconfService.XML, Draft02.MediaTypes.DATA + RestconfService.XML,
41 Draft02.MediaTypes.OPERATION + RestconfService.XML, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
42 public enum StructuredDataToXmlProvider implements MessageBodyWriter<StructuredData> {
45 private final static Logger logger = LoggerFactory.getLogger(StructuredDataToXmlProvider.class);
48 public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
53 public long getSize(StructuredData t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
58 public void writeTo(StructuredData t, Class<?> type, Type genericType, Annotation[] annotations,
59 MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
60 throws IOException, WebApplicationException {
61 CompositeNode data = t.getData();
63 throw new ResponseException(Response.Status.NOT_FOUND, "No data exists.");
66 XmlMapper xmlMapper = new XmlMapper();
67 Document domTree = xmlMapper.write(data, (DataNodeContainer) t.getSchema());
69 TransformerFactory tf = TransformerFactory.newInstance();
70 Transformer transformer = tf.newTransformer();
71 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
72 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
73 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
74 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
75 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
76 transformer.transform(new DOMSource(domTree), new StreamResult(entityStream));
77 } catch (TransformerException e) {
78 logger.error("Error during translation of Document to OutputStream", e);
79 throw new ResponseException(Response.Status.INTERNAL_SERVER_ERROR, e.getMessage());