Search in sources :

Example 1 with FilterMapping

use of org.eclipse.jetty.servlet.FilterMapping in project hadoop by apache.

the class HttpServer2 method defineFilter.

/**
   * Define a filter for a context and set up default url mappings.
   */
public static void defineFilter(ServletContextHandler ctx, String name, String classname, Map<String, String> parameters, String[] urls) {
    FilterHolder filterHolder = getFilterHolder(name, classname, parameters);
    FilterMapping fmap = getFilterMapping(name, urls);
    defineFilter(ctx, filterHolder, fmap);
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) FilterMapping(org.eclipse.jetty.servlet.FilterMapping)

Example 2 with FilterMapping

use of org.eclipse.jetty.servlet.FilterMapping in project hadoop by apache.

the class HttpServer2 method getFilterMapping.

private static FilterMapping getFilterMapping(String name, String[] urls) {
    FilterMapping fmap = new FilterMapping();
    fmap.setPathSpecs(urls);
    fmap.setDispatches(FilterMapping.ALL);
    fmap.setFilterName(name);
    return fmap;
}
Also used : FilterMapping(org.eclipse.jetty.servlet.FilterMapping)

Example 3 with FilterMapping

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

the class MultipartFilterTest method testBodyAlreadyConsumed.

@Test
public void testBodyAlreadyConsumed() throws Exception {
    tester.addServlet(NullServlet.class, "/null");
    FilterHolder holder = new FilterHolder();
    holder.setName("reader");
    holder.setFilter(new ReadAllFilter());
    tester.getContext().getServletHandler().addFilter(holder);
    FilterMapping mapping = new FilterMapping();
    mapping.setFilterName("reader");
    mapping.setPathSpec("/*");
    tester.getContext().getServletHandler().prependFilterMapping(mapping);
    String boundary = "XyXyXy";
    // generated and parsed test
    HttpTester.Request request = HttpTester.newRequest();
    HttpTester.Response response;
    request.setMethod("POST");
    request.setVersion("HTTP/1.0");
    request.setHeader("Host", "tester");
    request.setURI("/context/null");
    request.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
    request.setContent("How now brown cow");
    try (StacklessLogging stackless = new StacklessLogging(ServletHandler.class)) {
        response = HttpTester.parseResponse(tester.getResponses(request.generate()));
        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    }
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) FilterMapping(org.eclipse.jetty.servlet.FilterMapping) Matchers.containsString(org.hamcrest.Matchers.containsString) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Example 4 with FilterMapping

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

the class WebFilterAnnotation method apply.

/**
     * @see DiscoveredAnnotation#apply()
     */
public void apply() {
    // TODO verify against rules for annotation v descriptor
    Class clazz = getTargetClass();
    if (clazz == null) {
        LOG.warn(_className + " cannot be loaded");
        return;
    }
    //Servlet Spec 8.1.2
    if (!Filter.class.isAssignableFrom(clazz)) {
        LOG.warn(clazz.getName() + " is not assignable from javax.servlet.Filter");
        return;
    }
    MetaData metaData = _context.getMetaData();
    WebFilter filterAnnotation = (WebFilter) clazz.getAnnotation(WebFilter.class);
    if (filterAnnotation.value().length > 0 && filterAnnotation.urlPatterns().length > 0) {
        LOG.warn(clazz.getName() + " defines both @WebFilter.value and @WebFilter.urlPatterns");
        return;
    }
    String name = (filterAnnotation.filterName().equals("") ? clazz.getName() : filterAnnotation.filterName());
    String[] urlPatterns = filterAnnotation.value();
    if (urlPatterns.length == 0)
        urlPatterns = filterAnnotation.urlPatterns();
    FilterHolder holder = _context.getServletHandler().getFilter(name);
    if (holder == null) {
        //Filter with this name does not already exist, so add it
        holder = _context.getServletHandler().newFilterHolder(new Source(Source.Origin.ANNOTATION, clazz.getName()));
        holder.setName(name);
        holder.setHeldClass(clazz);
        metaData.setOrigin(name + ".filter.filter-class", filterAnnotation, clazz);
        holder.setDisplayName(filterAnnotation.displayName());
        metaData.setOrigin(name + ".filter.display-name", filterAnnotation, clazz);
        for (WebInitParam ip : filterAnnotation.initParams()) {
            holder.setInitParameter(ip.name(), ip.value());
            metaData.setOrigin(name + ".filter.init-param." + ip.name(), ip, clazz);
        }
        FilterMapping mapping = new FilterMapping();
        mapping.setFilterName(holder.getName());
        if (urlPatterns.length > 0) {
            ArrayList<String> paths = new ArrayList<String>();
            for (String s : urlPatterns) {
                paths.add(ServletPathSpec.normalize(s));
            }
            mapping.setPathSpecs(paths.toArray(new String[paths.size()]));
        }
        if (filterAnnotation.servletNames().length > 0) {
            ArrayList<String> names = new ArrayList<String>();
            for (String s : filterAnnotation.servletNames()) {
                names.add(s);
            }
            mapping.setServletNames(names.toArray(new String[names.size()]));
        }
        EnumSet<DispatcherType> dispatcherSet = EnumSet.noneOf(DispatcherType.class);
        for (DispatcherType d : filterAnnotation.dispatcherTypes()) {
            dispatcherSet.add(d);
        }
        mapping.setDispatcherTypes(dispatcherSet);
        metaData.setOrigin(name + ".filter.mappings", filterAnnotation, clazz);
        holder.setAsyncSupported(filterAnnotation.asyncSupported());
        metaData.setOrigin(name + ".filter.async-supported", filterAnnotation, clazz);
        _context.getServletHandler().addFilter(holder);
        _context.getServletHandler().addFilterMapping(mapping);
    } else {
        //init-params of the same name.
        for (WebInitParam ip : filterAnnotation.initParams()) {
            //if (holder.getInitParameter(ip.name()) == null)
            if (metaData.getOrigin(name + ".filter.init-param." + ip.name()) == Origin.NotSet) {
                holder.setInitParameter(ip.name(), ip.value());
                metaData.setOrigin(name + ".filter.init-param." + ip.name(), ip, clazz);
            }
        }
        FilterMapping[] mappings = _context.getServletHandler().getFilterMappings();
        boolean mappingExists = false;
        if (mappings != null) {
            for (FilterMapping m : mappings) {
                if (m.getFilterName().equals(name)) {
                    mappingExists = true;
                    break;
                }
            }
        }
        //from the annotation
        if (!mappingExists) {
            FilterMapping mapping = new FilterMapping();
            mapping.setFilterName(holder.getName());
            if (urlPatterns.length > 0) {
                ArrayList<String> paths = new ArrayList<String>();
                for (String s : urlPatterns) {
                    paths.add(ServletPathSpec.normalize(s));
                }
                mapping.setPathSpecs(paths.toArray(new String[paths.size()]));
            }
            if (filterAnnotation.servletNames().length > 0) {
                ArrayList<String> names = new ArrayList<String>();
                for (String s : filterAnnotation.servletNames()) {
                    names.add(s);
                }
                mapping.setServletNames(names.toArray(new String[names.size()]));
            }
            EnumSet<DispatcherType> dispatcherSet = EnumSet.noneOf(DispatcherType.class);
            for (DispatcherType d : filterAnnotation.dispatcherTypes()) {
                dispatcherSet.add(d);
            }
            mapping.setDispatcherTypes(dispatcherSet);
            _context.getServletHandler().addFilterMapping(mapping);
            metaData.setOrigin(name + ".filter.mappings", filterAnnotation, clazz);
        }
    }
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) WebFilter(javax.servlet.annotation.WebFilter) ArrayList(java.util.ArrayList) FilterMapping(org.eclipse.jetty.servlet.FilterMapping) Source(org.eclipse.jetty.servlet.Source) WebFilter(javax.servlet.annotation.WebFilter) Filter(javax.servlet.Filter) MetaData(org.eclipse.jetty.webapp.MetaData) WebInitParam(javax.servlet.annotation.WebInitParam) DispatcherType(javax.servlet.DispatcherType)

Example 5 with FilterMapping

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

the class FilterMappingMBean method getObjectNameBasis.

public String getObjectNameBasis() {
    if (_managed != null && _managed instanceof FilterMapping) {
        FilterMapping mapping = (FilterMapping) _managed;
        String name = mapping.getFilterName();
        if (name != null)
            return name;
    }
    return super.getObjectNameBasis();
}
Also used : FilterMapping(org.eclipse.jetty.servlet.FilterMapping)

Aggregations

FilterMapping (org.eclipse.jetty.servlet.FilterMapping)14 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)7 ServletHandler (org.eclipse.jetty.servlet.ServletHandler)4 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 DispatcherType (javax.servlet.DispatcherType)2 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)2 ServletMapping (org.eclipse.jetty.servlet.ServletMapping)2 MetaData (org.eclipse.jetty.webapp.MetaData)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 EventListener (java.util.EventListener)1 Filter (javax.servlet.Filter)1 SessionCookieConfig (javax.servlet.SessionCookieConfig)1 SessionTrackingMode (javax.servlet.SessionTrackingMode)1 WebFilter (javax.servlet.annotation.WebFilter)1 WebInitParam (javax.servlet.annotation.WebInitParam)1 JspPropertyGroupDescriptor (javax.servlet.descriptor.JspPropertyGroupDescriptor)1 TaglibDescriptor (javax.servlet.descriptor.TaglibDescriptor)1