package com.edify.web.servlet;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @author jarias
 * @since 6/2/12 7:10 PM
 */
public class EnvironmentInterceptor extends HandlerInterceptorAdapter {
    public static final String ENVIRONMENT_PARAM_NAME = "env";
    public static final String ASSETS_CACHE_BUSTER_PARAM_NAME = "cacheBuster";

    //Use for cache busting
    @Value("${GIT_SHA}")
    private String gitSha;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        WebApplicationContext webApplicationContext = RequestContextUtils.getWebApplicationContext(request);
        String[] activeProfiles = webApplicationContext.getEnvironment().getActiveProfiles();

        if (ArrayUtils.contains(activeProfiles, "production")) {
            request.setAttribute(ENVIRONMENT_PARAM_NAME, "production");
            request.setAttribute(ASSETS_CACHE_BUSTER_PARAM_NAME, gitSha);
        } else {
            request.setAttribute(ENVIRONMENT_PARAM_NAME, "development");
            //In development set the cache buster to a random UUID to avoid caching of static assets
            request.getSession().setAttribute(ASSETS_CACHE_BUSTER_PARAM_NAME, UUID.randomUUID());
        }
        return true;
    }
}

@@ -68,16 +68,75 @@
 yUtils;%0A
+import org.springframework.beans.factory.InitializingBean;%0A
 import o
@@ -186,16 +186,16 @@
 .Value;%0A
-
 import o
@@ -629,16 +629,44 @@
 Adapter 
+implements InitializingBean 
 %7B%0A    pu
@@ -855,16 +855,16 @@
 _SHA%7D%22)%0A
-
     priv
@@ -883,16 +883,404 @@
 itSha;%0A%0A
+    @Override%0A    public void afterPropertiesSet() throws Exception %7B%0A        /*%0A        Seems like in Heroku there is no way to get the GIT SHA commit%0A        So we set it to a random UUID each deploy would be a different value so it works in similar fashion as the git sha%0A        */%0A        if (gitSha.equals(%221%22)) %7B%0A            gitSha = UUID.randomUUID().toString();%0A        %7D%0A    %7D%0A%0A
     @Ove

Fix cache buster for JS/CSS for heroku deployments