You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cxf.apache.org by "Hendy Irawan (JIRA)" <ji...@apache.org> on 2009/07/09 09:34:14 UTC

[jira] Created: (CXF-2335) @Context does not work for JAX-RS member fields

@Context does not work for JAX-RS member fields
-----------------------------------------------

                 Key: CXF-2335
                 URL: https://issues.apache.org/jira/browse/CXF-2335
             Project: CXF
          Issue Type: Bug
          Components: REST
    Affects Versions: 2.2.2
            Reporter: Hendy Irawan


This works in Jersey, but not in CXF:

class ........ {

@Context
private UriInfo context;

}

The alternative, works on both CXF and Jersey:

class ...... {

@GET
public void something(@Context UriInfo context) {
  ...
}

}

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CXF-2335) Support @Context injection for JAX-RS Subresources

Posted by "Sergey Beryozkin (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CXF-2335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12729173#action_12729173 ] 

Sergey Beryozkin commented on CXF-2335:
---------------------------------------

That said, in case of subresources, the runtime can actually synchronize on the returned object and inject a thread-local context proxy which will work for both (1) & (2)... 

> Support @Context injection for JAX-RS Subresources
> --------------------------------------------------
>
>                 Key: CXF-2335
>                 URL: https://issues.apache.org/jira/browse/CXF-2335
>             Project: CXF
>          Issue Type: Improvement
>          Components: REST
>    Affects Versions: 2.2.2
>            Reporter: Hendy Irawan
>
> This works in Jersey, but not in CXF:
> class ........ {
> @Context
> private UriInfo context;
> }
> The alternative, works on both CXF and Jersey:
> class ...... {
> @GET
> public void something(@Context UriInfo context) {
>   ...
> }
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CXF-2335) @Context does not work for JAX-RS member fields

Posted by "Hendy Irawan (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CXF-2335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12729129#action_12729129 ] 

Hendy Irawan commented on CXF-2335:
-----------------------------------

My project is using Spring beans. But the Spring bean doesn't seem to require setters for @Context injection.

I've provided a test case here:

http://scm.ops4j.org/repos/ops4j/laboratory/users/ceefour/cxf-context-bug

Then test the following URLs:

http://localhost:8080/cxf-context-bug/test1
http://localhost:8080/cxf-context-bug/test2
http://localhost:8080/cxf-context-bug/group/test1
http://localhost:8080/cxf-context-bug/group/test2

Of all, the "http://localhost:8080/cxf-context-bug/group/test1" fails. Which is supposed to be injecting a subresource.

Let me know if I'm doing anything wrong.

On a different matter, I think that sample project is good for a CXF JAX-RS quickstart archetype :-)

> @Context does not work for JAX-RS member fields
> -----------------------------------------------
>
>                 Key: CXF-2335
>                 URL: https://issues.apache.org/jira/browse/CXF-2335
>             Project: CXF
>          Issue Type: Bug
>          Components: REST
>    Affects Versions: 2.2.2
>            Reporter: Hendy Irawan
>
> This works in Jersey, but not in CXF:
> class ........ {
> @Context
> private UriInfo context;
> }
> The alternative, works on both CXF and Jersey:
> class ...... {
> @GET
> public void something(@Context UriInfo context) {
>   ...
> }
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CXF-2335) @Context does not work for JAX-RS member fields

Posted by "Sergey Beryozkin (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CXF-2335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12729161#action_12729161 ] 

Sergey Beryozkin commented on CXF-2335:
---------------------------------------

Thanks for posting the actual test details.
JAX-RS spec does not actually provide for the contexts injection into subresource instances.

Consider this example :

(1)
public class GroupsResource {

    @Path("group")
    public GroupResource group() {
        return new GroupResource();
    }

}

and 

(2)
public class GroupsResource {

    private GroupResource group = new GroupResource();

    @Path("group")
    public GroupResource group() {
        return group;
    }

}

injecting in this last example (2) would not be thread safe.

Would you agree ?

I reckon it is easy enough for the GroupsResource class to manage the injection into individual subresource GroupResource instances which is what the spec recommends. One would just need to add a setUriInfo with no annotations on GroupResource and 

in (1) do 

GroupResource gr = new GroupResource()
gr.setUriInfo(uriInfo)

in (2)

you can add a setter as I suggested in the previous email and also inject the provided value into the shared GroupResource at the moment the method is called.

I can only see one possible option in resolving this JIRA : if a user configures jaxrs:server to allow for the subresource injection then the runtime will inject into subresources using a field injection if needed but the runtime but it will really work reliably for dynamically created subresource instances, otherwise, as in (2), it will be thread-unsafe




> @Context does not work for JAX-RS member fields
> -----------------------------------------------
>
>                 Key: CXF-2335
>                 URL: https://issues.apache.org/jira/browse/CXF-2335
>             Project: CXF
>          Issue Type: Bug
>          Components: REST
>    Affects Versions: 2.2.2
>            Reporter: Hendy Irawan
>
> This works in Jersey, but not in CXF:
> class ........ {
> @Context
> private UriInfo context;
> }
> The alternative, works on both CXF and Jersey:
> class ...... {
> @GET
> public void something(@Context UriInfo context) {
>   ...
> }
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CXF-2335) Support @Context injection for JAX-RS Subresources

Posted by "Hendy Irawan (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CXF-2335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12729182#action_12729182 ] 

Hendy Irawan commented on CXF-2335:
-----------------------------------

I agree with you.

In practice, I've resolved the problem using the solutions you provided.

I'm still curious though, because I'm not really sure how dependency injection is supposed to work in "ideal" work.

In a Spring+CXF DI-"controlled" app, beans can be created manually (as in "return new SubResource()") or by Spring. Furthermore, the beans can be configured by Spring, by CXF, manually, or by any combination of them.

However, I'm not aware of how to turn a bean that is manually created, into configured by CXF. I'm thinking of something like:

public SubResource getSub(@Context CXFContext ctx) {
  SubResource subRes = new SubResource();
  ctx.configureBean(subRes); // this will inject any necessary annotated fields etc.
  return subRes;
}

That way will allow SubResource interface/implementation to change independently, and the root resource not need to care about injecting CXF/JAX-RS related dependencies. However I'm not sure if that's the correct approach as well.

I think a best practice for this should be provided in the CXF user guide especially under JAX-RS page.

> Support @Context injection for JAX-RS Subresources
> --------------------------------------------------
>
>                 Key: CXF-2335
>                 URL: https://issues.apache.org/jira/browse/CXF-2335
>             Project: CXF
>          Issue Type: Improvement
>          Components: REST
>    Affects Versions: 2.2.2
>            Reporter: Hendy Irawan
>
> This works in Jersey, but not in CXF:
> class ........ {
> @Context
> private UriInfo context;
> }
> The alternative, works on both CXF and Jersey:
> class ...... {
> @GET
> public void something(@Context UriInfo context) {
>   ...
> }
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CXF-2335) @Context does not work for JAX-RS member fields

Posted by "Sergey Beryozkin (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CXF-2335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12729103#action_12729103 ] 

Sergey Beryozkin commented on CXF-2335:
---------------------------------------

We have quite a few tests which show the @Context fields get injected.

Is your class Spring-proxified by any chance ? If yes then

class Resource implements SomeInterfaceWithSetUriInfoMethod {

private UriInfo context;


@Context
public void setUriInfo(UriInfo ui) {
    context = ui;
}

} 

will work.

Can you provide more info please ? 


> @Context does not work for JAX-RS member fields
> -----------------------------------------------
>
>                 Key: CXF-2335
>                 URL: https://issues.apache.org/jira/browse/CXF-2335
>             Project: CXF
>          Issue Type: Bug
>          Components: REST
>    Affects Versions: 2.2.2
>            Reporter: Hendy Irawan
>
> This works in Jersey, but not in CXF:
> class ........ {
> @Context
> private UriInfo context;
> }
> The alternative, works on both CXF and Jersey:
> class ...... {
> @GET
> public void something(@Context UriInfo context) {
>   ...
> }
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CXF-2335) Support @Context injection for JAX-RS Subresources

Posted by "Sergey Beryozkin (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CXF-2335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12729198#action_12729198 ] 

Sergey Beryozkin commented on CXF-2335:
---------------------------------------

This will always work, irrespectively of the combination of various underlying technologies and it will be a thread-safe solution :

public class GroupsResource {

    private UriInfo uriInfo;
    private GroupResource gr1 = new GroupResource();

    @Context
    public void setUriInfo(UriInfo ui) {
        uriInfo = ui;
        gr1.setUriInfo(uriInfo); 
    }

    @Path("group1")
    public GroupResource group1() {
        return gr1;
    }

    @Path("group2")
    public GroupResource group2() {
        GroupResource gr2 = new GroupResource();
        gr2.setUriInfo(uriInfo); 
        return gr2;
    }

}

It will also work well in cases when users try to do proxy-based client invocations, as client proxies do not support @Context parameters on resource methods.

In meantime I will see what I can do with respect to the context injection into subresources... Perhaps in some cases modifying the subresource classes with extra methods might not be an option.


> Support @Context injection for JAX-RS Subresources
> --------------------------------------------------
>
>                 Key: CXF-2335
>                 URL: https://issues.apache.org/jira/browse/CXF-2335
>             Project: CXF
>          Issue Type: Improvement
>          Components: REST
>    Affects Versions: 2.2.2
>            Reporter: Hendy Irawan
>
> This works in Jersey, but not in CXF:
> class ........ {
> @Context
> private UriInfo context;
> }
> The alternative, works on both CXF and Jersey:
> class ...... {
> @GET
> public void something(@Context UriInfo context) {
>   ...
> }
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CXF-2335) Support @Context injection for JAX-RS Subresources

Posted by "Hendy Irawan (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CXF-2335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12729174#action_12729174 ] 

Hendy Irawan commented on CXF-2335:
-----------------------------------

So the problem becomes:

class RootResource {

@Path("sub")
public SubResource getSub() {
  return new SubResource();
}

}

and in SubResource:

class SubResource {

@Context
private UriInfo context;

}

where CXF doesn't inject the fields of SubResource instance returned by RootResource.getSub(). However, individual methods within that SubResource instance is injected properly.

> Support @Context injection for JAX-RS Subresources
> --------------------------------------------------
>
>                 Key: CXF-2335
>                 URL: https://issues.apache.org/jira/browse/CXF-2335
>             Project: CXF
>          Issue Type: Improvement
>          Components: REST
>    Affects Versions: 2.2.2
>            Reporter: Hendy Irawan
>
> This works in Jersey, but not in CXF:
> class ........ {
> @Context
> private UriInfo context;
> }
> The alternative, works on both CXF and Jersey:
> class ...... {
> @GET
> public void something(@Context UriInfo context) {
>   ...
> }
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (CXF-2335) Support @Context injection for JAX-RS Subresources

Posted by "Sergey Beryozkin (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CXF-2335?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Sergey Beryozkin updated CXF-2335:
----------------------------------

    Issue Type: Improvement  (was: Bug)
       Summary: Support @Context injection for JAX-RS Subresources  (was: @Context does not work for JAX-RS member fields)

I've updated the JIRA title to make it a bit more obvious that is is an enhancement request as opposed to a bug :-)



> Support @Context injection for JAX-RS Subresources
> --------------------------------------------------
>
>                 Key: CXF-2335
>                 URL: https://issues.apache.org/jira/browse/CXF-2335
>             Project: CXF
>          Issue Type: Improvement
>          Components: REST
>    Affects Versions: 2.2.2
>            Reporter: Hendy Irawan
>
> This works in Jersey, but not in CXF:
> class ........ {
> @Context
> private UriInfo context;
> }
> The alternative, works on both CXF and Jersey:
> class ...... {
> @GET
> public void something(@Context UriInfo context) {
>   ...
> }
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.