001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.util;
017:
018: import java.text.DateFormat;
019: import java.text.ParseException;
020: import java.text.SimpleDateFormat;
021: import java.util.Calendar;
022: import java.util.Date;
023: import java.util.Locale;
024:
025: /**
026: * Groups predefined magic HTTP header strings.
027: */
028: public final class HttpHeaders {
029:
030: public static final long MS_SEC = 1000;
031: public static final long MS_MIN = MS_SEC * 60;
032: public static final long MS_HR = MS_MIN * 60;
033: public static final long MS_DAY = MS_HR * 24;
034:
035: public static final long SEC_MIN = 60;
036: public static final long SEC_HR = SEC_MIN * 60;
037: public static final long SEC_DAY = SEC_HR * 24;
038: public static final long SEC_YR = SEC_DAY * 365;
039:
040: public static final String CACHE_CONTROL = "Cache-Control";
041: public static final String CACHE_CONTROL_MAXAGE = "max-age=";
042: public static final String CACHE_CONTROL_MUST_REVALIDATE = "must-revalidate";
043: public static final String CACHE_CONTROL_NO_CACHE = "no-cache";
044: public static final String CACHE_CONTROL_PRIVATE = "private";
045: public static final String CACHE_CONTROL_PUBLIC = "public";
046:
047: public static final String CONTENT_ENCODING = "Content-Encoding";
048: public static final String CONTENT_ENCODING_GZIP = "gzip";
049: public static final String CONTENT_LENGTH = "Content-Length";
050: public static final String CONTENT_TYPE = "Content-Type";
051: public static final String CONTENT_TYPE_APPLICATION_XJAVASCRIPT_UTF8 = "application/x-javascript; charset=utf-8";
052: public static final String CONTENT_TYPE_TEXT_CSS = "text/css";
053: public static final String CONTENT_TYPE_TEXT_HTML = "text/html";
054: public static final String CONTENT_TYPE_TEXT_HTML_UTF8 = "text/html; charset=utf-8";
055: public static final String CONTENT_TYPE_TEXT_PLAIN = "text/plain";
056:
057: public static final String DATE = "Date";
058: public static final String ETAG = "ETag";
059: public static final String EXPIRES = "Expires";
060: public static final String IF_MODIFIED_SINCE = "If-Modified-Since";
061: public static final String IF_NONE_MATCH = "If-None-Match";
062: public static final String LAST_MODIFIED = "Last-Modified";
063:
064: /**
065: * The Internet date format for HTTP.
066: */
067: private static DateFormat sHttpDateFormat = new SimpleDateFormat(
068: "EEE, d MMM yyyy HH:mm:ss", Locale.US);
069:
070: /**
071: * Converts an HTTP date string into a file-style date/time.
072: */
073: public static long fromInternetDateFormat(String timeStr) {
074: Date dateGmt;
075: try {
076: synchronized (sHttpDateFormat) {
077: dateGmt = sHttpDateFormat.parse(timeStr);
078: }
079: } catch (ParseException e) {
080: return 0;
081: }
082: dateGmt = gmtToDate(dateGmt);
083: return dateGmt.getTime();
084: }
085:
086: /**
087: * Converts a file-style date/time into a string form that is compatible with
088: * HTTP.
089: */
090: public static String toInternetDateFormat(long time) {
091: Date date = dateToGMT(new Date(time));
092: String dateGmt;
093: synchronized (sHttpDateFormat) {
094: dateGmt = sHttpDateFormat.format(date) + " GMT";
095: }
096: return dateGmt;
097: }
098:
099: /**
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 dateToGMT(Date date) {
106: Calendar cal = Calendar.getInstance();
107: long tzMillis = cal.get(Calendar.ZONE_OFFSET)
108: + cal.get(Calendar.DST_OFFSET);
109: return new Date(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 gmtToDate(Date date) {
119: Calendar cal = Calendar.getInstance();
120: long tzMillis = cal.get(Calendar.ZONE_OFFSET)
121: + cal.get(Calendar.DST_OFFSET);
122: return new Date(date.getTime() + tzMillis);
123: }
124: }
|