001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.params;
029
030import org.apache.http.annotation.ThreadingBehavior;
031import org.apache.http.annotation.Contract;
032
033import java.io.Serializable;
034import java.util.HashSet;
035import java.util.Map;
036import java.util.Set;
037import java.util.concurrent.ConcurrentHashMap;
038
039/**
040 * Default implementation of {@link HttpParams} interface.
041 * <p>
042 * Please note access to the internal structures of this class is not
043 * synchronized and therefore this class may be thread-unsafe.
044 *
045 * @since 4.0
046 *
047 * @deprecated (4.3) use configuration classes provided 'org.apache.http.config'
048 *  and 'org.apache.http.client.config'
049 */
050@Deprecated
051@Contract(threading = ThreadingBehavior.SAFE)
052public class BasicHttpParams extends AbstractHttpParams implements Serializable, Cloneable {
053
054    private static final long serialVersionUID = -7086398485908701455L;
055
056    /** Map of HTTP parameters that this collection contains. */
057    private final Map<String, Object> parameters = new ConcurrentHashMap<String, Object>();
058
059    public BasicHttpParams() {
060        super();
061    }
062
063    @Override
064    public Object getParameter(final String name) {
065        return this.parameters.get(name);
066    }
067
068    @Override
069    public HttpParams setParameter(final String name, final Object value) {
070        if (name == null) {
071            return this;
072        }
073        if (value != null) {
074            this.parameters.put(name, value);
075        } else {
076            this.parameters.remove(name);
077        }
078        return this;
079    }
080
081    @Override
082    public boolean removeParameter(final String name) {
083        //this is to avoid the case in which the key has a null value
084        if (this.parameters.containsKey(name)) {
085            this.parameters.remove(name);
086            return true;
087        } else {
088            return false;
089        }
090    }
091
092    /**
093     * Assigns the value to all the parameter with the given names
094     *
095     * @param names array of parameter names
096     * @param value parameter value
097     */
098    public void setParameters(final String[] names, final Object value) {
099        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
196    @Override
197    public String toString() {
198        return "[parameters=" + parameters + "]";
199    }
200}