943a93c6cefa3947172b259c33daef7cf30f890e
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / rest / impl / NormalizedNodeXmlBodyWriter.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.sal.rest.impl;
9
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.restconf.common.context.NormalizedNodeContext;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
42 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
43 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
44 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
45 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
46 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
47 import org.xml.sax.SAXException;
48
49 /**
50  * Normalized node writer for XML.
51  *
52  * @deprecated This class will be replaced by NormalizedNodeXmlBodyWriter from restconf-nb-rfc8040
53  */
54 @Deprecated
55 @Provider
56 @Produces({
57     Draft02.MediaTypes.API + RestconfService.XML,
58     Draft02.MediaTypes.DATA + RestconfService.XML,
59     Draft02.MediaTypes.OPERATION + RestconfService.XML,
60     MediaType.APPLICATION_XML,
61     MediaType.TEXT_XML
62 })
63 public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
64
65     private static final XMLOutputFactory XML_FACTORY;
66
67     static {
68         XML_FACTORY = XMLOutputFactory.newFactory();
69         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
70     }
71
72     @Override
73     public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
74             final MediaType mediaType) {
75         return type.equals(NormalizedNodeContext.class);
76     }
77
78     @Override
79     public long getSize(final NormalizedNodeContext context, final Class<?> type, final Type genericType,
80             final Annotation[] annotations, final MediaType mediaType) {
81         return -1;
82     }
83
84     @Override
85     public void writeTo(final NormalizedNodeContext context, final Class<?> type, final Type genericType,
86             final Annotation[] annotations, final MediaType mediaType,
87             final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException,
88             WebApplicationException {
89         for (final Entry<String, Object> entry : context.getNewHeaders().entrySet()) {
90             httpHeaders.add(entry.getKey(), entry.getValue());
91         }
92         final InstanceIdentifierContext<?> pathContext = context.getInstanceIdentifierContext();
93         if (context.getData() == null) {
94             return;
95         }
96
97         XMLStreamWriter xmlWriter;
98         try {
99             xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name());
100             if (context.getWriterParameters().isPrettyPrint()) {
101                 xmlWriter = new IndentingXMLStreamWriter(xmlWriter);
102             }
103         } catch (final XMLStreamException | FactoryConfigurationError e) {
104             throw new IllegalStateException(e);
105         }
106         final NormalizedNode data = context.getData();
107         final SchemaPath schemaPath = pathContext.getSchemaNode().getPath();
108
109         writeNormalizedNode(xmlWriter, schemaPath, pathContext, data, context.getWriterParameters().getDepth());
110     }
111
112     private static void writeNormalizedNode(final XMLStreamWriter xmlWriter, final SchemaPath schemaPath,
113             final InstanceIdentifierContext<?> pathContext, NormalizedNode data, final @Nullable Integer depth)
114             throws IOException {
115         final RestconfNormalizedNodeWriter nnWriter;
116         final EffectiveModelContext schemaCtx = pathContext.getSchemaContext();
117         if (SchemaPath.ROOT.equals(schemaPath)) {
118             nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, schemaPath, depth);
119             if (data instanceof DOMSourceAnyxmlNode) {
120                 try {
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);
126                 }
127             } else {
128                 writeElements(xmlWriter, nnWriter, (ContainerNode) data);
129             }
130         }  else if (pathContext.getSchemaNode() instanceof RpcDefinition) {
131             nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx,
132                     ((RpcDefinition) pathContext.getSchemaNode()).getOutput().getPath(), depth);
133             writeElements(xmlWriter, nnWriter, (ContainerNode) data);
134         } else {
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)
141                     .build();
142             }
143             nnWriter.write(data);
144         }
145         nnWriter.flush();
146     }
147
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);
152         if (depth != null) {
153             return DepthAwareNormalizedNodeWriter.forStreamWriter(xmlStreamWriter, depth);
154         }
155
156         return RestconfDelegatingNormalizedNodeWriter.forStreamWriter(xmlStreamWriter);
157     }
158
159     private static void writeElements(final XMLStreamWriter xmlWriter, final RestconfNormalizedNodeWriter nnWriter,
160             final ContainerNode data) throws IOException {
161         final QName name = data.getIdentifier().getNodeType();
162         try {
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);
168             }
169             nnWriter.flush();
170             xmlWriter.writeEndElement();
171             xmlWriter.flush();
172         } catch (final XMLStreamException e) {
173             throw new IOException("Failed to write elements", e);
174         }
175     }
176 }