cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1333417 - in /cxf/branches/2.5.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/
Date Thu, 03 May 2012 12:11:50 GMT
Author: sergeyb
Date: Thu May  3 12:11:49 2012
New Revision: 1333417

URL: http://svn.apache.org/viewvc?rev=1333417&view=rev
Log:
Merged revisions 1333400 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1333400 | sergeyb | 2012-05-03 11:29:10 +0100 (Thu, 03 May 2012) | 1 line
  
  [CXF-4284] Applying few more updates from Govindaram PS
........

Modified:
    cxf/branches/2.5.x-fixes/   (props changed)
    cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
    cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
    cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java

Propchange: cxf/branches/2.5.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/trunk:r1333400

Propchange: cxf/branches/2.5.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=1333417&r1=1333416&r2=1333417&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
(original)
+++ cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
Thu May  3 12:11:49 2012
@@ -53,6 +53,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;
@@ -533,8 +534,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) {
@@ -543,6 +544,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(". ");
         }
@@ -550,11 +564,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/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java?rev=1333417&r1=1333416&r2=1333417&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
(original)
+++ cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
Thu May  3 12:11:49 2012
@@ -237,7 +237,7 @@ public class JSONProvider extends Abstra
         } 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) {
@@ -358,7 +358,7 @@ public class JSONProvider extends Abstra
         } 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/branches/2.5.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java?rev=1333417&r1=1333416&r2=1333417&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
(original)
+++ cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
Thu May  3 12:11:49 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;
@@ -73,6 +75,21 @@ import org.junit.Test;
 
 public class JSONProviderTest extends Assert {
 
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testReadMalformedJson() throws Exception {
+        MessageBodyReader<?> p = new JSONProvider();
+        byte[] bytes = "junk".getBytes();
+         
+        try {
+            p.readFrom((Class)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());
+        }
+    }
+
     @SuppressWarnings("unchecked")
     @Test
     public void testReadEmbeddedArray() throws Exception {



Mime
View raw message