Hi
On 12/09/13 18:32, Mandy Warren wrote:
Hi,
I am trying to write a Fault Interceptor to handle exceptions both from my
application code & from other interceptors. The Interceptor needs to change
the HTTP status code to something appropriate and set the message body with
info that explains the error in more detail (either in JSON or XML
depending on the request mime type).
Many of the examples I have seen modify the HttpResponse using code such as:
HttpServletResponse response = (HttpServletResponse) message
.getExchange().getInMessage()
.get(AbstractHTTPDestination.HTTP_RESPONSE);
however because we have integration tests which use the CXF local transport
rather than HTTP this code doesn't seem to work.
I need a solution which works for both HTTP & Local Transport.
Here's the code I have so far but I get a null pointer thrown as the
OutMessage seems to be null..
public class MyFaultInterceptor extends AbstractPhaseInterceptor<Message> {
public CAPTWOFaultInterceptor() {
super(Phase.PRE_STREAM);
}
public void handleMessage(Message message) throws Fault {
Exception ex = message.getContent(Exception.class);
if (ex == null) {
LOGGER.debug("unexpected null exception");
throw new RuntimeException("Exception is expected");
}
if (!(ex instanceof Fault)) {
LOGGER.debug("unexpected exception type");
throw new RuntimeException("Fault is expected");
}
Fault fault = (Fault)ex;
Throwable causingException = fault.getCause();
LOGGER.debug("handling exception
{}"+causingException.getClass().getName());
if (causingException instanceof SomeBadException) {
Response response =
Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
// outMessage seems to be null!
message.getExchange().getOutMessage().put(Response.class,
response);
}
message.getInterceptorChain().abort(); // not sure if I need this!
}
Any help would be much appreciated!
I think the only way to make it work in a portable way across multiple
transports is to work with JAX-RS 2.0 ExceptionMapper (for catching the
exceptions) and also replace the CXF interceptors with JAX-RS 2.0 in/out
filters - this way it is guaranteed that JAX-RS Response will be
produced and thus it will work even for Local transport.
Using CXF interceptors in JAX-RS would also work with Local transport in
normal flows but the exceptions thrown from such interceptors can only
be handled in fault interceptors, where realistically you need to work
with HttpServletResponse;
I'm not sure why an out message is null; that solution probably won't
work anyway, though may be we should look into it too; I'm not sure
actually we have Local transport tests dealing with JAX-RS server
errors, I'll have a look
Sergey
Many thanks
Mandy
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com