cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1539064 - in /cxf/trunk: core/src/main/java/org/apache/cxf/validation/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/validation/
Date Tue, 05 Nov 2013 17:10:33 GMT
Author: sergeyb
Date: Tue Nov  5 17:10:33 2013
New Revision: 1539064

URL: http://svn.apache.org/r1539064
Log:
[CXF-5309] Validation for response entities wrapped in JAX-RS Response

Modified:
    cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInInterceptor.java
    cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationOutInterceptor.java
    cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInvoker.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationOutInterceptor.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationUtils.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/JAXRSClientServerValidationTest.java

Modified: cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInInterceptor.java?rev=1539064&r1=1539063&r2=1539064&view=diff
==============================================================================
--- cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInInterceptor.java
(original)
+++ cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationInInterceptor.java
Tue Nov  5 17:10:33 2013
@@ -36,7 +36,7 @@ public abstract class AbstractValidation
     protected void handleValidation(final Message message, final Object resourceInstance,
                                     final Method method, final List<Object> arguments)
{
         ValidationProvider provider = getProvider(message);
-        provider.validate(resourceInstance, method, arguments.toArray());
+        provider.validateParameters(resourceInstance, method, arguments.toArray());
         message.getExchange().put(ValidationProvider.class, provider);
     }
 }

Modified: cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationOutInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationOutInterceptor.java?rev=1539064&r1=1539063&r2=1539064&view=diff
==============================================================================
--- cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationOutInterceptor.java
(original)
+++ cxf/trunk/core/src/main/java/org/apache/cxf/validation/AbstractValidationOutInterceptor.java
Tue Nov  5 17:10:33 2013
@@ -35,18 +35,11 @@ public abstract class AbstractValidation
     @Override
     protected void handleValidation(final Message message, final Object resourceInstance,
                                     final Method method, final List<Object> arguments)
{  
-        if (arguments.size() == 1) {
-            Object responseObject = getResponseObject(arguments.get(0));
-            if (responseObject != null) {
-                getOutProvider(message).validate(resourceInstance, method, responseObject);
-            }
+        if (arguments.size() == 1 && arguments.get(0) != null) {
+            getOutProvider(message).validateReturnValue(resourceInstance, method, arguments.get(0));
         }        
     }
     
-    protected Object getResponseObject(Object object) {
-        return object;
-    }
-    
     protected ValidationProvider getOutProvider(Message message) {
         ValidationProvider provider = message.getExchange().get(ValidationProvider.class);
         return provider == null ? getProvider(message) : provider;

Modified: cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationProvider.java?rev=1539064&r1=1539063&r2=1539064&view=diff
==============================================================================
--- cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationProvider.java (original)
+++ cxf/trunk/core/src/main/java/org/apache/cxf/validation/ValidationProvider.java Tue Nov
 5 17:10:33 2013
@@ -76,9 +76,9 @@ public class ValidationProvider {
         this.factory = factory;
     }
     
-    public< T > void validate(final T instance, final Method method, final Object[]
arguments) {
+    public< T > void validateParameters(final T instance, final Method method, final
Object[] arguments) {
         
-        final ExecutableValidator methodValidator = factory.getValidator().forExecutables();
+        final ExecutableValidator methodValidator = getExecutableValidator();
         final Set< ConstraintViolation< T > > violations = methodValidator.validateParameters(instance,

             method, arguments);
         
@@ -87,8 +87,8 @@ public class ValidationProvider {
         }                
     }
     
-    public< T > void validate(final T instance, final Method method, final Object returnValue)
{
-        final ExecutableValidator methodValidator = factory.getValidator().forExecutables();
+    public< T > void validateReturnValue(final T instance, final Method method, final
Object returnValue) {
+        final ExecutableValidator methodValidator = getExecutableValidator();
         final Set<ConstraintViolation< T > > violations = methodValidator.validateReturnValue(instance,

             method, returnValue);
         
@@ -96,4 +96,27 @@ public class ValidationProvider {
             throw new ResponseConstraintViolationException(violations);
         }                
     }
+    
+    public< T > void validateReturnValue(final T returnValue) {
+        final Set<ConstraintViolation< T > > violations = doValidateBean(returnValue);
+        if (!violations.isEmpty()) {
+            throw new ResponseConstraintViolationException(violations);
+        }                
+    }
+    
+    public< T > void validateBean(final T bean) {
+        final Set<ConstraintViolation< T > > violations = doValidateBean(bean);
+        if (!violations.isEmpty()) {
+            throw new ConstraintViolationException(violations);
+        }                
+    }
+    
+    private< T > Set<ConstraintViolation< T > > doValidateBean(final T
bean) {
+        return factory.getValidator().validate(bean);
+    }
+    
+    private ExecutableValidator getExecutableValidator() {
+        
+        return factory.getValidator().forExecutables();
+    }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInvoker.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInvoker.java?rev=1539064&r1=1539063&r2=1539064&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInvoker.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationInvoker.java
Tue Nov  5 17:10:33 2013
@@ -23,6 +23,7 @@ import java.util.List;
 import java.util.logging.Logger;
 
 import javax.validation.ValidationException;
+import javax.ws.rs.core.Response;
 
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.jaxrs.JAXRSInvoker;
@@ -48,13 +49,16 @@ public class JAXRSValidationInvoker exte
         
         ValidationProvider theProvider = getProvider(message);
         
-        theProvider.validate(serviceObject, m, params.toArray());
+        theProvider.validateParameters(serviceObject, m, params.toArray());
         
         Object response = super.invoke(exchange, serviceObject, m, params);
         
-        Object responseToValidate = ValidationUtils.getResponseObject(response);
-        if (responseToValidate != null) {
-            theProvider.validate(serviceObject, m, responseToValidate);
+        if (response != null) {
+            if (response instanceof Response && ((Response)response).getEntity()
!= null) {
+                theProvider.validateReturnValue(((Response)response).getEntity());
+            } else {
+                theProvider.validateReturnValue(serviceObject, m, response);
+            }
         }
         
         return response;

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationOutInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationOutInterceptor.java?rev=1539064&r1=1539063&r2=1539064&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationOutInterceptor.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/JAXRSValidationOutInterceptor.java
Tue Nov  5 17:10:33 2013
@@ -19,6 +19,9 @@
 package org.apache.cxf.jaxrs.validation;
 
 import java.lang.reflect.Method;
+import java.util.List;
+
+import javax.ws.rs.core.Response;
 
 import org.apache.cxf.message.Message;
 import org.apache.cxf.validation.AbstractValidationOutInterceptor;
@@ -45,8 +48,15 @@ public class JAXRSValidationOutIntercept
         }
     }
     
-    @Override 
-    protected Object getResponseObject(Object o) {  
-        return ValidationUtils.getResponseObject(o);
+    @Override
+    protected void handleValidation(final Message message, final Object resourceInstance,
+                                    final Method method, final List<Object> arguments)
{  
+        if (arguments.size() == 1) {
+            if (arguments.get(0) instanceof Response && ((Response)arguments.get(0)).getEntity()
!= null) {
+                getOutProvider(message).validateReturnValue(((Response)arguments.get(0)).getEntity());
+            } else {
+                super.handleValidation(message, resourceInstance, method, arguments);
+            }
+        }        
     }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationUtils.java?rev=1539064&r1=1539063&r2=1539064&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationUtils.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationUtils.java
Tue Nov  5 17:10:33 2013
@@ -20,8 +20,6 @@ package org.apache.cxf.jaxrs.validation;
 
 import java.util.logging.Logger;
 
-import javax.ws.rs.core.Response;
-
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
 import org.apache.cxf.jaxrs.model.OperationResourceInfo;
@@ -52,7 +50,5 @@ public final class ValidationUtils {
         
     }
     
-    public static Object getResponseObject(Object o) {  
-        return o instanceof Response ? ((Response)o).getEntity() : o;
-    }
+    
 }

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=1539064&r1=1539063&r2=1539064&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
Tue Nov  5 17:10:33 2013
@@ -50,6 +50,12 @@ public class BookStoreWithValidation ext
         return books.get(id);
     }
     
+    @GET
+    @Path("/booksResponse/{bookId}")
+    public Response getBookResponse(@PathParam("bookId") String id) {
+        return Response.ok(books.get(id)).build();
+    }
+    
     @POST
     @Path("/books")
     public Response addBook(@Context final UriInfo uriInfo, 

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=1539064&r1=1539063&r2=1539064&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
Tue Nov  5 17:10:33 2013
@@ -139,6 +139,15 @@ public class JAXRSClientServerValidation
         r = createWebClient("/bookstore/books/1234").get();
         assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), r.getStatus());
     }
+    
+    @Test
+    public void testThatResponseValidationForOneResponseBookFails()  {
+        Response r = createWebClient("/bookstore/books").post(new Form().param("id", "1234"));
+        assertEquals(Status.CREATED.getStatusCode(), r.getStatus());
+
+        r = createWebClient("/bookstore/booksResponse/1234").get();
+        assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), r.getStatus());
+    }
 
     @Test
     public void testThatResponseValidationForAllBooksFails()  {



Mime
View raw message