Search in sources :

Example 31 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project jetty.project by eclipse.

the class ProxyServletTest method testResponseHeadersAreNotRemoved.

@Test
public void testResponseHeadersAreNotRemoved() throws Exception {
    startServer(new EmptyHttpServlet());
    startProxy();
    proxyContext.stop();
    final String headerName = "X-Test";
    final String headerValue = "test-value";
    proxyContext.addFilter(new FilterHolder(new Filter() {

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            ((HttpServletResponse) response).addHeader(headerName, headerValue);
            chain.doFilter(request, response);
        }

        @Override
        public void destroy() {
        }
    }), "/*", EnumSet.of(DispatcherType.REQUEST));
    proxyContext.start();
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(headerValue, response.getHeaders().get(headerName));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Filter(javax.servlet.Filter) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterConfig(javax.servlet.FilterConfig) Test(org.junit.Test)

Example 32 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project jetty.project by eclipse.

the class StandardDescriptorProcessor method start.

/**
     * {@inheritDoc}
     */
public void start(WebAppContext context, Descriptor descriptor) {
    for (FilterHolder h : context.getServletHandler().getFilters()) {
        _filterHolderMap.put(h.getName(), h);
        _filterHolders.add(h);
    }
    if (context.getServletHandler().getFilterMappings() != null)
        _filterMappings.addAll(Arrays.asList(context.getServletHandler().getFilterMappings()));
    for (ServletHolder h : context.getServletHandler().getServlets()) {
        _servletHolderMap.put(h.getName(), h);
        _servletHolders.add(h);
    }
    if (context.getServletHandler().getServletMappings() != null)
        _servletMappings.addAll(Arrays.asList(context.getServletHandler().getServletMappings()));
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder)

Example 33 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project jetty.project by eclipse.

the class StandardDescriptorProcessor method visitFilter.

public void visitFilter(WebAppContext context, Descriptor descriptor, XmlParser.Node node) {
    String name = node.getString("filter-name", false, true);
    FilterHolder holder = _filterHolderMap.get(name);
    if (holder == null) {
        holder = context.getServletHandler().newFilterHolder(new Source(Source.Origin.DESCRIPTOR, descriptor.getResource().toString()));
        holder.setName(name);
        _filterHolderMap.put(name, holder);
        _filterHolders.add(holder);
    }
    String filter_class = node.getString("filter-class", false, true);
    if (filter_class != null) {
        ((WebDescriptor) descriptor).addClassName(filter_class);
        switch(context.getMetaData().getOrigin(name + ".filter.filter-class")) {
            case NotSet:
                {
                    //no class set yet
                    holder.setClassName(filter_class);
                    context.getMetaData().setOrigin(name + ".filter.filter-class", descriptor);
                    break;
                }
            case WebXml:
            case WebDefaults:
            case WebOverride:
                {
                    //filter class was set in web.xml, only allow other web xml descriptors (override/default) to change it
                    if (!(descriptor instanceof FragmentDescriptor)) {
                        holder.setClassName(filter_class);
                        context.getMetaData().setOrigin(name + ".filter.filter-class", descriptor);
                    }
                    break;
                }
            case WebFragment:
                {
                    //the filter class was set up by a web fragment, all fragments must be the same
                    if (!holder.getClassName().equals(filter_class))
                        throw new IllegalStateException("Conflicting filter-class for filter " + name + " in " + descriptor.getResource());
                    break;
                }
            default:
                // TODO throw ISE?
                LOG.warn(new Throwable());
        }
    }
    Iterator<XmlParser.Node> iter = node.iterator("init-param");
    while (iter.hasNext()) {
        XmlParser.Node paramNode = iter.next();
        String pname = paramNode.getString("param-name", false, true);
        String pvalue = paramNode.getString("param-value", false, true);
        switch(context.getMetaData().getOrigin(name + ".filter.init-param." + pname)) {
            case NotSet:
                {
                    //init-param not already set, so set it
                    holder.setInitParameter(pname, pvalue);
                    context.getMetaData().setOrigin(name + ".filter.init-param." + pname, descriptor);
                    break;
                }
            case WebXml:
            case WebDefaults:
            case WebOverride:
                {
                    //otherwise just ignore it
                    if (!(descriptor instanceof FragmentDescriptor)) {
                        holder.setInitParameter(pname, pvalue);
                        context.getMetaData().setOrigin(name + ".filter.init-param." + pname, descriptor);
                    }
                    break;
                }
            case WebFragment:
                {
                    //previously set by a web-fragment, make sure that the value matches, otherwise its an error
                    if (!holder.getInitParameter(pname).equals(pvalue))
                        throw new IllegalStateException("Mismatching init-param " + pname + "=" + pvalue + " in " + descriptor.getResource());
                    break;
                }
            default:
                // TODO throw ISE?
                LOG.warn(new Throwable());
        }
    }
    String async = node.getString("async-supported", false, true);
    if (async != null)
        holder.setAsyncSupported(async.length() == 0 || Boolean.valueOf(async));
    if (async != null) {
        boolean val = async.length() == 0 || Boolean.valueOf(async);
        switch(context.getMetaData().getOrigin(name + ".filter.async-supported")) {
            case NotSet:
                {
                    //set it
                    holder.setAsyncSupported(val);
                    context.getMetaData().setOrigin(name + ".filter.async-supported", descriptor);
                    break;
                }
            case WebXml:
            case WebDefaults:
            case WebOverride:
                {
                    //async-supported set by previous web xml descriptor, only allow override if we're parsing another web descriptor(web.xml/web-override.xml/web-default.xml)
                    if (!(descriptor instanceof FragmentDescriptor)) {
                        holder.setAsyncSupported(val);
                        context.getMetaData().setOrigin(name + ".filter.async-supported", descriptor);
                    }
                    break;
                }
            case WebFragment:
                {
                    //async-supported set by another fragment, this fragment's value must match
                    if (holder.isAsyncSupported() != val)
                        throw new IllegalStateException("Conflicting async-supported=" + async + " for filter " + name + " in " + descriptor.getResource());
                    break;
                }
            default:
                // TODO throw ISE?
                LOG.warn(new Throwable());
        }
    }
}
Also used : XmlParser(org.eclipse.jetty.xml.XmlParser) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Node(org.eclipse.jetty.xml.XmlParser.Node) Node(org.eclipse.jetty.xml.XmlParser.Node) Source(org.eclipse.jetty.servlet.Source)

Example 34 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project killbill by killbill.

the class HttpServer method createServletContextHandler.

private ServletContextHandler createServletContextHandler(final String resourceBase, final Iterable<EventListener> eventListeners, final Map<FilterHolder, String> filterHolders) {
    final ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.NO_SESSIONS);
    context.setContextPath("/");
    if (resourceBase != null) {
        // Required if you want a webapp directory. See ContextHandler#getResource and http://docs.codehaus.org/display/JETTY/Embedding+Jetty
        final String webapp = this.getClass().getClassLoader().getResource(resourceBase).toExternalForm();
        context.setResourceBase(webapp);
    }
    // Jersey insists on using java.util.logging (JUL)
    final EventListener listener = new JULServletContextListener();
    context.addEventListener(listener);
    for (final EventListener eventListener : eventListeners) {
        context.addEventListener(eventListener);
    }
    for (final FilterHolder filterHolder : filterHolders.keySet()) {
        context.addFilter(filterHolder, filterHolders.get(filterHolder), EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    }
    // Make sure Guice filter all requests
    final FilterHolder filterHolder = new FilterHolder(GuiceFilter.class);
    context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    // Backend servlet for Guice - never used
    final ServletHolder sh = new ServletHolder(DefaultServlet.class);
    context.addServlet(sh, "/*");
    return context;
}
Also used : JULServletContextListener(org.killbill.commons.skeleton.listeners.JULServletContextListener) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) EventListener(java.util.EventListener) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 35 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project neo4j by neo4j.

the class Jetty9WebServer method loadStaticContent.

private void loadStaticContent(SessionManager sm, String mountPoint) {
    String contentLocation = staticContent.get(mountPoint);
    try {
        SessionHandler sessionHandler = new SessionHandler(sm);
        sessionHandler.setServer(getJetty());
        final WebAppContext staticContext = new WebAppContext();
        staticContext.setServer(getJetty());
        staticContext.setContextPath(mountPoint);
        staticContext.setSessionHandler(sessionHandler);
        staticContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
        URL resourceLoc = getClass().getClassLoader().getResource(contentLocation);
        if (resourceLoc != null) {
            URL url = resourceLoc.toURI().toURL();
            final Resource resource = Resource.newResource(url);
            staticContext.setBaseResource(resource);
            addFiltersTo(staticContext);
            staticContext.addFilter(new FilterHolder(new NoCacheHtmlFilter()), "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD));
            handlers.addHandler(staticContext);
        } else {
            log.warn("No static content available for Neo Server at %s, management console may not be available.", jettyAddress);
        }
    } catch (Exception e) {
        log.error("Unknown error loading static content", e);
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Resource(org.eclipse.jetty.util.resource.Resource) URL(java.net.URL) ServletException(javax.servlet.ServletException) URISyntaxException(java.net.URISyntaxException) PortBindException(org.neo4j.helpers.PortBindException) BindException(java.net.BindException) IOException(java.io.IOException)

Aggregations

FilterHolder (org.eclipse.jetty.servlet.FilterHolder)84 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)44 Test (org.junit.Test)35 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)32 Server (org.eclipse.jetty.server.Server)21 CountDownLatch (java.util.concurrent.CountDownLatch)18 Filter (javax.servlet.Filter)11 IOException (java.io.IOException)10 URL (java.net.URL)10 DispatcherType (javax.servlet.DispatcherType)9 ServletException (javax.servlet.ServletException)8 AuthenticationToken (org.apache.hadoop.security.authentication.server.AuthenticationToken)8 ServerConnector (org.eclipse.jetty.server.ServerConnector)8 File (java.io.File)7 HttpURLConnection (java.net.HttpURLConnection)7 PrivilegedActionException (java.security.PrivilegedActionException)7 AuthenticationException (org.apache.hadoop.security.authentication.client.AuthenticationException)7 FilterMapping (org.eclipse.jetty.servlet.FilterMapping)7 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)7 List (java.util.List)6