View Javadoc

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          } else {
88              return false;
89          }
90      }
91  
92      /**
93       * Assigns the value to all the parameter with the given names
94       *
95       * @param names array of parameter names
96       * @param value parameter value
97       */
98      public void setParameters(final String[] names, final Object value) {
99          for (final String name : names) {
100             setParameter(name, value);
101         }
102     }
103 
104     /**
105      * Is the parameter set?
106      * <p>
107      * Uses {@link #getParameter(String)} (which is overrideable) to
108      * fetch the parameter value, if any.
109      * <p>
110      * Also @see {@link #isParameterSetLocally(String)}
111      *
112      * @param name parameter name
113      * @return true if parameter is defined and non-null
114      */
115     public boolean isParameterSet(final String name) {
116         return getParameter(name) != null;
117     }
118 
119     /**
120      * Is the parameter set in this object?
121      * <p>
122      * The parameter value is fetched directly.
123      * <p>
124      * Also @see {@link #isParameterSet(String)}
125      *
126      * @param name parameter name
127      * @return true if parameter is defined and non-null
128      */
129     public boolean isParameterSetLocally(final String name) {
130         return this.parameters.get(name) != null;
131     }
132 
133     /**
134      * Removes all parameters from this collection.
135      */
136     public void clear() {
137         this.parameters.clear();
138     }
139 
140     /**
141      * Creates a copy of these parameters.
142      * This implementation calls {@link #clone()}.
143      *
144      * @return  a new set of params holding a copy of the
145      *          <i>local</i> parameters in this object.
146      *
147      * @throws UnsupportedOperationException if the clone() fails
148      */
149     @Override
150     public HttpParams copy() {
151         try {
152             return (HttpParams) clone();
153         } catch (final CloneNotSupportedException ex) {
154             throw new UnsupportedOperationException("Cloning not supported");
155         }
156     }
157 
158     /**
159      * Clones the instance.
160      * Uses {@link #copyParams(HttpParams)} to copy the parameters.
161      */
162     @Override
163     public Object clone() throws CloneNotSupportedException {
164         final BasicHttpParams clone = (BasicHttpParams) super.clone();
165         copyParams(clone);
166         return clone;
167     }
168 
169     /**
170      * Copies the locally defined parameters to the argument parameters.
171      * This method is called from {@link #clone()}.
172      *
173      * @param target    the parameters to which to copy
174      * @since 4.2
175      */
176     public void copyParams(final HttpParams target) {
177         for (final Map.Entry<String, Object> me : this.parameters.entrySet()) {
178             target.setParameter(me.getKey(), me.getValue());
179         }
180     }
181 
182     /**
183      * Returns the current set of names.
184      *
185      * Changes to the underlying HttpParams are not reflected
186      * in the set - it is a snapshot.
187      *
188      * @return the names, as a Set&lt;String&gt;
189      * @since 4.2
190      */
191     @Override
192     public Set<String> getNames() {
193         return new HashSet<String>(this.parameters.keySet());
194     }
195 }