1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
27
28 package org.apache.http.params;
29
30 import org.apache.http.annotation.ThreadingBehavior;
31 import org.apache.http.annotation.Contract;
32
33 import java.io.Serializable;
34 import java.util.HashSet;
35 import java.util.Map;
36 import java.util.Set;
37 import java.util.concurrent.ConcurrentHashMap;
38
39 /**
40 * Default implementation of {@link HttpParams} interface.
41 * <p>
42 * Please note access to the internal structures of this class is not
43 * synchronized and therefore this class may be thread-unsafe.
44 *
45 * @since 4.0
46 *
47 * @deprecated (4.3) use configuration classes provided 'org.apache.http.config'
48 * and 'org.apache.http.client.config'
49 */
50 @Deprecated
51 @Contract(threading = ThreadingBehavior.SAFE)
52 public class BasicHttpParams extends AbstractHttpParams implements Serializable, Cloneable {
53
54 private static final long serialVersionUID = -7086398485908701455L;
55
56 /** Map of HTTP parameters that this collection contains. */
57 private final Map<String, Object> parameters = new ConcurrentHashMap<String, Object>();
58
59 public BasicHttpParams() {
60 super();
61 }
62
63 @Override
64 public Object getParameter(final String name) {
65 return this.parameters.get(name);
66 }
67
68 @Override
69 public HttpParams setParameter(final String name, final Object value) {
70 if (name == null) {
71 return this;
72 }
73 if (value != null) {
74 this.parameters.put(name, value);
75 } else {
76 this.parameters.remove(name);
77 }
78 return this;
79 }
80
81 @Override
82 public boolean removeParameter(final String name) {
83 //this is to avoid the case in which the key has a null value
84 if (this.parameters.containsKey(name)) {
85 this.parameters.remove(name);
86 return true;
87 }
88 return false;
89 }
90
91 /**
92 * Assigns the value to all the parameter with the given names
93 *
94 * @param names array of parameter names
95 * @param value parameter value
96 */
97 public void setParameters(final String[] names, final Object value) {
98 for (final String name : names) {
99 setParameter(name, value);
100 }
101 }
102
103 /**
104 * Is the parameter set?
105 * <p>
106 * Uses {@link #getParameter(String)} (which is overrideable) to
107 * fetch the parameter value, if any.
108 * <p>
109 * Also @see {@link #isParameterSetLocally(String)}
110 *
111 * @param name parameter name
112 * @return true if parameter is defined and non-null
113 */
114 public boolean isParameterSet(final String name) {
115 return getParameter(name) != null;
116 }
117
118 /**
119 * Is the parameter set in this object?
120 * <p>
121 * The parameter value is fetched directly.
122 * <p>
123 * Also @see {@link #isParameterSet(String)}
124 *
125 * @param name parameter name
126 * @return true if parameter is defined and non-null
127 */
128 public boolean isParameterSetLocally(final String name) {
129 return this.parameters.get(name) != null;
130 }
131
132 /**
133 * Removes all parameters from this collection.
134 */
135 public void clear() {
136 this.parameters.clear();
137 }
138
139 /**
140 * Creates a copy of these parameters.
141 * This implementation calls {@link #clone()}.
142 *
143 * @return a new set of params holding a copy of the
144 * <i>local</i> parameters in this object.
145 *
146 * @throws UnsupportedOperationException if the clone() fails
147 */
148 @Override
149 public HttpParams copy() {
150 try {
151 return (HttpParams) clone();
152 } catch (final CloneNotSupportedException ex) {
153 throw new UnsupportedOperationException("Cloning not supported");
154 }
155 }
156
157 /**
158 * Clones the instance.
159 * Uses {@link #copyParams(HttpParams)} to copy the parameters.
160 */
161 @Override
162 public Object clone() throws CloneNotSupportedException {
163 final BasicHttpParams/../org/apache/http/params/BasicHttpParams.html#BasicHttpParams">BasicHttpParams clone = (BasicHttpParams) super.clone();
164 copyParams(clone);
165 return clone;
166 }
167
168 /**
169 * Copies the locally defined parameters to the argument parameters.
170 * This method is called from {@link #clone()}.
171 *
172 * @param target the parameters to which to copy
173 * @since 4.2
174 */
175 public void copyParams(final HttpParams target) {
176 for (final Map.Entry<String, Object> me : this.parameters.entrySet()) {
177 target.setParameter(me.getKey(), me.getValue());
178 }
179 }
180
181 /**
182 * Returns the current set of names.
183 *
184 * Changes to the underlying HttpParams are not reflected
185 * in the set - it is a snapshot.
186 *
187 * @return the names, as a Set<String>
188 * @since 4.2
189 */
190 @Override
191 public Set<String> getNames() {
192 return new HashSet<String>(this.parameters.keySet());
193 }
194
195 @Override
196 public String toString() {
197 return "[parameters=" + parameters + "]";
198 }
199 }