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 java.io.IOException;
11 import java.io.OutputStream;
12 import java.lang.annotation.Annotation;
13 import java.lang.reflect.Type;
14 import java.net.URISyntaxException;
15 import java.nio.charset.StandardCharsets;
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.eclipse.jdt.annotation.Nullable;
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.util.NetconfUtil;
34 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
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.DOMSourceAnyxmlNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
41 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
42 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
43 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
44 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
47 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
48 import org.xml.sax.SAXException;
51 * Normalized node writer for XML.
53 * @deprecated This class will be replaced by NormalizedNodeXmlBodyWriter from restconf-nb-rfc8040
58 Draft02.MediaTypes.API + RestconfService.XML,
59 Draft02.MediaTypes.DATA + RestconfService.XML,
60 Draft02.MediaTypes.OPERATION + RestconfService.XML,
61 MediaType.APPLICATION_XML,
64 public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
66 private static final XMLOutputFactory XML_FACTORY;
69 XML_FACTORY = XMLOutputFactory.newFactory();
70 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
74 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
75 final MediaType mediaType) {
76 return type.equals(NormalizedNodeContext.class);
80 public long getSize(final NormalizedNodeContext context, final Class<?> type, final Type genericType,
81 final Annotation[] annotations, final MediaType mediaType) {
86 public void writeTo(final NormalizedNodeContext context, final Class<?> type, final Type genericType,
87 final Annotation[] annotations, final MediaType mediaType,
88 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException,
89 WebApplicationException {
90 for (final Entry<String, Object> entry : context.getNewHeaders().entrySet()) {
91 httpHeaders.add(entry.getKey(), entry.getValue());
93 final InstanceIdentifierContext<?> pathContext = context.getInstanceIdentifierContext();
94 if (context.getData() == null) {
98 XMLStreamWriter xmlWriter;
100 xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name());
101 if (context.getWriterParameters().isPrettyPrint()) {
102 xmlWriter = new IndentingXMLStreamWriter(xmlWriter);
104 } catch (final XMLStreamException | FactoryConfigurationError e) {
105 throw new IllegalStateException(e);
107 final NormalizedNode data = context.getData();
109 writeNormalizedNode(xmlWriter, pathContext, data, context.getWriterParameters().getDepth());
112 private static void writeNormalizedNode(final XMLStreamWriter xmlWriter,
113 final InstanceIdentifierContext<?> pathContext, NormalizedNode data, final @Nullable Integer depth)
115 final RestconfNormalizedNodeWriter nnWriter;
116 final EffectiveModelContext schemaCtx = pathContext.getSchemaContext();
117 if (pathContext.getSchemaNode() instanceof SchemaContext) {
118 nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, SchemaPath.ROOT, depth);
119 if (data instanceof DOMSourceAnyxmlNode) {
121 writeElements(xmlWriter, nnWriter,
122 (ContainerNode) NetconfUtil.transformDOMSourceToNormalizedNode(schemaCtx,
123 ((DOMSourceAnyxmlNode)data).body()).getResult());
124 } catch (XMLStreamException | URISyntaxException | SAXException e) {
125 throw new IOException("Cannot write anyxml", e);
128 writeElements(xmlWriter, nnWriter, (ContainerNode) data);
130 } else if (pathContext.getSchemaNode() instanceof RpcDefinition) {
131 final RpcDefinition rpc = (RpcDefinition) pathContext.getSchemaNode();
132 final SchemaPath path = SchemaPath.of(Absolute.of(rpc.getQName(), rpc.getOutput().getQName()));
133 nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, path, depth);
134 writeElements(xmlWriter, nnWriter, (ContainerNode) data);
136 final SchemaPath path;
137 if (pathContext.getSchemaNodeIdentifier() == null) {
138 path = SchemaPath.ROOT;
140 path = SchemaPath.of(pathContext.getSchemaNodeIdentifier()).getParent();
142 nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, path, depth);
143 if (data instanceof MapEntryNode) {
144 // Restconf allows returning one list item. We need to wrap it
145 // in map node in order to serialize it properly
146 data = ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType())
147 .addChild((MapEntryNode) data)
150 nnWriter.write(data);
155 private static RestconfNormalizedNodeWriter createNormalizedNodeWriter(final XMLStreamWriter xmlWriter,
156 final EffectiveModelContext schemaContext, final SchemaPath schemaPath, final @Nullable Integer depth) {
157 final NormalizedNodeStreamWriter xmlStreamWriter =
158 XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, schemaContext, schemaPath);
160 return DepthAwareNormalizedNodeWriter.forStreamWriter(xmlStreamWriter, depth);
163 return RestconfDelegatingNormalizedNodeWriter.forStreamWriter(xmlStreamWriter);
166 private static void writeElements(final XMLStreamWriter xmlWriter, final RestconfNormalizedNodeWriter nnWriter,
167 final ContainerNode data) throws IOException {
168 final QName name = data.getIdentifier().getNodeType();
170 xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, name.getLocalName(),
171 name.getNamespace().toString());
172 xmlWriter.writeDefaultNamespace(name.getNamespace().toString());
173 for (final NormalizedNode child : data.body()) {
174 nnWriter.write(child);
177 xmlWriter.writeEndElement();
179 } catch (final XMLStreamException e) {
180 throw new IOException("Failed to write elements", e);