001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: MockHeaders.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.test;
009:
010: import java.util.*;
011:
012: import java.text.ParseException;
013: import java.text.SimpleDateFormat;
014:
015: class MockHeaders {
016: private final static String[] DATE_FORMAT_SYNTAXES = {
017: "EEE, dd MMM yyyy HH:mm:ss zzz",
018: "EEE, dd-MMM-yy HH:mm:ss zzz", "EEE MMM dd HH:mm:ss yyyy",
019: "EEE, dd MMM yyyy HH:mm:ss zzz",
020: "EEE, dd-MMM-yy HH:mm:ss zzz", "dd MMM yyyy HH:mm:ss",
021: "dd-MMM-yy HH:mm:ss", };
022:
023: private final static SimpleDateFormat[] DATE_FORMATS;
024: private final static TimeZone TIMEZONE_GMT = TimeZone
025: .getTimeZone("GMT");
026: private final static ThreadLocal DATE_PARSED_CACHED = new ThreadLocal();
027: private final static String SET_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss 'GMT'";
028: static {
029: TIMEZONE_GMT.setID("GMT");
030: DATE_FORMATS = new SimpleDateFormat[DATE_FORMAT_SYNTAXES.length];
031: for (int i = 0; i < DATE_FORMATS.length; i++) {
032: DATE_FORMATS[i] = new SimpleDateFormat(
033: DATE_FORMAT_SYNTAXES[i], Locale.US);
034: DATE_FORMATS[i].setTimeZone(TIMEZONE_GMT);
035: }
036: }
037:
038: private Map<String, List<String>> mHeaders;
039: private SimpleDateFormat[] mDateFormats;
040:
041: public long getDateHeader(String name) {
042: String header = getHeader(name);
043: if (header == null) {
044: return -1;
045: }
046:
047: if (mDateFormats == null) {
048: mDateFormats = (SimpleDateFormat[]) DATE_PARSED_CACHED
049: .get();
050: if (mDateFormats == null) {
051: mDateFormats = (SimpleDateFormat[]) new SimpleDateFormat[DATE_FORMATS.length];
052: DATE_PARSED_CACHED.set(mDateFormats);
053: }
054: }
055:
056: for (int i = 0; i < mDateFormats.length; i++) {
057: // clone formatter for thread safety
058: if (mDateFormats[i] == null) {
059: mDateFormats[i] = (SimpleDateFormat) DATE_FORMATS[i]
060: .clone();
061: }
062:
063: try {
064: Date date = (Date) mDateFormats[i].parseObject(header);
065: return date.getTime();
066: } catch (ParseException e) {
067: // IllegalArgumentException will thrown at the end of the method
068: }
069: }
070:
071: if (header.endsWith(" GMT")) {
072: header = header.substring(0, header.length() - 4);
073: for (int i = 0; i < mDateFormats.length; i++) {
074: try {
075: Date date = (Date) mDateFormats[i]
076: .parseObject(header);
077: return date.getTime();
078: } catch (ParseException e) {
079: // IllegalArgumentException will thrown at the end of the method
080: }
081: }
082: }
083:
084: throw new IllegalArgumentException(header);
085: }
086:
087: public String getHeader(String name) {
088: if (null == mHeaders) {
089: return null;
090: }
091:
092: List<String> headers = mHeaders.get(name);
093: if (null == headers || 0 == headers.size()) {
094: return null;
095: }
096:
097: return headers.get(0);
098: }
099:
100: public Collection getHeaderNames() {
101: if (null == mHeaders) {
102: return Collections.EMPTY_LIST;
103: }
104:
105: return mHeaders.keySet();
106: }
107:
108: public Collection getHeaders(String name) {
109: if (null == mHeaders) {
110: return Collections.EMPTY_LIST;
111: }
112:
113: List<String> headers = mHeaders.get(name);
114: if (null == headers || 0 == headers.size()) {
115: return Collections.EMPTY_LIST;
116: }
117:
118: return headers;
119: }
120:
121: public int getIntHeader(String name) {
122: String header = getHeader(name);
123: if (null == header) {
124: return -1;
125: }
126:
127: try {
128: return Integer.parseInt(header);
129: } catch (NumberFormatException e) {
130: throw new IllegalArgumentException(header);
131: }
132: }
133:
134: public void addHeader(String name, String value) {
135: if (null == mHeaders) {
136: mHeaders = new HashMap<String, List<String>>();
137: }
138:
139: List<String> headers = mHeaders.get(name);
140: if (null == headers) {
141: headers = new ArrayList<String>();
142: mHeaders.put(name, headers);
143: }
144:
145: headers.add(value);
146: }
147:
148: public void addDateHeader(String name, long date) {
149: addHeader(name, formatDate(date));
150: }
151:
152: public void addIntHeader(String name, int integer) {
153: addHeader(name, String.valueOf(integer));
154: }
155:
156: public boolean containsHeader(String name) {
157: if (null == mHeaders) {
158: return false;
159: }
160:
161: return mHeaders.containsKey(name);
162: }
163:
164: public void setDateHeader(String name, long date) {
165: setHeader(name, formatDate(date));
166: }
167:
168: private String formatDate(long date) {
169: SimpleDateFormat format = new SimpleDateFormat(SET_DATE_FORMAT);
170: HttpCal calendar = new HttpCal();
171: calendar.setTimeInMillis(date);
172: String formatted_date = format.format(calendar.getTime());
173: return formatted_date;
174: }
175:
176: public void setHeader(String name, final String value) {
177: if (null == mHeaders) {
178: mHeaders = new HashMap<String, List<String>>();
179: }
180:
181: mHeaders.put(name, new ArrayList<String>() {
182: {
183: add(value);
184: }
185: });
186: }
187:
188: public void setIntHeader(String name, int value) {
189: setHeader(name, String.valueOf(value));
190: }
191:
192: public void removeHeader(String name) {
193: if (null == mHeaders) {
194: return;
195: }
196:
197: mHeaders.remove(name);
198: }
199:
200: private static class HttpCal extends GregorianCalendar {
201: HttpCal() {
202: super (TIMEZONE_GMT);
203: }
204:
205: public void setTimeInMillis(long arg0) {
206: super .setTimeInMillis(arg0);
207: }
208:
209: public long getTimeInMillis() {
210: return super.getTimeInMillis();
211: }
212: }
213: }
|