You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by iris ding <ir...@gmail.com> on 2014/07/22 08:26:27 UTC

@context injection through setter method - method name restriction

I found out CXF has method name restriction for @context injection through
setter method:
private void checkContextMethod(Method m, Object provider) {
        Class<?> type = m.getParameterTypes()[0];
        if (m.getName().equals("set" + type.getSimpleName())) {        
            addContextMethod(type, m, provider);
        }
    }

It is fine we enforces the method starts with 'set', but I do not think we
should enforce other naming check.  Instead, according to spec, we should
add check to enforce the Parameter type for the setter method  be one of
following:
    UriInfo
    HttpHeaders
    Request
    SecurityContext

What do you think about this problem? 

Iris Ding





--
View this message in context: http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712.html
Sent from the cxf-dev mailing list archive at Nabble.com.

Re: @context injection through setter method - method name restriction

Posted by iris ding <ir...@gmail.com>.
Sergey,

Thanks. I am willing to contribute  I will start to work in the
ClassCastException soon.

Iris Ding



--
View this message in context: http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712p5747118.html
Sent from the cxf-dev mailing list archive at Nabble.com.

Re: @context injection through setter method - method name restriction

Posted by Sergey Beryozkin <sb...@gmail.com>.
Thanks. I think Andrey has started working on it, so it is under control,
Would you like to start contributing ? It will be welcome, may be you 
can start with a patch to the ClassCastException (InputStream) issue, so 
that InternalServerError is thrown instead, I don't think we should 
start converting the in InputStream to ByteArrayInputStream, etc...

Cheers, Sergey
On 25/07/14 12:43, iris ding wrote:
> Thanks a lot Sergey.  Will fix #1 in cxf-5897.
>
> Iris Ding
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712p5746890.html
> Sent from the cxf-dev mailing list archive at Nabble.com.
>


-- 
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com

Re: @context injection through setter method - method name restriction

Posted by iris ding <ir...@gmail.com>.
Thanks a lot Sergey.  Will fix #1 in cxf-5897. 

Iris Ding



--
View this message in context: http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712p5746890.html
Sent from the cxf-dev mailing list archive at Nabble.com.

Re: @context injection through setter method - method name restriction

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 24/07/14 13:34, iris ding wrote:
> I think we can make it follow below two rules:
>
> 1. If the parameterType is not an Interface, just ignore the method.
> 2. Scan available providers to make sure there is one provider who can deal
> with the parametertype, otherwise, just ignore it as well.
Yes, 1. can be implemented as part the JIRA you created (the proxy won't 
be created for non interfaces either way but no point in even trying to 
do it).
2. will be implemented some time later. We have the expected NPE 
reported to applications accessing such 'noop' contexts but I agree 
makes sense to optimize, no point in the application code having 
injected no-op proxies

Sergey
>
> any thoughts?
>
> Iris Ding
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712p5746841.html
> Sent from the cxf-dev mailing list archive at Nabble.com.
>


-- 
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com

Re: @context injection through setter method - method name restriction

Posted by iris ding <ir...@gmail.com>.
I think we can make it follow below two rules:

1. If the parameterType is not an Interface, just ignore the method.
2. Scan available providers to make sure there is one provider who can deal
with the parametertype, otherwise, just ignore it as well.

any thoughts?

Iris Ding



--
View this message in context: http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712p5746841.html
Sent from the cxf-dev mailing list archive at Nabble.com.

Re: @context injection through setter method - method name restriction

Posted by Sergey Beryozkin <sb...@gmail.com>.
OK, thanks for the clarification...

Note, there's some weakness in the current approach to do with the proxy 
being injected for custom interfaces even if no provider for a given 
non-standard interface exists, something that Andrey was working upon. 
At the moment, in such cases, NPE will be thrown when the application 
code access such a context which is expected, though we will try to 
optimize it and remove the injected proxies earlier if it is known that 
NPE will be inevitable, a bit of work will need to be done to get it 
optimized


Cheers, Sergey
On 24/07/14 09:21, iris ding wrote:
> Sergey,
>
> I mean if we want to support parameter type(custom context) in setter method
> other than those specified in spec, we can not add check about parameter
> type.
>
> Iris Ding
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712p5746829.html
> Sent from the cxf-dev mailing list archive at Nabble.com.
>


Re: @context injection through setter method - method name restriction

Posted by iris ding <ir...@gmail.com>.
Sergey,

I mean if we want to support parameter type(custom context) in setter method
other than those specified in spec, we can not add check about parameter
type.

Iris Ding



--
View this message in context: http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712p5746829.html
Sent from the cxf-dev mailing list archive at Nabble.com.

Re: @context injection through setter method - method name restriction

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 23/07/14 17:35, iris ding wrote:
> OK, Then Sergey if we want to allow custom context we need support it via
> another way..
What do you mean ? CXF supports it its way, different from the Jersey way...

Thanks, Sergey
>
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712p5746802.html
> Sent from the cxf-dev mailing list archive at Nabble.com.
>


Re: @context injection through setter method - method name restriction

Posted by iris ding <ir...@gmail.com>.
OK, Then Sergey if we want to allow custom context we need support it via
another way..




--
View this message in context: http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712p5746802.html
Sent from the cxf-dev mailing list archive at Nabble.com.

Re: @context injection through setter method - method name restriction

Posted by Sergey Beryozkin <sb...@gmail.com>.
The only standard context class the injection of which is not supported is

JAX-RS 2.0 Configuration.

It is supported in the new Client API, but I've honestly no idea what 
does it mean to inject it into a given JAX-RS service resource class...

Having it used directly by the application class does not seem to make 
sense, but may be I'm wrong, I'll need to look into it further.

Can you please give me a favor and investigate if people are using it at 
all as a server-side injected context ? I'll open a JIRA in meantime to 
track the issue

Thanks, Sergey

On 23/07/14 17:09, Sergey Beryozkin wrote:
> HttpSerletRequest/etc also have to be supported in mist cases...
>
> Cheers, Sergey
> On 23/07/14 15:22, Sergey Beryozkin wrote:
>> On 23/07/14 12:31, Grant Yang wrote:
>>> This code makes me wonder what will happen if we just have
>>> @Context
>>> public void setString(String string) {
>>>    System.out.println("shall that happen?"\n");
>>> }
>> CXF supports custom contexts by creating proxies with the help of Java
>> Proxy so in this case it won't be created as well as for any other
>> non-interface types.
>>
>> No validation has been done so far on the injection, we are starting
>> doing some work around it.
>>
>> Next, when you have
>>
>> @Context
>> public void setMyInterface(MyInterface interface) {
>>      System.out.println("shall that happen?"\n");
>> }
>>
>> you will get a proxy injected but also NPE at runtime unless a
>> ContextProvider supporting MyInterface. We will be working on avoiding
>> keeping such proxies if no ContextProvider is available
>>
>> Sergey
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712p5746783.html
>>>
>>>
>>> Sent from the cxf-dev mailing list archive at Nabble.com.
>>>
>>
>
>


-- 
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com

Re: @context injection through setter method - method name restriction

Posted by Sergey Beryozkin <sb...@gmail.com>.
HttpSerletRequest/etc also have to be supported in mist cases...

Cheers, Sergey
On 23/07/14 15:22, Sergey Beryozkin wrote:
> On 23/07/14 12:31, Grant Yang wrote:
>> This code makes me wonder what will happen if we just have
>> @Context
>> public void setString(String string) {
>>    System.out.println("shall that happen?"\n");
>> }
> CXF supports custom contexts by creating proxies with the help of Java
> Proxy so in this case it won't be created as well as for any other
> non-interface types.
>
> No validation has been done so far on the injection, we are starting
> doing some work around it.
>
> Next, when you have
>
> @Context
> public void setMyInterface(MyInterface interface) {
>      System.out.println("shall that happen?"\n");
> }
>
> you will get a proxy injected but also NPE at runtime unless a
> ContextProvider supporting MyInterface. We will be working on avoiding
> keeping such proxies if no ContextProvider is available
>
> Sergey
>>
>>
>>
>> --
>> View this message in context:
>> http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712p5746783.html
>>
>> Sent from the cxf-dev mailing list archive at Nabble.com.
>>
>



Re: @context injection through setter method - method name restriction

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 23/07/14 17:02, iris ding wrote:
> According to spec in chapter 9,  only below types are allowed to be injected
> via @context:
>
> Application
> UriInfo
> HttpHeaders
> Request
> SecurityContext
> Providers
> ResourceContext
> Configuration
>
> So we can add a check to make sure the parameterType is one of them. If it
> is other type we can just ignore it.  any thoughts?
>
> Have CXF already supported all above types now?
The above list is incomplete, ResourceInfo, ContextResolver are two 
contexts which are missing...
FYI, Jersey supports the injection of custom contexts too...

Cheers, Sergey

>
> Iris Ding
>
>
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712p5746796.html
> Sent from the cxf-dev mailing list archive at Nabble.com.
>


-- 
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com

Re: @context injection through setter method - method name restriction

Posted by iris ding <ir...@gmail.com>.
According to spec in chapter 9,  only below types are allowed to be injected
via @context:

Application
UriInfo
HttpHeaders
Request
SecurityContext
Providers
ResourceContext
Configuration

So we can add a check to make sure the parameterType is one of them. If it
is other type we can just ignore it.  any thoughts?

Have CXF already supported all above types now?

Iris Ding





--
View this message in context: http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712p5746796.html
Sent from the cxf-dev mailing list archive at Nabble.com.

Re: @context injection through setter method - method name restriction

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 23/07/14 12:31, Grant Yang wrote:
> This code makes me wonder what will happen if we just have
> @Context
> public void setString(String string) {
>    System.out.println("shall that happen?"\n");
> }
CXF supports custom contexts by creating proxies with the help of Java 
Proxy so in this case it won't be created as well as for any other 
non-interface types.

No validation has been done so far on the injection, we are starting 
doing some work around it.

Next, when you have

@Context
public void setMyInterface(MyInterface interface) {
     System.out.println("shall that happen?"\n");
}

you will get a proxy injected but also NPE at runtime unless a 
ContextProvider supporting MyInterface. We will be working on avoiding 
keeping such proxies if no ContextProvider is available

Sergey
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712p5746783.html
> Sent from the cxf-dev mailing list archive at Nabble.com.
>


Re: @context injection through setter method - method name restriction

Posted by Grant Yang <yg...@cn.ibm.com>.
This code makes me wonder what will happen if we just have 
@Context
public void setString(String string) {
  System.out.println("shall that happen?"\n");
}



--
View this message in context: http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712p5746783.html
Sent from the cxf-dev mailing list archive at Nabble.com.

Re: @context injection through setter method - method name restriction

Posted by iris ding <ir...@gmail.com>.
Thanks Andrei and Sergey. 

An issue is opened to track this:
https://issues.apache.org/jira/browse/CXF-5897




--
View this message in context: http://cxf.547215.n5.nabble.com/context-injection-through-setter-method-method-name-restriction-tp5746712p5746781.html
Sent from the cxf-dev mailing list archive at Nabble.com.

Re: @context injection through setter method - method name restriction

Posted by Sergey Beryozkin <sb...@gmail.com>.
+1


Thanks, Sergey
On 22/07/14 11:40, Andrei Shakirin wrote:
> Hi Iris,
>
> This restriction was already discussed in http://cxf.547215.n5.nabble.com/Getting-hold-of-JAX-RS-MessageContext-tt5745816.html .
> Yes, it makes sense to relax / drop the restriction.
>
> Could you please create an issue for this?
>
> Regards,
> Andrei.
>
>> -----Original Message-----
>> From: iris ding [mailto:irisdingbj@gmail.com]
>> Sent: Dienstag, 22. Juli 2014 08:26
>> To: dev@cxf.apache.org
>> Subject: @context injection through setter method - method name restriction
>>
>> I found out CXF has method name restriction for @context injection through
>> setter method:
>> private void checkContextMethod(Method m, Object provider) {
>>          Class<?> type = m.getParameterTypes()[0];
>>          if (m.getName().equals("set" + type.getSimpleName())) {
>>              addContextMethod(type, m, provider);
>>          }
>>      }
>>
>> It is fine we enforces the method starts with 'set', but I do not think we should
>> enforce other naming check.  Instead, according to spec, we should add check
>> to enforce the Parameter type for the setter method  be one of
>> following:
>>      UriInfo
>>      HttpHeaders
>>      Request
>>      SecurityContext
>>
>> What do you think about this problem?
>>
>> Iris Ding
>>
>>
>>
>>
>>
>> --
>> View this message in context: http://cxf.547215.n5.nabble.com/context-
>> injection-through-setter-method-method-name-restriction-tp5746712.html
>> Sent from the cxf-dev mailing list archive at Nabble.com.



RE: @context injection through setter method - method name restriction

Posted by Andrei Shakirin <as...@talend.com>.
Hi Iris,

This restriction was already discussed in http://cxf.547215.n5.nabble.com/Getting-hold-of-JAX-RS-MessageContext-tt5745816.html .
Yes, it makes sense to relax / drop the restriction.

Could you please create an issue for this?

Regards,
Andrei.

> -----Original Message-----
> From: iris ding [mailto:irisdingbj@gmail.com]
> Sent: Dienstag, 22. Juli 2014 08:26
> To: dev@cxf.apache.org
> Subject: @context injection through setter method - method name restriction
> 
> I found out CXF has method name restriction for @context injection through
> setter method:
> private void checkContextMethod(Method m, Object provider) {
>         Class<?> type = m.getParameterTypes()[0];
>         if (m.getName().equals("set" + type.getSimpleName())) {
>             addContextMethod(type, m, provider);
>         }
>     }
> 
> It is fine we enforces the method starts with 'set', but I do not think we should
> enforce other naming check.  Instead, according to spec, we should add check
> to enforce the Parameter type for the setter method  be one of
> following:
>     UriInfo
>     HttpHeaders
>     Request
>     SecurityContext
> 
> What do you think about this problem?
> 
> Iris Ding
> 
> 
> 
> 
> 
> --
> View this message in context: http://cxf.547215.n5.nabble.com/context-
> injection-through-setter-method-method-name-restriction-tp5746712.html
> Sent from the cxf-dev mailing list archive at Nabble.com.