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.protocol;
29  
30  import org.apache.http.util.Args;
31  
32  /**
33   * {@link HttpContext} implementation that delegates resolution of an attribute
34   * to the given default {@link HttpContext} instance if the attribute is not
35   * present in the local one. The state of the local context can be mutated,
36   * whereas the default context is treated as read-only.
37   *
38   * @since 4.0
39   *
40   * @deprecated (4.3) no longer used.
41   */
42  @Deprecated
43  public final class DefaultedHttpContext implements HttpContext {
44  
45      private final HttpContext local;
46      private final HttpContext defaults;
47  
48      public DefaultedHttpContext(final HttpContextext.html#HttpContext">HttpContext local, final HttpContext defaults) {
49          super();
50          this.local = Args.notNull(local, "HTTP context");
51          this.defaults = defaults;
52      }
53  
54      @Override
55      public Object getAttribute(final String id) {
56          final Object obj = this.local.getAttribute(id);
57          if (obj == null) {
58              return this.defaults.getAttribute(id);
59          }
60          return obj;
61      }
62  
63      @Override
64      public Object removeAttribute(final String id) {
65          return this.local.removeAttribute(id);
66      }
67  
68      @Override
69      public void setAttribute(final String id, final Object obj) {
70          this.local.setAttribute(id, obj);
71      }
72  
73      public HttpContext getDefaults() {
74          return this.defaults;
75      }
76  
77      @Override
78      public String toString() {
79          final StringBuilder buf = new StringBuilder();
80          buf.append("[local: ").append(this.local);
81          buf.append("defaults: ").append(this.defaults);
82          buf.append("]");
83          return buf.toString();
84      }
85  
86  }