You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by mb...@apache.org on 2003/09/18 01:29:05 UTC
cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params DefaultHttpParams.java HttpParamsFactory.java DefaultHttpParamsFactory.java
mbecke 2003/09/17 16:29:05
Modified: httpclient/src/java/org/apache/commons/httpclient/util
DateParser.java
httpclient/src/java/org/apache/commons/httpclient
HttpClient.java
httpclient/src/java/org/apache/commons/httpclient/params
DefaultHttpParams.java HttpParamsFactory.java
DefaultHttpParamsFactory.java
Log:
Adds Javadocs and moves DateParser formats to configuration system.
PR: 15435
Submitted by: Michael Becke
Reviewed by: Oleg Kalnichevski
Revision Changes Path
1.7 +44 -29 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/DateParser.java
Index: DateParser.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/DateParser.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- DateParser.java 25 Aug 2003 03:09:57 -0000 1.6
+++ DateParser.java 17 Sep 2003 23:29:05 -0000 1.7
@@ -65,10 +65,17 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.Date;
+import java.util.Iterator;
import java.util.Locale;
import java.util.TimeZone;
+import org.apache.commons.httpclient.params.DefaultHttpParams;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
/**
* A utility class for parsing HTTP dates as used in cookies and other headers.
* This class handles dates as defined by RFC 2616 section 3.3.1 as well as
@@ -79,6 +86,17 @@
*/
public class DateParser {
+ /** Log object for this class. */
+ private static final Log LOG = LogFactory.getLog(DateParser.class);
+
+ /**
+ * The key used to look up the date patterns used for parsing. The String patterns are stored
+ * in a {@link Collection} and must be compatible with {@link SimpleDateFormat}.
+ *
+ * @see org.apache.commons.httpclient.params.DefaultHttpParams
+ */
+ public static final String KEY_DATE_PATTERNS = "http.dateParser.patterns";
+
/**
* Date format pattern used to parse HTTP date headers in RFC 1123 format.
*/
@@ -95,26 +113,9 @@
*/
public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
- /** The patterns used for parsing dates */
- private static final String[] DATE_PATTERNS = {
- PATTERN_RFC1123,
- PATTERN_RFC1036,
- PATTERN_ASCTIME,
- "EEE, dd-MMM-yyyy HH:mm:ss z",
- "EEE, dd-MMM-yyyy HH-mm-ss z",
- "EEE, dd MMM yy HH:mm:ss z",
- "EEE dd-MMM-yyyy HH:mm:ss z",
- "EEE dd MMM yyyy HH:mm:ss z",
- "EEE dd-MMM-yyyy HH-mm-ss z",
- "EEE dd-MMM-yy HH:mm:ss z",
- "EEE dd MMM yy HH:mm:ss z",
- "EEE,dd-MMM-yy HH:mm:ss z",
- "EEE,dd-MMM-yyyy HH:mm:ss z",
- "EEE, dd-MM-yyyy HH:mm:ss z",
- };
-
/**
- * Parses a date value.
+ * Parses a date value. The formats used for parsing the date value are retrieved from
+ * the default http params.
*
* @param dateValue the date value to parse
*
@@ -122,13 +123,25 @@
*
* @throws DateParseException if the value could not be parsed using any of the
* supported date formats
+ *
+ * @see DefaultHttpParams#getDefaultParams()
*/
public static Date parseDate(String dateValue) throws DateParseException {
- return parseDate(dateValue, DATE_PATTERNS);
+
+ Collection patterns = (Collection) DefaultHttpParams.getDefaultParams().getParameter(
+ KEY_DATE_PATTERNS
+ );
+ if (patterns == null) {
+ LOG.warn("DateParser patterns not included in the default params.");
+ patterns = Arrays.asList(
+ new String[] { PATTERN_ASCTIME, PATTERN_RFC1036, PATTERN_RFC1123 }
+ );
+ }
+ return parseDate(dateValue, patterns);
}
/**
- * Parses the date value using the array of date formats.
+ * Parses the date value using the given date formats.
*
* @param dateValue the date value to parse
* @param dateFormats the date formats to use
@@ -139,7 +152,7 @@
*/
private static Date parseDate(
String dateValue,
- String[] dateFormats
+ Collection dateFormats
) throws DateParseException {
if (dateValue == null) {
@@ -155,14 +168,16 @@
dateValue = dateValue.substring (1, dateValue.length() - 1);
}
- SimpleDateFormat dateParser = null;
+ SimpleDateFormat dateParser = null;
+ Iterator formatIter = dateFormats.iterator();
- for (int i = 0; i < dateFormats.length; i++) {
+ while (formatIter.hasNext()) {
+ String format = (String) formatIter.next();
if (dateParser == null) {
- dateParser = new SimpleDateFormat(dateFormats[i], Locale.US);
+ dateParser = new SimpleDateFormat(format, Locale.US);
dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
} else {
- dateParser.applyPattern(dateFormats[i]);
+ dateParser.applyPattern(format);
}
try {
return dateParser.parse(dateValue);
1.83 +20 -16 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java
Index: HttpClient.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v
retrieving revision 1.82
retrieving revision 1.83
diff -u -r1.82 -r1.83
--- HttpClient.java 16 Sep 2003 21:29:45 -0000 1.82
+++ HttpClient.java 17 Sep 2003 23:29:05 -0000 1.83
@@ -121,13 +121,22 @@
// ----------------------------------------------------------- Constructors
/**
- * Creates an instance of HttpClient using a
- * {@link SimpleHttpConnectionManager simple HTTP connection manager}.
+ * Creates an instance of HttpClient using the default connection manager.
*
- * @see SimpleHttpConnectionManager
+ * @see HttpClientParams#getConnectionManagerClass()
*/
public HttpClient() {
- this(new SimpleHttpConnectionManager());
+
+ super();
+ try {
+ this.httpConnectionManager =
+ (HttpConnectionManager) params.getConnectionManagerClass().newInstance();
+ } catch (Exception e) {
+ LOG.warn("Error instantiating connection manager class, defaulting to"
+ + " SimpleHttpConnectionManager",
+ e);
+ this.httpConnectionManager = new SimpleHttpConnectionManager();
+ }
}
/**
@@ -143,12 +152,7 @@
throw new IllegalArgumentException("httpConnectionManager cannot be null");
}
- this.state = new HttpState();
this.httpConnectionManager = httpConnectionManager;
-
- this.hostConfiguration = new HostConfiguration();
- this.params = new HttpClientParams();
-
}
// ----------------------------------------------------- Instance Variables
@@ -162,15 +166,15 @@
/**
* The {@link HttpState HTTP state} associated with this HttpClient.
*/
- private HttpState state;
+ private HttpState state = new HttpState();
- private HttpClientParams params;
+ private HttpClientParams params = new HttpClientParams();
/**
* The {@link HostConfiguration host configuration} associated with
* the HttpClient
*/
- private HostConfiguration hostConfiguration;
+ private HostConfiguration hostConfiguration = new HostConfiguration();
// ------------------------------------------------------------- Properties
1.2 +30 -15 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/DefaultHttpParams.java
Index: DefaultHttpParams.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/DefaultHttpParams.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefaultHttpParams.java 11 Sep 2003 20:08:33 -0000 1.1
+++ DefaultHttpParams.java 17 Sep 2003 23:29:05 -0000 1.2
@@ -87,15 +87,32 @@
private static HttpParamsFactory httpParamsFactory = new DefaultHttpParamsFactory();
+ /**
+ * Gets the default HttpParams to be used.
+ *
+ * @return the value returned from <code>HttpParamsFactory#getDefaultParams()</code>
+ *
+ * @see HttpParamsFactory#getDefaultParams()
+ */
public static HttpParams getDefaultParams() {
return httpParamsFactory.getDefaultParams();
}
+ /**
+ * Sets the factory that will provide the default HttpParams.
+ *
+ * @param httpParamsFactory an instance of HttpParamsFactory
+ *
+ * @see #getDefaultParams()
+ */
public static void setHttpParamsFactory(HttpParamsFactory httpParamsFactory) {
+ if (httpParamsFactory == null) {
+ throw new IllegalArgumentException("httpParamsFactory may not be null");
+ }
DefaultHttpParams.httpParamsFactory = httpParamsFactory;
}
-
+ /** The set of default values to defer to */
private HttpParams defaults = null;
/** Hash map of HTTP parameters that this object contains */
@@ -106,6 +123,12 @@
this.defaults = defaults;
}
+ /**
+ * Initializes this class with defaults set to the value returned from
+ * <code>DefaultHttpParams.getDefaultParams()</code>.
+ *
+ * @see #getDefaultParams()
+ */
public DefaultHttpParams() {
this(getDefaultParams());
}
@@ -212,18 +235,10 @@
}
public boolean isParameterTrue(final String name) {
- Object param = getParameter(name);
- if (param == null) {
- return false;
- }
- return ((Boolean)param).booleanValue() == true;
+ return getBooleanParameter(name, false);
}
public boolean isParameterFalse(final String name) {
- Object param = getParameter(name);
- if (param == null) {
- return false;
- }
- return ((Boolean)param).booleanValue() == false;
+ return !getBooleanParameter(name, true);
}
}
1.2 +75 -2 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/HttpParamsFactory.java
Index: HttpParamsFactory.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/HttpParamsFactory.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HttpParamsFactory.java 11 Sep 2003 20:08:33 -0000 1.1
+++ HttpParamsFactory.java 17 Sep 2003 23:29:05 -0000 1.2
@@ -1,9 +1,82 @@
+/*
+ * $Header$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution, if
+ * any, must include the following acknowlegement:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowlegement may appear in the software itself,
+ * if and wherever such third-party acknowlegements normally appear.
+ *
+ * 4. The names "The Jakarta Project", "Commons", and "Apache Software
+ * Foundation" must not be used to endorse or promote products derived
+ * from this software without prior written permission. For written
+ * permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache"
+ * nor may "Apache" appear in their names without prior written
+ * permission of the Apache Group.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ * [Additional notices, if required by prior licensing conditions]
+ *
+ */
+
package org.apache.commons.httpclient.params;
-
-
+/**
+ * A factory for getting the default set of parameters to use when creating an instance of
+ * <code>HttpParams</code>.
+ *
+ * @see org.apache.commons.httpclient.params.DefaultHttpParams#setHttpParamsFactory(HttpParamsFactory)
+ */
public interface HttpParamsFactory {
+ /**
+ * Gets the default parameters. This method may be called more than once and is not required
+ * to always return the same value.
+ *
+ * @return an instance of HttpParams
+ */
HttpParams getDefaultParams();
}
1.2 +92 -0 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/DefaultHttpParamsFactory.java
Index: DefaultHttpParamsFactory.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/DefaultHttpParamsFactory.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefaultHttpParamsFactory.java 11 Sep 2003 20:08:33 -0000 1.1
+++ DefaultHttpParamsFactory.java 17 Sep 2003 23:29:05 -0000 1.2
@@ -1,6 +1,74 @@
+/*
+ * $Header$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution, if
+ * any, must include the following acknowlegement:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowlegement may appear in the software itself,
+ * if and wherever such third-party acknowlegements normally appear.
+ *
+ * 4. The names "The Jakarta Project", "Commons", and "Apache Software
+ * Foundation" must not be used to endorse or promote products derived
+ * from this software without prior written permission. For written
+ * permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache"
+ * nor may "Apache" appear in their names without prior written
+ * permission of the Apache Group.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ * [Additional notices, if required by prior licensing conditions]
+ *
+ */
+
package org.apache.commons.httpclient.params;
+import java.util.ArrayList;
+import java.util.Arrays;
+
import org.apache.commons.httpclient.HttpVersion;
+import org.apache.commons.httpclient.SimpleHttpConnectionManager;
+import org.apache.commons.httpclient.util.DateParser;
@@ -31,6 +99,30 @@
params.setParameter(HttpMethodParams.USER_AGENT, "Jakarta Commons-HttpClient/2.1m1");
params.setVersion(HttpVersion.HTTP_1_1);
+ params.setConnectionManagerClass(SimpleHttpConnectionManager.class);
+
+ ArrayList datePatterns = new ArrayList();
+ datePatterns.addAll(
+ Arrays.asList(
+ new String[] {
+ DateParser.PATTERN_RFC1123,
+ DateParser.PATTERN_RFC1036,
+ DateParser.PATTERN_ASCTIME,
+ "EEE, dd-MMM-yyyy HH:mm:ss z",
+ "EEE, dd-MMM-yyyy HH-mm-ss z",
+ "EEE, dd MMM yy HH:mm:ss z",
+ "EEE dd-MMM-yyyy HH:mm:ss z",
+ "EEE dd MMM yyyy HH:mm:ss z",
+ "EEE dd-MMM-yyyy HH-mm-ss z",
+ "EEE dd-MMM-yy HH:mm:ss z",
+ "EEE dd MMM yy HH:mm:ss z",
+ "EEE,dd-MMM-yy HH:mm:ss z",
+ "EEE,dd-MMM-yyyy HH:mm:ss z",
+ "EEE, dd-MM-yyyy HH:mm:ss z",
+ }
+ )
+ );
+ params.setParameter(DateParser.KEY_DATE_PATTERNS, datePatterns);
// TODO: To be removed. Provided for backward compatibility
String agent = System.getProperties().getProperty("httpclient.useragent");
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org