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.params.HttpParams;
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 public final class DefaultedHttpParams extends AbstractHttpParams {
44
45 private final HttpParams local;
46 private final HttpParams defaults;
47
48 /**
49 * Create the defaulted set of HttpParams.
50 *
51 * @param local the mutable set of HttpParams
52 * @param defaults the default set of HttpParams, not mutated by this class
53 */
54 public DefaultedHttpParams(final HttpParams local, final HttpParams defaults) {
55 super();
56 if (local == null) {
57 throw new IllegalArgumentException("HTTP parameters may not be null");
58 }
59 this.local = local;
60 this.defaults = defaults;
61 }
62
63 /**
64 * Creates a copy of the local collection with the same default
65 *
66 * @deprecated (4.1)
67 */
68 @Deprecated
69 public HttpParams copy() {
70 HttpParams clone = this.local.copy();
71 return new DefaultedHttpParams(clone, this.defaults);
72 }
73
74 /**
75 * Retrieves the value of the parameter from the local collection and, if the
76 * parameter is not set locally, delegates its resolution to the default
77 * collection.
78 */
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 public boolean removeParameter(final String name) {
92 return this.local.removeParameter(name);
93 }
94
95 /**
96 * Sets the parameter in the local collection. This method <i>does not</i>
97 * modify the default collection.
98 */
99 public HttpParams setParameter(final String name, final Object value) {
100 return this.local.setParameter(name, value);
101 }
102
103 /**
104 *
105 * @return the default HttpParams collection
106 * @deprecated (4.1.1) do not use, will be removed in a later version
107 */
108 @Deprecated
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<String>
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 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<String>
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<String>
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(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 }