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 HttpContext local, final HttpContext defaults) {
49          super();
50          this.local = Args.notNull(local, "HTTP context");
51          this.defaults = defaults;
52      }
53  
54      public Object getAttribute(final String id) {
55          final Object obj = this.local.getAttribute(id);
56          if (obj == null) {
57              return this.defaults.getAttribute(id);
58          } else {
59              return obj;
60          }
61      }
62  
63      public Object removeAttribute(final String id) {
64          return this.local.removeAttribute(id);
65      }
66  
67      public void setAttribute(final String id, final Object obj) {
68          this.local.setAttribute(id, obj);
69      }
70  
71      public HttpContext getDefaults() {
72          return this.defaults;
73      }
74  
75      @Override
76      public String toString() {
77          final StringBuilder buf = new StringBuilder();
78          buf.append("[local: ").append(this.local);
79          buf.append("defaults: ").append(this.defaults);
80          buf.append("]");
81          return buf.toString();
82      }
83  
84  }