Bump MRI upstreams
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / rest / impl / NormalizedNodeJsonBodyWriter.java
index 7891d4037295d4e2a7abffec93bd7a34431db1bd..b671833c2510a8b20ace4d48d789542ab6805931 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.netconf.sal.rest.impl;
 
-import com.google.common.base.Optional;
 import com.google.gson.stream.JsonWriter;
 import java.io.IOException;
 import java.io.OutputStream;
@@ -15,6 +14,7 @@ import java.io.OutputStreamWriter;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.nio.charset.StandardCharsets;
 import java.util.Map.Entry;
 import javax.ws.rs.Produces;
@@ -23,18 +23,23 @@ import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.MessageBodyWriter;
 import javax.ws.rs.ext.Provider;
+import javax.xml.stream.XMLStreamException;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.netconf.sal.rest.api.Draft02;
 import org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter;
 import org.opendaylight.netconf.sal.rest.api.RestconfService;
-import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
-import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
+import org.opendaylight.netconf.util.NetconfUtil;
+import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
+import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
+import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
@@ -43,17 +48,21 @@ import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.xml.sax.SAXException;
 
 /**
  * Normalized node writer for JSON.
  *
- * @deprecated This class will be replaced by
- * {@link org.opendaylight.restconf.jersey.providers.NormalizedNodeJsonBodyWriter}
+ * @deprecated This class will be replaced by NormalizedNodeJsonBodyWriter from restconf-nb-rfc8040
  */
 @Deprecated
 @Provider
-@Produces({ Draft02.MediaTypes.API + RestconfService.JSON, Draft02.MediaTypes.DATA + RestconfService.JSON,
-        Draft02.MediaTypes.OPERATION + RestconfService.JSON, MediaType.APPLICATION_JSON })
+@Produces({
+    Draft02.MediaTypes.API + RestconfService.JSON,
+    Draft02.MediaTypes.DATA + RestconfService.JSON,
+    Draft02.MediaTypes.OPERATION + RestconfService.JSON,
+    MediaType.APPLICATION_JSON
+})
 public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
 
     private static final int DEFAULT_INDENT_SPACES_NUM = 2;
@@ -74,10 +83,12 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
     public void writeTo(final NormalizedNodeContext context, final Class<?> type, final Type genericType,
             final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders,
             final OutputStream entityStream) throws IOException, WebApplicationException {
-        for (final Entry<String, Object> entry : context.getNewHeaders().entrySet()) {
-            httpHeaders.add(entry.getKey(), entry.getValue());
+        if (httpHeaders != null) {
+            for (final Entry<String, Object> entry : context.getNewHeaders().entrySet()) {
+                httpHeaders.add(entry.getKey(), entry.getValue());
+            }
         }
-        final NormalizedNode<?, ?> data = context.getData();
+        NormalizedNode<?, ?> data = context.getData();
         if (data == null) {
             return;
         }
@@ -87,17 +98,18 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
                 (InstanceIdentifierContext<SchemaNode>) context.getInstanceIdentifierContext();
 
         final SchemaPath path = identifierCtx.getSchemaNode().getPath();
-        final JsonWriter jsonWriter = createJsonWriter(entityStream, context.getWriterParameters().isPrettyPrint());
-        jsonWriter.beginObject();
-        writeNormalizedNode(
-                jsonWriter,path,identifierCtx,data, Optional.fromNullable(context.getWriterParameters().getDepth()));
-        jsonWriter.endObject();
-        jsonWriter.flush();
+        try (JsonWriter jsonWriter = createJsonWriter(entityStream, context.getWriterParameters().isPrettyPrint())) {
+            jsonWriter.beginObject();
+            writeNormalizedNode(
+                    jsonWriter, path, identifierCtx, data, context.getWriterParameters().getDepth());
+            jsonWriter.endObject();
+            jsonWriter.flush();
+        }
     }
 
     private static void writeNormalizedNode(final JsonWriter jsonWriter, SchemaPath path,
             final InstanceIdentifierContext<SchemaNode> context, NormalizedNode<?, ?> data,
-            final Optional<Integer> depth) throws IOException {
+            final @Nullable Integer depth) throws IOException {
         final RestconfNormalizedNodeWriter nnWriter;
         if (SchemaPath.ROOT.equals(path)) {
             /*
@@ -105,7 +117,17 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
              *  which is not visible in restconf
              */
             nnWriter = createNormalizedNodeWriter(context,path,jsonWriter, depth);
-            writeChildren(nnWriter,(ContainerNode) data);
+            if (data instanceof ContainerNode) {
+                writeChildren(nnWriter,(ContainerNode) data);
+            } else if (data instanceof DOMSourceAnyxmlNode) {
+                try {
+                    writeChildren(nnWriter,
+                            (ContainerNode) NetconfUtil.transformDOMSourceToNormalizedNode(
+                                    context.getSchemaContext(), ((DOMSourceAnyxmlNode)data).getValue()).getResult());
+                } catch (XMLStreamException | URISyntaxException | SAXException e) {
+                    throw new IOException("Cannot write anyxml.", e);
+                }
+            }
         } else if (context.getSchemaNode() instanceof RpcDefinition) {
             /*
              *  RpcDefinition is not supported as initial codec in JSONStreamWriter,
@@ -138,24 +160,22 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
 
     private static RestconfNormalizedNodeWriter createNormalizedNodeWriter(
             final InstanceIdentifierContext<SchemaNode> context, final SchemaPath path, final JsonWriter jsonWriter,
-            final Optional<Integer> depth) {
+            final @Nullable Integer depth) {
 
         final SchemaNode schema = context.getSchemaNode();
         final JSONCodecFactory codecs = getCodecFactory(context);
 
         final URI initialNs;
         if (schema instanceof DataSchemaNode && !((DataSchemaNode)schema).isAugmenting()
-                && !(schema instanceof SchemaContext)) {
-            initialNs = schema.getQName().getNamespace();
-        } else if (schema instanceof RpcDefinition) {
+                && !(schema instanceof SchemaContext) || schema instanceof RpcDefinition) {
             initialNs = schema.getQName().getNamespace();
         } else {
             initialNs = null;
         }
         final NormalizedNodeStreamWriter streamWriter =
                 JSONNormalizedNodeStreamWriter.createNestedWriter(codecs, path, initialNs, jsonWriter);
-        if (depth.isPresent()) {
-            return DepthAwareNormalizedNodeWriter.forStreamWriter(streamWriter, depth.get());
+        if (depth != null) {
+            return DepthAwareNormalizedNodeWriter.forStreamWriter(streamWriter, depth);
         }
 
         return RestconfDelegatingNormalizedNodeWriter.forStreamWriter(streamWriter);
@@ -172,6 +192,6 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
 
     private static JSONCodecFactory getCodecFactory(final InstanceIdentifierContext<?> context) {
         // TODO: Performance: Cache JSON Codec factory and schema context
-        return JSONCodecFactory.getShared(context.getSchemaContext());
+        return JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(context.getSchemaContext());
     }
 }