001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.webengine.jaxrs.servlet;
020
021import java.util.Collections;
022import java.util.Enumeration;
023import java.util.List;
024
025import javax.servlet.Filter;
026import javax.servlet.FilterConfig;
027import javax.servlet.ServletConfig;
028import javax.servlet.ServletContext;
029import javax.servlet.ServletException;
030
031import org.nuxeo.ecm.webengine.jaxrs.BundleNotFoundException;
032import org.nuxeo.ecm.webengine.jaxrs.servlet.config.FilterDescriptor;
033import org.nuxeo.ecm.webengine.jaxrs.servlet.config.FilterSetDescriptor;
034import org.nuxeo.ecm.webengine.jaxrs.servlet.mapping.Path;
035
036/**
037 * A filter set is a collections of filters that should be run for a given request in a servlet context.
038 * <p>
039 * The filter set is selected when it match the current pathInfo of the request. Only one filter set can match a given
040 * path - the first one which is matching will be used, all the other filter sets defined by a servlet will be ignored.
041 *
042 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
043 */
044public class FilterSet {
045
046    protected FilterSetDescriptor descriptor;
047
048    private Filter[] filters;
049
050    public FilterSet(FilterSetDescriptor descriptor) {
051        this.descriptor = descriptor;
052    }
053
054    public boolean matches(String pathInfo) {
055        return descriptor.matches(pathInfo);
056    }
057
058    public boolean matches(Path pathInfo) {
059        return descriptor.matches(pathInfo);
060    }
061
062    public Filter[] getFilters() {
063        return filters;
064    }
065
066    public void init(ServletConfig config) throws ServletException {
067        try {
068            List<FilterDescriptor> fds = descriptor.getFilters();
069            filters = new Filter[fds.size()];
070            for (int i = 0, len = fds.size(); i < len; i++) {
071                FilterDescriptor fd = fds.get(i);
072                Filter filter = fd.getFilter();
073                filter.init(new FilterConfigAdapter(fd, config));
074                filters[i] = filter;
075            }
076        } catch (ReflectiveOperationException | BundleNotFoundException e) {
077            throw new ServletException("Failed to initialize filter set", e);
078        }
079    }
080
081    public void destroy() {
082        for (Filter filter : filters) {
083            filter.destroy();
084        }
085        descriptor = null;
086        filters = null;
087    }
088
089    static class FilterConfigAdapter implements FilterConfig {
090        protected final ServletConfig config;
091
092        protected final FilterDescriptor fd;
093
094        public FilterConfigAdapter(FilterDescriptor fd, ServletConfig config) {
095            this.fd = fd;
096            this.config = config;
097        }
098
099        @Override
100        public String getFilterName() {
101            return fd.getRawClassRef();
102        }
103
104        @Override
105        public String getInitParameter(String key) {
106            return fd.getInitParams().get(key);
107        }
108
109        @Override
110        public Enumeration<String> getInitParameterNames() {
111            return Collections.enumeration(fd.getInitParams().keySet());
112        }
113
114        @Override
115        public ServletContext getServletContext() {
116            return config.getServletContext();
117        }
118
119    }
120
121}