You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Pratyush Chandra <co...@yahoo.com> on 2011/03/31 13:14:19 UTC

Accessing MessageContext and http headers across service layers

I can inject @Context in my service implementation and get say locale from
http headers. I want to pass this information something like this :

 serviceimpl -> managerimpl -> daoimpl

I want locale to be available to managerimpl and daoimpl as well. Is their a
way to pass this information to other layers apart from service impl,
without passing it through function parameters. Something like
servletcontext or global variables per request ?



--
View this message in context: http://cxf.547215.n5.nabble.com/Accessing-MessageContext-and-http-headers-across-service-layers-tp4273158p4273158.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Accessing MessageContext and http headers across service layers

Posted by Daniel Kulp <dk...@apache.org>.
On Friday, May 13, 2011 3:42:12 PM Pratyush Chandra wrote:
> Hmm..then a good way to have kind of global data in one service request...
> Thanks a lot !

Yep.  That's pretty much exactly what it's there for.   That said, be careful 
what things you set in the "setContent".   That is what CXF uses for the 
content as it transforms the content from form to form during the 
interceptors.    Thus, things like streams and readers and such are already 
"used".  You likely should use msg.setProperty(...) instead using your own 
defined keys.

Dan


> 
> --
> View this message in context:
> http://cxf.547215.n5.nabble.com/Accessing-MessageContext-and-http-headers-
> across-service-layers-tp4273158p4394104.html Sent from the cxf-user mailing
> list archive at Nabble.com.

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

Re: Accessing MessageContext and http headers across service layers

Posted by Pratyush Chandra <co...@yahoo.com>.
Hmm..then a good way to have kind of global data in one service request...
Thanks a lot !

--
View this message in context: http://cxf.547215.n5.nabble.com/Accessing-MessageContext-and-http-headers-across-service-layers-tp4273158p4394104.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Accessing MessageContext and http headers across service layers

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

On Fri, May 13, 2011 at 1:25 PM, Pratyush Chandra
<co...@yahoo.com> wrote:
> Is PhaseInterceptorChain.getCurrentMessage() thread safe ?
>
> Also if I do something like,
>
> PhaseInterceptorChain.getCurrentMessage().setContent(String.class,"Myname");
>
> i.e set some data in in-interceptor...will be threadsafe and global to
> access from my code flow..across from wherever i call ?
>
> PhaseInterceptorChain.getCurrentMessage().getContent(String.class);
>
>
>
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Accessing-MessageContext-and-http-headers-across-service-layers-tp4273158p4392956.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>



-- 
Sergey Beryozkin

Application Integration Division of Talend
http://sberyozkin.blogspot.com

Re: Accessing MessageContext and http headers across service layers

Posted by Pratyush Chandra <co...@yahoo.com>.
Is PhaseInterceptorChain.getCurrentMessage() thread safe ?

Also if I do something like,

PhaseInterceptorChain.getCurrentMessage().setContent(String.class,"Myname"); 

i.e set some data in in-interceptor...will be threadsafe and global to
access from my code flow..across from wherever i call ?

PhaseInterceptorChain.getCurrentMessage().getContent(String.class); 






--
View this message in context: http://cxf.547215.n5.nabble.com/Accessing-MessageContext-and-http-headers-across-service-layers-tp4273158p4392956.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Accessing MessageContext and http headers across service layers

Posted by Pratyush Chandra <co...@yahoo.com>.
I think i got your point...thanks :)

--
View this message in context: http://cxf.547215.n5.nabble.com/Accessing-MessageContext-and-http-headers-across-service-layers-tp4273158p4274741.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Accessing MessageContext and http headers across service layers

Posted by Sergey Beryozkin <sb...@gmail.com>.
If manager and dao impls could also act as JAX-RS subresources (i.e, they
could themselves service JAX-RS requests) then, provided CXF gets updated,
CXF might be able to inject @Contexts such HttpHeaders/etc into subresource
fields.

Cheers, Sergey

On Thu, Mar 31, 2011 at 2:06 PM, Pratyush Chandra
<co...@yahoo.com>wrote:

> Message message = PhaseInterceptorChain.getCurrentMessage(); worked like a
> charm...is it a global throught my service request ?
>
> For your 1st approach,
>
>
> I have access to manager class instance in serviceimpl...and dao in
> manager...
> I dont know how to inject in subresources ? Can you elaborate more on
> subresources ?
>
>
>
> --
> View this message in context:
> http://cxf.547215.n5.nabble.com/Accessing-MessageContext-and-http-headers-across-service-layers-tp4273158p4273331.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>



-- 
Sergey Beryozkin

Application Integration Division of Talend <http://www.talend.com>
http://sberyozkin.blogspot.com

Re: Accessing MessageContext and http headers across service layers

Posted by Pratyush Chandra <co...@yahoo.com>.
Message message = PhaseInterceptorChain.getCurrentMessage(); worked like a
charm...is it a global throught my service request ?

For your 1st approach,


I have access to manager class instance in serviceimpl...and dao in
manager...
I dont know how to inject in subresources ? Can you elaborate more on
subresources ?



--
View this message in context: http://cxf.547215.n5.nabble.com/Accessing-MessageContext-and-http-headers-across-service-layers-tp4273158p4273331.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Accessing MessageContext and http headers across service layers

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

On Thu, Mar 31, 2011 at 12:14 PM, Pratyush Chandra <coolpratyush_c@yahoo.com
> wrote:

> I can inject @Context in my service implementation and get say locale from
> http headers. I want to pass this information something like this :
>
>  serviceimpl -> managerimpl -> daoimpl
>
> I want locale to be available to managerimpl and daoimpl as well. Is their
> a
> way to pass this information to other layers apart from service impl,
> without passing it through function parameters. Something like
> servletcontext or global variables per request ?
>
>
>
Can serviceimpl, managerimpl and daoimpl all act as JAX-RS subresources ? In
principle, injecting contexts into subresources is possible, the problem is
that JAX-RS runtime does not know if a given subresource is a singleton or
created on every new request, so injecting thread-safe contexts can be
problematic. That said, it may be done, but with the help of the contextual
properties. For example, if a message property is set specifying that
subresources are singletons then the JAX-RS runtime will synchronize when
accessing a given subresources otherwise it will inject a given context
directly

Alternatively you can access a current message using this code:
PhaseInterceptorChain.getCurrentMessage() and then get HttpServletRequest
from it, using AbstractHttpDestination.HTTP_REQUEST property

Cheers, Sergey