/**
 * Copyright (C) 2014 Esup Portail http://www.esup-portail.org
 * @Author (C) 2012 Julien Gribonvald <julien.gribonvald@recia.fr>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *                 http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.esupportail.publisher.web.filter;

import java.io.IOException;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * This filter is used in production, to serve static resources generated by "grunt build".
 * <p/>
 * <p>
 * It is configured to serve resources from the "dist" directory, which is the Grunt
 * destination directory.
 * </p>
 */
@Slf4j
public class StaticResourcesProductionFilter implements Filter {

    /**
     * Pattern pour les ressources statiques
     */
    private Pattern staticResourcesPattern = Pattern.compile("^/ui/(favicon\\.ico|css/.*|fonts/.*|img/.*|js/.*|swagger-ui/.*)$");

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // Nothing to initialize
    }

    @Override
    public void destroy() {
        // Nothing to destroy
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String contextPath = ((HttpServletRequest) request).getContextPath();
        String requestURI = httpRequest.getRequestURI();
        requestURI = StringUtils.substringAfter(requestURI, contextPath);
        if (StringUtils.equals("/", requestURI) || StringUtils.equals("/ui/", requestURI) || !staticResourcesPattern.matcher(requestURI).matches()) {
            requestURI = "/ui/index.html";
        }
        String newURI = requestURI.replace("/ui/", "/dist/");
        log.debug("RequestDispatcher - setting newURI to {}", newURI);
        request.getRequestDispatcher(newURI).forward(request, response);
    }
}

@@ -1063,16 +1063,63 @@
 Request;
+%0Aimport javax.servlet.http.HttpServletResponse;
 %0A%0Aimport
@@ -2150,38 +2150,19 @@
 h = 
-((HttpServletRequest) request)
+httpRequest
 .get
@@ -2354,20 +2354,418 @@
 uestURI)
- %7C%7C 
+) %7B%0A            // Redirection permanaente vers /ui si on essaye d'acc%C3%A9der %C3%A0 /%0A            log.debug(%22Permanent redirection from / to /ui/%22);%0A            HttpServletResponse httpResponse = (HttpServletResponse) response;%0A            httpResponse.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);%0A            httpResponse.setHeader(%22Location%22, contextPath + %22/ui/%22);%0A        %7D else %7B%0A            if (
 StringUt
@@ -2859,32 +2859,36 @@
 ) %7B%0A            
+   
+ 
 requestURI = %22/u
@@ -2914,18 +2914,24 @@
     
-%7D%0A
+    %7D%0A  
         
 Stri
@@ -2922,24 +2922,26 @@
 %7D%0A          
+  
 String newUR
@@ -2978,24 +2978,28 @@
  %22/dist/%22);%0A
+    
         log.
@@ -3061,24 +3061,28 @@
 I);%0A        
+ 
+   
 request.getR
@@ -3134,16 +3134,26 @@
 ponse);%0A
+        %7D%0A
     %7D%0A%7D%0A

Fix rediection vers /ui si on essaye d'accéder à /