01: package org.apache.turbine.util;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
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: * The date format to use for HTTP Dates.
38: */
39: private static SimpleDateFormat httpDateFormat;
40:
41: static {
42: httpDateFormat = new SimpleDateFormat(
43: "EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
44: httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
45: }
46:
47: /**
48: * Formats a java Date according to rfc 1123, the rfc standard for dates in
49: * http.
50: *
51: * @param date The Date to format
52: * @return A String represeentation of the date
53: */
54: public static String formatHttpDate(Date date) {
55: synchronized (httpDateFormat) {
56: return httpDateFormat.format(date);
57: }
58: }
59:
60: /**
61: * This method sets the required expiration headers in the response for a
62: * given RunData object. This method attempts to set all relevant headers,
63: * both for HTTP 1.0 and HTTP 1.1.
64: *
65: * @param data The RunData object we are setting cache information for.
66: * @param expiry The number of seconds untill the document should expire,
67: * <code>0</code> indicating immediate expiration (i.e. no caching).
68: */
69: public static void setCacheHeaders(RunData data, int expiry) {
70: if (0 == expiry) {
71: data.getResponse().setHeader("Pragma", "no-cache");
72: data.getResponse().setHeader("Cache-Control", "no-cache");
73: data.getResponse().setHeader("Expires",
74: formatHttpDate(new Date()));
75: } else {
76: Date expiryDate = new Date(System.currentTimeMillis()
77: + expiry);
78: data.getResponse().setHeader("Expires",
79: formatHttpDate(expiryDate));
80: }
81: }
82:
83: }
|