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.netconf.sal.rest.impl;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Throwables;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Type;
16 import javanet.staxutils.IndentingXMLStreamWriter;
17 import javax.ws.rs.Produces;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.MultivaluedMap;
21 import javax.ws.rs.ext.MessageBodyWriter;
22 import javax.ws.rs.ext.Provider;
23 import javax.xml.XMLConstants;
24 import javax.xml.stream.FactoryConfigurationError;
25 import javax.xml.stream.XMLOutputFactory;
26 import javax.xml.stream.XMLStreamException;
27 import javax.xml.stream.XMLStreamWriter;
28 import org.opendaylight.netconf.sal.rest.api.Draft02;
29 import org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter;
30 import org.opendaylight.netconf.sal.rest.api.RestconfService;
31 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
32 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
33 import org.opendaylight.restconf.Draft15;
34 import org.opendaylight.restconf.utils.RestconfConstants;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
40 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter;
41 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
42 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
47 @Produces({ Draft02.MediaTypes.API + RestconfService.XML, Draft02.MediaTypes.DATA + RestconfService.XML,
48 Draft02.MediaTypes.OPERATION + RestconfService.XML,
49 Draft15.MediaTypes.API + RestconfConstants.XML, Draft15.MediaTypes.DATA + RestconfConstants.XML,
50 Draft15.MediaTypes.OPERATION + RestconfConstants.XML, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
51 public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
53 private static final XMLOutputFactory XML_FACTORY;
56 XML_FACTORY = XMLOutputFactory.newFactory();
57 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
61 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
62 final MediaType mediaType) {
63 return type.equals(NormalizedNodeContext.class);
67 public long getSize(final NormalizedNodeContext t, final Class<?> type, final Type genericType,
68 final Annotation[] annotations, final MediaType mediaType) {
73 public void writeTo(final NormalizedNodeContext t, final Class<?> type, final Type genericType,
74 final Annotation[] annotations, final MediaType mediaType,
75 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException,
76 WebApplicationException {
77 final InstanceIdentifierContext<?> pathContext = t.getInstanceIdentifierContext();
78 if (t.getData() == null) {
82 XMLStreamWriter xmlWriter;
84 xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream);
85 if (t.getWriterParameters().isPrettyPrint()) {
86 xmlWriter = new IndentingXMLStreamWriter(xmlWriter);
88 } catch (final XMLStreamException e) {
89 throw new IllegalStateException(e);
90 } catch (final FactoryConfigurationError e) {
91 throw new IllegalStateException(e);
93 final NormalizedNode<?, ?> data = t.getData();
94 final SchemaPath schemaPath = pathContext.getSchemaNode().getPath();
98 writeNormalizedNode(xmlWriter, schemaPath, pathContext, data, t.getWriterParameters().getDepth());
102 private void writeNormalizedNode(final XMLStreamWriter xmlWriter, final SchemaPath schemaPath, final InstanceIdentifierContext<?>
103 pathContext, NormalizedNode<?, ?> data, final Optional<Integer> depth) throws IOException {
104 final RestconfNormalizedNodeWriter nnWriter;
105 final SchemaContext schemaCtx = pathContext.getSchemaContext();
106 if (SchemaPath.ROOT.equals(schemaPath)) {
107 nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, schemaPath, depth);
108 writeElements(xmlWriter, nnWriter, (ContainerNode) data);
109 } else if (pathContext.getSchemaNode() instanceof RpcDefinition) {
110 nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx,
111 ((RpcDefinition) pathContext.getSchemaNode()).getOutput().getPath(), depth);
112 writeElements(xmlWriter, nnWriter, (ContainerNode) data);
114 nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, schemaPath.getParent(), depth);
115 if (data instanceof MapEntryNode) {
116 // Restconf allows returning one list item. We need to wrap it
117 // in map node in order to serialize it properly
118 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).addChild((MapEntryNode) data).build();
120 nnWriter.write(data);
125 private RestconfNormalizedNodeWriter createNormalizedNodeWriter(final XMLStreamWriter xmlWriter,
126 final SchemaContext schemaContext, final SchemaPath schemaPath, final Optional<Integer> depth) {
127 final NormalizedNodeStreamWriter xmlStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, schemaContext, schemaPath);
128 if (depth.isPresent()) {
129 return DepthAwareNormalizedNodeWriter.forStreamWriter(xmlStreamWriter, depth.get());
131 return RestconfDelegatingNormalizedNodeWriter.forStreamWriter(xmlStreamWriter);
135 private void writeElements(final XMLStreamWriter xmlWriter, final RestconfNormalizedNodeWriter nnWriter,
136 final ContainerNode data)
139 final QName name = data.getNodeType();
140 xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, name.getLocalName(), name.getNamespace().toString());
141 xmlWriter.writeDefaultNamespace(name.getNamespace().toString());
142 for(final NormalizedNode<?,?> child : data.getValue()) {
143 nnWriter.write(child);
146 xmlWriter.writeEndElement();
148 } catch (final XMLStreamException e) {
149 Throwables.propagate(e);