001/*
002 * (C) Copyright 2015 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 */
016package org.nuxeo.ecm.platform.ui.web.auth.service;
017
018import java.io.IOException;
019import java.util.ArrayList;
020import java.util.List;
021
022import javax.servlet.FilterChain;
023import javax.servlet.ServletException;
024import javax.servlet.ServletRequest;
025import javax.servlet.ServletResponse;
026
027import org.nuxeo.ecm.platform.ui.web.auth.NuxeoAuthenticationFilter;
028import org.nuxeo.ecm.platform.ui.web.auth.interfaces.NuxeoAuthPreFilter;
029
030public class NuxeoAuthFilterChain implements FilterChain {
031
032    protected List<NuxeoAuthPreFilter> preFilters = new ArrayList<NuxeoAuthPreFilter>();
033
034    protected NuxeoAuthenticationFilter mainFilter;
035
036    protected FilterChain standardFilterChain;
037
038    public NuxeoAuthFilterChain(List<NuxeoAuthPreFilter> preFilters, FilterChain standardFilterChain,
039            NuxeoAuthenticationFilter mainFilter) {
040        this.preFilters.addAll(preFilters);
041        this.mainFilter = mainFilter;
042        this.standardFilterChain = standardFilterChain;
043    }
044
045    @Override
046    public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
047        if (preFilters != null && !preFilters.isEmpty()) {
048            NuxeoAuthPreFilter preFilter = preFilters.remove(0);
049            preFilter.doFilter(request, response, this);
050        } else {
051            mainFilter.doFilterInternal(request, response, standardFilterChain);
052        }
053    }
054
055}