Index: java/org/apache/commons/httpclient/Header.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Header.java,v retrieving revision 1.16 diff -u -r1.16 Header.java --- java/org/apache/commons/httpclient/Header.java 13 May 2004 04:03:25 -0000 1.16 +++ java/org/apache/commons/httpclient/Header.java 14 Sep 2004 16:03:59 -0000 @@ -137,22 +137,8 @@ * * @since 3.0 */ - public boolean isAutogenerated() - { + public boolean isAutogenerated() { return isAutogenerated; - } - - /** - * Sets the values of the auto-generated header flag. - * - * @return b true if the header is autogenerated, - * false otherwise. - * - * @since 3.0 - */ - public void setAutogenerated(boolean b) - { - isAutogenerated = b; } } Index: java/org/apache/commons/httpclient/HttpMethodBase.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v retrieving revision 1.211 diff -u -r1.211 HttpMethodBase.java --- java/org/apache/commons/httpclient/HttpMethodBase.java 19 Aug 2004 21:39:26 -0000 1.211 +++ java/org/apache/commons/httpclient/HttpMethodBase.java 14 Sep 2004 16:04:05 -0000 @@ -1,5 +1,5 @@ /* - * $Header: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v 1.211 2004/08/19 21:39:26 olegk Exp $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v 1.211 2004/08/19 21:39:26 olegk Exp $ * $Revision: 1.211 $ * $Date: 2004/08/19 21:39:26 $ * @@ -1147,15 +1147,13 @@ if ((cookies != null) && (cookies.length > 0)) { if (getParams().isParameterTrue(HttpMethodParams.SINGLE_COOKIE_HEADER)) { // In strict mode put all cookies on the same header - Header header = matcher.formatCookieHeader(cookies); - header.setAutogenerated(true); - getRequestHeaderGroup().addHeader(header); + String s = matcher.formatCookies(cookies); + getRequestHeaderGroup().addHeader(new Header("Cookie", s, true)); } else { // In non-strict mode put each cookie on a separate header for (int i = 0; i < cookies.length; i++) { - Header header = matcher.formatCookieHeader(cookies[i]); - header.setAutogenerated(true); - getRequestHeaderGroup().addHeader(header); + String s = matcher.formatCookie(cookies[i]); + getRequestHeaderGroup().addHeader(new Header("Cookie", s, true)); } } } Index: java/org/apache/commons/httpclient/HttpMethodDirector.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodDirector.java,v retrieving revision 1.28 diff -u -r1.28 HttpMethodDirector.java --- java/org/apache/commons/httpclient/HttpMethodDirector.java 5 Jul 2004 22:46:58 -0000 1.28 +++ java/org/apache/commons/httpclient/HttpMethodDirector.java 14 Sep 2004 16:04:08 -0000 @@ -1,5 +1,5 @@ /* - * $Header: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodDirector.java,v 1.28 2004/07/05 22:46:58 olegk Exp $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodDirector.java,v 1.28 2004/07/05 22:46:58 olegk Exp $ * $Revision: 1.28 $ * $Date: 2004/07/05 22:46:58 $ * @@ -30,7 +30,9 @@ package org.apache.commons.httpclient; import java.io.IOException; +import java.util.Collection; import java.util.HashSet; +import java.util.Iterator; import java.util.Map; import java.util.Set; @@ -44,6 +46,7 @@ import org.apache.commons.httpclient.auth.CredentialsNotAvailableException; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.auth.MalformedChallengeException; +import org.apache.commons.httpclient.params.HostParams; import org.apache.commons.httpclient.params.HttpClientParams; import org.apache.commons.httpclient.params.HttpConnectionParams; import org.apache.commons.httpclient.params.HttpMethodParams; @@ -130,6 +133,17 @@ // Global -> HttpClient -> HostConfiguration -> HttpMethod this.hostConfiguration.getParams().setDefaults(this.params); method.getParams().setDefaults(this.hostConfiguration.getParams()); + + // Generate default request headers + Collection defaults = (Collection)this.hostConfiguration.getParams(). + getParameter(HostParams.DEFAULT_HEADERS); + if (defaults != null) { + Iterator i = defaults.iterator(); + while (i.hasNext()) { + method.addRequestHeader((Header)i.next()); + } + } + try { int maxRedirects = this.params.getIntParameter(HttpClientParams.MAX_REDIRECTS, 100); Index: java/org/apache/commons/httpclient/params/HostParams.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/HostParams.java,v retrieving revision 1.2 diff -u -r1.2 HostParams.java --- java/org/apache/commons/httpclient/params/HostParams.java 13 May 2004 04:01:22 -0000 1.2 +++ java/org/apache/commons/httpclient/params/HostParams.java 14 Sep 2004 16:04:09 -0000 @@ -51,6 +51,15 @@ private static final Log LOG = LogFactory.getLog(HttpParams.class); /** + * Defines the request headers to be sent per default with each request. + *

+ * This parameter expects a value of type {@link java.util.Collection}. The + * collection is expected to contain {@link Header}s. + *

+ */ + public static final String DEFAULT_HEADERS = "http.default-headers"; + + /** * Creates a new collection of parameters with the collection returned * by {@link #getDefaultParams()} as a parent. The collection will defer * to its parent for a default value if a particular parameter is not Index: test/org/apache/commons/httpclient/TestNoHost.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v retrieving revision 1.38 diff -u -r1.38 TestNoHost.java --- test/org/apache/commons/httpclient/TestNoHost.java 11 May 2004 20:43:55 -0000 1.38 +++ test/org/apache/commons/httpclient/TestNoHost.java 14 Sep 2004 16:04:09 -0000 @@ -1,5 +1,5 @@ /* - * $Header: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v 1.38 2004/05/11 20:43:55 olegk Exp $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v 1.38 2004/05/11 20:43:55 olegk Exp $ * $Revision: 1.38 $ * $Date: 2004/05/11 20:43:55 $ * ==================================================================== @@ -90,6 +90,7 @@ suite.addTest(TestEquals.suite()); suite.addTestSuite(TestIdleConnectionTimeout.class); suite.addTest(TestMethodAbort.suite()); + suite.addTest(TestHttpParams.suite()); return suite; } Index: test/org/apache/commons/httpclient/TestHttpParams.java =================================================================== RCS file: test/org/apache/commons/httpclient/TestHttpParams.java diff -N test/org/apache/commons/httpclient/TestHttpParams.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test/org/apache/commons/httpclient/TestHttpParams.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,128 @@ +/* + * $Header$ + * $Revision$ + * $Date$ + * ==================================================================== + * + * Copyright 1999-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + * + * 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 + * . + * + * [Additional notices, if required by prior licensing conditions] + * + */ + +package org.apache.commons.httpclient; + +import java.io.IOException; +import java.util.ArrayList; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.params.HostParams; +import org.apache.commons.httpclient.protocol.Protocol; +import org.apache.commons.httpclient.server.HttpService; +import org.apache.commons.httpclient.server.SimpleRequest; +import org.apache.commons.httpclient.server.SimpleResponse; + +/** + * HTTP preference framework tests. + * + * @author Oleg Kalnichevski + * + * @version $Revision$ + */ +public class TestHttpParams extends HttpClientTestBase { + + // ------------------------------------------------------------ Constructor + public TestHttpParams(String testName) { + super(testName); + } + + // ------------------------------------------------------------------- Main + public static void main(String args[]) { + String[] testCaseName = { TestHttpParams.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + + // ------------------------------------------------------- TestCase Methods + + public static Test suite() { + return new TestSuite(TestHttpParams.class); + } + + private class SimpleService implements HttpService { + + public SimpleService() { + super(); + } + + public boolean process(final SimpleRequest request, final SimpleResponse response) + throws IOException + { + String uri = request.getRequestLine().getUri(); + HttpVersion httpversion = request.getRequestLine().getHttpVersion(); + + if ("/miss/".equals(uri)) { + response.setStatusLine(httpversion, HttpStatus.SC_MOVED_TEMPORARILY); + response.addHeader(new Header("Location", "/hit/")); + response.setBodyString("Missed!"); + } else if ("/hit/".equals(uri)) { + response.setStatusLine(httpversion, HttpStatus.SC_OK); + response.setBodyString("Hit!"); + } else { + response.setStatusLine(httpversion, HttpStatus.SC_NOT_FOUND); + response.setBodyString(uri + " not found"); + } + return true; + } + } + + public void testDefaultHeaders() throws IOException { + this.server.setHttpService(new SimpleService()); + + ArrayList defaults = new ArrayList(); + defaults.add(new Header("this-header", "value1")); + defaults.add(new Header("that-header", "value1")); + defaults.add(new Header("that-header", "value2")); + defaults.add(new Header("User-Agent", "test")); + + HostConfiguration hostconfig = new HostConfiguration(); + hostconfig.setHost( + this.server.getLocalAddress(), + this.server.getLocalPort(), + Protocol.getProtocol("http")); + hostconfig.getParams().setParameter(HostParams.DEFAULT_HEADERS, defaults); + + GetMethod httpget = new GetMethod("/miss/"); + try { + this.client.executeMethod(hostconfig, httpget); + } finally { + httpget.releaseConnection(); + } + assertEquals(HttpStatus.SC_OK, httpget.getStatusCode()); + Header[] thisheader = httpget.getRequestHeaders("this-header"); + assertEquals(1, thisheader.length); + Header[] thatheader = httpget.getRequestHeaders("that-header"); + assertEquals(2, thatheader.length); + assertEquals("test", httpget.getRequestHeader("User-Agent").getValue()); + } +}