View Javadoc

1   package org.apache.turbine.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "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      /***
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  }