cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1392092 - /cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java
Date Sun, 30 Sep 2012 18:29:44 GMT
Author: sergeyb
Date: Sun Sep 30 18:29:43 2012
New Revision: 1392092

URL: http://svn.apache.org/viewvc?rev=1392092&view=rev
Log:
[CXF-4528] Making simpler to customize the error message and optionally including it with
Responses

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java?rev=1392092&r1=1392091&r2=1392092&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java
Sun Sep 30 18:29:43 2012
@@ -24,6 +24,7 @@ import java.io.StringWriter;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.ext.ExceptionMapper;
 
@@ -45,6 +46,7 @@ public class WebApplicationExceptionMapp
     private static final Logger LOG = LogUtils.getL7dLogger(WebApplicationExceptionMapper.class);
     private static final String ERROR_MESSAGE_START = "WebApplicationException has been caught,
status: ";
     private boolean printStackTrace = true;
+    private boolean addMessageToResponse;
     
     public Response toResponse(WebApplicationException ex) {
         
@@ -52,6 +54,8 @@ public class WebApplicationExceptionMapp
         if (r == null) {
             r = Response.serverError().build();
         }
+        boolean doAddMessage = r.getEntity() != null ? false : addMessageToResponse;
+        
         
         Message msg = PhaseInterceptorChain.getCurrentMessage();
         FaultListener flogger = null;
@@ -59,16 +63,21 @@ public class WebApplicationExceptionMapp
             flogger = (FaultListener)PhaseInterceptorChain.getCurrentMessage()
                 .getContextualProperty(FaultListener.class.getName());
         }
+        String errorMessage = doAddMessage || flogger != null 
+            ? buildErrorMessage(r, ex) : null; 
         if (flogger == null
-            || !flogger.faultOccurred(ex, buildErrorMessage(r, ex), msg)) {
+            || !flogger.faultOccurred(ex, errorMessage, msg)) {
             Level level = printStackTrace ? Level.WARNING : Level.FINE;
             LOG.log(level, getStackTrace(ex));
         }
         
+        if (doAddMessage) {
+            r = Response.fromResponse(r).entity(errorMessage).type(MediaType.TEXT_PLAIN).build();
+        }
         return r;
     }
 
-    private String buildErrorMessage(Response r, WebApplicationException ex) {
+    protected String buildErrorMessage(Response r, WebApplicationException ex) {
         StringBuilder sb = new StringBuilder();
         sb.append(ERROR_MESSAGE_START).append(r.getStatus());
         
@@ -100,5 +109,15 @@ public class WebApplicationExceptionMapp
         this.printStackTrace = printStackTrace;
     }
 
+    /**
+     * Controls whether to add an error message to Response or not,
+     * @param addMessageToResponse add a message to Response, ignored
+     *        if the captuted WebApplicationException has 
+     *        a Response with a non-null entity
+     */
+    public void setAddMessageToResponse(boolean addMessageToResponse) {
+        this.addMessageToResponse = addMessageToResponse;
+    }
+
     
 }



Mime
View raw message