001/*
002 * (C) Copyright 2011 Nuxeo SAS (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 *     tdelprat, jcarsique
016 *
017 */
018
019package org.nuxeo.wizard;
020
021import java.io.IOException;
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;
029import javax.servlet.http.HttpServletRequest;
030
031/**
032 * Filter that let the default servlet handle the resources and forward processing calls to the servlet
033 *
034 * @author Tiry (tdelprat@nuxeo.com)
035 * @since 5.4.2
036 */
037public class ResourceFilter implements Filter {
038
039    protected String[] resourcesPrefix = { "/css/", "/images/", "/scripts/", "/jsp/" };
040
041    protected boolean isResourceCall(String uri) {
042        for (String prefix : resourcesPrefix) {
043            if (uri.startsWith(prefix)) {
044                return true;
045            }
046        }
047        return false;
048    }
049
050    @Override
051    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
052            ServletException {
053
054        HttpServletRequest httpRequest = (HttpServletRequest) request;
055
056        String uri = httpRequest.getRequestURI();
057        uri = uri.replaceFirst(httpRequest.getContextPath(), "");
058
059        if (!isResourceCall(uri)) {
060            request.getRequestDispatcher("/router" + uri).forward(request, response);
061        } else {
062            chain.doFilter(httpRequest, response);
063        }
064    }
065
066    @Override
067    public void init(FilterConfig filterConfig) throws ServletException {
068    }
069
070    @Override
071    public void destroy() {
072    }
073
074}