You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by inteloid <ha...@gmail.com> on 2013/08/07 15:46:21 UTC

Statically access MessageContext

Hi all.

I have a spring managed CXF application where CXF resources simply forward
calls to spring services. 
Also I've set up CXF implementation of OAuth2 framework.

Now my problem is that I want to get the credentials of user from my spring
services, i.e. somehow I need statically access MessageContext object. The
question is how to do that?

Thanks in advance.



--
View this message in context: http://cxf.547215.n5.nabble.com/Statically-access-MessageContext-tp5732065.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Statically access MessageContext

Posted by Sergey Beryozkin <sb...@gmail.com>.
Then I guess you can try

new MessageContextImpl(PhaseInterceptorChain.getCurrentMessage());

and proceed from there by working with OAuthContextUtils

or

OAuthContext context = 
PhaseInterceptorChain.getCurrentMessage().getContent(OAuthContext.class)

and work with the OAuth context directly

HTH, Sergey

On 12/08/13 14:21, inteloid wrote:
> We use spring configuration, and can't make our services per request.
>
> The stack is as follows:
>
> Web->JAX-RS Resource->Spring Service (Business Logic)->DB Layer->DB
>
> JAX-RS resources effectively get access to valid MessageContext by
> implementing
> @Context
> void setMessageContext(MessageContext mc)
> The problem is, I don't need it inside my resource methods as they just
> forward calls to appropriate business logic services.
>
> Our business services are singletons and this can't be changed for many
> reasons. To implement security checks inside them (which are also business
> related checks) we need data from MessageContext (well, we'll wrap it in
> something fancy and less transport dependent, we need only clinetId and
> userSubject).
>
> Currently, I'm trying to build a Utility class, which will be used by
> business services, which gets that MessageContext data from some static
> place.
>
> Hope I've explained properly, if not I can post some diagrams.
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Statically-access-MessageContext-tp5732065p5732197.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

Re: Statically access MessageContext

Posted by inteloid <ha...@gmail.com>.
We use spring configuration, and can't make our services per request.

The stack is as follows:

Web->JAX-RS Resource->Spring Service (Business Logic)->DB Layer->DB

JAX-RS resources effectively get access to valid MessageContext by
implementing
@Context
void setMessageContext(MessageContext mc)
The problem is, I don't need it inside my resource methods as they just
forward calls to appropriate business logic services.

Our business services are singletons and this can't be changed for many
reasons. To implement security checks inside them (which are also business
related checks) we need data from MessageContext (well, we'll wrap it in
something fancy and less transport dependent, we need only clinetId and
userSubject).

Currently, I'm trying to build a Utility class, which will be used by
business services, which gets that MessageContext data from some static
place.

Hope I've explained properly, if not I can post some diagrams.



--
View this message in context: http://cxf.547215.n5.nabble.com/Statically-access-MessageContext-tp5732065p5732197.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Statically access MessageContext

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 11/08/13 17:21, inteloid wrote:
> Thanks Sergey.
>
> from Daniel's suggestion I didn't find a way to get OAuth2 properties like
> clientId, roles, etc. Do you know how to get them?
I guess you need to use

http://svn.apache.org/repos/asf/cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/OAuthContextUtils.java

It depends on the injected MessageContext.
Actually, you said it was not possible to inject it ? Given that you 
mentioned a per-request (Spring) scope, may be this can help:

http://cxf.apache.org/docs/jaxrs-services-configuration.html#JAXRSServicesConfiguration-FromSpring


Sergey
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Statically-access-MessageContext-tp5732065p5732184.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

Re: Statically access MessageContext

Posted by inteloid <ha...@gmail.com>.
Thanks Sergey.

from Daniel's suggestion I didn't find a way to get OAuth2 properties like
clientId, roles, etc. Do you know how to get them?



--
View this message in context: http://cxf.547215.n5.nabble.com/Statically-access-MessageContext-tp5732065p5732184.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Statically access MessageContext

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 11/08/13 02:48, inteloid wrote:
> Thank you very much Daniel.
>
> One more question, I've digged the codes and noticed that CXF relies on
> Java's standard ThreadLocals, which means, request scope may not be the same
> as thread scope, as tomcat uses a threadpool.
>
> How CXF deals with that problem to make sure contexts are not messed up?

As far as CXF is concerned, it clears its ThreadLocals once the current 
invocation is complete,

Cheers, Sergey

>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Statically-access-MessageContext-tp5732065p5732182.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>


Re: Statically access MessageContext

Posted by inteloid <ha...@gmail.com>.
Thank you very much Daniel.

One more question, I've digged the codes and noticed that CXF relies on
Java's standard ThreadLocals, which means, request scope may not be the same
as thread scope, as tomcat uses a threadpool. 

How CXF deals with that problem to make sure contexts are not messed up?



--
View this message in context: http://cxf.547215.n5.nabble.com/Statically-access-MessageContext-tp5732065p5732182.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Statically access MessageContext

Posted by Daniel Kulp <dk...@apache.org>.
The easiest is likely to call PhaseInterceptorChain.getCurrentMessage() to grab the internal CXF message and pull the various properties out of there.

Dan


On Aug 7, 2013, at 9:46 AM, inteloid <ha...@gmail.com> wrote:

> Hi all.
> 
> I have a spring managed CXF application where CXF resources simply forward
> calls to spring services. 
> Also I've set up CXF implementation of OAuth2 framework.
> 
> Now my problem is that I want to get the credentials of user from my spring
> services, i.e. somehow I need statically access MessageContext object. The
> question is how to do that?
> 
> Thanks in advance.
> 
> 
> 
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Statically-access-MessageContext-tp5732065.html
> Sent from the cxf-user mailing list archive at Nabble.com.

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