users@jersey.java.net

RE: [Jersey] Design Question / Sub Resources

From: Rabick, Mark A (IS) <"Rabick,>
Date: Thu, 21 May 2009 14:43:23 -0500

Paul/Anyone,

You mentioned earlier in this thread the use of @ResourceContext to get
managed sub-resources:

>> It is possible to inject information into sub-resources using
>> the ResourceContext class.
>>
>> @Path("{id}/branch")
>> public BranchResource getBranchResource(@Context ResourceContext
>> rc) {
>> ScopedBranchResource sbr =
>> rc.getReource(ScopedBranchResource.class);
>> ...
>> return sbr;
>> }

Is the above code correct? Shouldn't the line in the body be:

        ScopedBranchResource sbr = rc.getReource(BranchResource.class);

My other question is that the @Path has a param {id}. In my case, I'm
passing the bound id value to the constructor of my subresource. Is
this possible?

        @Path("{key}")
        public NodeResource getNodeResource(@PathParam("key") String
key) {

                return new NodeV1Resource(key, nodeRemote);
        }

The nodeRemote variable is a local variable in the parent resource class
that is a remote interface to an EJB that I'm presently passing to the
subresource because it is need to process operations on Node instances.

How would the above method be constructed using the @Context
ResourceContext rx?

--mark
_______________________________________________
Mark A. Rabick - Software Engineer
Em: mark.rabick_at_ngc.com

 

> -----Original Message-----
> From: Paul.Sandoz_at_Sun.COM [mailto:Paul.Sandoz_at_Sun.COM]
> Sent: Tuesday, May 19, 2009 9:57 AM
> To: users_at_jersey.dev.java.net
> Subject: Re: [Jersey] Design Question / Sub Resources
>
>
> On May 19, 2009, at 4:51 PM, Rabick, Mark A (IS) wrote:
>
> >> It is possible to inject information into sub-resources using the
> > ResourceContext class.
> >
> > So based on the above statement, will the retrieved sub-resource
> > instance perform injection of its @Context elements as well
> as other
> > @Provider injections like an InjectableProvider like annotation
> > mappers?
> >
>
> Yes, the returned instance is a managed instance, so it will
> also be scoped according to the life-cycle. It should work no
> matter how the component is managed, by Jersey, Guice,
> Spring, or EJB integration with GF v3.
>
> Paul.
>
> > Ie.
> >
> > @Provider
> > public class EJBProvider implements InjectableProvider<EJB, Type>
> >
> > For injecting ejb-s using the javax.ejb.EJB annotation of
> session bean
> > remotes?
> >
> > -mark
> >
> > _______________________________________________
> > Mark A. Rabick - Software Engineer
> > Em: mark.rabick_at_ngc.com
> >
> >
> >
> >> -----Original Message-----
> >> From: Paul.Sandoz_at_Sun.COM [mailto:Paul.Sandoz_at_Sun.COM]
> >> Sent: Tuesday, May 19, 2009 8:09 AM
> >> To: users_at_jersey.dev.java.net
> >> Subject: Re: [Jersey] Design Question / Sub Resources
> >>
> >>
> >> On May 19, 2009, at 12:31 PM, aloleary wrote:
> >>
> >>>
> >>> Hello,
> >>> I may be having a slow day today but I'm trying to figure
> >> out how to
> >>> do the following:
> >>>
> >>> Very simple example: I have two root "resource" providers:
> >>>
> >>> /headoffice
> >>>
> >>> /branch
> >>>
> >>> where:
> >>> GET on /headoffice returns a list of headoffice names GET on
> >>> /branchreturns a list of all branch names
> >>>
> >>> I would like to use the BranchResource as a Sub Resource
> >> provider on
> >>> HeadOfficeResource for re-use and to offer calls like:
> >>>
> >>> /headoffice/{id}/branch
> >>> /headoffice/{id}/branch/{id} etc...
> >>>
> >>> So now the branch resource is providing information on the
> >> branch but
> >>> scoped for a specific headoffice (the headoffice 'id' would
> >> be used in
> >>> DB queries
> >>> etc)
> >>>
> >>> However Im not sure how to annotate the
> HeadOfficeResource to allow
> >>> this
> >>>
> >>> @Path("/headoffice")
> >>> public cleass HeadOfficeResource {
> >>>
> >>> ....
> >>>
> >>> // ------------------------------------
> >>> // Sub Resources
> >>>
> >>> @Path("{id}/branch") << ??
> >>> public BranchResource getBranchResource(); << ??
> >>>
> >>> }
> >>>
> >>> I know there is no injection done for sub resources so
> >> there is no way
> >>> for me to get access to this particular {id} so is it a case of
> >>> supplying the
> >>> getBranchResource() with the id as a constructor param and
> >> using that
> >>> with the calls i.e.:
> >>>
> >>> public BranchResource getBranchResource(@PathParam("id")
> >> final String
> >>> headOfficeId) {
> >>> return new BranchResource(headOfficeId); }
> >>>
> >>> Is there a better more JAX-RS/Jersey way of approaching this ?
> >>>
> >>> I would like to avoid having GlobalBranchResource and
> >>> HeadOfficeSpecifcBranchResource ;-) if possible
> >>>
> >>
> >> I think subclassing might be a good solution in some
> >> circumstances if the subclass contains specific information
> >> related to the scoping that is not present in the base class.
> >>
> >>
> >> It is possible to inject information into sub-resources using
> >> the ResourceContext class.
> >>
> >> @Path("{id}/branch")
> >> public BranchResource getBranchResource(@Context ResourceContext
> >> rc) {
> >> ScopedBranchResource sbr =
> >> rc.getReource(ScopedBranchResource.class);
> >> ...
> >> return sbr;
> >> }
> >>
> >>
> >> As of yet i have not been able to come up with a satisfactory
> >> solution for construction with type safe parameters and injection.
> >>
> >> Paul.
> >>
> >>
> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >> For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >>
> >>
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> > For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>