Thanks Sergey.
It worked like a charm for application/json content-types but its
return 304 with no payload (obviously) for text/xml content types.
Can you throw light as to what might be going on here?
Here is the snippet based on what you had suggested.


=========
/**
 * REST fault out interceptor that can send exceptions raised by the
 * interceptor, if any, according to the Accept header set by the client(s)
 *
 * @author vinay
 *
 */
public class RestFaultOutInterceptor extends JAXRSOutInterceptor {
        private static final Logger LOG = LoggerFactory
                        .getLogger(RestFaultOutInterceptor.class);

        public RestFaultOutInterceptor() {
                getBefore().add(LoggingOutInterceptor.class.getName());
        }

        public void handleMessage(Message message) {
                Exception ex = message.getContent(Exception.class);
                LOG.info("Got exception " + ex);
                if (ex == null) {
                        throw new RuntimeException("Exception is expected");
                }
                if (!(ex instanceof Fault)) {
                        throw new RuntimeException("Fault is expected");
                }
                Exception cause = (Exception) ex.getCause();
                Response response = JAXRSUtils.convertFaultToResponse(cause, 
message
                                .getExchange().getInMessage());
                message.setContent(List.class, new 
MessageContentsList(response));
                super.handleMessage(message);
                message.getInterceptorChain().abort();
        }
}
===============

Regards,
Vinay

On Wed, Apr 13, 2011 at 10:58 AM, Sergey Beryozkin <[email protected]> wrote:
> Hi
>
> On Wed, Apr 13, 2011 at 3:20 PM, vinayc <[email protected]> wrote:
>>
>> Hi,
>>  I am using CXF 2.3.3, and I have a custom cxf "in" interceptor attached
>> to
>> a REST service.
>> When this interceptor throws an exception, xmlfaultoutinterceptor is
>> called.
>>
> Yes, that will happen if the exception is thrown from a CXF interceptor,
> outside of the JAX-RS scope...
>
>>
>> Is there an existing cxf fault out interceptor that can format the message
>> based on  the "Accept:" header set by the client since right now even if
>> the
>> caller is setting "application/json" type, it still returns an "text/xml"
>> content back (since its XmlFaultOutIntereceptor that sends the fault
>> back)?
>>
>
> Can you try the following:
> - add a custom out fault interceptor which will delegate internally to an
> instance of JAXRSOutInterceptor.handleMessage() and abort the current out
> fault chain afterwards ? Before delegating, it should probably do the
> following:
>
> 1. Get the Exception from the message:
> Exception ex = ((Fault)message.getContent(Exception.class)).getCause();
>
> 2. Next you need to wrap it in JAX-RS Response
>
> You can do it manually:
>
> Response r =
> Response.status(status).type("application/json").entity(exceptionObject).build();
>
>
> or if you have ExceptionMappers then you can use this call:
>
> Response r = JAXRSUtils.convertFaultToResponse(ex,
> message.getExchange().getInMessage());
>
> 3. Set Response on the message:
>
> message.setContent(List.class, new MessageContentList(r));
>
> 4. new JAXRSOutInterceptor.handleMessage(message);
> 5. message.getInterceptorChain().abort();
>
> that may work
> Cheers, Sergey
>
>>
>> Thanks
>>
>> Regards,
>> Vinay
>>
>> --
>> View this message in context:
>> http://cxf.547215.n5.nabble.com/Handling-exceptions-thrown-by-interceptors-for-rest-calls-tp4300713p4300713.html
>> Sent from the cxf-user mailing list archive at Nabble.com.
>
>



-- 
Warm regards,
Vinay Chandrasekharan

Reply via email to