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 2020/03/21 07:56:31 UTC

Announce: Simplified js frontend delivery and jersey REST APIs from karaf

I have written two servlets that simplifies js frontend delivery and
implementing jersey REST APIs in karaf.

The servlets are deployed to maven central and have been released under
the Apache v2 license.

The servlets are intended as base classes for DS (Declarative Services)
components implementing javax.Servlet OSGi services that will plug into
the web whiteboard.

The FrontendServlet[1] does the following:
 1. Loads and serves up resources from the bundle classpath of the DS
    component (typically index.html and bundle.js, but also CSS, font
    files and images) 
 2. Sets Content-Type for the resources it serves
 3. Has a list of paths that will serve up index.html to bootstrap the
    web application, this is used to be able to reload URLs navigated to
    by the react router[3] 

The JerseyServlet[2] does the following
 1. Set a default package to where Jersey should scan for classes
    implementing REST endpoints (subpackage ".resources" of the package
    the DS component resides in) 
 2. Provides a registration mechanism that can be used to pass injected
    OSGi services to the HK2 dependency injection, so that the OSGi
    services can be injected into the classes implementing REST
    endpoints 

My demo applications have been updated to use these servlets:
 https://github.com/steinarb/frontend-karaf-demo/blob/master/src/main/java/no/priv/bang/demos/frontendkarafdemo/ReactServlet.java#L14
 https://github.com/steinarb/jersey-demo/blob/master/jerseyinkaraf.webapi/src/main/java/no/priv/bang/demos/jerseyinkaraf/webapi/CounterServiceServlet.java#L27

I've also written blog posts describing how to use the new servlets.
 https://steinar.bang.priv.no/2020/03/16/simplified-delivery-of-react-js-from-apache-karaf/
 https://steinar.bang.priv.no/2020/03/20/simplified-rest-apis-from-karaf-using-jersey/

References:
[1] <https://github.com/steinarb/servlet#frontend>
[2] <https://github.com/steinarb/servlet#jersey>
[3] <https://reacttraining.com/react-router/web/guides/quick-start>


Re: Announce: Simplified js frontend delivery and jersey REST APIs from karaf

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

I’m pleased to see people doing work with Karaf and React, but it looks as though this is at least partly duplicating/rebuilding the existing functionality provided by the HTTP and JAX-RS whiteboard specifications. I’m thinking specifically:

>  Loads and serves up resources from the bundle classpath of the DS
>    component (typically index.html and bundle.js, but also CSS, font
>    files and images) 

This looks and sounds a lot like the ability to register resources using the HTTP whiteboard (https://osgi.org/specification/osgi.cmpn/7.0.0/service.http.whiteboard.html#d0e121459 <https://osgi.org/specification/osgi.cmpn/7.0.0/service.http.whiteboard.html#d0e121459>)

> 2. Sets Content-Type for the resources it serves

This looks a lot like the job performed by a ServletContextHelper (assuming you need to customise the defaults) (https://osgi.org/specification/osgi.cmpn/7.0.0/service.http.whiteboard.html#service.http.whiteboard.servletcontext <https://osgi.org/specification/osgi.cmpn/7.0.0/service.http.whiteboard.html#service.http.whiteboard.servletcontext>)

> 1. Set a default package to where Jersey should scan for classes
>    implementing REST endpoints (subpackage ".resources" of the package
>    the DS component resides in) 
> 2. Provides a registration mechanism that can be used to pass injected
>    OSGi services to the HK2 dependency injection, so that the OSGi
>    services can be injected into the classes implementing REST
>    endpoints 


This looks and sounds like a more limited version of the JAX-RS whiteboard (https://osgi.org/specification/osgi.cmpn/7.0.0/service.jaxrs.html <https://osgi.org/specification/osgi.cmpn/7.0.0/service.jaxrs.html>). Using the JAX-RS Whiteboard allows you to inject OSGi services into any component (you can provide your resources as services directly, or wrapped in an application), and you remain decoupled from the JAX-RS provider.


I’m all for simplifying the use of React frontends, but this approach seems to ignore most of the work that’s already been done to simplify the use of HTTP/REST in OSGi/Karaf. Would it not have made more sense to build examples on top of these existing structures. It would have been particularly nice to show how to set up these projects, and then add in a configuration for the “Frontend Servlet” to do the relevant path mapping for react routing (which sounds like a useful addition). As it is this project provides yet another different way to package your application bundles, and it's one that doesn’t fit with the rest of the advice/examples out in the world.

All the best,

Tim

> On 21 Mar 2020, at 07:56, Steinar Bang <sb...@dod.no> wrote:
> 
> I have written two servlets that simplifies js frontend delivery and
> implementing jersey REST APIs in karaf.
> 
> The servlets are deployed to maven central and have been released under
> the Apache v2 license.
> 
> The servlets are intended as base classes for DS (Declarative Services)
> components implementing javax.Servlet OSGi services that will plug into
> the web whiteboard.
> 
> The FrontendServlet[1] does the following:
> 1. Loads and serves up resources from the bundle classpath of the DS
>    component (typically index.html and bundle.js, but also CSS, font
>    files and images) 
> 2. Sets Content-Type for the resources it serves
> 3. Has a list of paths that will serve up index.html to bootstrap the
>    web application, this is used to be able to reload URLs navigated to
>    by the react router[3] 
> 
> The JerseyServlet[2] does the following
> 1. Set a default package to where Jersey should scan for classes
>    implementing REST endpoints (subpackage ".resources" of the package
>    the DS component resides in) 
> 2. Provides a registration mechanism that can be used to pass injected
>    OSGi services to the HK2 dependency injection, so that the OSGi
>    services can be injected into the classes implementing REST
>    endpoints 
> 
> My demo applications have been updated to use these servlets:
> https://github.com/steinarb/frontend-karaf-demo/blob/master/src/main/java/no/priv/bang/demos/frontendkarafdemo/ReactServlet.java#L14
> https://github.com/steinarb/jersey-demo/blob/master/jerseyinkaraf.webapi/src/main/java/no/priv/bang/demos/jerseyinkaraf/webapi/CounterServiceServlet.java#L27
> 
> I've also written blog posts describing how to use the new servlets.
> https://steinar.bang.priv.no/2020/03/16/simplified-delivery-of-react-js-from-apache-karaf/
> https://steinar.bang.priv.no/2020/03/20/simplified-rest-apis-from-karaf-using-jersey/
> 
> References:
> [1] <https://github.com/steinarb/servlet#frontend>
> [2] <https://github.com/steinarb/servlet#jersey>
> [3] <https://reacttraining.com/react-router/web/guides/quick-start>
>