I have a condition I need to check for in my CXF REST service that may result
in having to throw back an error with status 403 (forbidden). I'd like to log
this occurred, but I don't need to have a stack trace appear in my log.
I had tried to make this happen, but it's still putting the
WebApplicationException stack trace into my log.
Another possibly related problem is that the client isn't getting a 403, it's
getting a 500.
In my controller handler, when I detect the condition, I do this:
throw new WebApplicationException(Response.Status.FORBIDDEN)
I've defined the following:
---------------
public class WebApplicationExceptionMapper implements
ExceptionMapper<WebApplicationException> {
private static Logger logger =
Logger.getLogger(WebApplicationExceptionMapper.class);
@Override
public Response toResponse(WebApplicationException ex) {
if (ex.getResponse().getStatus() ==
Response.Status.FORBIDDEN.getStatusCode())
logger.error("Request failed with FORBIDDEN status code.");
else
logger.error(ex);
return null;
}
}
---------------
In my debugger, I saw it throw the exception and the first if block, but in my
console it shows the following (slightly elided) exception:
<Mar 23, 2011 8:57:45 AM PDT> <Error> <HTTP> <BEA-101020>
<[ServletContext@8348067[app:shop module:/apiservice path:/apiservice
spec-version:2.5]] Servlet failed with Exception
java.lang.RuntimeException: org.apache.cxf.interceptor.Fault
at
org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:102)
at
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:315)
at
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:113)
at
org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:97)
at
org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:461)
Truncated. see log file for complete stacktrace
Caused By: org.apache.cxf.interceptor.Fault
at
org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:155)
at
org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:121)
at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:153)
at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:87)
at
org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
Truncated. see log file for complete stacktrace
Caused By: javax.ws.rs.WebApplicationException
at mypackage.MyClass.checkCondition(MyClass.java:NN)