1 /* 2 * $Header: /u/cvs/Projects/EnhydraOrg/enhydra3x/Enhydra/modules/Tomcat/src/share/org/apache/tomcat/util/Attic/HttpDate.java,v 1.2 2000/02/26 02:32:27 shawn Exp $ 3 * $Revision: 1.2 $ 4 * $Date: 2000/02/26 02:32:27 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>. 59 * 60 * [Additional notices, if required by prior licensing conditions] 61 * 62 */ 63 64 65 package org.apache.tomcat.util; 66 67 import java.io.IOException; 68 import java.io.OutputStream; 69 import java.util; 70 import java.text; 71 72 /** 73 * This class can be used to efficiently parse and write an RFC 1123 74 * formatted date in an HTTP message header. Also supports reading the 75 * RFC 1036 format and ANSI C's asctime() format, as suggested by HTTP/1.0 76 * and mandated by HTTP/1.1. 77 * 78 * @author dac@eng.sun.com 79 * @author Jason Hunter [jch@eng.sun.com] 80 * @author James Todd [gonzo@eng.sun.com] 81 */ 82 83 public class HttpDate extends Ascii { 84 85 private StringManager sm = 86 StringManager.getManager(Constants.Package); 87 88 // ONLY FOR COMPAT -- KILL ASAP -- just make sure that dependant 89 // classes know what's up. ref. MimeHeaderField 90 private static final String DATESTR = "Sun, 06 Nov 1994 08:49:37 GMT"; 91 public static final int DATELEN = DATESTR.length(); 92 // END COMPAT -- DON'T FORGET TO KILL 93 94 // we force our locale here as all http dates are in english 95 private final static Locale loc = Locale.US; 96 97 // all http dates are expressed as time at GMT 98 private final static TimeZone zone = TimeZone.getTimeZone("GMT"); 99 100 // format for RFC 1123 date string -- "Sun, 06 Nov 1994 08:49:37 GMT" 101 private final static String rfc1123Pattern = 102 "EEE, dd MMM yyyyy HH:mm:ss z"; 103 104 // format for RFC 1036 date string -- "Sunday, 06-Nov-94 08:49:37 GMT" 105 private final static String rfc1036Pattern = 106 "EEEEEEEEE, dd-MMM-yy HH:mm:ss z"; 107 108 // format for C asctime() date string -- "Sun Nov 6 08:49:37 1994" 109 private final static String asctimePattern = 110 "EEE MMM d HH:mm:ss yyyyy"; 111 112 private final static SimpleDateFormat rfc1123Format = 113 new SimpleDateFormat(rfc1123Pattern, loc); 114 115 private final static SimpleDateFormat rfc1036Format = 116 new SimpleDateFormat(rfc1036Pattern, loc); 117 118 private final static SimpleDateFormat asctimeFormat = 119 new SimpleDateFormat(asctimePattern, loc); 120 121 static { 122 rfc1123Format.setTimeZone(zone); 123 rfc1036Format.setTimeZone(zone); 124 asctimeFormat.setTimeZone(zone); 125 } 126 127 // protected so that oldcookieexpiry in cookieutils can use 128 // yes, this is sloppy as crap and could stand to be done better. 129 protected Calendar calendar = new GregorianCalendar(zone, loc); 130 131 public HttpDate() { 132 calendar.setTime(new Date(System.currentTimeMillis())); 133 } 134 135 public HttpDate(long ms) { 136 calendar.setTime(new Date(ms)); 137 } 138 139 public void setTime() { 140 calendar.setTime(new Date(System.currentTimeMillis())); 141 } 142 143 public void setTime(long ms) { 144 calendar.setTime(new Date(ms)); 145 } 146 147 public void parse(String dateString) { 148 try { 149 Date date = rfc1123Format.parse(dateString); 150 calendar.setTime(date); 151 return; 152 } catch (ParseException e) { } 153 154 try { 155 Date date = rfc1036Format.parse(dateString); 156 calendar.setTime(date); 157 return; 158 } catch (ParseException e) { } 159 160 try { 161 Date date = asctimeFormat.parse(dateString); 162 calendar.setTime(date); 163 return; 164 } catch (ParseException pe) { 165 String msg = sm.getString("httpDate.pe", dateString); 166 167 throw new IllegalArgumentException(msg); 168 } 169 } 170 171 public void parse(byte[] b, int off, int len) { 172 // ok -- so this is pretty stoopid, but the old version of this 173 // source took this arg set, so we will too for now (backwards compat) 174 String dateString = new String(b, off, len); 175 parse(dateString); 176 } 177 178 public void write(OutputStream out) throws IOException { 179 String dateString = rfc1123Format.format(calendar.getTime()); 180 byte[] b = dateString.getBytes(); 181 out.write(b); 182 } 183 184 public String toString() { 185 return rfc1123Format.format(calendar.getTime()); 186 } 187 188 public long getTime() { 189 return calendar.getTime().getTime(); 190 } 191 192 public static long getCurrentTime() { 193 return System.currentTimeMillis(); 194 } 195 196 // KILL, THIS IS ONLY HERE FOR TEMP COMPAT as MimeHeaderField uses it. 197 public int getBytes(byte[] buf, int off, int len) { 198 if (len < DATELEN) { 199 String msg = sm.getString("httpDate.iae", new Integer(len)); 200 201 throw new IllegalArgumentException(msg); 202 } 203 204 String dateString = rfc1123Format.format(calendar.getTime()); 205 byte[] b = dateString.getBytes(); 206 System.arraycopy(b, 0, buf, off, DATELEN); 207 return DATELEN; 208 } 209 }