You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by HG-1 <hg...@proofpoint.com> on 2009/03/18 22:40:25 UTC

Set HTTP Response in Interceptor

I am using interceptor to check for existence of particular headers. If
headers are not there, then I want to the HTTP response to be a specific
HTTP resonse. But it seems that if I throw a fault, it always it always
returns 500 response. How can I send custom response codes in interceptor?

Here is my sample interceptor:

public class AuthCxfInterceptor extends AbstractPhaseInterceptor<Message> 
{
	
	public AuthCxfInterceptor() 
	{
        super(Phase.READ);
	}
	
	
	@Override
	public void handleMessage(Message message) throws Fault
	{
        HashMap pheaders = (HashMap) message.get(Message.PROTOCOL_HEADERS);        		
        
        Exception unauthException = new
WebApplicationException(Response.status(Status.UNAUTHORIZED).build());
        List<?> hdrValues = (List<?>) pheaders.get("myheaderkey");

        if (hdrValues.size() < 1)
        {
        	log.info("Missing myheaderkey");
            throw new Fault(unauthException);
        }
}

-- 
View this message in context: http://www.nabble.com/Set-HTTP-Response-in-Interceptor-tp22589098p22589098.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Set HTTP Response in Interceptor

Posted by Daniel Kulp <dk...@apache.org>.
There is a Message.RESPONSE_CODE key that would be an integer for the for 
response code.  However, I think that needs to be set on the Fault message 
that would be created as part of the fault processing.   Thus, you MAY need to 
add an interceptor to the FaultOutChain to look for your fault and adjust the 
code.

Dan


On Wed March 18 2009 5:40:25 pm HG-1 wrote:
> I am using interceptor to check for existence of particular headers. If
> headers are not there, then I want to the HTTP response to be a specific
> HTTP resonse. But it seems that if I throw a fault, it always it always
> returns 500 response. How can I send custom response codes in interceptor?
>
> Here is my sample interceptor:
>
> public class AuthCxfInterceptor extends AbstractPhaseInterceptor<Message>
> {
>
> 	public AuthCxfInterceptor()
> 	{
>         super(Phase.READ);
> 	}
>
>
> 	@Override
> 	public void handleMessage(Message message) throws Fault
> 	{
>         HashMap pheaders = (HashMap) message.get(Message.PROTOCOL_HEADERS);
>
>         Exception unauthException = new
> WebApplicationException(Response.status(Status.UNAUTHORIZED).build());
>         List<?> hdrValues = (List<?>) pheaders.get("myheaderkey");
>
>         if (hdrValues.size() < 1)
>         {
>         	log.info("Missing myheaderkey");
>             throw new Fault(unauthException);
>         }
> }

-- 
Daniel Kulp
dkulp@apache.org
http://www.dankulp.com/blog

Re: Set HTTP Response in Interceptor

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi,

At the moment if you throw a Fault like this then it will escape the JAXRS runtime. I'll have to add JAXRS in/out fault interceptors 
to handle cases like this one which will check if the cause of a given Fault is a WebApplicationException.
The easiest way at the moment is to write a blocking CXF JAXRS RequestFilter and return a Response instance for the request to be 
blocked. This filter instance can be registered as a provider from Spring, please see [1] for more info

Cheers, Sergey

[1] http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-Filters

>
> I am using interceptor to check for existence of particular headers. If
> headers are not there, then I want to the HTTP response to be a specific
> HTTP resonse. But it seems that if I throw a fault, it always it always
> returns 500 response. How can I send custom response codes in interceptor?
>
> Here is my sample interceptor:
>
> public class AuthCxfInterceptor extends AbstractPhaseInterceptor<Message>
> {
>
> public AuthCxfInterceptor()
> {
>        super(Phase.READ);
> }
>
>
> @Override
> public void handleMessage(Message message) throws Fault
> {
>        HashMap pheaders = (HashMap) message.get(Message.PROTOCOL_HEADERS);
>
>        Exception unauthException = new
> WebApplicationException(Response.status(Status.UNAUTHORIZED).build());
>        List<?> hdrValues = (List<?>) pheaders.get("myheaderkey");
>
>        if (hdrValues.size() < 1)
>        {
>        log.info("Missing myheaderkey");
>            throw new Fault(unauthException);
>        }
> }
>
> -- 
> View this message in context: http://www.nabble.com/Set-HTTP-Response-in-Interceptor-tp22589098p22589098.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>