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.annotation.NotThreadSafe;
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  @NotThreadSafe
41  public final class DefaultedHttpContext implements HttpContext {
42  
43      private final HttpContext local;
44      private final HttpContext defaults;
45  
46      public DefaultedHttpContext(final HttpContext local, final HttpContext defaults) {
47          super();
48          if (local == null) {
49              throw new IllegalArgumentException("HTTP context may not be null");
50          }
51          this.local = local;
52          this.defaults = defaults;
53      }
54  
55      public Object getAttribute(final String id) {
56          Object obj = this.local.getAttribute(id);
57          if (obj == null) {
58              return this.defaults.getAttribute(id);
59          } else {
60              return obj;
61          }
62      }
63  
64      public Object removeAttribute(final String id) {
65          return this.local.removeAttribute(id);
66      }
67  
68      public void setAttribute(final String id, final Object obj) {
69          this.local.setAttribute(id, obj);
70      }
71  
72      public HttpContext getDefaults() {
73          return this.defaults;
74      }
75  
76      @Override
77      public String toString() {
78          StringBuilder buf = new StringBuilder();
79          buf.append("[local: ").append(this.local);
80          buf.append("defaults: ").append(this.defaults);
81          buf.append("]");
82          return buf.toString();
83      }
84  
85  }