cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: Optional reporting of the validation exception message, patch from Vjacheslav V. Borisov applied, This closes #170
Date Thu, 15 Sep 2016 20:38:45 GMT
Repository: cxf
Updated Branches:
  refs/heads/master c0b847032 -> c18450505


Optional reporting of the validation exception message, patch from Vjacheslav V. Borisov applied,
This closes #170


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/c1845050
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/c1845050
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/c1845050

Branch: refs/heads/master
Commit: c184505050812a32f0c496c6b237eff407fb1f18
Parents: c0b8470
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Thu Sep 15 21:38:28 2016 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Thu Sep 15 21:38:28 2016 +0100

----------------------------------------------------------------------
 .../validation/ValidationExceptionMapper.java   | 36 ++++++++++++++++----
 1 file changed, 30 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/c1845050/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationExceptionMapper.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationExceptionMapper.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationExceptionMapper.java
index b15ad0b..b0d0938 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationExceptionMapper.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/validation/ValidationExceptionMapper.java
@@ -25,6 +25,7 @@ import javax.validation.ConstraintViolation;
 import javax.validation.ConstraintViolationException;
 import javax.validation.ValidationException;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.ResponseBuilder;
 import javax.ws.rs.ext.ExceptionMapper;
 import javax.ws.rs.ext.Provider;
 
@@ -35,25 +36,48 @@ import org.apache.cxf.validation.ResponseConstraintViolationException;
 @Provider
 public class ValidationExceptionMapper implements ExceptionMapper< ValidationException
> {
     private static final Logger LOG = LogUtils.getL7dLogger(ValidationExceptionMapper.class);
+    private boolean addMessageToResponse;
     
     @Override
     public Response toResponse(ValidationException exception) {
         Response.Status errorStatus = Response.Status.INTERNAL_SERVER_ERROR;
         if (exception instanceof ConstraintViolationException) { 
             
+            StringBuilder responseBody = addMessageToResponse ? new StringBuilder() : null;
+            
             final ConstraintViolationException constraint = (ConstraintViolationException)
exception;
             
             for (final ConstraintViolation< ? > violation: constraint.getConstraintViolations())
{
-                LOG.log(Level.WARNING, 
-                    violation.getRootBeanClass().getSimpleName() 
-                    + "." + violation.getPropertyPath() 
-                    + ": " + violation.getMessage());
+                String message = getMessage(violation);
+                if (responseBody != null) {
+                    responseBody.append(message).append("\n");
+                }
+                LOG.log(Level.WARNING, message);
             }
             
             if (!(constraint instanceof ResponseConstraintViolationException)) {
                 errorStatus = Response.Status.BAD_REQUEST;
             }
-        } 
-        return JAXRSUtils.toResponse(errorStatus);
+            ResponseBuilder rb = JAXRSUtils.toResponseBuilder(errorStatus);
+            if (responseBody != null) {
+                rb.entity(responseBody);
+            }
+            return rb.build();
+        } else {
+            return JAXRSUtils.toResponse(errorStatus);
+        }
+    }
+    private String getMessage(ConstraintViolation<?> violation) {
+        return violation.getRootBeanClass().getSimpleName() 
+            + "." + violation.getPropertyPath() 
+            + ": " + violation.getMessage();
     }
+    /**
+     * Controls whether to add a constraint validation message to Response or not
+     * @param addMessageToResponse add a constraint validation message to Response
+     */
+    public void setAddMessageToResponse(boolean addMessageToResponse) {
+        this.addMessageToResponse = addMessageToResponse;
+    }
+
 }


Mime
View raw message