KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > limiter > ConditionalGetPlugin


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.plugin.limiter;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.blojsom.blog.Blog;
36 import org.blojsom.blog.Comment;
37 import org.blojsom.blog.Entry;
38 import org.blojsom.plugin.Plugin;
39 import org.blojsom.plugin.PluginException;
40 import org.blojsom.util.BlojsomUtils;
41
42 import javax.servlet.http.HttpServletRequest JavaDoc;
43 import javax.servlet.http.HttpServletResponse JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Date JavaDoc;
46 import java.util.Map JavaDoc;
47
48 /**
49  * ConditionalGetPlugin
50  *
51  * @author David Czarnecki
52  * @version $Id: ConditionalGetPlugin.java,v 1.2 2006/03/20 22:50:43 czarneckid Exp $
53  * @since blojsom 3.0
54  */

55 public class ConditionalGetPlugin implements Plugin {
56
57     private Log _logger = LogFactory.getLog(ConditionalGetPlugin.class);
58
59     private static final String JavaDoc IF_MODIFIED_SINCE_HEADER = "If-Modified-Since";
60     private static final String JavaDoc IF_NONE_MATCH_HEADER = "If-None-Match";
61
62     /**
63      * Default constructor.
64      */

65     public ConditionalGetPlugin() {
66     }
67
68     /**
69      * Initialize this plugin. This method only called when the plugin is instantiated.
70      *
71      * @throws PluginException If there is an error initializing the plugin
72      */

73     public void init() throws PluginException {
74     }
75
76     /**
77      * Process the blog entries
78      *
79      * @param httpServletRequest Request
80      * @param httpServletResponse Response
81      * @param blog {@link Blog} instance
82      * @param context Context
83      * @param entries Blog entries retrieved for the particular request
84      * @return Modified set of blog entries
85      * @throws PluginException If there is an error processing the blog entries
86      */

87     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
88         if (entries.length > 0) {
89             long ifModifiedSinceHeader;
90             try {
91                 ifModifiedSinceHeader = httpServletRequest.getDateHeader(IF_MODIFIED_SINCE_HEADER);
92             } catch (Exception JavaDoc e) {
93                 ifModifiedSinceHeader = -1;
94             }
95
96             // Check first to see if neither of the headers we need to check are present
97
if ((ifModifiedSinceHeader == -1) && (httpServletRequest.getHeader(IF_NONE_MATCH_HEADER) == null)) {
98                 if (_logger.isDebugEnabled()) {
99                     _logger.debug("No If-Modified-Since or If-None-Match HTTP headers present.");
100                 }
101             } else {
102                 ArrayList JavaDoc datesToCheck = new ArrayList JavaDoc(5);
103                 Date JavaDoc[] dates;
104                 Comment[] comments;
105                 Comment comment;
106
107                 // If there are comments, add those to the dates to check, latest date first
108
if (entries[0].getNumComments() > 0) {
109                     comments = entries[0].getCommentsAsArray();
110                     for (int i = comments.length - 1; i >= 0; i--) {
111                         comment = comments[i];
112                         datesToCheck.add(comment.getCommentDate());
113                     }
114                 }
115
116                 // And add the date for the latest entry
117
datesToCheck.add(entries[0].getDate());
118
119                 dates = (Date JavaDoc[]) datesToCheck.toArray(new Date JavaDoc[datesToCheck.size()]);
120
121                 // Cache the If-Modified-Since and If-None-Match headers since they will not change
122
Date JavaDoc ifModifiedSinceDate = null;
123                 try {
124                     ifModifiedSinceHeader = httpServletRequest.getDateHeader(IF_MODIFIED_SINCE_HEADER);
125                 } catch (Exception JavaDoc e) {
126                     ifModifiedSinceHeader = -1;
127                 }
128
129                 if (ifModifiedSinceHeader != -1) {
130                     ifModifiedSinceDate = new Date JavaDoc(ifModifiedSinceHeader);
131                 }
132
133                 String JavaDoc ifNoneMatchHeader = null;
134                 if (httpServletRequest.getHeader(IF_NONE_MATCH_HEADER) != null) {
135                     ifNoneMatchHeader = httpServletRequest.getHeader(IF_NONE_MATCH_HEADER);
136                 }
137
138                 // Check the dates to see if anything matches
139
Date JavaDoc latestEntryDate;
140                 for (int i = 0; i < dates.length; i++) {
141                     latestEntryDate = dates[i];
142
143                     if (ifModifiedSinceDate != null) {
144                         if (latestEntryDate.toString().equals(ifModifiedSinceDate.toString())) {
145                             if (_logger.isDebugEnabled()) {
146                                 _logger.debug("Returning 304 response based on If-Modified-Since header");
147                             }
148                             httpServletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
149                             break;
150                         } else {
151                             if (_logger.isDebugEnabled()) {
152                                 _logger.debug("Latest entry date/If-Modified-Since date: " + latestEntryDate.toString() + "/" + ifModifiedSinceDate.toString());
153                             }
154                         }
155                     } else if (ifNoneMatchHeader != null) {
156                         String JavaDoc calculatedIfNoneMatchHeader = "\"" + BlojsomUtils.digestString(BlojsomUtils.getISO8601Date(latestEntryDate)) + "\"";
157                         if (ifNoneMatchHeader.equals(calculatedIfNoneMatchHeader)) {
158                             if (_logger.isDebugEnabled()) {
159                                 _logger.debug("Returning 304 response based on If-None-Match header");
160                             }
161                             httpServletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
162                             break;
163                         } else {
164                             if (_logger.isDebugEnabled()) {
165                                 _logger.debug("Calculated ETag/If-None-Match ETag: " + calculatedIfNoneMatchHeader + "/" + ifNoneMatchHeader);
166                             }
167                         }
168                     } else {
169                         if (_logger.isDebugEnabled()) {
170                             _logger.debug("No If-Modified-Since or If-None-Match HTTP headers present and not caught in initial check.");
171                         }
172                     }
173                 }
174             }
175         }
176
177         return entries;
178     }
179
180     /**
181      * Perform any cleanup for the plugin. Called after {@link #process}.
182      *
183      * @throws PluginException If there is an error performing cleanup for this plugin
184      */

185     public void cleanup() throws PluginException {
186     }
187
188     /**
189      * Called when BlojsomServlet is taken out of service
190      *
191      * @throws PluginException If there is an error in finalizing this plugin
192      */

193     public void destroy() throws PluginException {
194     }
195 }
196
Popular Tags