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.util.HashSet;
31  import java.util.Set;
32  
33  import org.apache.http.util.Args;
34  
35  /**
36   * {@link HttpParams} implementation that delegates resolution of a parameter
37   * to the given default {@link HttpParams} instance if the parameter is not
38   * present in the local one. The state of the local collection can be mutated,
39   * whereas the default collection is treated as read-only.
40   *
41   * @since 4.0
42   *
43   * @deprecated (4.3) use configuration classes provided 'org.apache.http.config'
44   *  and 'org.apache.http.client.config'
45   */
46  @Deprecated
47  public final class DefaultedHttpParams extends AbstractHttpParams {
48  
49      private final HttpParams local;
50      private final HttpParams defaults;
51  
52      /**
53       * Create the defaulted set of HttpParams.
54       *
55       * @param local the mutable set of HttpParams
56       * @param defaults the default set of HttpParams, not mutated by this class
57       */
58      public DefaultedHttpParams(final HttpParams local, final HttpParams defaults) {
59          super();
60          this.local = Args.notNull(local, "Local HTTP parameters");
61          this.defaults = defaults;
62      }
63  
64      /**
65       * Creates a copy of the local collection with the same default
66       */
67      @Override
68      public HttpParams copy() {
69          final HttpParams clone = this.local.copy();
70          return new DefaultedHttpParams(clone, this.defaults);
71      }
72  
73      /**
74       * Retrieves the value of the parameter from the local collection and, if the
75       * parameter is not set locally, delegates its resolution to the default
76       * collection.
77       */
78      @Override
79      public Object getParameter(final String name) {
80          Object obj = this.local.getParameter(name);
81          if (obj == null && this.defaults != null) {
82              obj = this.defaults.getParameter(name);
83          }
84          return obj;
85      }
86  
87      /**
88       * Attempts to remove the parameter from the local collection. This method
89       * <i>does not</i> modify the default collection.
90       */
91      @Override
92      public boolean removeParameter(final String name) {
93          return this.local.removeParameter(name);
94      }
95  
96      /**
97       * Sets the parameter in the local collection. This method <i>does not</i>
98       * modify the default collection.
99       */
100     @Override
101     public HttpParams setParameter(final String name, final Object value) {
102         return this.local.setParameter(name, value);
103     }
104 
105     /**
106      *
107      * @return the default HttpParams collection
108      */
109     public HttpParams getDefaults() {
110         return this.defaults;
111     }
112 
113     /**
114      * Returns the current set of names
115      * from both the local and default HttpParams instances.
116      *
117      * Changes to the underlying HttpParams intances are not reflected
118      * in the set - it is a snapshot.
119      *
120      * @return the combined set of names, as a Set&lt;String&gt;
121      * @since 4.2
122      * @throws UnsupportedOperationException if either the local or default HttpParams instances do not implement HttpParamsNames
123      */
124     @Override
125     public Set<String> getNames() {
126         final Set<String> combined = new HashSet<String>(getNames(defaults));
127         combined.addAll(getNames(this.local));
128         return combined ;
129     }
130 
131     /**
132      * Returns the current set of defaults names.
133      *
134      * Changes to the underlying HttpParams are not reflected
135      * in the set - it is a snapshot.
136      *
137      * @return the names, as a Set&lt;String&gt;
138      * @since 4.2
139      * @throws UnsupportedOperationException if the default HttpParams instance does not implement HttpParamsNames
140      */
141     public Set<String> getDefaultNames() {
142         return new HashSet<String>(getNames(this.defaults));
143     }
144 
145     /**
146      * Returns the current set of local names.
147      *
148      * Changes to the underlying HttpParams are not reflected
149      * in the set - it is a snapshot.
150      *
151      * @return the names, as a Set&lt;String&gt;
152      * @since 4.2
153      * @throws UnsupportedOperationException if the local HttpParams instance does not implement HttpParamsNames
154      */
155     public Set<String> getLocalNames() {
156         return new HashSet<String>(getNames(this.local));
157     }
158 
159     // Helper method
160     private Set<String> getNames(final HttpParams params) {
161         if (params instanceof HttpParamsNames) {
162             return ((HttpParamsNames) params).getNames();
163         }
164         throw new UnsupportedOperationException("HttpParams instance does not implement HttpParamsNames");
165     }
166 
167 }