From: Tony Tkacik Date: Tue, 3 Jun 2014 16:21:56 +0000 (+0000) Subject: Merge "Bug 1100 - Invoking an RPC with no input should not throw 500 when expected" X-Git-Tag: release/helium~716 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=bf8f203e671b9156e2e3fe07910565e115fa1b10;hp=-c Merge "Bug 1100 - Invoking an RPC with no input should not throw 500 when expected" --- bf8f203e671b9156e2e3fe07910565e115fa1b10 diff --combined opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonReader.java index 593c104dfd,cdea81f2b7..1f7b061e92 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonReader.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonReader.java @@@ -31,6 -31,12 +31,12 @@@ class JsonReader JsonParser parser = new JsonParser(); JsonElement rootElement = parser.parse(new InputStreamReader(entityStream)); + if( rootElement.isJsonNull() ) + { + //no content, so return null to indicate no input + return null; + } + if (!rootElement.isJsonObject()) { throw new UnsupportedFormatException("Root element of Json has to be Object"); } @@@ -87,7 -93,7 +93,7 @@@ } } else if (childType.isJsonPrimitive()) { JsonPrimitive childPrimitive = childType.getAsJsonPrimitive(); - String value = childPrimitive.getAsString(); + String value = childPrimitive.getAsString().trim(); parent.addValue(new SimpleNodeWrapper(getNamespaceFor(childName), getLocalNameFor(childName), resolveValueOfElement(value))); } diff --combined opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlReader.java index 5b95f0de1a,2965ae7209..171805a179 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlReader.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlReader.java @@@ -9,6 -9,8 +9,8 @@@ package org.opendaylight.controller.sal import static com.google.common.base.Preconditions.checkArgument; + import java.io.BufferedInputStream; + import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.util.Stack; @@@ -32,7 -34,17 +34,17 @@@ public class XmlReader private final static XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); private XMLEventReader eventReader; - public CompositeNodeWrapper read(InputStream entityStream) throws XMLStreamException, UnsupportedFormatException { + public CompositeNodeWrapper read(InputStream entityStream) throws XMLStreamException, + UnsupportedFormatException, + IOException { + //Get an XML stream which can be marked, and reset, so we can check and see if there is + //any content being provided. + entityStream = getMarkableStream(entityStream); + + if( isInputStreamEmpty( entityStream ) ) { + return null; + } + eventReader = xmlInputFactory.createXMLEventReader(entityStream); if (eventReader.hasNext()) { @@@ -91,6 -103,31 +103,31 @@@ return root; } + /** + * If the input stream is not markable, then it wraps the input stream with a buffered stream, + * which is mark able. That way we can check if the stream is empty safely. + * @param entityStream + * @return + */ + private InputStream getMarkableStream(InputStream entityStream) { + if( !entityStream.markSupported() ) + { + entityStream = new BufferedInputStream( entityStream ); + } + return entityStream; + } + + private boolean isInputStreamEmpty(InputStream entityStream) + throws IOException { + boolean isEmpty = false; + entityStream.mark( 1 ); + if( entityStream.read() == -1 ){ + isEmpty = true; + } + entityStream.reset(); + return isEmpty; + } + private boolean isSimpleNodeEvent(final XMLEvent event) throws XMLStreamException { checkArgument(event != null, "XML Event cannot be NULL!"); if (event.isStartElement()) { @@@ -165,7 -202,7 +202,7 @@@ } } } - return data; + return data == null ? null : data.trim(); } private String getAdditionalData(XMLEvent event) throws XMLStreamException {