Jetty adding web fragment to web context programmatically

643 views Asked by At

I've started to use embedded Jetty Server to start some 3rd-party WAR. So I use WebAppContext:

Server server = new Server(port);
WebAppContext ctx = new WebAppContext();
ctx.setContextPath("/samplePath");
ctx.setWar(<PATH_TO_WAR>);
server.setHandler(ctx);
server.start();
server.join();

This code works (I've omitted exception handling for the sake of brevity here), however now I would like to add some functionality to the war which I want to leave intact (I mean, don't extract change and compress again). My functionality should include an integration with some custom SSO solution which should add the following:

  • A Context Listener
  • A Filter
  • Some Context Param Variables definitions that should be read by these Filter and listener

I can't change this SSO solution because its not developed by our team, we rather take it as a thirdparty.

So I thought that adding all this to module with web-fragment would be the best approach.

My question is: what is the best way to make Jetty's web context to be 'aware' of this web fragment? A working code snippet would be great :)

Of course if there is a better alternative than web fragments for this, I'll be glad to know

The version of Jetty I currently use is (from my pom.xml): 9.2.10.v20150310

Thanks a lot in advance!

1

There are 1 answers

3
Pasupathi Rajamanickam On

Here is the way you can specify a web app as well as a filter

import java.io.IOException;
import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.webapp.WebAppContext;

public class MultipleHandler {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Server server = new Server();
        ServerConnector connectorA = new ServerConnector(server);
        connectorA.setPort(55);
        connectorA.setName("connA"); // connector name A
        server.addConnector(connectorA);
        HandlerCollection contexts = new HandlerCollection();
        server.setHandler(contexts);
        // A WebApp
        WebAppContext appA = new WebAppContext();
        appA.setContextPath("/samplePath");
        appA.setWar("<warFilePath>");
        appA.setVirtualHosts(new String[] { "@connA" }); // connector name A
        contexts.addHandler(appA);

        //Filter handler
        ServletHandler handler = new ServletHandler();
        handler.addFilterWithMapping(DoWork.class, "/filter",
                EnumSet.of(DispatcherType.REQUEST));
        contexts.addHandler(handler);       

        try {
            server.start(); 
            server.join(); 
        } catch (Throwable t) {
            t.printStackTrace(System.err);
        }

    }


    public static class DoWork implements Filter {

        @Override
        public void destroy() {
            // TODO Auto-generated method stub

        }

        @Override
        public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
             System.out.print("Request filtered");

        }

        @Override
        public void init(FilterConfig arg0) throws ServletException {
            // TODO Auto-generated method stub

        }

    }

}