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 public HttpParams copy() {
68 final HttpParams clone = this.local.copy();
69 return new DefaultedHttpParams(clone, this.defaults);
70 }
71
72 /**
73 * Retrieves the value of the parameter from the local collection and, if the
74 * parameter is not set locally, delegates its resolution to the default
75 * collection.
76 */
77 public Object getParameter(final String name) {
78 Object obj = this.local.getParameter(name);
79 if (obj == null && this.defaults != null) {
80 obj = this.defaults.getParameter(name);
81 }
82 return obj;
83 }
84
85 /**
86 * Attempts to remove the parameter from the local collection. This method
87 * <i>does not</i> modify the default collection.
88 */
89 public boolean removeParameter(final String name) {
90 return this.local.removeParameter(name);
91 }
92
93 /**
94 * Sets the parameter in the local collection. This method <i>does not</i>
95 * modify the default collection.
96 */
97 public HttpParams setParameter(final String name, final Object value) {
98 return this.local.setParameter(name, value);
99 }
100
101 /**
102 *
103 * @return the default HttpParams collection
104 */
105 public HttpParams getDefaults() {
106 return this.defaults;
107 }
108
109 /**
110 * Returns the current set of names
111 * from both the local and default HttpParams instances.
112 *
113 * Changes to the underlying HttpParams intances are not reflected
114 * in the set - it is a snapshot.
115 *
116 * @return the combined set of names, as a Set<String>
117 * @since 4.2
118 * @throws UnsupportedOperationException if either the local or default HttpParams instances do not implement HttpParamsNames
119 */
120 @Override
121 public Set<String> getNames() {
122 final Set<String> combined = new HashSet<String>(getNames(defaults));
123 combined.addAll(getNames(this.local));
124 return combined ;
125 }
126
127 /**
128 * Returns the current set of defaults names.
129 *
130 * Changes to the underlying HttpParams are not reflected
131 * in the set - it is a snapshot.
132 *
133 * @return the names, as a Set<String>
134 * @since 4.2
135 * @throws UnsupportedOperationException if the default HttpParams instance does not implement HttpParamsNames
136 */
137 public Set<String> getDefaultNames() {
138 return new HashSet<String>(getNames(this.defaults));
139 }
140
141 /**
142 * Returns the current set of local names.
143 *
144 * Changes to the underlying HttpParams are not reflected
145 * in the set - it is a snapshot.
146 *
147 * @return the names, as a Set<String>
148 * @since 4.2
149 * @throws UnsupportedOperationException if the local HttpParams instance does not implement HttpParamsNames
150 */
151 public Set<String> getLocalNames() {
152 return new HashSet<String>(getNames(this.local));
153 }
154
155 // Helper method
156 private Set<String> getNames(final HttpParams params) {
157 if (params instanceof HttpParamsNames) {
158 return ((HttpParamsNames) params).getNames();
159 }
160 throw new UnsupportedOperationException("HttpParams instance does not implement HttpParamsNames");
161 }
162
163 }