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