0

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!

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97

1 Answers1

0

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

        }

    }

}
Pasupathi Rajamanickam
  • 1,982
  • 1
  • 24
  • 48
  • sorry, but this doesn't help me. Filter is not an issue since it can be set right by calling addFilter to WebAppContext. Unlike that, I couldn't find the way to add ContextFilter and Context Parameter variables. Hence the question. Can't accept this as an answer, sorry – Mark Bramnik May 26 '15 at 11:40
  • Ya sure. If you didn't see it, http://stackoverflow.com/questions/19530806/java-jetty-how-to-add-filter-to-embedded-jetty – Pasupathi Rajamanickam May 26 '15 at 11:43
  • Thanks, Yes, I haven't seen this, but Jetty's API uses java.util.EventListener which doesn't implement javax.servlet.ServletContextListener so I can't access the servlet context from the methods of that listener. I'm looking for solution that would allow me putting the web fragement jar in some folder in the filesystem and jetty would load it with the same classloader it uses to load the war itself. – Mark Bramnik May 26 '15 at 12:11