001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.limiter;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.blojsom.blog.Blog;
035: import org.blojsom.blog.Comment;
036: import org.blojsom.blog.Entry;
037: import org.blojsom.plugin.Plugin;
038: import org.blojsom.plugin.PluginException;
039: import org.blojsom.util.BlojsomUtils;
040:
041: import javax.servlet.http.HttpServletRequest;
042: import javax.servlet.http.HttpServletResponse;
043: import java.util.ArrayList;
044: import java.util.Date;
045: import java.util.Map;
046:
047: /**
048: * ConditionalGetPlugin
049: *
050: * @author David Czarnecki
051: * @version $Id: ConditionalGetPlugin.java,v 1.3 2007/01/17 02:35:10 czarneckid Exp $
052: * @since blojsom 3.0
053: */
054: public class ConditionalGetPlugin implements Plugin {
055:
056: private Log _logger = LogFactory.getLog(ConditionalGetPlugin.class);
057:
058: private static final String IF_MODIFIED_SINCE_HEADER = "If-Modified-Since";
059: private static final String IF_NONE_MATCH_HEADER = "If-None-Match";
060:
061: /**
062: * Default constructor.
063: */
064: public ConditionalGetPlugin() {
065: }
066:
067: /**
068: * Initialize this plugin. This method only called when the plugin is instantiated.
069: *
070: * @throws PluginException If there is an error initializing the plugin
071: */
072: public void init() throws PluginException {
073: }
074:
075: /**
076: * Process the blog entries
077: *
078: * @param httpServletRequest Request
079: * @param httpServletResponse Response
080: * @param blog {@link Blog} instance
081: * @param context Context
082: * @param entries Blog entries retrieved for the particular request
083: * @return Modified set of blog entries
084: * @throws PluginException If there is an error processing the blog entries
085: */
086: public Entry[] process(HttpServletRequest httpServletRequest,
087: HttpServletResponse httpServletResponse, Blog blog,
088: Map context, Entry[] entries) throws PluginException {
089: if (entries.length > 0) {
090: long ifModifiedSinceHeader;
091: try {
092: ifModifiedSinceHeader = httpServletRequest
093: .getDateHeader(IF_MODIFIED_SINCE_HEADER);
094: } catch (Exception e) {
095: ifModifiedSinceHeader = -1;
096: }
097:
098: // Check first to see if neither of the headers we need to check are present
099: if ((ifModifiedSinceHeader == -1)
100: && (httpServletRequest
101: .getHeader(IF_NONE_MATCH_HEADER) == null)) {
102: if (_logger.isDebugEnabled()) {
103: _logger
104: .debug("No If-Modified-Since or If-None-Match HTTP headers present.");
105: }
106: } else {
107: ArrayList datesToCheck = new ArrayList(5);
108: Date[] dates;
109: Comment[] comments;
110: Comment comment;
111:
112: // If there are comments, add those to the dates to check, latest date first
113: if (entries[0].getNumComments() > 0) {
114: comments = entries[0].getCommentsAsArray();
115: for (int i = comments.length - 1; i >= 0; i--) {
116: comment = comments[i];
117: datesToCheck.add(comment.getCommentDate());
118: }
119: }
120:
121: // And add the date for the latest entry
122: datesToCheck.add(entries[0].getDate());
123:
124: dates = (Date[]) datesToCheck
125: .toArray(new Date[datesToCheck.size()]);
126:
127: // Cache the If-Modified-Since and If-None-Match headers since they will not change
128: Date ifModifiedSinceDate = null;
129: try {
130: ifModifiedSinceHeader = httpServletRequest
131: .getDateHeader(IF_MODIFIED_SINCE_HEADER);
132: } catch (Exception e) {
133: ifModifiedSinceHeader = -1;
134: }
135:
136: if (ifModifiedSinceHeader != -1) {
137: ifModifiedSinceDate = new Date(
138: ifModifiedSinceHeader);
139: }
140:
141: String ifNoneMatchHeader = null;
142: if (httpServletRequest.getHeader(IF_NONE_MATCH_HEADER) != null) {
143: ifNoneMatchHeader = httpServletRequest
144: .getHeader(IF_NONE_MATCH_HEADER);
145: }
146:
147: // Check the dates to see if anything matches
148: Date latestEntryDate;
149: for (int i = 0; i < dates.length; i++) {
150: latestEntryDate = dates[i];
151:
152: if (ifModifiedSinceDate != null) {
153: if (latestEntryDate.toString().equals(
154: ifModifiedSinceDate.toString())) {
155: if (_logger.isDebugEnabled()) {
156: _logger
157: .debug("Returning 304 response based on If-Modified-Since header");
158: }
159: httpServletResponse
160: .setStatus(HttpServletResponse.SC_NOT_MODIFIED);
161: break;
162: } else {
163: if (_logger.isDebugEnabled()) {
164: _logger
165: .debug("Latest entry date/If-Modified-Since date: "
166: + latestEntryDate
167: .toString()
168: + "/"
169: + ifModifiedSinceDate
170: .toString());
171: }
172: }
173: } else if (ifNoneMatchHeader != null) {
174: String calculatedIfNoneMatchHeader = "\""
175: + BlojsomUtils
176: .digestString(BlojsomUtils
177: .getISO8601Date(latestEntryDate))
178: + "\"";
179: if (ifNoneMatchHeader
180: .equals(calculatedIfNoneMatchHeader)) {
181: if (_logger.isDebugEnabled()) {
182: _logger
183: .debug("Returning 304 response based on If-None-Match header");
184: }
185: httpServletResponse
186: .setStatus(HttpServletResponse.SC_NOT_MODIFIED);
187: break;
188: } else {
189: if (_logger.isDebugEnabled()) {
190: _logger
191: .debug("Calculated ETag/If-None-Match ETag: "
192: + calculatedIfNoneMatchHeader
193: + "/"
194: + ifNoneMatchHeader);
195: }
196: }
197: } else {
198: if (_logger.isDebugEnabled()) {
199: _logger
200: .debug("No If-Modified-Since or If-None-Match HTTP headers present and not caught in initial check.");
201: }
202: }
203: }
204: }
205: }
206:
207: return entries;
208: }
209:
210: /**
211: * Perform any cleanup for the plugin. Called after {@link #process}.
212: *
213: * @throws PluginException If there is an error performing cleanup for this plugin
214: */
215: public void cleanup() throws PluginException {
216: }
217:
218: /**
219: * Called when BlojsomServlet is taken out of service
220: *
221: * @throws PluginException If there is an error in finalizing this plugin
222: */
223: public void destroy() throws PluginException {
224: }
225: }
|