1 16 package com.blandware.atleap.webapp.taglib.core.logic; 17 18 import com.blandware.atleap.webapp.util.core.GlobalProperties; 19 import com.blandware.atleap.webapp.util.core.WebappConstants; 20 import org.apache.struts.Globals; 21 22 import javax.servlet.ServletContext ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 import javax.servlet.jsp.JspException ; 26 import javax.servlet.jsp.PageContext ; 27 import javax.servlet.jsp.SkipPageException ; 28 import javax.servlet.jsp.tagext.SimpleTagSupport ; 29 import java.io.File ; 30 import java.util.Locale ; 31 import java.util.StringTokenizer ; 32 33 57 public class BrowserCacheTag extends SimpleTagSupport { 58 59 protected Boolean cache = null; 60 61 62 71 public Boolean getCache() { 72 return cache; 73 } 74 75 80 public void setCache(Boolean cache) { 81 this.cache = cache; 82 } 83 84 90 91 public void doTag() throws JspException { 92 93 PageContext pageContext = (PageContext ) getJspContext(); 94 95 HttpServletResponse response = (HttpServletResponse ) pageContext.getResponse(); 96 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 97 ServletContext context = pageContext.getServletContext(); 98 99 int maxAge = ((Integer )GlobalProperties.getInstance(context).getInteger(WebappConstants.CACHE_RESOURCE_MAXAGE_PROPERTY, -1)).intValue(); 100 101 boolean cache = this.cache == null ? maxAge >= 0 : this.cache.booleanValue(); 102 103 if ( !cache ) { 104 response.setHeader("Cache-Control", "no-cache,no-store,max-age=0"); 105 response.setHeader("Pragma", "no-cache"); 106 } else { 107 boolean isCache = false; 108 109 long lastModified = -1; 110 String uri = request.getRequestURI(); 111 String contextPath = request.getContextPath(); 112 if ( uri != null ) { 113 if ( contextPath != null && !contextPath.equals("") && !contextPath.equals("/") ) { 114 uri = uri.substring(contextPath.length()); 115 } 116 String realPath = context.getRealPath(uri); 117 if ( realPath != null ) { 118 119 File file = new File (realPath); 120 try { 121 lastModified = file.lastModified(); 122 } catch ( SecurityException ex ) { 123 } 125 126 if ( request.getMethod().equals("GET") && lastModified != -1 && lastModified != 0 ) { 128 isCache = true; 129 130 131 response.setHeader("Cache-Control", "public,max-age=" + maxAge); 132 response.setHeader("Pragma", ""); 133 134 String language = "en"; 136 Locale locale = (Locale ) request.getSession(true).getAttribute(Globals.LOCALE_KEY); 137 if ( locale != null ) { 138 language = locale.getLanguage(); 139 } 140 String username = request.getRemoteUser(); 141 StringBuffer eTagBuffer = new StringBuffer ("W/\"").append(language).append("-").append(username).append("-").append(lastModified).append("\""); 142 String eTag = eTagBuffer.toString(); 143 response.setHeader("ETag", eTag); 144 145 boolean conditionSatisfied = false; 146 String headerValue = request.getHeader("If-None-Match"); 147 if ( headerValue != null ) { 148 if ( !headerValue.equals("*") ) { 149 StringTokenizer commaTokenizer = 150 new StringTokenizer (headerValue, ","); 151 while ( !conditionSatisfied && commaTokenizer.hasMoreTokens() ) { 152 String currentToken = commaTokenizer.nextToken(); 153 if ( currentToken.trim().equals(eTag) ) { 154 conditionSatisfied = true; 155 } 156 } 157 } else { 158 conditionSatisfied = true; 159 } 160 if ( conditionSatisfied ) { 161 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 162 throw new SkipPageException (); 163 } 164 } 165 } 166 } 167 } 168 169 if ( !isCache ) { 171 response.setHeader("Cache-Control", "no-cache,no-store,max-age=0"); 172 response.setHeader("Pragma", "no-cache"); 173 } 174 } 175 } 176 177 } 178 | Popular Tags |