Ard Schrijvers pushed to branch feature/projectdocs at cms-community / 
hippo-repository


Commits:
60c56890 by Ard Schrijvers at 2017-10-20T13:56:34+02:00
REPO-1870 Avoid long stacktraces being logged by default

When hitting a non existing endpoint, or an endpoint with the wrong
method, by default, a long stacktrace is logged.

By default, we should log a single line warning and in debug level the
whole stacktrace.

Unfortunately using:

 final WebApplicationExceptionMapper webApplicationExceptionMapper = new 
WebApplicationExceptionMapper();
 webApplicationExceptionMapper.setPrintStackTrace(false);
 endpointFactory.setProvider(webApplicationExceptionMapper);

doesn't do the job because then nothing is logged any more (unless in FINE
log level).

Hence extend the WebApplicationExceptionMapper, log a warning and
delegate

- - - - -


1 changed file:

- jaxrs/src/main/java/org/onehippo/repository/jaxrs/RepositoryJaxrsService.java


Changes:

=====================================
jaxrs/src/main/java/org/onehippo/repository/jaxrs/RepositoryJaxrsService.java
=====================================
--- 
a/jaxrs/src/main/java/org/onehippo/repository/jaxrs/RepositoryJaxrsService.java
+++ 
b/jaxrs/src/main/java/org/onehippo/repository/jaxrs/RepositoryJaxrsService.java
@@ -29,13 +29,17 @@ import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Application;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
 
 import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
 import org.apache.cxf.endpoint.Server;
 import org.apache.cxf.jaxrs.JAXRSInvoker;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper;
 import org.apache.cxf.jaxrs.utils.ResourceUtils;
 import org.apache.cxf.transport.http.AbstractHTTPDestination;
 import org.apache.cxf.transport.http.DestinationRegistry;
@@ -190,6 +194,11 @@ public final class RepositoryJaxrsService {
             endpointFactory.setAddress(address);
             endpointFactory.setDestinationFactory(destinationFactory);
 
+            // don't print entire stacktraces
+            final WebApplicationExceptionLogger webApplicationExceptionLogger 
= new WebApplicationExceptionLogger();
+            webApplicationExceptionLogger.setPrintStackTrace(false);
+            endpointFactory.setProvider(webApplicationExceptionLogger);
+
             CXFRepositoryJaxrsEndpoint cxfEndpoint =
                     endpoint instanceof CXFRepositoryJaxrsEndpoint ? 
(CXFRepositoryJaxrsEndpoint)endpoint : null;
 
@@ -276,4 +285,18 @@ public final class RepositoryJaxrsService {
             }
         }
     }
+
+
+    private static class WebApplicationExceptionLogger extends 
WebApplicationExceptionMapper {
+
+        @Override
+        public Response toResponse(final WebApplicationException exception) {
+            if (log.isDebugEnabled()) {
+                log.warn(exception.toString(), exception);
+            } else {
+                log.warn(exception.toString());
+            }
+            return super.toResponse(exception);
+        }
+    }
 }



View it on GitLab: 
https://code.onehippo.org/cms-community/hippo-repository/commit/60c5689068287932769484c03927f6414213778f

---
View it on GitLab: 
https://code.onehippo.org/cms-community/hippo-repository/commit/60c5689068287932769484c03927f6414213778f
You're receiving this email because of your account on code.onehippo.org.
_______________________________________________
Hippocms-svn mailing list
Hippocms-svn@lists.onehippo.org
https://lists.onehippo.org/mailman/listinfo/hippocms-svn

Reply via email to