cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1212509 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/provider/ main/java/org/apache/cxf/jaxrs/utils/ test/java/org/apache/cxf/jaxrs/provider/
Date Fri, 09 Dec 2011 16:46:17 GMT
Author: sergeyb
Date: Fri Dec  9 16:46:16 2011
New Revision: 1212509

URL: http://svn.apache.org/viewvc?rev=1212509&view=rev
Log:
[CXF-3966] Getting JAXB providers to throw 400 in case of read errors

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.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=1212509&r1=1212508&r2=1212509&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
Fri Dec  9 16:46:16 2011
@@ -528,7 +528,7 @@ public abstract class AbstractJAXBProvid
         return sw.toString();
     }
     
-    protected static void handleJAXBException(JAXBException e) {
+    protected static void handleJAXBException(JAXBException e, boolean read) {
         LOG.warning(getStackTrace(e));
         StringBuilder sb = new StringBuilder();
         if (e.getMessage() != null) {
@@ -544,7 +544,9 @@ 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 r = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
+        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);
     }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=1212509&r1=1212508&r2=1212509&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
Fri Dec  9 16:46:16 2011
@@ -183,7 +183,7 @@ public class JAXBElementProvider extends
             return response;
             
         } catch (JAXBException e) {
-            handleJAXBException(e);
+            handleJAXBException(e, true);
         } catch (WebApplicationException e) {
             throw e;
         } catch (Exception e) {
@@ -256,7 +256,7 @@ public class JAXBElementProvider extends
                 marshal(actualObject, actualClass, genericType, encoding, os, m, anns);
             }
         } catch (JAXBException e) {
-            handleJAXBException(e);
+            handleJAXBException(e, true);
         }  catch (WebApplicationException e) {
             throw e;
         } catch (Exception e) {

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java?rev=1212509&r1=1212508&r2=1212509&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
Fri Dec  9 16:46:16 2011
@@ -224,7 +224,7 @@ public class JSONProvider extends Abstra
             return response;
             
         } catch (JAXBException e) {
-            handleJAXBException(e);
+            handleJAXBException(e, true);
         } catch (XMLStreamException e) {
             throw new WebApplicationException(e);
         } catch (WebApplicationException e) {
@@ -342,7 +342,7 @@ public class JSONProvider extends Abstra
             }
             
         } catch (JAXBException e) {
-            handleJAXBException(e);
+            handleJAXBException(e, false);
         } catch (XMLStreamException e) {
             throw new WebApplicationException(e);
         } catch (Exception e) {

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java?rev=1212509&r1=1212508&r2=1212509&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
Fri Dec  9 16:46:16 2011
@@ -305,10 +305,11 @@ public final class InjectionUtils {
                 return PrimitiveUtils.read(value, pClass);
             } catch (NumberFormatException nfe) {
                 //
-                //  For a path parameter this is probably a 404,
-                //  for others a 400...
+                //  For path, query & matrix parameters this is 404,
+                //  for others 400...
                 //
-                if (pType == ParameterType.PATH) {
+                if (pType == ParameterType.PATH || pType == ParameterType.QUERY
+                    || pType == ParameterType.MATRIX) {
                     throw new WebApplicationException(nfe, Response.Status.NOT_FOUND);
                 }
                 throw new WebApplicationException(nfe, Response.Status.BAD_REQUEST);

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java?rev=1212509&r1=1212508&r2=1212509&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
Fri Dec  9 16:46:16 2011
@@ -41,6 +41,7 @@ import java.util.TreeSet;
 
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
+import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.xml.bind.JAXBContext;
@@ -470,7 +471,7 @@ public class JAXBElementProviderTest ext
                          new MetadataMap<String, Object>(), bos);
         assertTrue(bos.toString().contains("thebook2"));
         assertTrue(bos.toString().contains("http://superbooks"));
-        
+        System.out.println(bos.toString());
         ByteArrayInputStream is = new ByteArrayInputStream(bos.toByteArray());
         Book2 book2 = 
             (Book2)provider.readFrom(
@@ -1023,6 +1024,20 @@ public class JAXBElementProviderTest ext
         doReadUnqualifiedCollection(data, "setBooks", List.class);
     }
     
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testReadMalformedXML() throws Exception {
+        JAXBElementProvider provider = new JAXBElementProvider();
+        try {
+            provider.readFrom((Class)Book.class, Book.class,
+                       new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String,
String>(), 
+                       new ByteArrayInputStream("<Book>".getBytes()));
+            fail("404 is expected");
+        } catch (WebApplicationException ex) {
+            assertEquals(400, ex.getResponse().getStatus());
+        }
+    }
+    
     @SuppressWarnings("unchecked")
     private void doReadUnqualifiedCollection(String data, String mName, Class<?> type)
throws Exception {
         JAXBElementProvider provider = new JAXBElementProvider();



Mime
View raw message