Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.impl.cookie; |
michael@0 | 29 | |
michael@0 | 30 | import java.lang.ref.SoftReference; |
michael@0 | 31 | import java.text.ParseException; |
michael@0 | 32 | import java.text.SimpleDateFormat; |
michael@0 | 33 | import java.util.Calendar; |
michael@0 | 34 | import java.util.Date; |
michael@0 | 35 | import java.util.HashMap; |
michael@0 | 36 | import java.util.Locale; |
michael@0 | 37 | import java.util.Map; |
michael@0 | 38 | import java.util.TimeZone; |
michael@0 | 39 | |
michael@0 | 40 | import ch.boye.httpclientandroidlib.annotation.Immutable; |
michael@0 | 41 | |
michael@0 | 42 | /** |
michael@0 | 43 | * A utility class for parsing and formatting HTTP dates as used in cookies and |
michael@0 | 44 | * other headers. This class handles dates as defined by RFC 2616 section |
michael@0 | 45 | * 3.3.1 as well as some other common non-standard formats. |
michael@0 | 46 | * |
michael@0 | 47 | * |
michael@0 | 48 | * @since 4.0 |
michael@0 | 49 | */ |
michael@0 | 50 | @Immutable |
michael@0 | 51 | public final class DateUtils { |
michael@0 | 52 | |
michael@0 | 53 | /** |
michael@0 | 54 | * Date format pattern used to parse HTTP date headers in RFC 1123 format. |
michael@0 | 55 | */ |
michael@0 | 56 | public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz"; |
michael@0 | 57 | |
michael@0 | 58 | /** |
michael@0 | 59 | * Date format pattern used to parse HTTP date headers in RFC 1036 format. |
michael@0 | 60 | */ |
michael@0 | 61 | public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz"; |
michael@0 | 62 | |
michael@0 | 63 | /** |
michael@0 | 64 | * Date format pattern used to parse HTTP date headers in ANSI C |
michael@0 | 65 | * <code>asctime()</code> format. |
michael@0 | 66 | */ |
michael@0 | 67 | public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy"; |
michael@0 | 68 | |
michael@0 | 69 | private static final String[] DEFAULT_PATTERNS = new String[] { |
michael@0 | 70 | PATTERN_RFC1036, |
michael@0 | 71 | PATTERN_RFC1123, |
michael@0 | 72 | PATTERN_ASCTIME |
michael@0 | 73 | }; |
michael@0 | 74 | |
michael@0 | 75 | private static final Date DEFAULT_TWO_DIGIT_YEAR_START; |
michael@0 | 76 | |
michael@0 | 77 | public static final TimeZone GMT = TimeZone.getTimeZone("GMT"); |
michael@0 | 78 | |
michael@0 | 79 | static { |
michael@0 | 80 | Calendar calendar = Calendar.getInstance(); |
michael@0 | 81 | calendar.setTimeZone(GMT); |
michael@0 | 82 | calendar.set(2000, Calendar.JANUARY, 1, 0, 0, 0); |
michael@0 | 83 | calendar.set(Calendar.MILLISECOND, 0); |
michael@0 | 84 | DEFAULT_TWO_DIGIT_YEAR_START = calendar.getTime(); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | /** |
michael@0 | 88 | * Parses a date value. The formats used for parsing the date value are retrieved from |
michael@0 | 89 | * the default http params. |
michael@0 | 90 | * |
michael@0 | 91 | * @param dateValue the date value to parse |
michael@0 | 92 | * |
michael@0 | 93 | * @return the parsed date |
michael@0 | 94 | * |
michael@0 | 95 | * @throws DateParseException if the value could not be parsed using any of the |
michael@0 | 96 | * supported date formats |
michael@0 | 97 | */ |
michael@0 | 98 | public static Date parseDate(String dateValue) throws DateParseException { |
michael@0 | 99 | return parseDate(dateValue, null, null); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | /** |
michael@0 | 103 | * Parses the date value using the given date formats. |
michael@0 | 104 | * |
michael@0 | 105 | * @param dateValue the date value to parse |
michael@0 | 106 | * @param dateFormats the date formats to use |
michael@0 | 107 | * |
michael@0 | 108 | * @return the parsed date |
michael@0 | 109 | * |
michael@0 | 110 | * @throws DateParseException if none of the dataFormats could parse the dateValue |
michael@0 | 111 | */ |
michael@0 | 112 | public static Date parseDate(final String dateValue, String[] dateFormats) |
michael@0 | 113 | throws DateParseException { |
michael@0 | 114 | return parseDate(dateValue, dateFormats, null); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | /** |
michael@0 | 118 | * Parses the date value using the given date formats. |
michael@0 | 119 | * |
michael@0 | 120 | * @param dateValue the date value to parse |
michael@0 | 121 | * @param dateFormats the date formats to use |
michael@0 | 122 | * @param startDate During parsing, two digit years will be placed in the range |
michael@0 | 123 | * <code>startDate</code> to <code>startDate + 100 years</code>. This value may |
michael@0 | 124 | * be <code>null</code>. When <code>null</code> is given as a parameter, year |
michael@0 | 125 | * <code>2000</code> will be used. |
michael@0 | 126 | * |
michael@0 | 127 | * @return the parsed date |
michael@0 | 128 | * |
michael@0 | 129 | * @throws DateParseException if none of the dataFormats could parse the dateValue |
michael@0 | 130 | */ |
michael@0 | 131 | public static Date parseDate( |
michael@0 | 132 | String dateValue, |
michael@0 | 133 | String[] dateFormats, |
michael@0 | 134 | Date startDate |
michael@0 | 135 | ) throws DateParseException { |
michael@0 | 136 | |
michael@0 | 137 | if (dateValue == null) { |
michael@0 | 138 | throw new IllegalArgumentException("dateValue is null"); |
michael@0 | 139 | } |
michael@0 | 140 | if (dateFormats == null) { |
michael@0 | 141 | dateFormats = DEFAULT_PATTERNS; |
michael@0 | 142 | } |
michael@0 | 143 | if (startDate == null) { |
michael@0 | 144 | startDate = DEFAULT_TWO_DIGIT_YEAR_START; |
michael@0 | 145 | } |
michael@0 | 146 | // trim single quotes around date if present |
michael@0 | 147 | // see issue #5279 |
michael@0 | 148 | if (dateValue.length() > 1 |
michael@0 | 149 | && dateValue.startsWith("'") |
michael@0 | 150 | && dateValue.endsWith("'") |
michael@0 | 151 | ) { |
michael@0 | 152 | dateValue = dateValue.substring (1, dateValue.length() - 1); |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | for (String dateFormat : dateFormats) { |
michael@0 | 156 | SimpleDateFormat dateParser = DateFormatHolder.formatFor(dateFormat); |
michael@0 | 157 | dateParser.set2DigitYearStart(startDate); |
michael@0 | 158 | |
michael@0 | 159 | try { |
michael@0 | 160 | return dateParser.parse(dateValue); |
michael@0 | 161 | } catch (ParseException pe) { |
michael@0 | 162 | // ignore this exception, we will try the next format |
michael@0 | 163 | } |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | // we were unable to parse the date |
michael@0 | 167 | throw new DateParseException("Unable to parse the date " + dateValue); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | /** |
michael@0 | 171 | * Formats the given date according to the RFC 1123 pattern. |
michael@0 | 172 | * |
michael@0 | 173 | * @param date The date to format. |
michael@0 | 174 | * @return An RFC 1123 formatted date string. |
michael@0 | 175 | * |
michael@0 | 176 | * @see #PATTERN_RFC1123 |
michael@0 | 177 | */ |
michael@0 | 178 | public static String formatDate(Date date) { |
michael@0 | 179 | return formatDate(date, PATTERN_RFC1123); |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | /** |
michael@0 | 183 | * Formats the given date according to the specified pattern. The pattern |
michael@0 | 184 | * must conform to that used by the {@link SimpleDateFormat simple date |
michael@0 | 185 | * format} class. |
michael@0 | 186 | * |
michael@0 | 187 | * @param date The date to format. |
michael@0 | 188 | * @param pattern The pattern to use for formatting the date. |
michael@0 | 189 | * @return A formatted date string. |
michael@0 | 190 | * |
michael@0 | 191 | * @throws IllegalArgumentException If the given date pattern is invalid. |
michael@0 | 192 | * |
michael@0 | 193 | * @see SimpleDateFormat |
michael@0 | 194 | */ |
michael@0 | 195 | public static String formatDate(Date date, String pattern) { |
michael@0 | 196 | if (date == null) throw new IllegalArgumentException("date is null"); |
michael@0 | 197 | if (pattern == null) throw new IllegalArgumentException("pattern is null"); |
michael@0 | 198 | |
michael@0 | 199 | SimpleDateFormat formatter = DateFormatHolder.formatFor(pattern); |
michael@0 | 200 | return formatter.format(date); |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | /** This class should not be instantiated. */ |
michael@0 | 204 | private DateUtils() { |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | /** |
michael@0 | 208 | * A factory for {@link SimpleDateFormat}s. The instances are stored in a |
michael@0 | 209 | * threadlocal way because SimpleDateFormat is not threadsafe as noted in |
michael@0 | 210 | * {@link SimpleDateFormat its javadoc}. |
michael@0 | 211 | * |
michael@0 | 212 | */ |
michael@0 | 213 | final static class DateFormatHolder { |
michael@0 | 214 | |
michael@0 | 215 | private static final ThreadLocal<SoftReference<Map<String, SimpleDateFormat>>> |
michael@0 | 216 | THREADLOCAL_FORMATS = new ThreadLocal<SoftReference<Map<String, SimpleDateFormat>>>() { |
michael@0 | 217 | |
michael@0 | 218 | @Override |
michael@0 | 219 | protected SoftReference<Map<String, SimpleDateFormat>> initialValue() { |
michael@0 | 220 | return new SoftReference<Map<String, SimpleDateFormat>>( |
michael@0 | 221 | new HashMap<String, SimpleDateFormat>()); |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | }; |
michael@0 | 225 | |
michael@0 | 226 | /** |
michael@0 | 227 | * creates a {@link SimpleDateFormat} for the requested format string. |
michael@0 | 228 | * |
michael@0 | 229 | * @param pattern |
michael@0 | 230 | * a non-<code>null</code> format String according to |
michael@0 | 231 | * {@link SimpleDateFormat}. The format is not checked against |
michael@0 | 232 | * <code>null</code> since all paths go through |
michael@0 | 233 | * {@link DateUtils}. |
michael@0 | 234 | * @return the requested format. This simple dateformat should not be used |
michael@0 | 235 | * to {@link SimpleDateFormat#applyPattern(String) apply} to a |
michael@0 | 236 | * different pattern. |
michael@0 | 237 | */ |
michael@0 | 238 | public static SimpleDateFormat formatFor(String pattern) { |
michael@0 | 239 | SoftReference<Map<String, SimpleDateFormat>> ref = THREADLOCAL_FORMATS.get(); |
michael@0 | 240 | Map<String, SimpleDateFormat> formats = ref.get(); |
michael@0 | 241 | if (formats == null) { |
michael@0 | 242 | formats = new HashMap<String, SimpleDateFormat>(); |
michael@0 | 243 | THREADLOCAL_FORMATS.set( |
michael@0 | 244 | new SoftReference<Map<String, SimpleDateFormat>>(formats)); |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | SimpleDateFormat format = formats.get(pattern); |
michael@0 | 248 | if (format == null) { |
michael@0 | 249 | format = new SimpleDateFormat(pattern, Locale.US); |
michael@0 | 250 | format.setTimeZone(TimeZone.getTimeZone("GMT")); |
michael@0 | 251 | formats.put(pattern, format); |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | return format; |
michael@0 | 255 | } |
michael@0 | 256 | |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | } |