/*
 * #%L
 * fujion
 * %%
 * Copyright (C) 2008 - 2018 Regenstrief Institute, Inc.
 * %%
 * 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.
 *
 * #L%
 */
package org.fujion.theme;

import java.io.IOException;

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.springframework.web.servlet.ThemeResolver;

/**
 * Performs URL rewrites for theme-based resources.
 */
public class ThemeServletFilter implements Filter {
    
    private final ThemeResolver themeResolver = ThemeResolvers.getInstance();
    
    public ThemeServletFilter() {
    }
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
                                                                                              ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        
        String themeName = themeResolver.resolveThemeName(httpRequest);
        Theme theme = ThemeRegistry.getInstance().get(themeName);
        
        if (theme != null) {
            String originalPath = httpRequest.getPathInfo();
            String requestPath = theme.translatePath(originalPath);
            
            if (requestPath != null) {
                requestPath = requestPath.isEmpty() ? "empty/" + originalPath : requestPath;
                httpRequest.getRequestDispatcher(requestPath).forward(httpRequest, response);
                return;
            }
        }
        
        chain.doFilter(request, response);
    }
    
    @Override
    public void destroy() {
    }
    
}

@@ -1134,21 +1134,17 @@
 ilter %7B%0A
-    
 %0A
+
     priv
@@ -1213,21 +1213,17 @@
 ance();%0A
-    
 %0A
+
     publ
@@ -1246,37 +1246,33 @@
 ilter() %7B%0A    %7D%0A
-    
 %0A
+
     @Override%0A  
@@ -1345,29 +1345,25 @@
 ion %7B%0A    %7D%0A
-    
 %0A
+
     @Overrid
@@ -1482,98 +1482,8 @@
 on,%0A
-                                                                                          
     
@@ -1497,24 +1497,24 @@
 Exception %7B%0A
+
         Http
@@ -1572,25 +1572,17 @@
 equest;%0A
-        
 %0A
+
         
@@ -1711,25 +1711,17 @@
 eName);%0A
-        
 %0A
+
         
@@ -1834,16 +1834,46 @@
 stPath =
+ originalPath == null ? null :
  theme.t
@@ -1900,28 +1900,16 @@
 lPath);%0A
-            
 %0A       
@@ -2179,17 +2179,9 @@
   %7D%0A
-        
 %0A
+
     
@@ -2225,21 +2225,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     @Ove
@@ -2274,15 +2274,11 @@
 %7B%0A    %7D%0A
-    
 %0A%7D%0A

Fix NPE in theme filter when getPathInfo returns null.