1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.apache.commons.httpclient.params;
31
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: 354155 $
42 *
43 * @since 3.0
44 */
45 public class HttpClientParams extends HttpMethodParams {
46
47 /***
48 * Sets the timeout in milliseconds used when retrieving an
49 * {@link org.apache.commons.httpclient.HttpConnection HTTP connection} from the
50 * {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}.
51 * <p>
52 * This parameter expects a value of type {@link Long}.
53 * </p>
54 */
55 public static final String CONNECTION_MANAGER_TIMEOUT = "http.connection-manager.timeout";
56
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 /***
68 * Defines whether authentication should be attempted preemptively.
69 * <p>
70 * This parameter expects a value of type {@link Boolean}.
71 * </p>
72 */
73 public static final String PREEMPTIVE_AUTHENTICATION = "http.authentication.preemptive";
74
75 /***
76 * Defines whether relative redirects should be rejected.
77 * <p>
78 * This parameter expects a value of type {@link Boolean}.
79 * </p>
80 */
81 public static final String REJECT_RELATIVE_REDIRECT = "http.protocol.reject-relative-redirect";
82
83 /***
84 * Defines the maximum number of redirects to be followed.
85 * The limit on number of redirects is intended to prevent infinite loops.
86 * <p>
87 * This parameter expects a value of type {@link Integer}.
88 * </p>
89 */
90 public static final String MAX_REDIRECTS = "http.protocol.max-redirects";
91
92 /***
93 * Defines whether circular redirects (redirects to the same location) should be allowed.
94 * The HTTP spec is not sufficiently clear whether circular redirects are permitted,
95 * therefore optionally they can be enabled
96 * <p>
97 * This parameter expects a value of type {@link Boolean}.
98 * </p>
99 */
100 public static final String ALLOW_CIRCULAR_REDIRECTS = "http.protocol.allow-circular-redirects";
101
102 /***
103 * Creates a new collection of parameters with the collection returned
104 * by {@link #getDefaultParams()} as a parent. The collection will defer
105 * to its parent for a default value if a particular parameter is not
106 * explicitly set in the collection itself.
107 *
108 * @see #getDefaultParams()
109 */
110 public HttpClientParams() {
111 super();
112 }
113
114 /***
115 * Creates a new collection of parameters with the given parent.
116 * The collection will defer to its parent for a default value
117 * if a particular parameter is not explicitly set in the collection
118 * itself.
119 *
120 * @param defaults the parent collection to defer to, if a parameter
121 * is not explictly set in the collection itself.
122 *
123 * @see #getDefaultParams()
124 */
125 public HttpClientParams(HttpParams defaults) {
126 super(defaults);
127 }
128
129 /***
130 * Returns the timeout in milliseconds used when retrieving an
131 * {@link org.apache.commons.httpclient.HttpConnection HTTP connection} from the
132 * {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}.
133 *
134 * @return timeout in milliseconds.
135 */
136 public long getConnectionManagerTimeout() {
137 return getLongParameter(CONNECTION_MANAGER_TIMEOUT, 0);
138 }
139
140 /***
141 * Sets the timeout in milliseconds used when retrieving an
142 * {@link org.apache.commons.httpclient.HttpConnection HTTP connection} from the
143 * {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}.
144 *
145 * @param timeout the timeout in milliseconds
146 */
147 public void setConnectionManagerTimeout(long timeout) {
148 setLongParameter(CONNECTION_MANAGER_TIMEOUT, timeout);
149 }
150
151 /***
152 * Returns the default
153 * {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}
154 * class.
155 * @return {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}
156 * factory class.
157 */
158 public Class getConnectionManagerClass() {
159 return (Class) getParameter(CONNECTION_MANAGER_CLASS);
160 }
161
162 /***
163 * Sets {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}
164 * class to be used der default.
165 * @param clazz
166 * {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}
167 * factory class.
168 */
169 public void setConnectionManagerClass(Class clazz) {
170 setParameter(CONNECTION_MANAGER_CLASS, clazz);
171 }
172
173 /***
174 * Returns <tt>true</tt> if authentication should be attempted preemptively,
175 * <tt>false</tt> otherwise.
176 *
177 * @return <tt>true</tt> if authentication should be attempted preemptively,
178 * <tt>false</tt> otherwise.
179 */
180 public boolean isAuthenticationPreemptive() {
181 return getBooleanParameter(PREEMPTIVE_AUTHENTICATION, false);
182 }
183
184 /***
185 * Sets whether authentication should be attempted preemptively.
186 *
187 * @param value <tt>true</tt> if authentication should be attempted preemptively,
188 * <tt>false</tt> otherwise.
189 */
190 public void setAuthenticationPreemptive(boolean value) {
191 setBooleanParameter(PREEMPTIVE_AUTHENTICATION, value);
192 }
193
194 private static final String[] PROTOCOL_STRICTNESS_PARAMETERS = {
195 REJECT_RELATIVE_REDIRECT,
196 ALLOW_CIRCULAR_REDIRECTS
197 };
198
199
200 public void makeStrict() {
201 super.makeStrict();
202 setParameters(PROTOCOL_STRICTNESS_PARAMETERS, Boolean.TRUE);
203 }
204
205
206 public void makeLenient() {
207 super.makeLenient();
208 setParameters(PROTOCOL_STRICTNESS_PARAMETERS, Boolean.FALSE);
209 }
210 }