You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@juneau.apache.org by Girish Vasmatkar <gi...@hotwaxsystems.com> on 2020/06/01 19:36:39 UTC

Adding Resources Programmatically (No Annotations)

Hi All,

I am evaluating Juneau as one of the possible REST implementations. While I
am able to define resources using @Rest annotation, I am looking for
something that enables me to add new
resources dynamically/programmatically.

For example, the @Rest annotation defines children property that I will not
be knowing until runtime. I am trying to have an XML configuration file
containing all resources and methods details and I would like to consume
that XML at boot up time and have Juneau configure these resources
programmatically.

I looked at RestContextBuilder but could not find how I can use it to
register resources and methods dynamically.

Best Regards,
Girish

Re: Adding Resources Programmatically (No Annotations)

Posted by James Bognar <ja...@gmail.com>.
HI Girish,

Sorry for the late reply.  I'm not entirely sure I fully understand
the scenario but this info might help.

The top-level Juneau REST resource class (the one that you attach
children to) is fundamentally nothing more than a sophisticated
servlet that uses reflection to find and initialize child resources
during servlet initialization.  There isn't any sort of global
registry anywhere.  Two different @Rest-annotated resources deployed
as servlet are entirely independent of each other.  But the advantage
is that since these are nothing more than servlets, you can use
whatever mechanisms are available for deploying them (e.g. @Servlet
annotations for example).

As far as dynamically adding child resources at runtime, I'm not
seeing a way to do this today.  The child resources are defined in an
unmodifiable map in the RestContext class.  The BasicRestCallHandler
class iterates over that map to find a matching child resource.  One
problem is that child resource paths aren't necessarily simple
strings, but may themselves contain resolvable variables (e.g.
"/foo/{foo}").

A possible feature enhancement would be to allow the ability to create
your own subclass of RestContext (i.e.
builder.contextClass(MyRestContext.class)) which would allow you to
override any existing behavior.  The RestContext class could then have
a resolveChild(String path) method that could be overridden to provide
the support you need.  If you're interested in that, please open a
JIRA ticket.  I'm not sure how quickly I would be able to get to it
since I'm currently consumed working in the client API.




On Fri, Jun 5, 2020 at 9:20 AM Girish Vasmatkar
<gi...@hotwaxsystems.com> wrote:
>
> Hi James -
>
> Thanks for the reply. Yes, I did look at the child and children methods. What I am trying to figure out is if there is a generic way of defining multiple children virtually (with different resource paths) and yet all the requests be handled by a single class. Our project as a well defined service oriented architecture defined.
>
> POST /api/supplier/
> GET /api/supplier/{id}
> GET /api/supplier/{id}/contacts/{contactId}
>
> I have above scenario in my mind where I already have various services written (or can be written) to respond to the above three example resource paths. I need to be able to dynamically add such resource paths (probably picking from an XML API file) and then have a single generic child (one new instance per request) that can cater to all such requests and decide appropriately at the runtime (based on instance variable) which service to call and return the response.
>
> I am sorry if I am too verbose but this is the architecture I have in my mind for our REST implementation and we want to use Juneau. Is there any example of such programmatic resource creation?
>
> Best Regards,
> Girish
>
>
>
>
>
>
>
>
> On Tue, Jun 2, 2020 at 5:58 PM James Bognar <ja...@gmail.com> wrote:
>>
>> Hi Girish,
>>
>> The RestContextBuilder class can be accessed via a lifecycle hook:
>> http://juneau.apache.org/site/apidocs-8.1.3/overview-summary.html#juneau-rest-server.Instantiation.LifecycleHooks
>>
>> From there you can add it via either the child() or children() methods:
>> http://juneau.apache.org/site/apidocs-8.1.3/org/apache/juneau/rest/RestContextBuilder.html#child-java.lang.String-java.lang.Object-
>>
>> On Mon, Jun 1, 2020 at 3:37 PM Girish Vasmatkar
>> <gi...@hotwaxsystems.com> wrote:
>> >
>> > Hi All,
>> >
>> > I am evaluating Juneau as one of the possible REST implementations. While I am able to define resources using @Rest annotation, I am looking for something that enables me to add new resources dynamically/programmatically.
>> >
>> > For example, the @Rest annotation defines children property that I will not be knowing until runtime. I am trying to have an XML configuration file containing all resources and methods details and I would like to consume that XML at boot up time and have Juneau configure these resources programmatically.
>> >
>> > I looked at RestContextBuilder but could not find how I can use it to register resources and methods dynamically.
>> >
>> > Best Regards,
>> > Girish

Re: Adding Resources Programmatically (No Annotations)

Posted by Girish Vasmatkar <gi...@hotwaxsystems.com>.
Hi James -

Thanks for the reply. Yes, I did look at the child and children methods.
What I am trying to figure out is if there is a generic way of defining
multiple children virtually (with different resource paths) and yet all the
requests be handled by a single class. Our project as a well defined
service oriented architecture defined.

POST /api/supplier/
GET /api/supplier/{id}
GET /api/supplier/{id}/contacts/{contactId}

I have above scenario in my mind where I already have various services
written (or can be written) to respond to the above three example resource
paths. I need to be able to dynamically add such resource paths (probably
picking from an XML API file) and then have a single generic child (one new
instance per request) that can cater to all such requests and decide
appropriately at the runtime (based on instance variable) which service to
call and return the response.

I am sorry if I am too verbose but this is the architecture I have in my
mind for our REST implementation and we want to use Juneau. Is there any
example of such programmatic resource creation?

Best Regards,
Girish








On Tue, Jun 2, 2020 at 5:58 PM James Bognar <ja...@gmail.com> wrote:

> Hi Girish,
>
> The RestContextBuilder class can be accessed via a lifecycle hook:
>
> http://juneau.apache.org/site/apidocs-8.1.3/overview-summary.html#juneau-rest-server.Instantiation.LifecycleHooks
>
> From there you can add it via either the child() or children() methods:
>
> http://juneau.apache.org/site/apidocs-8.1.3/org/apache/juneau/rest/RestContextBuilder.html#child-java.lang.String-java.lang.Object-
>
> On Mon, Jun 1, 2020 at 3:37 PM Girish Vasmatkar
> <gi...@hotwaxsystems.com> wrote:
> >
> > Hi All,
> >
> > I am evaluating Juneau as one of the possible REST implementations.
> While I am able to define resources using @Rest annotation, I am looking
> for something that enables me to add new resources
> dynamically/programmatically.
> >
> > For example, the @Rest annotation defines children property that I will
> not be knowing until runtime. I am trying to have an XML configuration file
> containing all resources and methods details and I would like to consume
> that XML at boot up time and have Juneau configure these resources
> programmatically.
> >
> > I looked at RestContextBuilder but could not find how I can use it to
> register resources and methods dynamically.
> >
> > Best Regards,
> > Girish
>

Re: Adding Resources Programmatically (No Annotations)

Posted by James Bognar <ja...@gmail.com>.
Hi Girish,

The RestContextBuilder class can be accessed via a lifecycle hook:
http://juneau.apache.org/site/apidocs-8.1.3/overview-summary.html#juneau-rest-server.Instantiation.LifecycleHooks

From there you can add it via either the child() or children() methods:
http://juneau.apache.org/site/apidocs-8.1.3/org/apache/juneau/rest/RestContextBuilder.html#child-java.lang.String-java.lang.Object-

On Mon, Jun 1, 2020 at 3:37 PM Girish Vasmatkar
<gi...@hotwaxsystems.com> wrote:
>
> Hi All,
>
> I am evaluating Juneau as one of the possible REST implementations. While I am able to define resources using @Rest annotation, I am looking for something that enables me to add new resources dynamically/programmatically.
>
> For example, the @Rest annotation defines children property that I will not be knowing until runtime. I am trying to have an XML configuration file containing all resources and methods details and I would like to consume that XML at boot up time and have Juneau configure these resources programmatically.
>
> I looked at RestContextBuilder but could not find how I can use it to register resources and methods dynamically.
>
> Best Regards,
> Girish