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 java.util.Map.Entry;
17 import javanet.staxutils.IndentingXMLStreamWriter;
18 import javax.ws.rs.Produces;
19 import javax.ws.rs.WebApplicationException;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.MultivaluedMap;
22 import javax.ws.rs.ext.MessageBodyWriter;
23 import javax.ws.rs.ext.Provider;
24 import javax.xml.XMLConstants;
25 import javax.xml.stream.FactoryConfigurationError;
26 import javax.xml.stream.XMLOutputFactory;
27 import javax.xml.stream.XMLStreamException;
28 import javax.xml.stream.XMLStreamWriter;
29 import org.opendaylight.netconf.sal.rest.api.Draft02;
30 import org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter;
31 import org.opendaylight.netconf.sal.rest.api.RestconfService;
32 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
33 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
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.RpcDefinition;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 * @deprecated This class will be replaced by
47 * {@link org.opendaylight.restconf.jersey.providers.NormalizedNodeXmlBodyWriter}
51 @Produces({ Draft02.MediaTypes.API + RestconfService.XML, Draft02.MediaTypes.DATA + RestconfService.XML,
52 Draft02.MediaTypes.OPERATION + RestconfService.XML, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
53 public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
55 private static final XMLOutputFactory XML_FACTORY;
58 XML_FACTORY = XMLOutputFactory.newFactory();
59 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
63 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
64 final MediaType mediaType) {
65 return type.equals(NormalizedNodeContext.class);
69 public long getSize(final NormalizedNodeContext t, final Class<?> type, final Type genericType,
70 final Annotation[] annotations, final MediaType mediaType) {
75 public void writeTo(final NormalizedNodeContext t, final Class<?> type, final Type genericType,
76 final Annotation[] annotations, final MediaType mediaType,
77 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException,
78 WebApplicationException {
79 for (final Entry<String, Object> entry : t.getNewHeaders().entrySet()) {
80 httpHeaders.add(entry.getKey(), entry.getValue());
82 final InstanceIdentifierContext<?> pathContext = t.getInstanceIdentifierContext();
83 if (t.getData() == null) {
87 XMLStreamWriter xmlWriter;
89 xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream);
90 if (t.getWriterParameters().isPrettyPrint()) {
91 xmlWriter = new IndentingXMLStreamWriter(xmlWriter);
93 } catch (final XMLStreamException e) {
94 throw new IllegalStateException(e);
95 } catch (final FactoryConfigurationError e) {
96 throw new IllegalStateException(e);
98 final NormalizedNode<?, ?> data = t.getData();
99 final SchemaPath schemaPath = pathContext.getSchemaNode().getPath();
101 writeNormalizedNode(xmlWriter, schemaPath, pathContext, data, Optional.fromNullable(t.getWriterParameters().getDepth()));
104 private void writeNormalizedNode(final XMLStreamWriter xmlWriter, final SchemaPath schemaPath,
105 final InstanceIdentifierContext<?> pathContext, NormalizedNode<?, ?> data, final Optional<Integer> depth)
107 final RestconfNormalizedNodeWriter nnWriter;
108 final SchemaContext schemaCtx = pathContext.getSchemaContext();
109 if (SchemaPath.ROOT.equals(schemaPath)) {
110 nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, schemaPath, depth);
111 writeElements(xmlWriter, nnWriter, (ContainerNode) data);
112 } else if (pathContext.getSchemaNode() instanceof RpcDefinition) {
113 nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx,
114 ((RpcDefinition) pathContext.getSchemaNode()).getOutput().getPath(), depth);
115 writeElements(xmlWriter, nnWriter, (ContainerNode) data);
117 nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, schemaPath.getParent(), depth);
118 if (data instanceof MapEntryNode) {
119 // Restconf allows returning one list item. We need to wrap it
120 // in map node in order to serialize it properly
121 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).addChild((MapEntryNode) data).build();
123 nnWriter.write(data);
128 private RestconfNormalizedNodeWriter createNormalizedNodeWriter(final XMLStreamWriter xmlWriter,
129 final SchemaContext schemaContext, final SchemaPath schemaPath, final Optional<Integer> depth) {
130 final NormalizedNodeStreamWriter xmlStreamWriter =
131 XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, schemaContext, schemaPath);
132 if (depth.isPresent()) {
133 return DepthAwareNormalizedNodeWriter.forStreamWriter(xmlStreamWriter, depth.get());
135 return RestconfDelegatingNormalizedNodeWriter.forStreamWriter(xmlStreamWriter);
139 private void writeElements(final XMLStreamWriter xmlWriter, final RestconfNormalizedNodeWriter nnWriter,
140 final ContainerNode data)
143 final QName name = data.getNodeType();
144 xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, name.getLocalName(),
145 name.getNamespace().toString());
146 xmlWriter.writeDefaultNamespace(name.getNamespace().toString());
147 for(final NormalizedNode<?,?> child : data.getValue()) {
148 nnWriter.write(child);
151 xmlWriter.writeEndElement();
153 } catch (final XMLStreamException e) {
154 Throwables.propagate(e);