001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thierry Delprat
016 */
017package org.nuxeo.apidoc.filter;
018
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.List;
022
023import javax.servlet.Filter;
024import javax.servlet.FilterChain;
025import javax.servlet.FilterConfig;
026import javax.servlet.ServletException;
027import javax.servlet.ServletRequest;
028import javax.servlet.ServletResponse;
029
030import org.nuxeo.runtime.api.Framework;
031
032public abstract class BaseApiDocFilter implements Filter {
033
034    public static final String APIDOC_FILTERS_ACTIVATED = "org.nuxeo.apidoc.activatefilter";
035
036    protected List<String> allowedConnectUrls = new ArrayList<String>();
037
038    protected Boolean activated;
039
040    protected boolean isFilterActivated() {
041        if (activated == null) {
042            // don't activate by default
043            activated = Boolean.valueOf(Framework.isBooleanPropertyTrue(APIDOC_FILTERS_ACTIVATED));
044        }
045        return activated.booleanValue();
046    }
047
048    protected abstract void internalDoFilter(ServletRequest request, ServletResponse response, FilterChain chain)
049            throws IOException, ServletException;
050
051    @Override
052    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
053            ServletException {
054
055        if (!isFilterActivated()) {
056            chain.doFilter(request, response);
057            return;
058        }
059        internalDoFilter(request, response, chain);
060    }
061
062    @Override
063    public void destroy() {
064    }
065
066    @Override
067    public void init(FilterConfig filterConfig) throws ServletException {
068    }
069
070}