*/
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;
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;
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.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.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
@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;
httpHeaders.add(entry.getKey(), entry.getValue());
}
}
- final NormalizedNode<?, ?> data = context.getData();
+ NormalizedNode<?, ?> data = context.getData();
if (data == null) {
return;
}
try (JsonWriter jsonWriter = createJsonWriter(entityStream, context.getWriterParameters().isPrettyPrint())) {
jsonWriter.beginObject();
writeNormalizedNode(
- jsonWriter, path, identifierCtx, data,
- Optional.fromNullable(context.getWriterParameters().getDepth()));
+ 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)) {
/*
* 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,
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);