cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1540118 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/
Date Fri, 08 Nov 2013 17:01:40 GMT
Author: sergeyb
Date: Fri Nov  8 17:01:40 2013
New Revision: 1540118

URL: http://svn.apache.org/r1540118
Log:
[CXF-5309] Optionally reporting more parameter info during the exception processing

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationExceptionMapper.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookStoreWithValidation.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookWithValidation.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/JAXRSClientServerValidationTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationExceptionMapper.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationExceptionMapper.java?rev=1540118&r1=1540117&r2=1540118&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationExceptionMapper.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationExceptionMapper.java
Fri Nov  8 17:01:40 2013
@@ -23,31 +23,38 @@ import java.util.logging.Logger;
 
 import javax.validation.ConstraintViolation;
 import javax.validation.ConstraintViolationException;
+import javax.validation.Path;
 import javax.validation.ValidationException;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.ext.ExceptionMapper;
 import javax.ws.rs.ext.Provider;
 
 import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.jaxrs.model.OperationResourceInfo;
+import org.apache.cxf.jaxrs.model.Parameter;
+import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.validation.ResponseConstraintViolationException;
 
 @Provider
 public class ValidationExceptionMapper implements ExceptionMapper< ValidationException
> {
     private static final Logger LOG = LogUtils.getL7dLogger(ValidationExceptionMapper.class);
+    private boolean reportParameterInfo;
     
     @Override
     public Response toResponse(ValidationException exception) {
         if (exception instanceof ConstraintViolationException) { 
-            final ConstraintViolationException constraint = (ConstraintViolationException)
exception;
             
+            final ConstraintViolationException constraint = (ConstraintViolationException)
exception;
+            final boolean isResponseException = constraint instanceof ResponseConstraintViolationException;
+                        
             for (final ConstraintViolation< ? > violation: constraint.getConstraintViolations())
{
                 LOG.log(Level.SEVERE, 
                     violation.getRootBeanClass().getSimpleName() 
-                    + "." + violation.getPropertyPath() 
+                    + "." + getPropertyPathDescription(violation.getPropertyPath(), isResponseException)

                     + ": " + violation.getMessage());
             }
             
-            if (constraint instanceof ResponseConstraintViolationException) {
+            if (isResponseException) {
                 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
             }
             
@@ -55,5 +62,63 @@ public class ValidationExceptionMapper i
         } else {
             return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
         }
+        
+        
+    }
+    
+    protected String getPropertyPathDescription(Path propertyPath, boolean isResponseException)
{
+        final String path = propertyPath.toString();
+        if (isResponseException || !isReportParameterInfo()) {
+            return path;
+        }
+        int index = path.lastIndexOf(".arg");
+        if (index == -1 || index + 4 >= path.length()) {
+            return path;
+        }
+        
+        boolean compositePath = false;
+        
+        int argPos;
+        try { 
+            String argPath = path.substring(index + 4);
+            int infoIndex = argPath.indexOf('[');
+            if (infoIndex == -1) {
+                infoIndex = argPath.indexOf('.');
+            }
+            if (infoIndex > 0) {
+                argPath = argPath.substring(0, infoIndex);
+                compositePath = true;
+            }
+            argPos = Integer.valueOf(argPath);
+        } catch (NumberFormatException ex) {
+            return path;
+        }
+        final OperationResourceInfo ori = JAXRSUtils.getCurrentMessage().getExchange().get(OperationResourceInfo.class);
+        if (argPos < 0 || argPos > ori.getParameters().size()) {
+            return path;
+        }
+        Parameter param = ori.getParameters().get(argPos);
+        
+        StringBuilder sb = new StringBuilder();
+        sb.append("(");
+        if (compositePath) {
+            sb.append("arg" + argPos + " ");
+        }
+        sb.append("JAXRS param is " + param.getType().toString());
+        if (param.getName() != null) {
+            sb.append("(\"" + param.getName() + "\")");
+        }
+        sb.append(", class: " + ori.getAnnotatedMethod().getParameterTypes()[argPos].getSimpleName());
+        sb.append(")");
+        return path + sb.toString();
+            
+    }
+
+    public boolean isReportParameterInfo() {
+        return reportParameterInfo;
+    }
+
+    public void setReportParameterInfo(boolean reportParameterInfo) {
+        this.reportParameterInfo = reportParameterInfo;
     }
 }

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookStoreWithValidation.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookStoreWithValidation.java?rev=1540118&r1=1540117&r2=1540118&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookStoreWithValidation.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookStoreWithValidation.java
Fri Nov  8 17:01:40 2013
@@ -20,11 +20,13 @@ package org.apache.cxf.systest.jaxrs.val
 
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import javax.validation.Valid;
 import javax.validation.constraints.NotNull;
 import javax.validation.constraints.Size;
+import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
 import javax.ws.rs.DefaultValue;
 import javax.ws.rs.FormParam;
@@ -67,6 +69,22 @@ public class BookStoreWithValidation ext
         return Response.created(uriInfo.getRequestUriBuilder().path(id).build()).build();
     }
     
+    @POST
+    @Path("/books/direct")
+    @Consumes("text/xml")
+    public Response addBookDirect(@Valid BookWithValidation book, @Context final UriInfo
uriInfo) {
+        books.put(book.getId(), book);   
+        return Response.created(uriInfo.getRequestUriBuilder().path(book.getId()).build()).build();
+    }
+    
+    @POST
+    @Path("/books/directmany")
+    @Consumes("text/xml")
+    public Response addBooksDirect(@Valid List<BookWithValidation> list, @Context final
UriInfo uriInfo) {
+        books.put(list.get(0).getId(), list.get(0));   
+        return Response.created(uriInfo.getRequestUriBuilder().path(list.get(0).getId()).build()).build();
+    }
+    
     @GET
     @Path("/books")
     @Override

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookWithValidation.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookWithValidation.java?rev=1540118&r1=1540117&r2=1540118&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookWithValidation.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/BookWithValidation.java
Fri Nov  8 17:01:40 2013
@@ -21,11 +21,6 @@ package org.apache.cxf.systest.jaxrs.val
 import javax.validation.constraints.NotNull;
 import javax.xml.bind.annotation.XmlRootElement;
 
-import com.fasterxml.jackson.annotation.JsonTypeInfo;
-import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
-import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
-
-@JsonTypeInfo(use = Id.CLASS, include = As.PROPERTY, property = "class")
 @XmlRootElement(name = "BookWithValidation")
 public class BookWithValidation {
     @NotNull private String name;

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/JAXRSClientServerValidationTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/JAXRSClientServerValidationTest.java?rev=1540118&r1=1540117&r2=1540118&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/JAXRSClientServerValidationTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/JAXRSClientServerValidationTest.java
Fri Nov  8 17:01:40 2013
@@ -19,6 +19,7 @@
 package org.apache.cxf.systest.jaxrs.validation;
 
 import java.util.Arrays;
+import java.util.Collections;
 
 import javax.ws.rs.core.Form;
 import javax.ws.rs.core.MediaType;
@@ -132,6 +133,27 @@ public class JAXRSClientServerValidation
     }
     
     @Test
+    public void testThatNoValidationConstraintsAreViolatedWithBook()  {
+        final Response r = createWebClient("/bookstore/books/direct").type("text/xml").post(
+              new BookWithValidation("BeanVal", "1"));
+        assertEquals(Status.CREATED.getStatusCode(), r.getStatus());
+    }
+    
+    @Test
+    public void testThatValidationConstraintsAreViolatedWithBook()  {
+        final Response r = createWebClient("/bookstore/books/direct").type("text/xml").post(
+              new BookWithValidation("BeanVal"));
+        assertEquals(Status.BAD_REQUEST.getStatusCode(), r.getStatus());
+    }
+    
+    @Test
+    public void testThatValidationConstraintsAreViolatedWithBooks()  {
+        final Response r = createWebClient("/bookstore/books/directmany").type("text/xml").postCollection(
+              Collections.singletonList(new BookWithValidation("BeanVal")), BookWithValidation.class);
+        assertEquals(Status.BAD_REQUEST.getStatusCode(), r.getStatus());
+    }
+    
+    @Test
     public void testThatResponseValidationForOneBookFails()  {
         Response r = createWebClient("/bookstore/books").post(new Form().param("id", "1234"));
         assertEquals(Status.CREATED.getStatusCode(), r.getStatus());



Mime
View raw message