Pax-Web Jetty-Bundle publishing Jersey App

1k views Asked by At

I'm trying to deploy a Jersey app on Apache Felix. I have these bundles installed:

    ID|State      |Level|Name
    0|Active     |    0|System Bundle (4.4.1)
    1|Active     |    1|Apache Felix Bundle Repository (2.0.2)
    2|Active     |    1|Apache Felix Gogo Command (0.14.0)
    4|Active     |    1|Apache Felix Gogo Shell (0.10.0)
   10|Active     |    1|Apache Felix Configuration Admin Service (1.2.8)
   11|Active     |    1|Apache Felix EventAdmin (1.2.10)
   35|Active     |    1|Apache Felix Gogo Runtime (0.12.1)
   90|Installed  |    1|Apache Felix Log Service (1.0.0)
   91|Resolved   |    1|OSGi R4 Compendium Bundle (4.0.0)
   92|Installed  |    1|Apache Felix Declarative Services (1.6.0)
   94|Installed  |    1|Apache Felix Web Management Console (3.1.2)
   95|Installed  |    1|Apache Felix iPOJO WebConsole Plugins (1.6.0)
   96|Resolved   |    1|Apache Felix Shell Service (1.4.2)
  119|Installed  |    1|Servlet 2.1 API (1.0.0)
  144|Active     |    1|OSGi JAX-RS Connector (4.1.0.201407081443)
  147|Active     |    1|jersey-all (2.10.1)
  157|Active     |    1|OPS4J Pax Web - Jetty Bundle (4.0.0)
  158|Active     |    1|OPS4J Pax Web - Runtime (4.0.0)
  163|Active     |    1|http://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7-sources.jar (0.0.0)
  165|Active     |    1|Java Servlet API (3.0.1)
  167|Active     |    1|OPS4J Pax Logging - API (1.8.1)
  170|Active     |    1|Apache Felix Dependency Manager (3.2.0)
  171|Active     |    1|OPS4J Pax Web - Extender - WAR (4.0.0)
  174|Active     |    1|Apache Felix Dependency Manager Runtime (3.2.0)
  177|Active     |    1|ASM (5.0.3)
  178|Active     |    1|ASM commons classes (5.0.3)
  179|Active     |    1|ASM Tree class visitor (5.0.3)
  182|Active     |    1|Apache XBean OSGI Bundle Utilities (3.18.0)
  183|Active     |    1|Apache XBean :: Classpath Resource Finder (3.18.0)
  216|Active     |    1|Auth Test (2.0.0.SNAPSHOT)

The last one, Auth Test is the Jersey app that I want to publish. I can install the bundle and start it. When I start it I get these logs:

g! start 216
Started Activator
org.ops4j.pax.web.pax-web-extender-war[org.ops4j.pax.web.extender.war.internal.WebEventDispatcher] : Sending web event WebEvent [replay=false, type=DEPLOYING, bundle=platform-component-web-rest-service-authentication [216], extenderBundle=org.ops4j.pax.web.pax-web-extender-war [171], cause=null, timestamp=1416226629360, contextPath=/authentication, collisionIds=null, httpService=null, httpContext=null] for bundle platform-component-web-rest-service-authentication
org.ops4j.pax.web.pax-web-extender-war[org.ops4j.pax.web.extender.war.internal.WebEventDispatcher] : org/osgi/service/web/DEPLOYING
org.ops4j.pax.web.pax-web-extender-war[org.ops4j.pax.web.extender.war.internal.parser.WebAppParser] : version found in web.xml - 3.0
org.ops4j.pax.web.pax-web-extender-war[org.ops4j.pax.web.extender.war.internal.parser.WebAppParser] : metadata-complete is: false
org.ops4j.pax.web.pax-web-extender-war[org.ops4j.pax.web.extender.war.internal.parser.WebAppParser] : scanning for ServletContainerInitializers
org.ops4j.pax.web.pax-web-extender-war[org.ops4j.pax.web.extender.war.internal.parser.WebAppParser] : found container initializers by SafeServiceLoader ... skip the old impl. 
org.ops4j.pax.web.pax-web-extender-war[org.ops4j.pax.web.extender.war.internal.Activator] : Scheduling start of extension for bundle platform-component-web-rest-service-authentication asynchronously
org.ops4j.pax.web.pax-web-extender-war[org.ops4j.pax.web.extender.war.internal.WebAppPublisher] : Publishing web application [WebApp{displayName=Authentication Manager,contextName=authentication,m_httpContext=null}]

I guess the whole problem is behind the web.xml which looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="platform-component-web-rest-manager-authentication" version="3.0">


    <display-name>Authentication Manager</display-name>


    <!-- ****************************************************************** -->
    <!-- SERVLETS -->
    <!-- ****************************************************************** -->         
    <servlet>
        <servlet-name>wd-platform-product-web</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>
                com.mycompany.platform.component.web.rest;
                com.mycompany.wd.platform.component.web.rest,
            </param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.scanning.recursive</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>
                com.wordnik.swagger.jersey.listing.ApiListingResourceJSON;
                com.wordnik.swagger.jersey.listing.JerseyApiDeclarationProvider;
                com.wordnik.swagger.jersey.listing.JerseyResourceListingProvider
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>wd-platform-product-web</servlet-name>
        <url-pattern>/rest/2.0.0/*</url-pattern>
    </servlet-mapping>


    <servlet>
        <servlet-name>swagger-default-jaxrs-config</servlet-name>
        <servlet-class>com.wordnik.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
        <init-param>
            <param-name>api.version</param-name>
            <param-value>2.0.0</param-value>
        </init-param>
        <init-param>
            <param-name>swagger.api.basepath</param-name>
            <param-value>/rest/2.0.0/</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

</web-app>

My activator class is the standard model:

import java.util.HashMap;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;

/**
 * This is to make sure we signal the application has been deployed/un-deployed
 * via the OSGi EventAdmin service.
 *
 * @author Jakub Podlesak (jakub.podlesak at oracle.com)
 */
public class WebAppContextListener implements BundleActivator, ServletContextListener {

    static EventAdmin ea;

    BundleContext bc;
    ServiceReference eaRef;

    synchronized static EventAdmin getEa() {
        return ea;
    }

    synchronized static void setEa(EventAdmin ea) {
        WebAppContextListener.ea = ea;
    }

    @Override
    public void contextInitialized(final ServletContextEvent sce) {
        if (getEa() != null) {
            final String contextPath = sce.getServletContext().getContextPath();
            getEa().sendEvent(new Event("jersey/test/DEPLOYED",new HashMap<String, String>(){{
                put("context-path", contextPath);}}));
        }
    }

    @Override
    public void contextDestroyed(final ServletContextEvent sce) {
        if (getEa() != null) {
            getEa().sendEvent(new Event("jersey/test/UNDEPLOYED",new HashMap<String, String>(){{put("context-path", sce.getServletContext().getContextPath());}}));
        }
    }

    @Override
    public void start(BundleContext context) throws Exception {
        System.out.println("Started Activator");
        bc = context;
        eaRef = bc.getServiceReference(EventAdmin.class.getName());
        if (eaRef != null) {
            setEa((EventAdmin)bc.getService(eaRef));
        }
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        if (eaRef != null) {
            setEa(null);
            bc.ungetService(eaRef);
        }
    }
}

And my main REST service looks like this:

@Path("/service/authentication")
@Api(value = "/service/authentication", description = "Authentication Service")
public class AuthenticationServiceRestService extends RestService
{

    ServiceLayer<Request<?>> serviceLayer;
    AuthenticationLayer<Request<?>> authenticationLayer;
    ExceptionLayer<Request<?>> exceptionLayer;


    public AuthenticationServiceRestService() throws AuthenticationServiceRestServiceException
    {
        this.serviceLayer = new ServiceLayer<Request<?>>();

        try
        {
            this.authenticationLayer = new AuthenticationLayer<Request<?>>(this.serviceLayer);
        }
        catch (AuthenticationServiceException exception)
        {
            throw new AuthenticationServiceRestServiceException(exception.getMessage(), exception);
        }

        this.exceptionLayer = new ExceptionLayer<Request<?>>(this.authenticationLayer);
    }


    @POST
    @Path("/auth")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Get authentication token.", response = AuthenticationTokenFacade.class)
    public Response getAuthenticationToken(@ApiParam(value = "Authentication Data", required = true) AuthenticationRequestFacade authenticationRequestFacade)
    {
        System.out.println("auth request");
        GetAuthenticationTokenJsonRequest getAuthenticationTokenJsonRequest = new GetAuthenticationTokenJsonRequest(authenticationRequestFacade);

        return exceptionLayer.execute(getAuthenticationTokenJsonRequest);
    }

When I make a POST request to http://localhost:8080/authentication/rest/2.0.0/service/authentication/auth I get 404. I don't even know if my whole service is published on jetty. How can I do that? Jetty displays this in the middle of a massive log when I make that request:

org.ops4j.pax.logging.pax-logging-api[org.ops4j.pax.web.service.spi.model.ServerModel] : Matching [/authentication/rest/2.0.0/service/authentication/auth]...
org.ops4j.pax.logging.pax-logging-api[org.ops4j.pax.web.service.spi.model.ServerModel] : Path [/authentication/rest/2.0.0/service/authentication/auth] does not match any context

Any help to make this work would be great. Thanks in advance and sorry about the long question but I had to make sure that all the information is provided.

1

There are 1 answers

0
Achim Nierbeck On BEST ANSWER

First, thanks for the detailed question this is much better this way, only thing missing is the version of Pax Web you're using ;-)
Second, you might be interested in using Karaf with Pax Web. It's easy to install Pax-Web in Karaf by issuing the

feature:install war

command to have Pax Web installed inside Karaf. Now why am I suggesting this at first, because you said you don't know if the application is actually available. For this there are two commands available in the Karaf shell that will tell you if it is or not. One would be

web:list

the other one is

http:list

either one will give you a list of a) the web application bundles deployed or b) the servlets registered.

This to give you a better feeling about the deployed application.
Now, what I don't get is, why you have an Activator in your WebApplicationBundle (WAB), cause for WABs you just use a "regular" war with OSGi Manifest including the Web-ContextPath. From there on the WebExtender will do the work for you, as in your case it has been done. If you want to have a ServletContextListener used, register it in the regular way in the web.xml or since you're using Servlet3 web.xml with the standard annotations.

Now the last part, the log message tells you already when issuing the request to the server, there hasn't been found a httpContext for the Request. The reason for this will most likely be somewhere in the long logs you had before that. It could be something got wrong registering the servlet. Again the web:list command might be of help to get a even better picture.

As Pax Web hasn't been explicitly tested for this :-) it could be of interest to provide a simple Sample to the Pax Web community to add it to the samples and to have Integration tests. It's also always good to ask at the ops4j mailinglist.