cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-7222] Making it easier to customize WAE and ValidationException mappers, patch from Dennis Kieselhorst applied, This closes #227
Date Mon, 23 Jan 2017 21:56:34 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 763698886 -> 57fcf8df2


[CXF-7222] Making it easier to customize WAE and ValidationException mappers, patch from Dennis
Kieselhorst applied, This closes #227


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

Branch: refs/heads/master
Commit: 57fcf8df231fb087011f500435528741c33a726c
Parents: 7636988
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Mon Jan 23 21:56:15 2017 +0000
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Mon Jan 23 21:56:15 2017 +0000

----------------------------------------------------------------------
 .../impl/WebApplicationExceptionMapper.java     | 10 +++++++-
 .../validation/ValidationExceptionMapper.java   | 25 +++++++++++++-------
 2 files changed, 25 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/57fcf8df/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java
index bbc0bfc..fa19168 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java
@@ -74,7 +74,7 @@ public class WebApplicationExceptionMapper
         
         if (doAddMessage) {
             r = JAXRSUtils.copyResponseIfNeeded(r);
-            r = JAXRSUtils.fromResponse(r).entity(errorMessage).type(MediaType.TEXT_PLAIN).build();
+            r = buildResponse(r, errorMessage);
         }
         return r;
     }
@@ -93,6 +93,14 @@ public class WebApplicationExceptionMapper
         }
         return sb.toString();
     }
+
+    protected Response buildResponse(Response response, String responseText) {
+        Response.ResponseBuilder rb = JAXRSUtils.fromResponse(response);
+        if (responseText != null) {
+            rb.type(MediaType.TEXT_PLAIN).entity(responseText);
+        }
+        return rb.build();
+    }
     
     /**
      * Control whether to log at WARN or FINE level.

http://git-wip-us.apache.org/repos/asf/cxf/blob/57fcf8df/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 146d105..7f5e5dc 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
@@ -24,6 +24,7 @@ import java.util.logging.Logger;
 import javax.validation.ConstraintViolation;
 import javax.validation.ConstraintViolationException;
 import javax.validation.ValidationException;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.ResponseBuilder;
 import javax.ws.rs.ext.ExceptionMapper;
@@ -34,7 +35,7 @@ import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.validation.ResponseConstraintViolationException;
 
 @Provider
-public class ValidationExceptionMapper implements ExceptionMapper< ValidationException
> {
+public class ValidationExceptionMapper implements ExceptionMapper<ValidationException>
{
     private static final Logger LOG = LogUtils.getL7dLogger(ValidationExceptionMapper.class);
     private boolean addMessageToResponse;
     
@@ -48,7 +49,7 @@ public class ValidationExceptionMapper implements ExceptionMapper< ValidationExc
             final ConstraintViolationException constraint = (ConstraintViolationException)
exception;
             
             for (final ConstraintViolation< ? > violation: constraint.getConstraintViolations())
{
-                String message = getMessage(violation);
+                String message = buildErrorMessage(violation);
                 if (responseBody != null) {
                     responseBody.append(message).append("\n");
                 }
@@ -58,22 +59,28 @@ public class ValidationExceptionMapper implements ExceptionMapper<
ValidationExc
             if (!(constraint instanceof ResponseConstraintViolationException)) {
                 errorStatus = Response.Status.BAD_REQUEST;
             }
-            ResponseBuilder rb = JAXRSUtils.toResponseBuilder(errorStatus);
-            if (responseBody != null) {
-                rb.entity(responseBody.toString());
-            }
-            return rb.build();
+            return buildResponse(errorStatus, responseBody != null ? responseBody.toString()
: null);
         } else {
-            return JAXRSUtils.toResponse(errorStatus);
+            return buildResponse(errorStatus, addMessageToResponse ? exception.getMessage()
: null);
         }
     }
-    private String getMessage(ConstraintViolation<?> violation) {
+
+    protected String buildErrorMessage(ConstraintViolation<?> violation) {
         return "Value " 
             + (violation.getInvalidValue() != null ? "'" + violation.getInvalidValue().toString()
+ "'" : "(null)")
             + " of " + violation.getRootBeanClass().getSimpleName()
             + "." + violation.getPropertyPath()
             + ": " + violation.getMessage();
     }
+
+    protected Response buildResponse(Response.Status errorStatus, String responseText) {
+        ResponseBuilder rb = JAXRSUtils.toResponseBuilder(errorStatus);
+        if (responseText != null) {
+            rb.type(MediaType.TEXT_PLAIN).entity(responseText);
+        }
+        return rb.build();
+    }
+
     /**
      * Controls whether to add a constraint validation message to Response or not
      * @param addMessageToResponse add a constraint validation message to Response


Mime
View raw message