001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
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-2.1.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 *     Thomas Roger
016 */
017
018package org.nuxeo.dam.filter;
019
020import java.io.IOException;
021
022import javax.servlet.Filter;
023import javax.servlet.FilterChain;
024import javax.servlet.FilterConfig;
025import javax.servlet.ServletException;
026import javax.servlet.ServletRequest;
027import javax.servlet.ServletResponse;
028import javax.servlet.http.HttpServletRequest;
029
030/**
031 * Filter to redirect "/dam/icons/*" resources to "/icons/" for backward compatibility.
032 *
033 * @since 5.7
034 */
035public class DamOldResourcesFilter implements Filter {
036
037    @Override
038    public void init(FilterConfig filterConfig) throws ServletException {
039    }
040
041    @Override
042    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
043            ServletException {
044        if (request instanceof HttpServletRequest) {
045            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
046            String contextPath = httpServletRequest.getContextPath();
047            String requestURI = httpServletRequest.getRequestURI();
048            requestURI = requestURI.replaceFirst(contextPath, "");
049            requestURI = requestURI.replace("/dam/icons", "/icons");
050            httpServletRequest.getRequestDispatcher(requestURI).forward(request, response);
051        }
052    }
053
054    @Override
055    public void destroy() {
056    }
057}