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 java.io.Serializable;
31  import java.util.HashSet;
32  import java.util.Map;
33  import java.util.Set;
34  import java.util.concurrent.ConcurrentHashMap;
35  
36  import org.apache.http.annotation.ThreadSafe;
37  
38  /**
39   * Default implementation of {@link HttpParams} interface.
40   * <p>
41   * Please note access to the internal structures of this class is not
42   * synchronized and therefore this class may be thread-unsafe.
43   *
44   * @since 4.0
45   *
46   * @deprecated (4.3) use configuration classes provided 'org.apache.http.config'
47   *  and 'org.apache.http.client.config'
48   */
49  @Deprecated
50  @ThreadSafe
51  public class BasicHttpParams extends AbstractHttpParams implements Serializable, Cloneable {
52  
53      private static final long serialVersionUID = -7086398485908701455L;
54  
55      /** Map of HTTP parameters that this collection contains. */
56      private final Map<String, Object> parameters = new ConcurrentHashMap<String, Object>();
57  
58      public BasicHttpParams() {
59          super();
60      }
61  
62      @Override
63      public Object getParameter(final String name) {
64          return this.parameters.get(name);
65      }
66  
67      @Override
68      public HttpParams setParameter(final String name, final Object value) {
69          if (name == null) {
70              return this;
71          }
72          if (value != null) {
73              this.parameters.put(name, value);
74          } else {
75              this.parameters.remove(name);
76          }
77          return this;
78      }
79  
80      @Override
81      public boolean removeParameter(final String name) {
82          //this is to avoid the case in which the key has a null value
83          if (this.parameters.containsKey(name)) {
84              this.parameters.remove(name);
85              return true;
86          } else {
87              return false;
88          }
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 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 }