001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.webengine.jaxrs.servlet;
013
014import java.util.Collections;
015import java.util.Enumeration;
016import java.util.List;
017
018import javax.servlet.Filter;
019import javax.servlet.FilterConfig;
020import javax.servlet.ServletConfig;
021import javax.servlet.ServletContext;
022import javax.servlet.ServletException;
023
024import org.nuxeo.ecm.webengine.jaxrs.BundleNotFoundException;
025import org.nuxeo.ecm.webengine.jaxrs.servlet.config.FilterDescriptor;
026import org.nuxeo.ecm.webengine.jaxrs.servlet.config.FilterSetDescriptor;
027import org.nuxeo.ecm.webengine.jaxrs.servlet.mapping.Path;
028
029/**
030 * A filter set is a collections of filters that should be run for a given request in a servlet context.
031 * <p>
032 * The filter set is selected when it match the current pathInfo of the request. Only one filter set can match a given
033 * path - the first one which is matching will be used, all the other filter sets defined by a servlet will be ignored.
034 *
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037public class FilterSet {
038
039    protected FilterSetDescriptor descriptor;
040
041    private Filter[] filters;
042
043    public FilterSet(FilterSetDescriptor descriptor) {
044        this.descriptor = descriptor;
045    }
046
047    public boolean matches(String pathInfo) {
048        return descriptor.matches(pathInfo);
049    }
050
051    public boolean matches(Path pathInfo) {
052        return descriptor.matches(pathInfo);
053    }
054
055    public Filter[] getFilters() {
056        return filters;
057    }
058
059    public void init(ServletConfig config) throws ServletException {
060        try {
061            List<FilterDescriptor> fds = descriptor.getFilters();
062            filters = new Filter[fds.size()];
063            for (int i = 0, len = fds.size(); i < len; i++) {
064                FilterDescriptor fd = fds.get(i);
065                Filter filter = fd.getFilter();
066                filter.init(new FilterConfigAdapter(fd, config));
067                filters[i] = filter;
068            }
069        } catch (ReflectiveOperationException | BundleNotFoundException e) {
070            throw new ServletException("Failed to initialize filter set", e);
071        }
072    }
073
074    public void destroy() {
075        for (Filter filter : filters) {
076            filter.destroy();
077        }
078        descriptor = null;
079        filters = null;
080    }
081
082    static class FilterConfigAdapter implements FilterConfig {
083        protected final ServletConfig config;
084
085        protected final FilterDescriptor fd;
086
087        public FilterConfigAdapter(FilterDescriptor fd, ServletConfig config) {
088            this.fd = fd;
089            this.config = config;
090        }
091
092        @Override
093        public String getFilterName() {
094            return fd.getRawClassRef();
095        }
096
097        @Override
098        public String getInitParameter(String key) {
099            return fd.getInitParams().get(key);
100        }
101
102        @Override
103        public Enumeration<String> getInitParameterNames() {
104            return Collections.enumeration(fd.getInitParams().keySet());
105        }
106
107        @Override
108        public ServletContext getServletContext() {
109            return config.getServletContext();
110        }
111
112    }
113
114}