KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > util > ModDateHeaderUtil


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.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 JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 /**
28  * Utility class to localize the modification date header-related logic.
29  */

30 public class ModDateHeaderUtil {
31     private static final Log log = LogFactory.getLog(ModDateHeaderUtil.class);
32
33     // Utility class with static methods; inhibit construction
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 JavaDoc request,
51                                                HttpServletResponse JavaDoc response,
52                                                long lastModifiedTimeMillis) {
53         long sinceDate = request.getDateHeader("If-Modified-Since");
54         // truncate to seconds
55
lastModifiedTimeMillis -= (lastModifiedTimeMillis % 1000);
56         log.debug("since date = " + sinceDate);
57         log.debug("last mod date (trucated to seconds) = " + lastModifiedTimeMillis);
58         if (lastModifiedTimeMillis <= sinceDate) {
59             log.debug("NOT MODIFIED " + request.getRequestURL());
60             response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
61             return true;
62         } else {
63             return false;
64         }
65     }
66
67     /**
68      * Set the Last-Modified header using the given time in milliseconds. Note that because the
69      * header has the granularity of one second, the value will get truncated to the nearest second that does not
70      * exceed the provided value.
71      * <p/>
72      * This will also set the Expires header to a date in the past. This forces clients to revalidate the cache each
73      * time.
74      *
75      * @param response
76      * @param lastModifiedTimeMillis
77      */

78     public static void setLastModifiedHeader(HttpServletResponse JavaDoc response, long lastModifiedTimeMillis) {
79         response.setDateHeader("Last-Modified", lastModifiedTimeMillis);
80         // Force clients to revalidate each time
81
// See RFC 2616 (HTTP 1.1 spec) secs 14.21, 13.2.1
82
response.setDateHeader("Expires", 0);
83         // We may also want this (See 13.2.1 and 14.9.4)
84
// response.setHeader("Cache-Control","must-revalidate");
85
}
86 }
87
Popular Tags