cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1333400 - in /cxf/trunk/rt: frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/ rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/
Date Thu, 03 May 2012 10:29:11 GMT
Author: sergeyb
Date: Thu May  3 10:29:10 2012
New Revision: 1333400

URL: http://svn.apache.org/viewvc?rev=1333400&view=rev
Log:
[CXF-4284] Applying few more updates from Govindaram PS

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
    cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
    cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=1333400&r1=1333399&r2=1333400&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
Thu May  3 10:29:10 2012
@@ -55,6 +55,7 @@ import javax.xml.bind.annotation.XmlRoot
 import javax.xml.bind.annotation.XmlType;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.validation.Schema;
@@ -543,8 +544,8 @@ public abstract class AbstractJAXBProvid
         ex.printStackTrace(new PrintWriter(sw));
         return sw.toString();
     }
-    
-    protected static void handleJAXBException(JAXBException e, boolean read) {
+    //TODO: move these methods into the dedicated utility class
+    protected static StringBuilder handleExceptionStart(Exception e) {
         LOG.warning(getStackTrace(e));
         StringBuilder sb = new StringBuilder();
         if (e.getMessage() != null) {
@@ -553,6 +554,19 @@ public abstract class AbstractJAXBProvid
         if (e.getCause() != null && e.getCause().getMessage() != null) {
             sb.append(e.getCause().getMessage()).append(". ");
         }
+        return sb;
+    }
+    
+    protected static void handleExceptionEnd(Throwable t, String message, boolean read) {
+        Response.Status status = read 
+            ? Response.Status.BAD_REQUEST : Response.Status.INTERNAL_SERVER_ERROR; 
+        Response r = Response.status(status)
+            .type(MediaType.TEXT_PLAIN).entity(message).build();
+        throw new WebApplicationException(t, r);
+    }
+    
+    protected static void handleJAXBException(JAXBException e, boolean read) {
+        StringBuilder sb = handleExceptionStart(e);
         if (e.getLinkedException() != null && e.getLinkedException().getMessage()
!= null) {
             sb.append(e.getLinkedException().getMessage()).append(". ");
         }
@@ -560,11 +574,12 @@ public abstract class AbstractJAXBProvid
             ? e.getLinkedException() : e.getCause() != null ? e.getCause() : e;
         String message = new org.apache.cxf.common.i18n.Message("JAXB_EXCEPTION", 
                              BUNDLE, sb.toString()).toString();
-        Response.Status status = read 
-            ? Response.Status.BAD_REQUEST : Response.Status.INTERNAL_SERVER_ERROR; 
-        Response r = Response.status(status)
-            .type(MediaType.TEXT_PLAIN).entity(message).build();
-        throw new WebApplicationException(t, r);
+        handleExceptionEnd(t, message, read);
+    }
+    
+    protected static void handleXMLStreamException(XMLStreamException e, boolean read) {
+        StringBuilder sb = handleExceptionStart(e);
+        handleExceptionEnd(e, sb.toString(), read);
     }
     
     public void setOutTransformElements(Map<String, String> outElements) {

Modified: cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java?rev=1333400&r1=1333399&r2=1333400&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
Thu May  3 10:29:10 2012
@@ -239,7 +239,7 @@ public class JSONProvider<T> extends Abs
         } catch (DepthExceededStaxException e) {
             throw new WebApplicationException(413);
         } catch (XMLStreamException e) {
-            throw new WebApplicationException(e, Response.status(400).build());
+            handleXMLStreamException(e, true);
         } catch (WebApplicationException e) {
             throw e;
         } catch (Exception e) {
@@ -360,7 +360,7 @@ public class JSONProvider<T> extends Abs
         } catch (JAXBException e) {
             handleJAXBException(e, false);
         } catch (XMLStreamException e) {
-            throw new WebApplicationException(e);
+            handleXMLStreamException(e, false);
         } catch (Exception e) {
             throw new WebApplicationException(e);
         }

Modified: cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java?rev=1333400&r1=1333399&r2=1333400&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java
Thu May  3 10:29:10 2012
@@ -36,7 +36,9 @@ import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
 
+import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
 import javax.ws.rs.ext.MessageBodyReader;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBElement;
@@ -76,6 +78,20 @@ import org.junit.Test;
 public class JSONProviderTest extends Assert {
 
     @Test
+    public void testReadMalformedJson() throws Exception {
+        MessageBodyReader<Tags> p = new JSONProvider<Tags>();
+        byte[] bytes = "junk".getBytes();
+         
+        try {
+            p.readFrom(Tags.class, null, null, 
+                                           null, null, new ByteArrayInputStream(bytes));
+            fail("404 is expected");
+        } catch (WebApplicationException ex) {
+            assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), ex.getResponse().getStatus());
+        }
+    }
+    
+    @Test
     public void testReadEmbeddedArray() throws Exception {
         String input = 
                 "{\"store\":"



Mime
View raw message