1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/HttpClientParams.java,v 1.7 2004/05/13 04:01:22 mbecke Exp $
  3. * $Revision: 1.7 $
  4. * $Date: 2004/05/13 04:01:22 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 1999-2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient.params;
  30. import org.apache.commons.logging.Log;
  31. import org.apache.commons.logging.LogFactory;
  32. /**
  33. * This class represents a collection of HTTP protocol parameters applicable to
  34. * {@link org.apache.commons.httpclient.HttpClient instances of HttpClient}.
  35. * Protocol parameters may be linked together to form a hierarchy. If a particular
  36. * parameter value has not been explicitly defined in the collection itself, its
  37. * value will be drawn from the parent collection of parameters.
  38. *
  39. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  40. *
  41. * @version $Revision: 1.7 $
  42. *
  43. * @since 3.0
  44. */
  45. public class HttpClientParams extends HttpMethodParams {
  46. /** Log object for this class. */
  47. private static final Log LOG = LogFactory.getLog(HttpParams.class);
  48. /**
  49. * Sets the timeout in milliseconds used when retrieving an
  50. * {@link org.apache.commons.httpclient.HttpConnection HTTP connection} from the
  51. * {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}.
  52. * <p>
  53. * This parameter expects a value of type {@link Long}.
  54. * </p>
  55. */
  56. public static final String CONNECTION_MANAGER_TIMEOUT = "http.connection-manager.timeout";
  57. /**
  58. * Defines the default
  59. * {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}
  60. * class.
  61. * <p>
  62. * This parameter expects a value of type {@link Class}.
  63. * </p>
  64. */
  65. public static final String CONNECTION_MANAGER_CLASS = "http.connection-manager.class";
  66. /**
  67. * Defines whether authentication should be attempted preemptively.
  68. * <p>
  69. * This parameter expects a value of type {@link Boolean}.
  70. * </p>
  71. */
  72. public static final String PREEMPTIVE_AUTHENTICATION = "http.authentication.preemptive";
  73. /**
  74. * Defines whether relative redirects should be rejected.
  75. * <p>
  76. * This parameter expects a value of type {@link Boolean}.
  77. * </p>
  78. */
  79. public static final String REJECT_RELATIVE_REDIRECT = "http.protocol.reject-relative-redirect";
  80. /**
  81. * Defines the maximum number of redirects to be followed.
  82. * The limit on number of redirects is intended to prevent infinite loops.
  83. * <p>
  84. * This parameter expects a value of type {@link Integer}.
  85. * </p>
  86. */
  87. public static final String MAX_REDIRECTS = "http.protocol.max-redirects";
  88. /**
  89. * Defines whether circular redirects (redirects to the same location) should be allowed.
  90. * The HTTP spec is not sufficiently clear whether circular redirects are permitted,
  91. * therefore optionally they can be enabled
  92. * <p>
  93. * This parameter expects a value of type {@link Boolean}.
  94. * </p>
  95. */
  96. public static final String ALLOW_CIRCULAR_REDIRECTS = "http.protocol.allow-circular-redirects";
  97. /**
  98. * Creates a new collection of parameters with the collection returned
  99. * by {@link #getDefaultParams()} as a parent. The collection will defer
  100. * to its parent for a default value if a particular parameter is not
  101. * explicitly set in the collection itself.
  102. *
  103. * @see #getDefaultParams()
  104. */
  105. public HttpClientParams() {
  106. super();
  107. }
  108. /**
  109. * Creates a new collection of parameters with the given parent.
  110. * The collection will defer to its parent for a default value
  111. * if a particular parameter is not explicitly set in the collection
  112. * itself.
  113. *
  114. * @param defaults the parent collection to defer to, if a parameter
  115. * is not explictly set in the collection itself.
  116. *
  117. * @see #getDefaultParams()
  118. */
  119. public HttpClientParams(HttpParams defaults) {
  120. super(defaults);
  121. }
  122. /**
  123. * Returns the timeout in milliseconds used when retrieving an
  124. * {@link org.apache.commons.httpclient.HttpConnection HTTP connection} from the
  125. * {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}.
  126. *
  127. * @return timeout in milliseconds.
  128. */
  129. public long getConnectionManagerTimeout() {
  130. return getLongParameter(CONNECTION_MANAGER_TIMEOUT, 0);
  131. }
  132. /**
  133. * Sets the timeout in milliseconds used when retrieving an
  134. * {@link org.apache.commons.httpclient.HttpConnection HTTP connection} from the
  135. * {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}.
  136. *
  137. * @param timeout the timeout in milliseconds
  138. */
  139. public void setConnectionManagerTimeout(long timeout) {
  140. setLongParameter(CONNECTION_MANAGER_TIMEOUT, timeout);
  141. }
  142. /**
  143. * Returns the default
  144. * {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}
  145. * class.
  146. * @return {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}
  147. * factory class.
  148. */
  149. public Class getConnectionManagerClass() {
  150. return (Class) getParameter(CONNECTION_MANAGER_CLASS);
  151. }
  152. /**
  153. * Sets {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}
  154. * class to be used der default.
  155. * @param clazz
  156. * {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}
  157. * factory class.
  158. */
  159. public void setConnectionManagerClass(Class clazz) {
  160. setParameter(CONNECTION_MANAGER_CLASS, clazz);
  161. }
  162. /**
  163. * Returns <tt>true</tt> if authentication should be attempted preemptively,
  164. * <tt>false</tt> otherwise.
  165. *
  166. * @return <tt>true</tt> if authentication should be attempted preemptively,
  167. * <tt>false</tt> otherwise.
  168. */
  169. public boolean isAuthenticationPreemptive() {
  170. return getBooleanParameter(PREEMPTIVE_AUTHENTICATION, false);
  171. }
  172. /**
  173. * Sets whether authentication should be attempted preemptively.
  174. *
  175. * @param value <tt>true</tt> if authentication should be attempted preemptively,
  176. * <tt>false</tt> otherwise.
  177. */
  178. public void setAuthenticationPreemptive(boolean value) {
  179. setBooleanParameter(PREEMPTIVE_AUTHENTICATION, value);
  180. }
  181. private static final String[] PROTOCOL_STRICTNESS_PARAMETERS = {
  182. REJECT_RELATIVE_REDIRECT,
  183. ALLOW_CIRCULAR_REDIRECTS
  184. };
  185. public void makeStrict() {
  186. super.makeStrict();
  187. setParameters(PROTOCOL_STRICTNESS_PARAMETERS, new Boolean(true));
  188. }
  189. public void makeLenient() {
  190. super.makeLenient();
  191. setParameters(PROTOCOL_STRICTNESS_PARAMETERS, new Boolean(false));
  192. }
  193. }