Author: fmui
Date: Wed Mar 26 13:03:56 2014
New Revision: 1581818
URL: http://svn.apache.org/r1581818
Log:
Improved exception handling
Modified:
chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/atompub/AtomPubParser.java
chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/XMLUtils.java
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/Dispatcher.java
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/AtomEntryParserTest.java
Modified:
chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/atompub/AtomPubParser.java
URL:
http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/atompub/AtomPubParser.java?rev=1581818&r1=1581817&r2=1581818&view=diff
==============================================================================
---
chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/atompub/AtomPubParser.java
(original)
+++
chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/atompub/AtomPubParser.java
Wed Mar 26 13:03:56 2014
@@ -135,8 +135,13 @@ public class AtomPubParser {
}
}
- parser.close();
} finally {
+ try {
+ parser.close();
+ } catch (XMLStreamException xse) {
+ // there is nothing we can do
+ }
+
// make sure the stream is read and closed in all cases
IOUtils.consumeAndClose(stream);
}
Modified:
chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/XMLUtils.java
URL:
http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/XMLUtils.java?rev=1581818&r1=1581817&r2=1581818&view=diff
==============================================================================
---
chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/XMLUtils.java
(original)
+++
chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/XMLUtils.java
Wed Mar 26 13:03:56 2014
@@ -225,12 +225,7 @@ public final class XMLUtils {
assert parser != null;
if (parser.hasNext()) {
- try {
- parser.next();
- } catch (XMLStreamException e) {
- // EOF exceptions
- return false;
- }
+ parser.next();
return true;
}
Modified:
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java
URL:
http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java?rev=1581818&r1=1581817&r2=1581818&view=diff
==============================================================================
---
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java
(original)
+++
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java
Wed Mar 26 13:03:56 2014
@@ -205,26 +205,28 @@ public final class AtomEntryParser {
cappedStream = new CappedInputStream(stream, MAX_STREAM_LENGTH);
XMLStreamReader parser = XMLUtils.createParser(cappedStream);
- while (true) {
- int event = parser.getEventType();
- if (event == XMLStreamReader.START_ELEMENT) {
- QName name = parser.getName();
+ try {
+ while (true) {
+ int event = parser.getEventType();
+ if (event == XMLStreamReader.START_ELEMENT) {
+ QName name = parser.getName();
- if (XMLConstants.NAMESPACE_ATOM.equals(name.getNamespaceURI())
- && (TAG_ENTRY.equals(name.getLocalPart()))) {
- parseEntry(parser);
- break;
- } else {
- throw new CmisInvalidArgumentException("XML is not an Atom
entry!");
+ if
(XMLConstants.NAMESPACE_ATOM.equals(name.getNamespaceURI())
+ && (TAG_ENTRY.equals(name.getLocalPart()))) {
+ parseEntry(parser);
+ break;
+ } else {
+ throw new CmisInvalidArgumentException("XML is not an
Atom entry!");
+ }
}
- }
- if (!XMLUtils.next(parser)) {
- break;
+ if (!XMLUtils.next(parser)) {
+ break;
+ }
}
+ } finally {
+ parser.close();
}
-
- parser.close();
}
/**
Modified:
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/Dispatcher.java
URL:
http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/Dispatcher.java?rev=1581818&r1=1581817&r2=1581818&view=diff
==============================================================================
---
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/Dispatcher.java
(original)
+++
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/Dispatcher.java
Wed Mar 26 13:03:56 2014
@@ -24,9 +24,12 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import javax.xml.stream.XMLStreamException;
import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
+import
org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
+import
org.apache.chemistry.opencmis.commons.impl.json.parser.JSONParseException;
import org.apache.chemistry.opencmis.commons.server.CallContext;
import org.apache.chemistry.opencmis.commons.server.CmisService;
import org.slf4j.Logger;
@@ -88,6 +91,10 @@ public class Dispatcher implements Seria
serviceCall.serve(context, service, repositoryId, request,
response);
} catch (CmisBaseException ce) {
throw ce;
+ } catch (XMLStreamException xse) {
+ throw new CmisInvalidArgumentException("Invalid XML!", xse);
+ } catch (JSONParseException jpe) {
+ throw new CmisInvalidArgumentException("Invalid JSON!", jpe);
} catch (Exception e) {
throw new CmisRuntimeException(e.getMessage(), e);
}
Modified:
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/AtomEntryParserTest.java
URL:
http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/AtomEntryParserTest.java?rev=1581818&r1=1581817&r2=1581818&view=diff
==============================================================================
---
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/AtomEntryParserTest.java
(original)
+++
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/AtomEntryParserTest.java
Wed Mar 26 13:03:56 2014
@@ -26,6 +26,8 @@ import static org.junit.Assert.assertTru
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import javax.xml.stream.XMLStreamException;
+
import org.apache.chemistry.opencmis.commons.PropertyIds;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.data.PropertyString;
@@ -203,17 +205,11 @@ public class AtomEntryParserTest {
assertNull(aep.getProperties());
}
- @Test
+ @Test(expected = XMLStreamException.class)
public void testEmptyStream() throws Exception {
ThresholdOutputStreamFactory streamFactory =
ThresholdOutputStreamFactory.newInstance(null, THRESHOLD,
MAX_SIZE, false);
- AtomEntryParser aep = new AtomEntryParser(new ByteArrayInputStream(new
byte[0]), streamFactory);
-
- assertNotNull(aep);
- assertNull(aep.getId());
- assertNull(aep.getObject());
- assertNull(aep.getContentStream());
- assertNull(aep.getProperties());
+ new AtomEntryParser(new ByteArrayInputStream(new byte[0]),
streamFactory);
}
private static byte[] parse(byte[] entry) throws Exception {