1 package org.apache.turbine.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.text.SimpleDateFormat;
20
21 import java.util.Date;
22 import java.util.Locale;
23 import java.util.TimeZone;
24
25 /***
26 * This class provides utilities for handling some semi-trivial HTTP stuff that
27 * would othterwise be handled elsewhere.
28 *
29 * @author <a href="mailto:magnus@handpoint.com">Magnús Þór Torfason</a>
30 * @version $Id: HttpUtils.java 278958 2005-09-06 09:35:39Z henning $
31 */
32 public class HttpUtils
33 {
34 /***
35 * The date format to use for HTTP Dates.
36 */
37 private static SimpleDateFormat httpDateFormat;
38
39 static
40 {
41 httpDateFormat = new SimpleDateFormat(
42 "EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
43 httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
44 }
45
46 /***
47 * Formats a java Date according to rfc 1123, the rfc standard for dates in
48 * http.
49 *
50 * @param date The Date to format
51 * @return A String represeentation of the date
52 */
53 public static String formatHttpDate(Date date)
54 {
55 synchronized (httpDateFormat)
56 {
57 return httpDateFormat.format(date);
58 }
59 }
60
61 /***
62 * This method sets the required expiration headers in the response for a
63 * given RunData object. This method attempts to set all relevant headers,
64 * both for HTTP 1.0 and HTTP 1.1.
65 *
66 * @param data The RunData object we are setting cache information for.
67 * @param expiry The number of seconds untill the document should expire,
68 * <code>0</code> indicating immediate expiration (i.e. no caching).
69 */
70 public static void setCacheHeaders(RunData data, int expiry)
71 {
72 if (0 == expiry)
73 {
74 data.getResponse().setHeader("Pragma", "no-cache");
75 data.getResponse().setHeader("Cache-Control", "no-cache");
76 data.getResponse().setHeader("Expires",
77 formatHttpDate(new Date()));
78 }
79 else
80 {
81 Date expiryDate = new Date(System.currentTimeMillis() + expiry);
82 data.getResponse().setHeader("Expires",
83 formatHttpDate(expiryDate));
84 }
85 }
86
87 }