KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > logic > BrowserCacheTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import javax.servlet.jsp.SkipPageException JavaDoc;
28 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
29 import java.io.File JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 /**
34  * <p>Provides headers for browser caching</p>
35  * <p>Note: use this class only at stand-alone JSP (like JS script page) in its
36  * first lines (before any output is given to browser).</p>
37  * <p>
38  * Allowed attrubutes are:
39  * <ul>
40  * <li>
41  * <b>cache</b> - may be <code>&quot;true&quot;</code> or
42  * <code>&quot;false&quot;</code>, or may be omitted. If omitted, then this
43  * tag will try to cache only if 'max age' global property is positive. If this
44  * attribute is used, it controlls whether to try to cache (if
45  * <code>&quot;true&quot;</code>) or not
46  * </li>
47  * </ul>
48  * </p>
49  * <p/>
50  * <p><a HREF="BrowserCache.java.htm"><i>View Source</i></a></p>
51  *
52  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
53  * @version $Revision: 1.1 $ $Date: 2005/10/20 07:18:50 $
54  * @jsp.tag name="browserCache"
55  * body-content="empty"
56  */

57 public class BrowserCacheTag extends SimpleTagSupport JavaDoc {
58
59     protected Boolean JavaDoc cache = null;
60
61
62     /**
63      * Returns whether to cache or not
64      *
65      * @return whether to cache
66      * @jsp.attribute required="false"
67      * rtexprvalue="true"
68      * type="java.lang.Boolean"
69      * description="Should the page be cached or not"
70      */

71     public Boolean JavaDoc getCache() {
72         return cache;
73     }
74
75     /**
76      * Sets whether to cache or not
77      *
78      * @param cache whether to cache
79      */

80     public void setCache(Boolean JavaDoc cache) {
81         this.cache = cache;
82     }
83
84     /**
85      * Processes the tag
86      *
87      * @throws javax.servlet.jsp.JspException if an error occurred while processing this tag
88      * @see javax.servlet.jsp.tagext.Tag#doEndTag
89      */

90
91     public void doTag() throws JspException JavaDoc {
92
93         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
94
95         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext.getResponse();
96         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
97         ServletContext JavaDoc context = pageContext.getServletContext();
98
99         int maxAge = ((Integer JavaDoc)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 JavaDoc uri = request.getRequestURI();
111             String JavaDoc contextPath = request.getContextPath();
112             if ( uri != null ) {
113                 if ( contextPath != null && !contextPath.equals("") && !contextPath.equals("/") ) {
114                     uri = uri.substring(contextPath.length());
115                 }
116                 String JavaDoc realPath = context.getRealPath(uri);
117                 if ( realPath != null ) {
118
119                     File JavaDoc file = new File JavaDoc(realPath);
120                     try {
121                         lastModified = file.lastModified();
122                     } catch ( SecurityException JavaDoc ex ) {
123                         //do nothing
124
}
125
126                     //check for entity tag
127
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                         //ETag generation
135
String JavaDoc language = "en";
136                         Locale JavaDoc locale = (Locale JavaDoc) request.getSession(true).getAttribute(Globals.LOCALE_KEY);
137                         if ( locale != null ) {
138                             language = locale.getLanguage();
139                         }
140                         String JavaDoc username = request.getRemoteUser();
141                         StringBuffer JavaDoc eTagBuffer = new StringBuffer JavaDoc("W/\"").append(language).append("-").append(username).append("-").append(lastModified).append("\"");
142                         String JavaDoc eTag = eTagBuffer.toString();
143                         response.setHeader("ETag", eTag);
144
145                         boolean conditionSatisfied = false;
146                         String JavaDoc headerValue = request.getHeader("If-None-Match");
147                         if ( headerValue != null ) {
148                             if ( !headerValue.equals("*") ) {
149                                 StringTokenizer JavaDoc commaTokenizer =
150                                         new StringTokenizer JavaDoc(headerValue, ",");
151                                 while ( !conditionSatisfied && commaTokenizer.hasMoreTokens() ) {
152                                     String JavaDoc 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 JavaDoc();
163                             }
164                         }
165                     }
166                 }
167             }
168
169             //check for setup headers
170
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