Servlet karaf feature

Aggregate feature repository

License

License

GroupId

GroupId

no.priv.bang.servlet
ArtifactId

ArtifactId

karaf
Last Version

Last Version

1.5.2
Release Date

Release Date

Type

Type

xml
Description

Description

Servlet karaf feature
Aggregate feature repository

Download karaf

Filename Size
karaf-1.5.2.pom 3 KB
karaf-1.5.2-features.xml 326 bytes
Browse

Dependencies

compile (2)

Group / Artifact Type Version
no.priv.bang.servlet : servlet.jersey jar 1.5.2
no.priv.bang.servlet : servlet.frontend jar 1.5.2

Project Modules

There are no modules declared in this project.

Java servlet common code

Servlet and filter classes that are intended to be inherited to cut down on boilerplate code

Status of the project

https://travis-ci.org/steinarb/servlet.svg?branch=master https://sonarcloud.io/api/project_badges/measure?project=no.priv.bang.servlet%3Aservlet&metric=alert_status#.svg https://maven-badges.herokuapp.com/maven-central/no.priv.bang.servlet/servlet/badge.svg https://www.javadoc.io/badge/no.priv.bang.servlet/servlet.svg

Sonarqube

https://sonarcloud.io/api/project_badges/measure?project=no.priv.bang.servlet%3Aservlet&metric=ncloc#.svg https://sonarcloud.io/api/project_badges/measure?project=no.priv.bang.servlet%3Aservlet&metric=bugs#.svg https://sonarcloud.io/api/project_badges/measure?project=no.priv.bang.servlet%3Aservlet&metric=vulnerabilities#.svg https://sonarcloud.io/api/project_badges/measure?project=no.priv.bang.servlet%3Aservlet&metric=code_smells#.svg https://sonarcloud.io/api/project_badges/measure?project=no.priv.bang.servlet%3Aservlet&metric=coverage#.svg

Release history

Date Version Comment
<2021-05-02 Sun 17:34> 1.5.4 Use a jersey feature version that supports java.time serialization/deserialization
<2021-04-19 Mon 01:00> 1.5.3 Use the OSGi adapters BoM
<2021-04-17 Sat 21:34> 1.5.2 Provide maven Bill of Materials to simplify usage
<2021-04-15 Thu 20:40> 1.5.1 Get maven dependencies and maven plugin config from a parent POM
<2021-04-12 Mon 18:10> 1.5.0 Built with karaf 4.3.0 and OSGi 7
<2021-01-24 Sun 21:25> 1.4.0 Use jersey 2.33 and jackson 2.12.1
<2020-09-12 Sat 16:30> 1.3.2 Fix issue #3 by requiring package from jersey-inject-hk2
<2020-09-11 Fri 22:25> 1.3.1 Does not pull in servicemix javax.inject at runtime
<2020-07-31 Fri 13:07> 1.3.0 Fix issue #2 in FrontendServlet, add content-type for .ico files
<2020-07-29 Wed 16:12> 1.2.0 Fix issue #1 in FrontendServlet
<2020-04-09 Thu 22:40> 1.1.3 Compile and runtime dependencies to jersey 2.30.1, runtime dependencies to jackson 2.10.3
<2020-03-05 Thu 18:22> 1.1.2 Use runtime dependency to jackson-databind 2.9.10.3 to fix security issue CVE-2020-8840
<2020-02-21 Fri 22:42> 1.1.1 Bugfix release of the JerseyServlet with correct method name
<2020-02-19 Wed 07:52> 1.1.0 First release of the JerseyServlet
<2020-01-12 Sun 23:26> 1.0.0 First release of the FrontendServlet

Overview of the project

Frontend

This is a servlet that’s intended to be extended by a servlet serving out a JS frontend packed by webpack.

The servlet will search for resources matching the pathInfo (minus the webcontext) on the classpath and serve them out, setting the content type based on the file name extension.

The servlet will try serving the file “index.html” (that must exist on the classpath) for a list of routes, that can be set by a subclass. This is to handle reloads of URLs set by e.g. the react router.

To use the servlet in an application built with maven, add the maven dependency:

<dependency>
 <groupId>no.priv.bang.servlet</groupId>
 <artifactId>servlet.frontend</artifactId>
 <version>1.5.4</version>
</dependency>

To use the servlet in a webapp running in the apache karaf web whiteboard apache:

  1. Add a maven dependency
    <dependency>
     <groupId>no.priv.bang.servlet</groupId>
     <artifactId>servlet.frontend</artifactId>
     <version>1.5.4</version>
     <scope>provided</scope>
    </dependency>
        
  2. Create an OSGi bundle containing a DS component registering with the web whiteboard
    @Component(service={Servlet.class}, property={"alias=/myapp"})
    public class ReactServlet extends FrontendServlet {
        public ReactServlet() {
            super();
            setRoutes("/", "/counter", "/about");
        }
    
        @Reference
        public void setLogservice(LogService logservice) {
            super.setLogService(logservice);
        }
    }
        
  3. Add a runtime dependency to the bundle project’s template feature repository (i.e. src/main/feature/feature.xml in the bundle project):
    <features>
     <repository>mvn:no.priv.bang.servlet/karaf/1.5.4/xml/features</repository>
     <feature name="my-servlet">
      <feature>frontend-servlet</feature>
     </feature>
    </features>
        

Processing content

In many cases, just sending resources found on the classpath, is what is wanted.

But in some cases it may be desirable to do processing on the resource found on the classpath, before it is returned.

One such example, is the “index.html” file that is used to boostrap the webapp returned by the FrontendServlet. In this case it is desirable to set Open Graph <meta> headers corresponding to the path the application is entered with.

This is so that you can give an URL to a specific subpage in a webapp, and that URL will return <meta> headers with information that will make the URL look nice in google searches and various social media.

To accomplish this, FrontendServlet has two overridable methods:

public class FrontendServlet extends HttpServlet{
    protected boolean thisIsAResourceThatShouldBeProcessed(String pathInfo, String resource, String contentType);
    protected void processResource(HttpServletResponse response, String pathInfo, String resource, String contentType) throws IOException;
}

The thisIsAResourceThatShouldBeProcessed() method is overridden to detect if a resource should be processed. If this method returns true, then processResource() will be called and no further handling of the request will be done by the FrontendServlet base class.

The FrontendServlet base implementation of processResource() returns the status code 501 Not Implemented.

Jersey

This is a servlet that’s intended to be extended by a servlet using jersey to implement a REST API.

The JerseyServlet does two things:

  1. Adds a way to add injected OSGi services to the HK2 dependency injection container, so that the OSGi services can be injected into Jersey resources, allowing the Jersey resources to be thin shims over OSGi service calls
  2. Adds the subpackage “.resources” of the servlet’s package as the default package to scan for Jersey resources Note! If a different package is set by configuration, this will override the default

To use the servlet in an application built with maven, add the maven dependency:

<dependency>
 <groupId>no.priv.bang.servlet</groupId>
 <artifactId>servlet.jersey</artifactId>
 <version>1.5.4</version>
</dependency>

To use the servlet in a webapp running in the apache karaf web whiteboard apache:

  1. Add a maven dependency
    <dependency>
     <groupId>no.priv.bang.servlet</groupId>
     <artifactId>servlet.jersey</artifactId>
     <version>1.5.4</version>
     <scope>provided</scope>
    </dependency>
        
  2. Create an OSGi bundle containing a DS component registering with the web whiteboard.
    package no.priv.bang.servlet.jersey.test;
    
    @Component(service={Servlet.class})
    public class ExampleJerseyServlet extends JerseyServlet {
    
        @Reference
        public void setHelloService(HelloService service) {
            addInjectedOsgiService(HelloService.class, service);
        }
    
        @Reference
        public void setLogService(LogService logservice) {
            super.setLogService(logservice);
        }
    }
        

    Note! The OSGi LogService must be added by a separate method, since the LogService is used by the JerseyServlet itself (as well as being added to HK2, which makes it possible to use LogService in Jersey resources).

  3. Add resources implementing REST API endpoints in the .resources sub-package of the servlet’s package, and use @Inject to inject the OSGi services that JerseyServlet adds to the HK2 dependency injection container:
    package no.priv.bang.servlet.jersey.test.resources;
    
    @Path("/hello")
    public class HelloResource {
    
        @Inject
        HelloService service;
    
        @GET
        @Produces("text/plain")
        public String getHello() {
            return service.hello();
        }
    }
        
  4. Add a runtime dependency to the bundle project’s template feature repository (i.e. src/main/feature/feature.xml in the bundle project):
    <features>
     <repository>mvn:no.priv.bang.servlet/karaf/1.5.4/xml/features</repository>
     <feature name="my-servlet">
      <feature>jersey-servlet</feature>
     </feature>
    </features>
        

License

This code is licensed under the Apache license v. 2. See the LICENSE file for details.

Versions

Version
1.5.2
1.5.1
1.5.0
1.4.0