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!
Many thanks
Mandy