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