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 import java.io.Serializable;
33 import java.util.HashMap;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 /***
39 * This class represents a collection of HTTP protocol parameters. Protocol parameters
40 * may be linked together to form a hierarchy. If a particular parameter value has not been
41 * explicitly defined in the collection itself, its value will be drawn from the parent
42 * collection of parameters.
43 *
44 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
45 *
46 * @version $Revision: 155418 $
47 *
48 * @since 3.0
49 */
50 public class DefaultHttpParams implements HttpParams, Serializable, Cloneable {
51
52 /*** Log object for this class. */
53 private static final Log LOG = LogFactory.getLog(DefaultHttpParams.class);
54
55 /*** HttpParams class factory. */
56 private static HttpParamsFactory httpParamsFactory = new DefaultHttpParamsFactory();
57
58 /***
59 * Gets the default HttpParams to be used.
60 *
61 * @return the value returned from <code>HttpParamsFactory#getDefaultParams()</code>
62 *
63 * @see HttpParamsFactory#getDefaultParams()
64 */
65 public static HttpParams getDefaultParams() {
66 return httpParamsFactory.getDefaultParams();
67 }
68
69 /***
70 * Sets the factory that will provide the default HttpParams.
71 *
72 * @param httpParamsFactory an instance of HttpParamsFactory
73 *
74 * @see #getDefaultParams()
75 */
76 public static void setHttpParamsFactory(HttpParamsFactory httpParamsFactory) {
77 if (httpParamsFactory == null) {
78 throw new IllegalArgumentException("httpParamsFactory may not be null");
79 }
80 DefaultHttpParams.httpParamsFactory = httpParamsFactory;
81 }
82
83 /*** The set of default values to defer to */
84 private HttpParams defaults = null;
85
86 /*** Hash map of HTTP parameters that this collection contains */
87 private HashMap parameters = null;
88
89 /***
90 * Creates a new collection of parameters with the given parent.
91 * The collection will defer to its parent for a default value
92 * if a particular parameter is not explicitly set in the collection
93 * itself.
94 *
95 * @param defaults the parent collection to defer to, if a parameter
96 * is not explictly set in the collection itself.
97 */
98 public DefaultHttpParams(final HttpParams defaults) {
99 super();
100 this.defaults = defaults;
101 }
102
103 /***
104 * Creates a new collection of parameters with the collection returned
105 * by {@link #getDefaultParams()} as a parent. The collection will defer
106 * to its parent for a default value if a particular parameter is not
107 * explicitly set in the collection itself.
108 *
109 * @see #getDefaultParams()
110 */
111 public DefaultHttpParams() {
112 this(getDefaultParams());
113 }
114
115 public synchronized HttpParams getDefaults() {
116 return this.defaults;
117 }
118
119 public synchronized void setDefaults(final HttpParams params) {
120 this.defaults = params;
121 }
122
123 public synchronized Object getParameter(final String name) {
124
125 Object param = null;
126 if (this.parameters != null) {
127 param = this.parameters.get(name);
128 }
129 if (param != null) {
130
131 return param;
132 } else {
133
134 if (this.defaults != null) {
135
136 return this.defaults.getParameter(name);
137 } else {
138
139 return null;
140 }
141 }
142 }
143
144 public synchronized void setParameter(final String name, final Object value) {
145 if (this.parameters == null) {
146 this.parameters = new HashMap();
147 }
148 this.parameters.put(name, value);
149 if (LOG.isDebugEnabled()) {
150 LOG.debug("Set parameter " + name + " = " + value);
151 }
152 }
153
154 /***
155 * Assigns the value to all the parameter with the given names
156 *
157 * @param names array of parameter name
158 * @param value parameter value
159 */
160 public synchronized void setParameters(final String[] names, final Object value) {
161 for (int i = 0; i < names.length; i++) {
162 setParameter(names[i], value);
163 }
164 }
165
166 public long getLongParameter(final String name, long defaultValue) {
167 Object param = getParameter(name);
168 if (param == null) {
169 return defaultValue;
170 }
171 return ((Long)param).longValue();
172 }
173
174 public void setLongParameter(final String name, long value) {
175 setParameter(name, new Long(value));
176 }
177
178 public int getIntParameter(final String name, int defaultValue) {
179 Object param = getParameter(name);
180 if (param == null) {
181 return defaultValue;
182 }
183 return ((Integer)param).intValue();
184 }
185
186 public void setIntParameter(final String name, int value) {
187 setParameter(name, new Integer(value));
188 }
189
190 public double getDoubleParameter(final String name, double defaultValue) {
191 Object param = getParameter(name);
192 if (param == null) {
193 return defaultValue;
194 }
195 return ((Double)param).doubleValue();
196 }
197
198 public void setDoubleParameter(final String name, double value) {
199 setParameter(name, new Double(value));
200 }
201
202 public boolean getBooleanParameter(final String name, boolean defaultValue) {
203 Object param = getParameter(name);
204 if (param == null) {
205 return defaultValue;
206 }
207 return ((Boolean)param).booleanValue();
208 }
209
210 public void setBooleanParameter(final String name, boolean value) {
211 setParameter(name, new Boolean(value));
212 }
213
214 public boolean isParameterSet(final String name) {
215 return getParameter(name) != null;
216 }
217
218 public boolean isParameterSetLocally(final String name) {
219 return this.parameters != null && this.parameters.get(name) != null;
220 }
221
222 public boolean isParameterTrue(final String name) {
223 return getBooleanParameter(name, false);
224 }
225
226 public boolean isParameterFalse(final String name) {
227 return !getBooleanParameter(name, false);
228 }
229
230 /***
231 * Removes all parameters from this collection.
232 */
233 public void clear() {
234 this.parameters = null;
235 }
236
237 /***
238 * Clones this collection of parameters. Please note that paramter values
239 * themselves are not cloned.
240 *
241 * @see java.io.Serializable
242 * @see java.lang.Object#clone()
243 */
244 public Object clone() throws CloneNotSupportedException
245 {
246 DefaultHttpParams clone = (DefaultHttpParams)super.clone();
247 if (this.parameters != null) {
248 clone.parameters = (HashMap)this.parameters.clone();
249 }
250 clone.setDefaults(this.defaults);
251 return clone;
252 }
253 }