You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by Steinar Bang <sb...@dod.no> on 2018/07/13 16:02:59 UTC

Re: Examples of using Jersey with pax web whiteboard extender?

>>>>> - <th...@yahoo.com>:

> This is what I used as my guide:
> https://github.com/jersey/jersey/tree/master/examples/osgi-http-service

Thanks!

What I've done so far, in my vaadin-to-react rewrite,
 https://github.com/steinarb/ukelonn/tree/using-react
is to just create a servlet for each service.
 https://github.com/steinarb/ukelonn/tree/5ca75f438407c594ed476bed790d984b0007cfdf/ukelonn.bundle/src/main/java/no/priv/bang/ukelonn/api

The servlets either handle POST or GET or both, and what's POSTed is a
JSON object that is turned into a Java bean by jackson, and what's
returned is a bean that is turned into a JSON object by Jackson.  The
beans are defined here:
 https://github.com/steinarb/ukelonn/tree/5ca75f438407c594ed476bed790d984b0007cfdf/ukelonn.bundle/src/main/java/no/priv/bang/ukelonn/api/beans
and here
 https://github.com/steinarb/ukelonn/tree/5ca75f438407c594ed476bed790d984b0007cfdf/ukelonn.api/src/main/java/no/priv/bang/ukelonn/beans

This has worked well enough and have the advantage of the servlets being
components that can have the class giving access to the database
injected.

The service definition is
 https://github.com/steinarb/ukelonn/blob/5ca75f438407c594ed476bed790d984b0007cfdf/ukelonn.api/src/main/java/no/priv/bang/ukelonn/UkelonnService.java#L36

The service implementation, is this DS component
 https://github.com/steinarb/ukelonn/blob/5ca75f438407c594ed476bed790d984b0007cfdf/ukelonn.bundle/src/main/java/no/priv/bang/ukelonn/impl/UkelonnServiceProvider.java#L42

However, with Jersey getting injection of the above service into the
resource classes isn't so easy.

The obvious first step is to make a DS component from ServletContainer
 http://javadox.com/org.glassfish.jersey.containers/jersey-container-servlet-core/2.7/org/glassfish/jersey/servlet/ServletContainer.html

But the ServletContainer wants to find and instanciate the resource
classes, so they can't be DS components in their own right.  And there
are no backpointers from a resource to the class that instanciated it.

So that would mean I would have to resort to static access methods for
singletons, which is what I did in the JSF/Primefaces version, and in
the first versions of the vaadin version.

And going back to using singletons feels very much like a step
backwards...:-)

If someone are looking for the react/webpack stuff, it is here:
 https://github.com/steinarb/ukelonn/tree/5ca75f438407c594ed476bed790d984b0007cfdf/ukelonn.bundle/src/main/frontend

To handle the login I use apache shiro, which is enabled by creating the
servlet context "/ukelonn" with this DS component
 https://github.com/steinarb/ukelonn/blob/5ca75f438407c594ed476bed790d984b0007cfdf/ukelonn.bundle/src/main/java/no/priv/bang/ukelonn/impl/UkelonnServletContextHelper.java
and then putting this filter on the "/ukelonn" servlet context
 https://github.com/steinarb/ukelonn/blob/5ca75f438407c594ed476bed790d984b0007cfdf/ukelonn.bundle/src/main/java/no/priv/bang/ukelonn/impl/UkelonnShiroFilter.java#L42
and putting all of the servlets into this context and making their paths
relative to the context.

The UkelonnShiroFilter handles all of the cookie magic during login and
logout, and blocks access to URLs according to login state and role(s)

The shiro.ini file that defines the various accesses to the URLs
(including the API URL), is here:
 https://github.com/steinarb/ukelonn/blob/5ca75f438407c594ed476bed790d984b0007cfdf/ukelonn.bundle/src/main/resources/shiro.ini


Re: Examples of using Jersey with pax web whiteboard extender?

Posted by Scott Lewis <sl...@composent.com>.
On 7/14/2018 3:14 AM, Steinar Bang wrote:
> <stuff deleted>
> [snip!]
>> it doesn't currently use the http whiteboard, but it could...and may
>> eventually.
> Ah, ok.  I need something that uses the http whiteboard.

I would welcome a contribution to use the http whiteboard for this 
project [1].   It would/will be a simple matter of changing the 
jersey.server and cxf.server code that creates a servlet and registers 
it via HttpService to using the whiteboard impl to do the same thing.

>
> But just annotating the service interface to get REST services would
> have been really neat. :-)

Yes.   One result is that for free you get a versionable, type-safe, 
proxy for your jaxrs service.  Also remote service dynamics and seemless 
use of DS and/or other injection based upon the service registry.  Also 
a standardized mgmt agent (chap 122 in cmpn spec) and service discovery.

Scott

[1] https://github.com/ECF/JaxRSProviders
[2] 
https://osgi.org/specification/osgi.cmpn/7.0.0/service.remoteserviceadmin.html

>


Re: Examples of using Jersey with pax web whiteboard extender?

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
Hi Steinar,

I recommend to use the annotation from  osgi.cmpn.

javax.ws.rs can be provided by the spec or a impl (like CXF).

Regards
JB

On 14/07/2018 12:14, Steinar Bang wrote:
>>>>>> Scott Lewis <sl...@composent.com>:
> 
> [snip!]
>> Yes.   I haven't looked at your annotations, and so don't know if they
>> all are from the javax.ws.rs packages...but assuming so...that's the
>> idea.
> 
> The DS annotations are from
>  mvn:org.osgi/org.osgi.service.component.annotations/6.0.0
> 
> The whiteboard constants used in the annotation arguments, are from
>  mvn:org.ops4j.pax.web/pax-web-extender/6.0.3
> 
> I haven introduced any javax.ws.rs stuff yet.
> 
> [snip!]
>> it doesn't currently use the http whiteboard, but it could...and may
>> eventually.
> 
> Ah, ok.  I need something that uses the http whiteboard.
> 
> But just annotating the service interface to get REST services would
> have been really neat. :-)
> 

-- 
Jean-Baptiste Onofré
jbonofre@apache.org
http://blog.nanthrax.net
Talend - http://www.talend.com

Re: Examples of using Jersey with pax web whiteboard extender?

Posted by Steinar Bang <sb...@dod.no>.
>>>>> Scott Lewis <sl...@composent.com>:

[snip!]
> Yes.   I haven't looked at your annotations, and so don't know if they
> all are from the javax.ws.rs packages...but assuming so...that's the
> idea.

The DS annotations are from
 mvn:org.osgi/org.osgi.service.component.annotations/6.0.0

The whiteboard constants used in the annotation arguments, are from
 mvn:org.ops4j.pax.web/pax-web-extender/6.0.3

I haven introduced any javax.ws.rs stuff yet.

[snip!]
> it doesn't currently use the http whiteboard, but it could...and may
> eventually.

Ah, ok.  I need something that uses the http whiteboard.

But just annotating the service interface to get REST services would
have been really neat. :-)


Re: Examples of using Jersey with pax web whiteboard extender?

Posted by Scott Lewis <sl...@composent.com>.
On 7/13/2018 3:40 PM, Steinar Bang wrote:
>
> That's interesting! Just annotating my service interface, and its
> implementation with jaxrs annotations, like the StudentService and
> StudentServiceImpl here...?
>   https://wiki.eclipse.org/Tutorial:_Exposing_a_Jax_REST_service_as_an_OSGi_Remote_Service

Yes.   I haven't looked at your annotations, and so don't know if they 
all are from the javax.ws.rs packages...but assuming so...that's the idea.

>
> (the downside is that I would have to move to karaf 4.2, and I ran into
> some issues doing when I inadvertently tried 4.2.0.  JB had some ideas
> about what was the cause of my problem, and I will look into them)

It's quite possible that things would run on Karaf 4.0 or 4.0 (with 
java8), but I haven't tested that so don't want to make any claims. Also 
I found a small issue with gogo on 4.2 that's been fixed in 4.2.1 
stream...but that only affects the added RSA console commands.

>
> How would the service provided by the StudentServiceImpl component be
> exposed as a REST service on HTTP in a servlet?

Yes.   For both it registers a servlet with HttpService (jetty). For CXF 
it's a subclass of

org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet

and for jersey it's a

org.glassfish.jersey.servlet.ServletContainer.ServletContainer

it doesn't currently use the http whiteboard, but it could...and may 
eventually.

>
>> Also:   I've very recently added bndtools 4 project and bndrun
>> templates for using either jersey or cxf to export the same jaxrs
>> service [2].
> That's probably the answer to my previous question? :-)
>
>> I haven't had a chance to create tutorial about these templates yet,
>> but if you are using bndtools 4 it might be helpful.
> I'm using 3.5.1 of maven-bundle-plugin, so probably not bndtools 4,
> unfortunately.

Ok.

Scott


Re: Examples of using Jersey with pax web whiteboard extender?

Posted by Steinar Bang <sb...@dod.no>.
>>>>> Scott Lewis <sl...@composent.com>:

> FYI:   I've recently created a tutorial describing how to export from
> Karaf a jaxrs-annotated remote service with either cxf or jersey
> [1].    It does require an interface with jaxrs annotations that are
> the same as the resource annotations, but you (Steinar) already have
> the UkelonService so I don't think that will be a burden.

That's interesting! Just annotating my service interface, and its
implementation with jaxrs annotations, like the StudentService and
StudentServiceImpl here...?
 https://wiki.eclipse.org/Tutorial:_Exposing_a_Jax_REST_service_as_an_OSGi_Remote_Service

(the downside is that I would have to move to karaf 4.2, and I ran into
some issues doing when I inadvertently tried 4.2.0.  JB had some ideas
about what was the cause of my problem, and I will look into them)

How would the service provided by the StudentServiceImpl component be
exposed as a REST service on HTTP in a servlet?

> Also:   I've very recently added bndtools 4 project and bndrun
> templates for using either jersey or cxf to export the same jaxrs
> service [2].  

That's probably the answer to my previous question? :-)

> I haven't had a chance to create tutorial about these templates yet,
> but if you are using bndtools 4 it might be helpful.

I'm using 3.5.1 of maven-bundle-plugin, so probably not bndtools 4,
unfortunately.

Thanks!


Re: Examples of using Jersey with pax web whiteboard extender?

Posted by Scott Lewis <sl...@composent.com>.
FYI:   I've recently created a tutorial describing how to export from 
Karaf a jaxrs-annotated remote service with either cxf or jersey [1].    
It does require an interface with jaxrs annotations that are the same as 
the resource annotations, but you (Steinar) already have the 
UkelonService so I don't think that will be a burden.

Also:   I've very recently added bndtools 4 project and bndrun templates 
for using either jersey or cxf to export the same jaxrs service [2].   I 
haven't had a chance to create tutorial about these templates yet, but 
if you are using bndtools 4 it might be helpful.

[1] https://wiki.eclipse.org/Tutorial:_JaxRS_Remote_Services_on_Karaf

[2] https://github.com/ECF/JaxRSProviders/issues/6


Re: Examples of using Jersey with pax web whiteboard extender?

Posted by Steinar Bang <sb...@dod.no>.
>>>>> Steinar Bang <sb...@dod.no>:

>> The service definition is
>> https://github.com/steinarb/ukelonn/blob/5ca75f438407c594ed476bed790d984b0007cfdf/ukelonn.api/src/main/java/no/priv/bang/ukelonn/UkelonnService.java#L36

>> The service implementation, is this DS component
>> https://github.com/steinarb/ukelonn/blob/5ca75f438407c594ed476bed790d984b0007cfdf/ukelonn.bundle/src/main/java/no/priv/bang/ukelonn/impl/UkelonnServiceProvider.java#L42

>> However, with Jersey getting injection of the above service into the
>> resource classes isn't so easy.

> Based in the following two stackoverflow threads I have a tentative plan
> for getting the service injected into Jersey resources using my current
> stack (ie. karaf 4.1.5 and OSGi 6.0.0):
>  https://stackoverflow.com/questions/38373867/inject-dao-instance-into-jersey-resource?noredirect=1&lq=1
>  https://stackoverflow.com/questions/16216759/dependency-injection-with-jersey-2-0
[snip!]

> Outline of my plan:
>  1. Make the service implementation extend ResourcesConfig

Turns out this wasn't necessary.  What's @Inject'ed into Jersey
resources by HK2 JSR330 injections, can be any class.

>  2. Create a class extending AbstractBinder, that in its configure()
>     method will bind an instance of the service implmentation to the
>     UkelonnService interface

I ended up just creating an anonymous inner and registering it
immediately.

>  3. In the @Activate method of the service implementation, register the
>     activated instance with Jersey, using the AbstractBinder above

This was wrong.  The instances that needs to be injected, must bek
registered with the ResourceConfig used by the ServletContainer.  They
cannot be registered with any old ResourceConfig.

>  4. In the resource classes use JSR330 @Inject of the UkelonnService

This was as predicted.  Needed to 

>  5. Create a whiteboard DS service from the JerseyServletContainer, with:
>     a. param configuration configuring a package to scan for resources
>     b. add a @Reference for an UkelonnService and an @Activate method, to
>        ensure that the servlet isn't activated, and plugged into the
>        whiteboard until a service is available that can be injected into
>        the created resources (and it should also ensure the servlet is
>        taken down if the UkelonnService implementation is taken down)

Actually, the ServletContainer DS component needs to have the
UkelonnService (and an OSGi LogService) injected as OSGi services,
because it needs to register them as instances in its ResourceConfig.

The trick to get everything working, was to first have the
ServletContainer create a ResourceConfig object containing all of the
servlet setup, then copy that ResourceConfig and register OSGi services
for Jersey injection and reload the ServletContainer with the copy.

The procedure to get Jersey working with OSGi services UkelonnService
and LogService injected into the Jersey resource objects, is then as
follows: 
 1. Create a whiteboard DS component exposing a Servlet service from
    Jersey ServletContainer, configuring the package to scan for API
    resources:
     https://github.com/steinarb/ukelonn/blob/59c6c693027e6d400ccd9e6b1777ce7f5adb4096/ukelonn.bundle/src/main/java/no/priv/bang/ukelonn/api/UkelonnRestApiServlet.java#L37
 2. Make the DS component of ServletContainer depend on OSGi services
    UkelonnService and LogService
     https://github.com/steinarb/ukelonn/blob/59c6c693027e6d400ccd9e6b1777ce7f5adb4096/ukelonn.bundle/src/main/java/no/priv/bang/ukelonn/api/UkelonnRestApiServlet.java#L74
 3. Override the ServletContainer.init(WebConfig) method, and:
    a. Call super.init(WebConfig) to make sure that a ServletConfig
       containing the information set up by the http whiteboard is
       created (contains the servletcontext, the servlet name and the
       package to scan for Jersey resources)
        https://github.com/steinarb/ukelonn/blob/59c6c693027e6d400ccd9e6b1777ce7f5adb4096/ukelonn.bundle/src/main/java/no/priv/bang/ukelonn/api/UkelonnRestApiServlet.java#L57
    b. Copy the ResourceConfig of the ServletContainer (because that
       ServletConfig is immutable after the setup, and calling
       ServletConfig.register() will cause an IllegalOperationException)
    c. On the ServletConfig copy, register an anonymous inner inheriting
       AbstractBinder that in its configure() method registers the
       OSGi services injected into the ServletContainer as JSR330
       injections in the Jersey resources
    d. Call ServletContainer.reload(ResourceConfig) with the
       ResourceConfig copy as the argument
  4. In the resources use JSR330 injections of UkelonnService and/or
     LogService, e.g.
      https://github.com/steinarb/ukelonn/blob/59c6c693027e6d400ccd9e6b1777ce7f5adb4096/ukelonn.bundle/src/main/java/no/priv/bang/ukelonn/api/resources/RegisterJob.java#L32


Re: Examples of using Jersey with pax web whiteboard extender?

Posted by Tim Ward <ti...@paremus.com>.
Hi Steinar,

If you’re looking to use JAX-RS then the standard approach would be to use the JAX-RS Whiteboard specification (https://osgi.org/specification/osgi.cmpn/7.0.0/service.jaxrs.html <https://osgi.org/specification/osgi.cmpn/7.0.0/service.jaxrs.html>). There are examples of how you can do this in OSGi enRoute (https://enroute.osgi.org <https://enroute.osgi.org/>). The reference implementation from Apache Aries is easily available in Maven Central (https://mvnrepository.com/artifact/org.apache.aries.jax.rs/org.apache.aries.jax.rs.whiteboard/1.0.0 <https://mvnrepository.com/artifact/org.apache.aries.jax.rs/org.apache.aries.jax.rs.whiteboard/1.0.0>)

The good news is that the JAX-RS whiteboard doesn’t need you to annotate your interfaces with JAX-RS annotations, or even use interfaces at all if you don’t want to. The whiteboard will automatically process any services that set the relevant properties from the specification. If you are using OSGi R7 then you can do this with Component Property annotations, but R7 isn’t required (you can just put the properties on using the component annotation). The Aries implementation also runs just fine on an R6 framework with R6 compendium services. It even uses the Http Service whiteboard 1.0 specification, which is what you want!

The really good news is that because your JAX-RS resources are provided as services you can use DS to inject your service dependencies. This prevents you from having to mess around trying to add service injection to the JAX-RS runtime.

In summary, you should be able to add Aries JAX-RS whiteboard 1.0.0, Aries JAX-RS API 1.0.0, and ServiceMix Specs  Annotation API 1.3 and start using it.

Best Regards,

Tim

> On 14 Jul 2018, at 23:18, Steinar Bang <sb...@dod.no> wrote:
> 
>>>>>> Scott Lewis <sl...@composent.com>:
> 
> 
>> If you would like to expidite, please open an issue here [1] for the
>> use of http-whiteboard in jersey and cxf distribution providers, and
>> it could happen pretty quickly.
> 
> https://github.com/ECF/JaxRSProviders/issues/7
> 
> (but as I say below: the biggest hurdle for me is transitioning to karaf
> 4.2.x as I ran into some problems when I (inadvertently) tried it last
> week)
> 
>> BTW:  why do you require http-whiteboard for implementation?   I'm not
>> arguing against using http-whiteboard, but I expect that using
>> HttpService directly and http-whiteboard can co-exist in a single
>> framework,  so why not consider both?
> 
> The ukelonn webapp[1] started out in July 2016 as a JSF/primefaces war
> bundle, configured with web.xml[2].  In October 2016 I started running the
> webapp on karaf (and using OSGi for webapps became a *lot* easier...).  
> 
> Then it was transformed into a vaadin application[3], still as a war
> bundle with web.xml.  Then it was transformed into vaadin webapp in a
> jar bundle with the servlets registering with the HttpService, then
> finally turned into a vaadin webapp using http whiteboard (the current
> master).
> 
> I'm now in the process of transforming this into a reactjs webapp[4],
> and am still using http whiteboard.  Registering with the HttpService is
> of course possible, but feels like a step backwards. :-)
> 
> The biggest hurdle will be the transition to karaf 4.2.0, which I
> eventually need to make, but right now it's more fun to work on the
> reactjs part of things.  So it will happen sometime in the future.
> 
> References:
> [1] <https://github.com/steinarb/ukelonn>
> [2] <https://github.com/steinarb/ukelonn/tree/using-primefaces>
> [3] <https://github.com/steinarb/ukelonn/tree/using-vaadin>
> [4] <https://github.com/steinarb/ukelonn/tree/using-react>
> 


Re: Examples of using Jersey with pax web whiteboard extender?

Posted by Steinar Bang <sb...@dod.no>.
>>>>> Scott Lewis <sl...@composent.com>:


> If you would like to expidite, please open an issue here [1] for the
> use of http-whiteboard in jersey and cxf distribution providers, and
> it could happen pretty quickly.

https://github.com/ECF/JaxRSProviders/issues/7

(but as I say below: the biggest hurdle for me is transitioning to karaf
4.2.x as I ran into some problems when I (inadvertently) tried it last
week)

> BTW:  why do you require http-whiteboard for implementation?   I'm not
> arguing against using http-whiteboard, but I expect that using
> HttpService directly and http-whiteboard can co-exist in a single
> framework,  so why not consider both?

The ukelonn webapp[1] started out in July 2016 as a JSF/primefaces war
bundle, configured with web.xml[2].  In October 2016 I started running the
webapp on karaf (and using OSGi for webapps became a *lot* easier...).  

Then it was transformed into a vaadin application[3], still as a war
bundle with web.xml.  Then it was transformed into vaadin webapp in a
jar bundle with the servlets registering with the HttpService, then
finally turned into a vaadin webapp using http whiteboard (the current
master).

I'm now in the process of transforming this into a reactjs webapp[4],
and am still using http whiteboard.  Registering with the HttpService is
of course possible, but feels like a step backwards. :-)

The biggest hurdle will be the transition to karaf 4.2.0, which I
eventually need to make, but right now it's more fun to work on the
reactjs part of things.  So it will happen sometime in the future.

References:
[1] <https://github.com/steinarb/ukelonn>
[2] <https://github.com/steinarb/ukelonn/tree/using-primefaces>
[3] <https://github.com/steinarb/ukelonn/tree/using-vaadin>
[4] <https://github.com/steinarb/ukelonn/tree/using-react>


Re: Examples of using Jersey with pax web whiteboard extender?

Posted by Scott Lewis <sl...@composent.com>.
On 7/14/2018 10:33 AM, Steinar Bang wrote:
> <stuff deleted>
> (However, once I'm on karaf 4.2.x and OSGi 7.0.0, and once I hear
> rumours that Scott Lewis' approach will be working with the http
> whiteboard, then ditching my resource classes and annotating the service
> is something I will try)

If you would like to expidite, please open an issue here [1] for the use 
of http-whiteboard in jersey and cxf distribution providers, and it 
could happen pretty quickly.

BTW:  why do you require http-whiteboard for implementation?   I'm not 
arguing against using http-whiteboard, but I expect that using 
HttpService directly and http-whiteboard can co-exist in a single 
framework,  so why not consider both?

Scott

[1] https://github.com/ECF/JaxRSProviders


Re: Examples of using Jersey with pax web whiteboard extender?

Posted by Steinar Bang <sb...@dod.no>.
>>>>> Steinar Bang <sb...@dod.no>:

> The service definition is
>  https://github.com/steinarb/ukelonn/blob/5ca75f438407c594ed476bed790d984b0007cfdf/ukelonn.api/src/main/java/no/priv/bang/ukelonn/UkelonnService.java#L36

> The service implementation, is this DS component
>  https://github.com/steinarb/ukelonn/blob/5ca75f438407c594ed476bed790d984b0007cfdf/ukelonn.bundle/src/main/java/no/priv/bang/ukelonn/impl/UkelonnServiceProvider.java#L42

> However, with Jersey getting injection of the above service into the
> resource classes isn't so easy.

Based in the following two stackoverflow threads I have a tentative plan
for getting the service injected into Jersey resources using my current
stack (ie. karaf 4.1.5 and OSGi 6.0.0):
 https://stackoverflow.com/questions/38373867/inject-dao-instance-into-jersey-resource?noredirect=1&lq=1
 https://stackoverflow.com/questions/16216759/dependency-injection-with-jersey-2-0

(However, once I'm on karaf 4.2.x and OSGi 7.0.0, and once I hear
rumours that Scott Lewis' approach will be working with the http
whiteboard, then ditching my resource classes and annotating the service
is something I will try)

Outline of my plan:
 1. Make the service implementation extend ResourcesConfig
 2. Create a class extending AbstractBinder, that in its configure()
    method will bind an instance of the service implmentation to the
    UkelonnService interface
 3. In the @Activate method of the service implementation, register the
    activated instance with Jersey, using the AbstractBinder above
 4. In the resource classes use JSR330 @Inject of the UkelonnService
 5. Create a whiteboard DS service from the JerseyServletContainer, with:
    a. param configuration configuring a package to scan for resources
    b. add a @Reference for an UkelonnService and an @Activate method, to
       ensure that the servlet isn't activated, and plugged into the
       whiteboard until a service is available that can be injected into
       the created resources (and it should also ensure the servlet is
       taken down if the UkelonnService implementation is taken down)