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.nio.charset.StandardCharsets;
17 import java.util.Map.Entry;
18 import javanet.staxutils.IndentingXMLStreamWriter;
19 import javax.ws.rs.Produces;
20 import javax.ws.rs.WebApplicationException;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.MultivaluedMap;
23 import javax.ws.rs.ext.MessageBodyWriter;
24 import javax.ws.rs.ext.Provider;
25 import javax.xml.XMLConstants;
26 import javax.xml.stream.FactoryConfigurationError;
27 import javax.xml.stream.XMLOutputFactory;
28 import javax.xml.stream.XMLStreamException;
29 import javax.xml.stream.XMLStreamWriter;
30 import org.opendaylight.netconf.sal.rest.api.Draft02;
31 import org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter;
32 import org.opendaylight.netconf.sal.rest.api.RestconfService;
33 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
34 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
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 * Normalized node writer for XML.
49 * @deprecated This class will be replaced by
50 * {@link org.opendaylight.restconf.jersey.providers.NormalizedNodeXmlBodyWriter}
54 @Produces({ Draft02.MediaTypes.API + RestconfService.XML, Draft02.MediaTypes.DATA + RestconfService.XML,
55 Draft02.MediaTypes.OPERATION + RestconfService.XML, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
56 public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
58 private static final XMLOutputFactory XML_FACTORY;
61 XML_FACTORY = XMLOutputFactory.newFactory();
62 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
66 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
67 final MediaType mediaType) {
68 return type.equals(NormalizedNodeContext.class);
72 public long getSize(final NormalizedNodeContext context, final Class<?> type, final Type genericType,
73 final Annotation[] annotations, final MediaType mediaType) {
78 public void writeTo(final NormalizedNodeContext context, final Class<?> type, final Type genericType,
79 final Annotation[] annotations, final MediaType mediaType,
80 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException,
81 WebApplicationException {
82 for (final Entry<String, Object> entry : context.getNewHeaders().entrySet()) {
83 httpHeaders.add(entry.getKey(), entry.getValue());
85 final InstanceIdentifierContext<?> pathContext = context.getInstanceIdentifierContext();
86 if (context.getData() == null) {
90 XMLStreamWriter xmlWriter;
92 xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name());
93 if (context.getWriterParameters().isPrettyPrint()) {
94 xmlWriter = new IndentingXMLStreamWriter(xmlWriter);
96 } catch (final XMLStreamException e) {
97 throw new IllegalStateException(e);
98 } catch (final FactoryConfigurationError e) {
99 throw new IllegalStateException(e);
101 final NormalizedNode<?, ?> data = context.getData();
102 final SchemaPath schemaPath = pathContext.getSchemaNode().getPath();
104 writeNormalizedNode(xmlWriter, schemaPath, pathContext, data,
105 Optional.fromNullable(context.getWriterParameters().getDepth()));
108 private static void writeNormalizedNode(final XMLStreamWriter xmlWriter, final SchemaPath schemaPath,
109 final InstanceIdentifierContext<?> pathContext, NormalizedNode<?, ?> data, final Optional<Integer> depth)
111 final RestconfNormalizedNodeWriter nnWriter;
112 final SchemaContext schemaCtx = pathContext.getSchemaContext();
113 if (SchemaPath.ROOT.equals(schemaPath)) {
114 nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, schemaPath, depth);
115 writeElements(xmlWriter, nnWriter, (ContainerNode) data);
116 } else if (pathContext.getSchemaNode() instanceof RpcDefinition) {
117 nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx,
118 ((RpcDefinition) pathContext.getSchemaNode()).getOutput().getPath(), depth);
119 writeElements(xmlWriter, nnWriter, (ContainerNode) data);
121 nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, schemaPath.getParent(), depth);
122 if (data instanceof MapEntryNode) {
123 // Restconf allows returning one list item. We need to wrap it
124 // in map node in order to serialize it properly
125 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).addChild((MapEntryNode) data).build();
127 nnWriter.write(data);
132 private static RestconfNormalizedNodeWriter createNormalizedNodeWriter(final XMLStreamWriter xmlWriter,
133 final SchemaContext schemaContext, final SchemaPath schemaPath, final Optional<Integer> depth) {
134 final NormalizedNodeStreamWriter xmlStreamWriter =
135 XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, schemaContext, schemaPath);
136 if (depth.isPresent()) {
137 return DepthAwareNormalizedNodeWriter.forStreamWriter(xmlStreamWriter, depth.get());
140 return RestconfDelegatingNormalizedNodeWriter.forStreamWriter(xmlStreamWriter);
143 private static void writeElements(final XMLStreamWriter xmlWriter, final RestconfNormalizedNodeWriter nnWriter,
144 final ContainerNode data)
147 final QName name = data.getNodeType();
148 xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, name.getLocalName(),
149 name.getNamespace().toString());
150 xmlWriter.writeDefaultNamespace(name.getNamespace().toString());
151 for (final NormalizedNode<?,?> child : data.getValue()) {
152 nnWriter.write(child);
155 xmlWriter.writeEndElement();
157 } catch (final XMLStreamException e) {
158 Throwables.propagate(e);