File Source: ModDateHeaderUtil.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * 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. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */
18
19 package org.apache.roller.weblogger.ui.rendering.util;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 /**
28 * Utility class to localize the modification date header-related logic.
29 */
30 public class ModDateHeaderUtil {
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil__static_init
*
* Postconditions:
* init'ed(log)
*/
31 private static final Log log = LogFactory.getLog(ModDateHeaderUtil.class);
32
33 // Utility class with static methods; inhibit construction
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil()
*/
34 private ModDateHeaderUtil() {
35
36 }
37
38 /**
39 * Sets the HTTP response status to 304 (NOT MODIFIED) if the request contains an
40 * If-Modified-Since header that specifies a time that is
41 * at or after the time specified by the value of lastModifiedTimeMillis
42 * <em>truncated to second granularity</em>. Returns true if
43 * the response status was set, false if not.
44 *
45 * @param request
46 * @param response
47 * @param lastModifiedTimeMillis
48 * @return true if a response status was sent, false otherwise.
49 */
50 public static boolean respondIfNotModified(HttpServletRequest request,
51 HttpServletResponse response,
52 long lastModifiedTimeMillis) {
/*
P/P * Method: bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)
*
* Preconditions:
* (soft) lastModifiedTimeMillis%1_000 - lastModifiedTimeMillis in -264+1..263
* (soft) request != null
* (soft) response != null
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@31 != null
*
* Postconditions:
* init'ed(return_value)
*/
53 long sinceDate = 0;
54 try {
55 sinceDate = request.getDateHeader("If-Modified-Since");
56 } catch(IllegalArgumentException ex) {
57 // this indicates there was some problem parsing the header value as a date
58 return false;
59 }
60
61 // truncate to seconds
62 lastModifiedTimeMillis -= (lastModifiedTimeMillis % 1000);
63 log.debug("since date = " + sinceDate);
64 log.debug("last mod date (trucated to seconds) = " + lastModifiedTimeMillis);
65 if (lastModifiedTimeMillis <= sinceDate) {
66 log.debug("NOT MODIFIED " + request.getRequestURL());
67 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
68 return true;
69 } else {
70 return false;
71 }
72 }
73
74 /**
75 * Set the Last-Modified header using the given time in milliseconds. Note that because the
76 * header has the granularity of one second, the value will get truncated to the nearest second that does not
77 * exceed the provided value.
78 * <p/>
79 * This will also set the Expires header to a date in the past. This forces clients to revalidate the cache each
80 * time.
81 *
82 * @param response
83 * @param lastModifiedTimeMillis
84 */
85 public static void setLastModifiedHeader(HttpServletResponse response, long lastModifiedTimeMillis) {
/*
P/P * Method: void setLastModifiedHeader(HttpServletResponse, long)
*
* Preconditions:
* response != null
*/
86 response.setDateHeader("Last-Modified", lastModifiedTimeMillis);
87 // Force clients to revalidate each time
88 // See RFC 2616 (HTTP 1.1 spec) secs 14.21, 13.2.1
89 response.setDateHeader("Expires", 0);
90 // We may also want this (See 13.2.1 and 14.9.4)
91 // response.setHeader("Cache-Control","must-revalidate");
92 }
93 }
SofCheck Inspector Build Version : 2.18479
| ModDateHeaderUtil.java |
2009-Jan-02 14:24:58 |
| ModDateHeaderUtil.class |
2009-Sep-04 03:12:45 |