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 com.google.common.base.Throwables;
11 import java.io.IOException;
12 import java.io.OutputStream;
13 import java.lang.annotation.Annotation;
14 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.stream.FactoryConfigurationError;
23 import javax.xml.stream.XMLOutputFactory;
24 import javax.xml.stream.XMLStreamException;
25 import javax.xml.stream.XMLStreamWriter;
26 import org.opendaylight.controller.sal.rest.api.Draft02;
27 import org.opendaylight.controller.sal.rest.api.RestconfService;
28 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
29 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
30 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
38 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
39 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
45 @Produces({ Draft02.MediaTypes.API + RestconfService.XML, Draft02.MediaTypes.DATA + RestconfService.XML,
46 Draft02.MediaTypes.OPERATION + RestconfService.XML, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
47 public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
49 private static final XMLOutputFactory XML_FACTORY;
52 XML_FACTORY = XMLOutputFactory.newFactory();
53 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
57 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
58 final MediaType mediaType) {
59 return type.equals(NormalizedNodeContext.class);
63 public long getSize(final NormalizedNodeContext t, final Class<?> type, final Type genericType,
64 final Annotation[] annotations, final MediaType mediaType) {
69 public void writeTo(final NormalizedNodeContext t, final Class<?> type, final Type genericType,
70 final Annotation[] annotations, final MediaType mediaType,
71 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException,
72 WebApplicationException {
73 InstanceIdentifierContext pathContext = t.getInstanceIdentifierContext();
74 if (t.getData() == null) {
75 throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
78 XMLStreamWriter xmlWriter;
80 xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream);
81 } catch (XMLStreamException e) {
82 throw new IllegalStateException(e);
83 } catch (FactoryConfigurationError e) {
84 throw new IllegalStateException(e);
86 NormalizedNode<?, ?> data = t.getData();
87 SchemaPath schemaPath = pathContext.getSchemaNode().getPath();
89 boolean isDataRoot = false;
90 if (SchemaPath.ROOT.equals(schemaPath)) {
93 schemaPath = schemaPath.getParent();
96 NormalizedNodeStreamWriter jsonWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
97 pathContext.getSchemaContext(), schemaPath);
98 NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(jsonWriter);
100 writeRootElement(xmlWriter, nnWriter, (ContainerNode) data);
102 if (data instanceof MapEntryNode) {
103 // Restconf allows returning one list item. We need to wrap it
104 // in map node in order to serialize it properly
105 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).addChild((MapEntryNode) data).build();
107 nnWriter.write(data);
112 private void writeRootElement(XMLStreamWriter xmlWriter, NormalizedNodeWriter nnWriter, ContainerNode data)
115 QName name = SchemaContext.NAME;
116 xmlWriter.writeStartElement(name.getNamespace().toString(), name.getLocalName());
117 for (DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
118 nnWriter.write(child);
121 xmlWriter.writeEndElement();
123 } catch (XMLStreamException e) {
124 Throwables.propagate(e);