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