KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > HttpUtils


1 package org.apache.turbine.util;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.text.SimpleDateFormat JavaDoc;
20
21 import java.util.Date JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.TimeZone JavaDoc;
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,v 1.4.2.4 2004/08/03 05:16:14 seade Exp $
31  */

32 public class HttpUtils
33 {
34     /**
35      * The date format to use for HTTP Dates.
36      */

37     private static SimpleDateFormat JavaDoc httpDateFormat;
38
39     static
40     {
41         httpDateFormat = new SimpleDateFormat JavaDoc(
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 JavaDoc formatHttpDate(Date JavaDoc 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 JavaDoc()));
78         }
79         else
80         {
81             Date JavaDoc expiryDate = new Date JavaDoc(System.currentTimeMillis() + expiry);
82             data.getResponse().setHeader("Expires",
83                     formatHttpDate(expiryDate));
84         }
85     }
86
87 }
88
Popular Tags