1 package org.apache.turbine.util;
2
3 /*
4 * Copyright (c) 1997-2000 The Java Apache Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * 3. All advertising materials mentioning features or use of this
19 * software must display the following acknowledgment:
20 * "This product includes software developed by the Java Apache
21 * Project for use in the Apache JServ servlet engine project
22 * <http://java.apache.org/>."
23 *
24 * 4. The names "Apache JServ", "Apache JServ Servlet Engine", "Turbine",
25 * "Apache Turbine", "Turbine Project", "Apache Turbine Project" and
26 * "Java Apache Project" must not be used to endorse or promote products
27 * derived from this software without prior written permission.
28 *
29 * 5. Products derived from this software may not be called "Apache JServ"
30 * nor may "Apache" nor "Apache JServ" appear in their names without
31 * prior written permission of the Java Apache Project.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the Java Apache
36 * Project for use in the Apache JServ servlet engine project
37 * <http://java.apache.org/>."
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JAVA APACHE PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 *
52 * This software consists of voluntary contributions made by many
53 * individuals on behalf of the Java Apache Group. For more information
54 * on the Java Apache Project and the Apache JServ Servlet Engine project,
55 * please see <http://java.apache.org/>.
56 *
57 */
58
59 import java.text.SimpleDateFormat;
60
61 import java.util.Date;
62 import java.util.Locale;
63 import java.util.TimeZone;
64
65 import org.apache.turbine.util.RunData;
66
67 /***
68 * This class provides utilities for handling some semi-trivial
69 * HTTP stuff that would othterwize be handled elsewhere.
70 *
71 * @author <a href="mailto:magnus@handpoint.com">Magnús Þór Torfason</a>
72 */
73 public class HttpUtils
74 {
75 /***
76 * The date format to use for HTTP Dates.
77 */
78 private static SimpleDateFormat httpDateFormat;
79
80 static
81 {
82 httpDateFormat = new SimpleDateFormat(
83 "EEE, dd MMM yyyyy HH:mm:ss z", Locale.US );
84 httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
85 }
86
87 /***
88 * Formats a java Date according to rfc 1123, the rfc
89 * standard for dates in http.
90 *
91 * @param date The Date to format
92 * @return A String represeentation of the date
93 */
94 public static String formatHttpDate(Date date)
95 {
96 synchronized (httpDateFormat)
97 {
98 return httpDateFormat.format(date);
99 }
100 }
101
102 /***
103 * This method sets the required expiration headers in the response
104 * for a given RunData object. This method attempts to set all
105 * relevant headers, both for HTTP 1.0 and HTTP 1.1.
106 *
107 * @param data The RunData object we are setting cache information for.
108 * @param expiry The number of seconds untill the document should expire,
109 * <code>0</code> indicating immediate expiration (i.e. no caching).
110 */
111 public static void setCacheHeaders(RunData data, int expiry)
112 {
113 if ( expiry == 0 )
114 {
115 data.getResponse().setHeader("Pragma", "no-cache");
116 data.getResponse().setHeader("Cache-Control", "no-cache");
117 data.getResponse().setHeader(
118 "Expires", formatHttpDate(new Date()));
119 }
120 else
121 {
122 Date expiryDate = new Date( System.currentTimeMillis() + expiry );
123 data.getResponse().setHeader(
124 "Expires", formatHttpDate(expiryDate));
125 }
126 }
127 }
This page was automatically generated by Maven