Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does embedded Jetty have the ability to set the init-params of a filter?

Suppose I'm running embedded jetty with the following code. I want to pass in some parameters to my filter. When I look at the Jetty API for adding a filter - there are no options to set init-params. My question is Does embedded Jetty have the ability to set the init-params of 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 javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;

public class JettyFilter {

  public static void main(final String[] args) throws Exception {
    Server server = new Server(8080);

    ServletHandler handler = new ServletHandler();
    server.setHandler(handler);

    handler.addServletWithMapping(HelloServlet.class, "/*");
    handler.addFilterWithMapping(HelloPrintingFilter.class, "/*",
        EnumSet.of(DispatcherType.REQUEST));

    server.start();
    server.join();
  }

  public static class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
      response.setContentType("text/html");
      response.setStatus(HttpServletResponse.SC_OK);
      response.getWriter().println("<h1>Hello SimpleServlet</h1>");
    }
  }

  public static class HelloPrintingFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
      System.out.print("hello from filter");
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {

    }

    @Override
    public void destroy() {}
  }
}
like image 642
hawkeye Avatar asked Mar 05 '14 04:03

hawkeye


2 Answers

Just to summarise - it looks like this:

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

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 javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class TestEmbeddedFilter {

    public static void main(String[] args) {
        Server server = new Server(8082);

        ServletContextHandler context = new ServletContextHandler(
                ServletContextHandler.SESSIONS);
        context.setContextPath("/");

        ServletHandler handler = new ServletHandler();
        ServletHolder sh = new ServletHolder(new MyServlet());
        context.addServlet(sh, "/*");

        FilterHolder fh = handler.addFilterWithMapping(MyFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
        fh.setInitParameter("initParamKey", "InitParamValue");

        context.addFilter(fh, "/*", EnumSet.of(DispatcherType.REQUEST));


        context.setHandler(handler);
        server.setHandler(context);
        try {
            server.start();
            // server.join();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @SuppressWarnings("serial")
    public static class MyServlet extends HttpServlet {

        @Override
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
            response.setContentType("text/html");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().println("<h1>Hello MyServlet</h1>");

        }
    }

    public static class MyFilter implements Filter {

        public void destroy() {
            System.out.println("Stopping filter");
        }

        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            System.out.println("doFilter called with: " + request);

            chain.doFilter(request, response);
        }

        public void init(FilterConfig filterConfig) throws ServletException {
            Enumeration<String> enums = filterConfig.getInitParameterNames();

            while (enums.hasMoreElements()) {
                String param = (String) enums.nextElement();
                System.out.println(param + ":" + filterConfig.getInitParameter(param));
            }
        }

    }

}
like image 127
hawkeye Avatar answered Oct 25 '22 06:10

hawkeye


Assuming you're using this version (or higher) of Jetty's ServletHandler, the addFilterWithMapping(..) method returns a FilterHolder which provides a setInitParameter(..) method to set your init parameter.

like image 27
Sotirios Delimanolis Avatar answered Oct 25 '22 06:10

Sotirios Delimanolis