/*
 * Copyright 2010 Kantega AS
 *
 * 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 no.kantega.publishing.admin.taglib.expires;

import no.kantega.commons.taglib.expires.DigestPrettyPrinter;
import no.kantega.commons.taglib.expires.ResourceKeyProvider;
import no.kantega.publishing.common.Aksess;
import no.kantega.publishing.spring.RuntimeMode;
import org.springframework.beans.factory.InitializingBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Returns an MD5 hash of the build number (SCM revision) or current time stamp if in development mode.
 */
public class BuildNumberResourceKeyProvider implements ResourceKeyProvider, InitializingBean {

    private RuntimeMode runtimeMode;
    private String key;

    public String getUniqueKey(HttpServletRequest request, HttpServletResponse response, String url) {
        if (runtimeMode == RuntimeMode.DEVELOPMENT) {
            return Long.toString(System.currentTimeMillis());
        } else {
            return key;
        }
    }

    public void afterPropertiesSet() throws Exception {
        try {
            MessageDigest dig = MessageDigest.getInstance("MD5");
            dig.update(Aksess.getWebappRevision().getBytes("utf-8"));
            key = DigestPrettyPrinter.prettyPrintDigest(dig.digest()).substring(0, 10);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public void setRuntimeMode(RuntimeMode runtimeMode) {
        this.runtimeMode = runtimeMode;
    }
}

@@ -916,16 +916,113 @@
 ngBean;%0A
+import org.springframework.web.context.ServletContextAware;%0A%0Aimport javax.servlet.ServletContext;
 %0Aimport 
@@ -1126,32 +1126,172 @@
 .io.
-UnsupportedEncodingExcep
+File;%0Aimport java.io.IOException;%0Aimport java.io.UnsupportedEncodingException;%0Aimport java.net.MalformedURLException;%0Aimport java.net.URL;%0Aimport java.net.URLConnec
 tion
@@ -1580,16 +1580,37 @@
 zingBean
+, ServletContextAware
  %7B%0A%0A    
@@ -1665,16 +1665,59 @@
 ing key;
+%0A    private ServletContext servletContext;
 %0A%0A    pu
@@ -1883,57 +1883,594 @@
     
-return Long.toString(System.currentTimeMillis());
+%0A            %0A            try %7B%0A                // Try as servlet context resource first%0A                URL resource = servletContext.getResource(url);%0A                if(resource != null) %7B%0A                    return Long.toString(resource.openConnection().getLastModified());%0A                %7D else %7B%0A                    return Long.toString(System.currentTimeMillis());%0A                %7D%0A            %7D catch (MalformedURLException e) %7B%0A                throw new RuntimeException(e);%0A            %7D catch (IOException e) %7B%0A                throw new RuntimeException(e);%0A            %7D%0A
 %0A   
@@ -3122,11 +3122,131 @@
 e;%0A    %7D
+%0A%0A    public void setServletContext(ServletContext servletContext) %7B%0A        this.servletContext = servletContext;%0A    %7D
 %0A%7D%0A

Use the lastModified time of the resource as a key when in dev mode and the resource exists (servletContext.getResource() is non null for the path).