KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > util > HttpHeaders


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

16 package com.google.gwt.dev.util;
17
18 import java.text.DateFormat JavaDoc;
19 import java.text.ParseException JavaDoc;
20 import java.text.SimpleDateFormat JavaDoc;
21 import java.util.Calendar JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Locale JavaDoc;
24
25 /**
26  * Groups predefined magic HTTP header strings.
27  */

28 public final class HttpHeaders {
29
30   public static final long MS_SEC = 1000;
31   public static final long MS_MIN = MS_SEC * 60;
32   public static final long MS_HR = MS_MIN * 60;
33   public static final long MS_DAY = MS_HR * 24;
34
35   public static final long SEC_MIN = 60;
36   public static final long SEC_HR = SEC_MIN * 60;
37   public static final long SEC_DAY = SEC_HR * 24;
38   public static final long SEC_YR = SEC_DAY * 365;
39
40   public static final String JavaDoc CACHE_CONTROL = "Cache-Control";
41   public static final String JavaDoc CACHE_CONTROL_MAXAGE = "max-age=";
42   public static final String JavaDoc CACHE_CONTROL_MUST_REVALIDATE = "must-revalidate";
43   public static final String JavaDoc CACHE_CONTROL_NO_CACHE = "no-cache";
44   public static final String JavaDoc CACHE_CONTROL_PRIVATE = "private";
45   public static final String JavaDoc CACHE_CONTROL_PUBLIC = "public";
46
47   public static final String JavaDoc CONTENT_ENCODING = "Content-Encoding";
48   public static final String JavaDoc CONTENT_ENCODING_GZIP = "gzip";
49   public static final String JavaDoc CONTENT_LENGTH = "Content-Length";
50   public static final String JavaDoc CONTENT_TYPE = "Content-Type";
51   public static final String JavaDoc CONTENT_TYPE_APPLICATION_XJAVASCRIPT_UTF8 = "application/x-javascript; charset=utf-8";
52   public static final String JavaDoc CONTENT_TYPE_TEXT_CSS = "text/css";
53   public static final String JavaDoc CONTENT_TYPE_TEXT_HTML = "text/html";
54   public static final String JavaDoc CONTENT_TYPE_TEXT_HTML_UTF8 = "text/html; charset=utf-8";
55   public static final String JavaDoc CONTENT_TYPE_TEXT_PLAIN = "text/plain";
56
57   public static final String JavaDoc DATE = "Date";
58   public static final String JavaDoc ETAG = "ETag";
59   public static final String JavaDoc EXPIRES = "Expires";
60   public static final String JavaDoc IF_MODIFIED_SINCE = "If-Modified-Since";
61   public static final String JavaDoc IF_NONE_MATCH = "If-None-Match";
62   public static final String JavaDoc LAST_MODIFIED = "Last-Modified";
63
64   /**
65    * The Internet date format for HTTP.
66    */

67   private static DateFormat JavaDoc sHttpDateFormat = new SimpleDateFormat JavaDoc(
68       "EEE, d MMM yyyy HH:mm:ss", Locale.US);
69
70   /**
71    * Converts an HTTP date string into a file-style date/time.
72    */

73   public static long fromInternetDateFormat(String JavaDoc timeStr) {
74     Date JavaDoc dateGmt;
75     try {
76       synchronized (sHttpDateFormat) {
77         dateGmt = sHttpDateFormat.parse(timeStr);
78       }
79     } catch (ParseException JavaDoc e) {
80       return 0;
81     }
82     dateGmt = gmtToDate(dateGmt);
83     return dateGmt.getTime();
84   }
85
86   /**
87    * Converts a file-style date/time into a string form that is compatible with
88    * HTTP.
89    */

90   public static String JavaDoc toInternetDateFormat(long time) {
91     Date JavaDoc date = dateToGMT(new Date JavaDoc(time));
92     String JavaDoc dateGmt;
93     synchronized (sHttpDateFormat) {
94       dateGmt = sHttpDateFormat.format(date) + " GMT";
95     }
96     return dateGmt;
97   }
98
99   /**
100    * Converts a local date to GMT.
101    *
102    * @param date the date in the local time zone
103    * @return the GMT version
104    */

105   private static Date JavaDoc dateToGMT(Date JavaDoc date) {
106     Calendar JavaDoc cal = Calendar.getInstance();
107     long tzMillis = cal.get(Calendar.ZONE_OFFSET)
108         + cal.get(Calendar.DST_OFFSET);
109     return new Date JavaDoc(date.getTime() - tzMillis);
110   }
111
112   /**
113    * Converts a GMT into a local date.
114    *
115    * @param date the date in GMT
116    * @return the the local time zone version
117    */

118   private static Date JavaDoc gmtToDate(Date JavaDoc date) {
119     Calendar JavaDoc cal = Calendar.getInstance();
120     long tzMillis = cal.get(Calendar.ZONE_OFFSET)
121         + cal.get(Calendar.DST_OFFSET);
122     return new Date JavaDoc(date.getTime() + tzMillis);
123   }
124 }
125
Popular Tags