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.SchemaPath;
46 import org.xml.sax.SAXException;
49 * Normalized node writer for XML.
51 * @deprecated This class will be replaced by NormalizedNodeXmlBodyWriter from restconf-nb-rfc8040
56 Draft02.MediaTypes.API + RestconfService.XML,
57 Draft02.MediaTypes.DATA + RestconfService.XML,
58 Draft02.MediaTypes.OPERATION + RestconfService.XML,
59 MediaType.APPLICATION_XML,
62 public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
64 private static final XMLOutputFactory XML_FACTORY;
67 XML_FACTORY = XMLOutputFactory.newFactory();
68 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
72 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
73 final MediaType mediaType) {
74 return type.equals(NormalizedNodeContext.class);
78 public long getSize(final NormalizedNodeContext context, final Class<?> type, final Type genericType,
79 final Annotation[] annotations, final MediaType mediaType) {
84 public void writeTo(final NormalizedNodeContext context, final Class<?> type, final Type genericType,
85 final Annotation[] annotations, final MediaType mediaType,
86 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException,
87 WebApplicationException {
88 for (final Entry<String, Object> entry : context.getNewHeaders().entrySet()) {
89 httpHeaders.add(entry.getKey(), entry.getValue());
91 final InstanceIdentifierContext pathContext = context.getInstanceIdentifierContext();
92 if (context.getData() == null) {
96 XMLStreamWriter xmlWriter;
98 xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name());
99 if (context.getWriterParameters().isPrettyPrint()) {
100 xmlWriter = new IndentingXMLStreamWriter(xmlWriter);
102 } catch (final XMLStreamException | FactoryConfigurationError e) {
103 throw new IllegalStateException(e);
105 final NormalizedNode data = context.getData();
106 final SchemaPath schemaPath = pathContext.getSchemaNode().getPath();
108 writeNormalizedNode(xmlWriter, schemaPath, pathContext, data, context.getWriterParameters().getDepth());
111 private static void writeNormalizedNode(final XMLStreamWriter xmlWriter, final SchemaPath schemaPath,
112 final InstanceIdentifierContext pathContext, NormalizedNode data, final @Nullable Integer depth)
114 final RestconfNormalizedNodeWriter nnWriter;
115 final EffectiveModelContext schemaCtx = pathContext.getSchemaContext();
116 if (SchemaPath.ROOT.equals(schemaPath)) {
117 nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, schemaPath, depth);
118 if (data instanceof DOMSourceAnyxmlNode) {
120 writeElements(xmlWriter, nnWriter,
121 (ContainerNode) NetconfUtil.transformDOMSourceToNormalizedNode(schemaCtx,
122 ((DOMSourceAnyxmlNode)data).body()).getResult());
123 } catch (XMLStreamException | URISyntaxException | SAXException e) {
124 throw new IOException("Cannot write anyxml", e);
127 writeElements(xmlWriter, nnWriter, (ContainerNode) data);
129 } else if (pathContext.getSchemaNode() instanceof RpcDefinition) {
130 final var rpc = (RpcDefinition) pathContext.getSchemaNode();
131 nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx,
132 SchemaPath.create(true, rpc.getQName(), rpc.getOutput().getQName()), depth);
133 writeElements(xmlWriter, nnWriter, (ContainerNode) data);
135 nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, schemaPath.getParent(), depth);
136 if (data instanceof MapEntryNode) {
137 // Restconf allows returning one list item. We need to wrap it
138 // in map node in order to serialize it properly
139 data = ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType())
140 .addChild((MapEntryNode) data)
143 nnWriter.write(data);
148 private static RestconfNormalizedNodeWriter createNormalizedNodeWriter(final XMLStreamWriter xmlWriter,
149 final EffectiveModelContext schemaContext, final SchemaPath schemaPath, final @Nullable Integer depth) {
150 final NormalizedNodeStreamWriter xmlStreamWriter =
151 XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, schemaContext, schemaPath);
153 return DepthAwareNormalizedNodeWriter.forStreamWriter(xmlStreamWriter, depth);
156 return RestconfDelegatingNormalizedNodeWriter.forStreamWriter(xmlStreamWriter);
159 private static void writeElements(final XMLStreamWriter xmlWriter, final RestconfNormalizedNodeWriter nnWriter,
160 final ContainerNode data) throws IOException {
161 final QName name = data.getIdentifier().getNodeType();
163 xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, name.getLocalName(),
164 name.getNamespace().toString());
165 xmlWriter.writeDefaultNamespace(name.getNamespace().toString());
166 for (final NormalizedNode child : data.body()) {
167 nnWriter.write(child);
170 xmlWriter.writeEndElement();
172 } catch (final XMLStreamException e) {
173 throw new IOException("Failed to write elements", e);