From: Jan Hajnar Date: Thu, 21 May 2015 07:46:57 +0000 (+0200) Subject: Bug 2153 - pretty printer X-Git-Tag: release/lithium~40 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=d40d77c862ece5276cee68073a13386bbc5a347a Bug 2153 - pretty printer NormalizedNodeJsonBodyWriter - added ability to create JsonWritter with prettyPrint enabled NormalizedNodeXmlBodyWriter - added IndentingXMLStreamWriter when prettyPrint is enabled NormalizedNodeContext - info about requirements on indentation (true | false) was added RestconfImpl - added pretty print parsing method and added pretty print detection to GET methods and RPC call methods Change-Id: I44cfa778279d67f7dc1bf1532cd7fc1087b492dc Signed-off-by: Jozef Gloncak Signed-off-by: Jan Hajnar --- diff --git a/opendaylight/md-sal/sal-rest-connector/pom.xml b/opendaylight/md-sal/sal-rest-connector/pom.xml index 3fcecfd929..ee13e1c898 100644 --- a/opendaylight/md-sal/sal-rest-connector/pom.xml +++ b/opendaylight/md-sal/sal-rest-connector/pom.xml @@ -102,6 +102,12 @@ 0.7.0-SNAPSHOT + + net.java.dev.stax-utils + stax-utils + 20070216 + + ch.qos.logback logback-classic @@ -143,6 +149,7 @@ *, com.sun.jersey.spi.container.servlet, org.eclipse.jetty.servlets + stax-utils /restconf diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeJsonBodyWriter.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeJsonBodyWriter.java index 5c17f2a14a..2fa37c7745 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeJsonBodyWriter.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeJsonBodyWriter.java @@ -47,6 +47,8 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath; Draft02.MediaTypes.OPERATION + RestconfService.JSON, MediaType.APPLICATION_JSON }) public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter { + private static final int DEFAULT_INDENT_SPACES_NUM = 2; + @Override public boolean isWriteable(final Class type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) { return type.equals(NormalizedNodeContext.class); @@ -70,7 +72,7 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter context = (InstanceIdentifierContext) t.getInstanceIdentifierContext(); SchemaPath path = context.getSchemaNode().getPath(); - final JsonWriter jsonWriter = createJsonWriter(entityStream); + final JsonWriter jsonWriter = createJsonWriter(entityStream, t.getWriterParameters().isPrettyPrint()); jsonWriter.beginObject(); writeNormalizedNode(jsonWriter,path,context,data); jsonWriter.endObject(); @@ -136,10 +138,13 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter context) { diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeXmlBodyWriter.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeXmlBodyWriter.java index f6b7027b34..9a540e72e9 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeXmlBodyWriter.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeXmlBodyWriter.java @@ -12,6 +12,7 @@ import java.io.IOException; import java.io.OutputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Type; +import javanet.staxutils.IndentingXMLStreamWriter; import javax.ws.rs.Produces; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; @@ -76,6 +77,9 @@ public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter context; private final NormalizedNode data; + private final WriterParameters writerParameters; - public NormalizedNodeContext(final InstanceIdentifierContext context, final NormalizedNode data) { + public NormalizedNodeContext(final InstanceIdentifierContext context, + final NormalizedNode data, WriterParameters writerParameters) { this.context = context; this.data = data; + this.writerParameters = writerParameters; + } + + public NormalizedNodeContext(final InstanceIdentifierContext context, + final NormalizedNode data) { + this.context = context; + this.data = data; + // default writer parameters + this.writerParameters = new WriterParameters(false, Integer.MAX_VALUE); } public InstanceIdentifierContext getInstanceIdentifierContext() { @@ -20,4 +31,8 @@ public class NormalizedNodeContext { public NormalizedNode getData() { return data; } + + public WriterParameters getWriterParameters() { + return writerParameters; + } } diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/QueryParametersParser.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/QueryParametersParser.java new file mode 100644 index 0000000000..b567d2a1ee --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/QueryParametersParser.java @@ -0,0 +1,56 @@ +/** + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.sal.restconf.impl; + +import com.google.common.base.Strings; +import javax.ws.rs.core.UriInfo; + +public class QueryParametersParser { + + private enum UriParameters { + PRETTY_PRINT("prettyPrint"), + DEPTH("depth"); + + private String uriParameterName; + + UriParameters(final String uriParameterName) { + this.uriParameterName = uriParameterName; + } + + @Override + public String toString() { + return uriParameterName; + } + } + + public static WriterParameters parseKnownWriterParameters(final UriInfo info) { + boolean prettyPrint; + int depth; + String param = info.getQueryParameters(false).getFirst(UriParameters.DEPTH.toString()); + if (Strings.isNullOrEmpty(param) || "unbounded".equals(param)) { + depth = Integer.MAX_VALUE; + } else { + try { + depth = Integer.valueOf(param); + if (depth < 1) { + throw new RestconfDocumentedException(new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE, + "Invalid depth parameter: " + depth, null, + "The depth parameter must be an integer > 1 or \"unbounded\"")); + } + } catch (final NumberFormatException e) { + throw new RestconfDocumentedException(new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE, + "Invalid depth parameter: " + e.getMessage(), null, + "The depth parameter must be an integer > 1 or \"unbounded\"")); + } + } + param = info.getQueryParameters(false).getFirst(UriParameters.PRETTY_PRINT.toString()); + prettyPrint = "true".equals(param); + return new WriterParameters(prettyPrint, depth); + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java index d37f8dda5c..38d41ddb2e 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java @@ -103,22 +103,6 @@ import org.slf4j.LoggerFactory; public class RestconfImpl implements RestconfService { - private enum UriParameters { - PRETTY_PRINT("prettyPrint"), - DEPTH("depth"); - - private String uriParameterName; - - UriParameters(final String uriParameterName) { - this.uriParameterName = uriParameterName; - } - - @Override - public String toString() { - return uriParameterName; - } - } - private static final RestconfImpl INSTANCE = new RestconfImpl(); private static final int NOTIFICATION_PORT = 8181; @@ -209,7 +193,8 @@ public class RestconfImpl implements RestconfService { moduleContainerBuilder.withChild(allModuleMap); return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, modulesSchemaNode, - null, schemaContext), moduleContainerBuilder.build()); + null, schemaContext), moduleContainerBuilder.build(), + QueryParametersParser.parseKnownWriterParameters(uriInfo)); } /** @@ -240,7 +225,8 @@ public class RestconfImpl implements RestconfService { moduleContainerBuilder.withChild(mountPointModulesMap); return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, modulesSchemaNode, - mountPoint, controllerContext.getGlobalSchema()), moduleContainerBuilder.build()); + mountPoint, controllerContext.getGlobalSchema()), moduleContainerBuilder.build(), + QueryParametersParser.parseKnownWriterParameters(uriInfo)); } @Override @@ -276,7 +262,7 @@ public class RestconfImpl implements RestconfService { Preconditions.checkState(moduleSchemaNode instanceof ListSchemaNode); return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, moduleSchemaNode, mountPoint, - schemaContext), moduleMap); + schemaContext), moduleMap, QueryParametersParser.parseKnownWriterParameters(uriInfo)); } @Override @@ -305,7 +291,7 @@ public class RestconfImpl implements RestconfService { return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, streamsContainerSchemaNode, null, - schemaContext), streamsContainerBuilder.build()); + schemaContext), streamsContainerBuilder.build(), QueryParametersParser.parseKnownWriterParameters(uriInfo)); } @Override @@ -475,7 +461,8 @@ public class RestconfImpl implements RestconfService { } return new NormalizedNodeContext(new InstanceIdentifierContext(null, - resultNodeSchema, mountPoint, schemaContext), resultData); + resultNodeSchema, mountPoint, schemaContext), resultData, + QueryParametersParser.parseKnownWriterParameters(uriInfo)); } private DOMRpcResult checkRpcResponse(final CheckedFuture response) { @@ -653,7 +640,7 @@ public class RestconfImpl implements RestconfService { } return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, resultNodeSchema, mountPoint, - schemaContext), resultData); + schemaContext), resultData, QueryParametersParser.parseKnownWriterParameters(uriInfo)); } private RpcDefinition findRpc(final SchemaContext schemaContext, final String identifierDecoded) { @@ -691,35 +678,11 @@ public class RestconfImpl implements RestconfService { LOG.debug(errMsg + identifier); throw new RestconfDocumentedException(errMsg, ErrorType.APPLICATION, ErrorTag.DATA_MISSING); } - return new NormalizedNodeContext(iiWithData, data); - } - - // FIXME: Move this to proper place - @SuppressWarnings("unused") - private Integer parseDepthParameter(final UriInfo info) { - final String param = info.getQueryParameters(false).getFirst(UriParameters.DEPTH.toString()); - if (Strings.isNullOrEmpty(param) || "unbounded".equals(param)) { - return null; - } - - try { - final Integer depth = Integer.valueOf(param); - if (depth < 1) { - throw new RestconfDocumentedException(new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, - "Invalid depth parameter: " + depth, null, - "The depth parameter must be an integer > 1 or \"unbounded\"")); - } - - return depth; - } catch (final NumberFormatException e) { - throw new RestconfDocumentedException(new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, - "Invalid depth parameter: " + e.getMessage(), null, - "The depth parameter must be an integer > 1 or \"unbounded\"")); - } + return new NormalizedNodeContext(iiWithData, data, QueryParametersParser.parseKnownWriterParameters(uriInfo)); } @Override - public NormalizedNodeContext readOperationalData(final String identifier, final UriInfo info) { + public NormalizedNodeContext readOperationalData(final String identifier, final UriInfo uriInfo) { final InstanceIdentifierContext iiWithData = controllerContext.toInstanceIdentifier(identifier); final DOMMountPoint mountPoint = iiWithData.getMountPoint(); NormalizedNode data = null; @@ -734,7 +697,7 @@ public class RestconfImpl implements RestconfService { LOG.debug(errMsg + identifier); throw new RestconfDocumentedException(errMsg , ErrorType.APPLICATION, ErrorTag.DATA_MISSING); } - return new NormalizedNodeContext(iiWithData, data); + return new NormalizedNodeContext(iiWithData, data, QueryParametersParser.parseKnownWriterParameters(uriInfo)); } @Override diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/WriterParameters.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/WriterParameters.java new file mode 100644 index 0000000000..9b26f60dbf --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/WriterParameters.java @@ -0,0 +1,20 @@ +package org.opendaylight.controller.sal.restconf.impl; + +public class WriterParameters { + private final int depth; + private final boolean prettyPrint; + + public WriterParameters(final boolean prettyPrint, final int depth) { + this.prettyPrint = prettyPrint; + this.depth = depth; + } + + public int getDepth() { + return depth; + } + + public boolean isPrettyPrint() { + return prettyPrint; + } +} +