You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by SriramSundararajan01 <Sr...@gmail.com> on 2013/02/22 22:43:51 UTC

Not able to change uri in ContainerrequestFilter CXF2.7.3

Hi Team,

   I got CXF2.7.3 installed in today and was trying to get hands on with
ContainerRequest and Response Filter

I have problems in getting the following changes working

*1. Change the URI in ContainerRequestFilter*
      I wrote a simple filter class, turned on prematching,  and added that
as a provider in the xml file. I wrote the following two lines in the filter
method

/URI redirectURI = new URI(uriInfo.getBaseUri().toString());
reqCtx.setRequestUri(redirectURI);/


Any uri that I give here I always get an exception
"javax.ws.rs.NotFoundException" even though the resource method is there 

  Not sure what is wrong.

*Replace request body in container request filter*
 I replaced the entity stream  inside the filter, but still the request body
that is posted with the request is being picked up and not the entity stream
that i replaced in the filter.

reqCtx.setEntityStream(new ByteArrayInputStream(str.getBytes()));


*ContainerResponseFilter:*

      I created a custom name binding for the filter and it worked with
charm. I thought of returning an object in the resource method and in the
filter forward that to the jsp page based on the custom annotation. 

@Path("/mvcfilter/")
	@GET
	@Produces(MediaType.TEXT_HTML)
	@MVCTemplate
	public DemoBean mvcfilter()  {	    
			DemoBean bean = new DemoBean();
	     return bean;
}

in the filter ,
Response res = Response.ok(RedirectionStatus.LOGIN_SUCCESS).
        entity(responseContext.getEntity()).build();
        responseContext.setEntity(res,
          responseContext.getEntityAnnotations(),
          responseContext.getMediaType());

I thought of using the enum way to redirect the response to jsp page. But I
got "No MessageBodyWriter found for ResponseImpl"  The fillter is being
picked up but not sure whether i can do this or not

    
Can some one please help me out to resove these issues?



--
View this message in context: http://cxf.547215.n5.nabble.com/Not-able-to-change-uri-in-ContainerrequestFilter-CXF2-7-3-tp5723617.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Not able to change uri in ContainerrequestFilter CXF2.7.3

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

thanks for reporting the issues, comments below
On 22/02/13 21:43, SriramSundararajan01 wrote:
> Hi Team,
>
>     I got CXF2.7.3 installed in today and was trying to get hands on with
> ContainerRequest and Response Filter
>
> I have problems in getting the following changes working
>
> *1. Change the URI in ContainerRequestFilter*
>        I wrote a simple filter class, turned on prematching,  and added that
> as a provider in the xml file. I wrote the following two lines in the filter
> method
>
> /URI redirectURI = new URI(uriInfo.getBaseUri().toString());
> reqCtx.setRequestUri(redirectURI);/
>
>
> Any uri that I give here I always get an exception
> "javax.ws.rs.NotFoundException" even though the resource method is there
>
>    Not sure what is wrong.
>
There was an issue with resetting a request URI with a new absolute URI, 
but seem to work OK when a relative URI is provided.
In your case you are resetting a request URI with the (absolute) base 
URI, so that should be fixed now, but note request URI is not 
necessarily the same as base URI, so unless you have a root resource 
listening at "/" with say @GET method also listening on "/", this 
replacement will likely won't work.

> *Replace request body in container request filter*
>   I replaced the entity stream  inside the filter, but still the request body
> that is posted with the request is being picked up and not the entity stream
> that i replaced in the filter.
>
> reqCtx.setEntityStream(new ByteArrayInputStream(str.getBytes()));
>
This has been fixed
>
> *ContainerResponseFilter:*
>
>        I created a custom name binding for the filter and it worked with
> charm. I thought of returning an object in the resource method and in the
> filter forward that to the jsp page based on the custom annotation.
>
> @Path("/mvcfilter/")
> 	@GET
> 	@Produces(MediaType.TEXT_HTML)
> 	@MVCTemplate
> 	public DemoBean mvcfilter()  {	
> 			DemoBean bean = new DemoBean();
> 	     return bean;
> }
>
> in the filter ,
> Response res = Response.ok(RedirectionStatus.LOGIN_SUCCESS).
>          entity(responseContext.getEntity()).build();
>          responseContext.setEntity(res,
>            responseContext.getEntityAnnotations(),
>            responseContext.getMediaType());
>
> I thought of using the enum way to redirect the response to jsp page. But I
> got "No MessageBodyWriter found for ResponseImpl"  The fillter is being
> picked up but not sure whether i can do this or not
>

Right, this code is not likely to work as expected :-). 
ContainerResponseContext is the view into the actual Response, so if you 
set and entity on it, then it will be assumed to be an entity of the 
underlying Response.

Besides,

 > Response res = Response.ok(RedirectionStatus.LOGIN_SUCCESS).
 >          entity(responseContext.getEntity()).build();

actually sets the entity twice, once via ok(), and then via entity(), 
and then the whole newly built Response is finally set as an entity on 
the actual Response.


What are you trying to do anyway ? For the enums be recognized by 
RequestDispatcherProvider as valid responses this provider needs to be 
configured directly and it is typically done for the purposes of linking 
otherwise empty Responses with (HTML) views, but in this case you return 
DemoBean. Assuming it is just an empty Response,  as in

public Response mvcfilter()  {	
     return Response.ok().build();
}

then, in a filter, you'd just do

responseContext.setEntity(RedirectionStatus.LOGIN_SUCCESS, ...)

FYI, I've submitted a request to have a simpler setEntity added to 
ContainerResponseContext (without having to specify annotations & media 
type)

HTH, Sergey
>
> Can some one please help me out to resove these issues?
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Not-able-to-change-uri-in-ContainerrequestFilter-CXF2-7-3-tp5723617.html
> Sent from the cxf-user mailing list archive at Nabble.com.


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com