1 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 ; 43 import javax.servlet.http.HttpServletResponse ; 44 import java.util.ArrayList ; 45 import java.util.Date ; 46 import java.util.Map ; 47 48 55 public class ConditionalGetPlugin implements Plugin { 56 57 private Log _logger = LogFactory.getLog(ConditionalGetPlugin.class); 58 59 private static final String IF_MODIFIED_SINCE_HEADER = "If-Modified-Since"; 60 private static final String IF_NONE_MATCH_HEADER = "If-None-Match"; 61 62 65 public ConditionalGetPlugin() { 66 } 67 68 73 public void init() throws PluginException { 74 } 75 76 87 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map 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 e) { 93 ifModifiedSinceHeader = -1; 94 } 95 96 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 datesToCheck = new ArrayList (5); 103 Date [] dates; 104 Comment[] comments; 105 Comment comment; 106 107 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 datesToCheck.add(entries[0].getDate()); 118 119 dates = (Date []) datesToCheck.toArray(new Date [datesToCheck.size()]); 120 121 Date ifModifiedSinceDate = null; 123 try { 124 ifModifiedSinceHeader = httpServletRequest.getDateHeader(IF_MODIFIED_SINCE_HEADER); 125 } catch (Exception e) { 126 ifModifiedSinceHeader = -1; 127 } 128 129 if (ifModifiedSinceHeader != -1) { 130 ifModifiedSinceDate = new Date (ifModifiedSinceHeader); 131 } 132 133 String ifNoneMatchHeader = null; 134 if (httpServletRequest.getHeader(IF_NONE_MATCH_HEADER) != null) { 135 ifNoneMatchHeader = httpServletRequest.getHeader(IF_NONE_MATCH_HEADER); 136 } 137 138 Date 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 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 185 public void cleanup() throws PluginException { 186 } 187 188 193 public void destroy() throws PluginException { 194 } 195 } 196 | Popular Tags |