You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Sadhana Jain <Sa...@rovicorp.com> on 2009/09/28 19:53:07 UTC

Accessing JAX-RS MessageContext in a filter

 
Hi All,

I am trying to get access to MessageContext in a filter (that implements RequestHandler interface)
by doing this:

private MessageContext mc; 

@Context
public void setMessageContext(MessageContext msgC) {
  mc=msgC;  
}

Because I saw in the list that a bug was filed about if @Context was used on a fields like this:
@Context
private MessageContext mc; 

But even using Context on the setter does not work. Do I need to do any wiring in the Spring file when I use the
Setter like above?

Is @Context suppose to work with filters ( I thought they should as filters are like providers)?

What I am trying to do using Context is set a Map that I created in the filter after processing http headers
And I want to access that Map in some other class later in the code and would like to use @Context to get the map.
Is this a good use of @Context? Or can I do this in any other way?

Thanks a lot for any help.
Sadhana

Sadhana Jain
Sr. Software Engineer


Rovi Corporation
795 Folsom St, Suite 200
San Francisco, CA 94107
Direct: 415.247.5023 | Mobile: 925.212.6495
sadhana.jain@rovicorp.com
rovicorp.com


Rovi. The new name for Macrovision.



RE: HELP! Was:RE: Accessing JAX-RS MessageContext in a filter

Posted by "Pydipati, Karuna" <kp...@stubhub.com>.
 
I sent this mail to my team sometime back. Hope, this may be helpful. I
am using it in Impl class. Not in filter.

================
In the REST world, in order to get/set Cookie information or any other
information that is stored in httpRequest, here is the simple and
elegant way. 
 
1. DO NOT add anything in web service interface. Not even in method
signature (Common mistake is that developers try to add arguments to get
Cookie information)
2. Have a filed MessageContext in Impl class and annotate it with
@Context
3. Try to access it in any method. This is thread safe interface because
it implements ThreadLocalMessageContext
4. You can access all the below mentioned information from
MessageContext
    UriInfo, 
    SecurityContext, 
    HttpHeaders, 
    Providers, 
    Request, 
    ContextResolver, 
    Servlet types (HttpServletRequest, HttpServletResponse,
ServletContext, ServletConfig)
 
 
 
Here is example for usage of MessageContext
 
public class SomeWebServiceImpl implements SomeWebServiceService {

    @Context
    MessageContext resp;
 
    public SomeApiResponse someMethod(SomeRequest someRequest) {
        AuthenticatedSession authenticatedSession = null;
        SomeApiResponse someApiResponse = new SomeApiResponse();
        try {
        
            HttpServletResponse httpServletResponse =
resp.getHttpServletResponse().addCookie(STUB_SESS);
            
            HttpHeaders httpheaders = resp.getHttpHeaders();
            
            HttpServletRequest httpServletRequest =
resp.getHttpServletRequest();
            
            ServletConfig servletConfig = resp.getServletConfig();
            
            ServletContext servletContext = resp.getServletContext();
            
            UriInfo uriInfo = resp.getUriInfo();
            
            Providers providers = resp.getProviders();
            
        } catch ( Exception e) {
            log.error(" Exception: " + e);
        }
 
        return someApiResponses;
 
    }
 
 
 
public interface SomeWebServiceService {
 
 @Path("identified")
 @POST
 public  SomeApiResponse someMethod(SomeRequest someRequest);
 
}
====================

Regards
Karuna Pydipati
StubHub/eBay - Platform & Services
Phone: (415)222-8752
Email: kpydipati@ebay.com

 


-----Original Message-----
From: Sadhana Jain [mailto:Sadhana.Jain@rovicorp.com] 
Sent: Tuesday, September 29, 2009 12:55 AM
To: users@cxf.apache.org
Cc: Sergey Beryozkin
Subject: HELP! Was:RE: Accessing JAX-RS MessageContext in a filter

 
Hi cxf group,

Could you please respond to my question below? The main question is if
Context get/set is allowed in JAX-RS filters?
I need an answer urgently as I need to deliver some code by tomorrow And
I need to figure out whether to implement a work around or there is
Something simple I am missing and have overlooked in using JAX-RS CXF?

Thanks very much for your help in the past. Hope to hear from Someone
soon.

Sadhana


-----Original Message-----
From: Sadhana Jain [mailto:Sadhana.Jain@rovicorp.com]
Sent: Monday, September 28, 2009 10:53 AM
To: users@cxf.apache.org
Subject: Accessing JAX-RS MessageContext in a filter

 
Hi All,

I am trying to get access to MessageContext in a filter (that implements
RequestHandler interface) by doing this:

private MessageContext mc; 

@Context
public void setMessageContext(MessageContext msgC) {
  mc=msgC;
}

Because I saw in the list that a bug was filed about if @Context was
used on a fields like this:
@Context
private MessageContext mc; 

But even using Context on the setter does not work. Do I need to do any
wiring in the Spring file when I use the Setter like above?

Is @Context suppose to work with filters ( I thought they should as
filters are like providers)?

What I am trying to do using Context is set a Map that I created in the
filter after processing http headers And I want to access that Map in
some other class later in the code and would like to use @Context to get
the map.
Is this a good use of @Context? Or can I do this in any other way?

Thanks a lot for any help.
Sadhana

Sadhana Jain
Sr. Software Engineer


Rovi Corporation
795 Folsom St, Suite 200
San Francisco, CA 94107
Direct: 415.247.5023 | Mobile: 925.212.6495 sadhana.jain@rovicorp.com
rovicorp.com


Rovi. The new name for Macrovision.



Re: HELP! Was:RE: Accessing JAX-RS MessageContext in a filter

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

Unfortunatley, it will only be possible to inject contexts in filters starting from 2.2.4.
However, as a workaround, you can get most of what you can get from a MessageContext using a provided Message instance
here's some examples :

HttpServletRequest request = (HttpServletRequest)message.get(AbstractHttpDestination.HTTP_REQUEST)
HttpHeaders headers = new HttpHeadersImpl(message);
UriInfo ui = new UriInfoImpl(m, null);
// etc...

let me know please if you need more info,
cheers, Sergey


----- Original Message ----- 
From: "Sadhana Jain" <Sa...@rovicorp.com>
To: <us...@cxf.apache.org>
Cc: "Sergey Beryozkin" <sb...@progress.com>
Sent: Tuesday, September 29, 2009 8:54 AM
Subject: HELP! Was:RE: Accessing JAX-RS MessageContext in a filter



Hi cxf group,

Could you please respond to my question below? The main
question is if Context get/set is allowed in JAX-RS filters?
I need an answer urgently as I need to deliver some code by tomorrow
And I need to figure out whether to implement a work around or there is
Something simple I am missing and have overlooked in using JAX-RS CXF?

Thanks very much for your help in the past. Hope to hear from
Someone soon.

Sadhana


-----Original Message-----
From: Sadhana Jain [mailto:Sadhana.Jain@rovicorp.com]
Sent: Monday, September 28, 2009 10:53 AM
To: users@cxf.apache.org
Subject: Accessing JAX-RS MessageContext in a filter


Hi All,

I am trying to get access to MessageContext in a filter (that implements RequestHandler interface) by doing this:

private MessageContext mc;

@Context
public void setMessageContext(MessageContext msgC) {
  mc=msgC;
}

Because I saw in the list that a bug was filed about if @Context was used on a fields like this:
@Context
private MessageContext mc;

But even using Context on the setter does not work. Do I need to do any wiring in the Spring file when I use the Setter like above?

Is @Context suppose to work with filters ( I thought they should as filters are like providers)?

What I am trying to do using Context is set a Map that I created in the filter after processing http headers And I want to access 
that Map in some other class later in the code and would like to use @Context to get the map.
Is this a good use of @Context? Or can I do this in any other way?

Thanks a lot for any help.
Sadhana

Sadhana Jain
Sr. Software Engineer


Rovi Corporation
795 Folsom St, Suite 200
San Francisco, CA 94107
Direct: 415.247.5023 | Mobile: 925.212.6495 sadhana.jain@rovicorp.com rovicorp.com


Rovi. The new name for Macrovision.



HELP! Was:RE: Accessing JAX-RS MessageContext in a filter

Posted by Sadhana Jain <Sa...@rovicorp.com>.
 
Hi cxf group,

Could you please respond to my question below? The main
question is if Context get/set is allowed in JAX-RS filters?
I need an answer urgently as I need to deliver some code by tomorrow
And I need to figure out whether to implement a work around or there is
Something simple I am missing and have overlooked in using JAX-RS CXF?

Thanks very much for your help in the past. Hope to hear from
Someone soon.

Sadhana


-----Original Message-----
From: Sadhana Jain [mailto:Sadhana.Jain@rovicorp.com] 
Sent: Monday, September 28, 2009 10:53 AM
To: users@cxf.apache.org
Subject: Accessing JAX-RS MessageContext in a filter

 
Hi All,

I am trying to get access to MessageContext in a filter (that implements RequestHandler interface) by doing this:

private MessageContext mc; 

@Context
public void setMessageContext(MessageContext msgC) {
  mc=msgC;
}

Because I saw in the list that a bug was filed about if @Context was used on a fields like this:
@Context
private MessageContext mc; 

But even using Context on the setter does not work. Do I need to do any wiring in the Spring file when I use the Setter like above?

Is @Context suppose to work with filters ( I thought they should as filters are like providers)?

What I am trying to do using Context is set a Map that I created in the filter after processing http headers And I want to access that Map in some other class later in the code and would like to use @Context to get the map.
Is this a good use of @Context? Or can I do this in any other way?

Thanks a lot for any help.
Sadhana

Sadhana Jain
Sr. Software Engineer


Rovi Corporation
795 Folsom St, Suite 200
San Francisco, CA 94107
Direct: 415.247.5023 | Mobile: 925.212.6495 sadhana.jain@rovicorp.com rovicorp.com


Rovi. The new name for Macrovision.