cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1392397 - in /cxf/branches/2.6.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java
Date Mon, 01 Oct 2012 15:32:26 GMT
Author: sergeyb
Date: Mon Oct  1 15:32:25 2012
New Revision: 1392397

URL: http://svn.apache.org/viewvc?rev=1392397&view=rev
Log:
Merged revisions 1392027,1392082,1392092 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1392027 | bimargulies | 2012-09-30 15:08:02 +0100 (Sun, 30 Sep 2012) | 3 lines
  
  CXF-4528: change the logging severity in WebApplicationExceptionMapper when 
  the app has a FaultListener that actually asked for logging.
........
  r1392082 | sergeyb | 2012-09-30 18:32:32 +0100 (Sun, 30 Sep 2012) | 1 line
  
  [CXF-4528] More updates to the mapper, if fault listener has handled the exception - do
not log, otherwise do log either at warning (default) or fine level
........
  r1392092 | sergeyb | 2012-09-30 19:29:43 +0100 (Sun, 30 Sep 2012) | 1 line
  
  [CXF-4528] Making simpler to customize the error message and optionally including it with
Responses
........

Modified:
    cxf/branches/2.6.x-fixes/   (props changed)
    cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java

Propchange: cxf/branches/2.6.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/trunk:r1392027-1392092

Propchange: cxf/branches/2.6.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java?rev=1392397&r1=1392396&r2=1392397&view=diff
==============================================================================
--- cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java
(original)
+++ cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java
Mon Oct  1 15:32:25 2012
@@ -23,8 +23,8 @@ import java.io.PrintWriter;
 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;
 
@@ -33,12 +33,20 @@ import org.apache.cxf.logging.FaultListe
 import org.apache.cxf.message.Message;
 import org.apache.cxf.phase.PhaseInterceptorChain;
 
+/**
+ * Default exception mapper for {@link WebApplicationException}.
+ * This class interacts with {@link FaultListener}.  
+ * If {@link FaultListener} is available and has indicated that it handled the exception
then
+ * no more logging is done, otherwise a message is logged at WARN (default) or FINE level
+ * which can be controlled with a printStackTrace property  
+ */
 public class WebApplicationExceptionMapper 
     implements ExceptionMapper<WebApplicationException> {
 
     private static final Logger LOG = LogUtils.getL7dLogger(WebApplicationExceptionMapper.class);
     private static final String ERROR_MESSAGE_START = "WebApplicationException has been caught,
status: ";
-    private boolean printStackTrace;
+    private boolean printStackTrace = true;
+    private boolean addMessageToResponse;
     
     public Response toResponse(WebApplicationException ex) {
         
@@ -46,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;
@@ -53,23 +63,21 @@ public class WebApplicationExceptionMapp
             flogger = (FaultListener)PhaseInterceptorChain.getCurrentMessage()
                 .getContextualProperty(FaultListener.class.getName());
         }
-        if (flogger != null || LOG.isLoggable(Level.FINE)) {
-            String errorMessage = buildErrorMessage(r, ex);
-            
-            boolean doDefault = 
-                flogger != null ? flogger.faultOccurred(ex, errorMessage, msg) : true;
-            if (doDefault && LOG.isLoggable(Level.FINE)) {
-                LOG.log(Level.FINE, errorMessage, ex);
-            }
-        }
-        if (printStackTrace) {
-            LOG.warning(getStackTrace(ex));
+        String errorMessage = doAddMessage || flogger != null 
+            ? buildErrorMessage(r, ex) : null; 
+        if (flogger == null
+            || !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());
         
@@ -89,10 +97,27 @@ public class WebApplicationExceptionMapp
         ex.printStackTrace(new PrintWriter(sw));
         return sw.toString();
     }
-    
+
+    /**
+     * Control whether to log at WARN or FINE level.
+     * Note this property is ignored if a registered {@link FaultListener} 
+     * has handled the exception
+     * @param printStackTrace if set to true then WARN level is used (default),
+     *        otherwise - FINE level.
+     */
     public void setPrintStackTrace(boolean printStackTrace) {
         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