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.HttpConnection;
31  import org.apache.http.HttpHost;
32  import org.apache.http.HttpRequest;
33  import org.apache.http.HttpResponse;
34  import org.apache.http.util.Args;
35  
36  /**
37   * Implementation of {@link HttpContext} that provides convenience
38   * setters for user assignable attributes and getter for readable attributes.
39   *
40   * @since 4.3
41   */
42  public class HttpCoreContext implements HttpContext {
43  
44      /**
45       * Attribute name of a {@link org.apache.http.HttpConnection} object that
46       * represents the actual HTTP connection.
47       */
48      public static final String HTTP_CONNECTION  = "http.connection";
49  
50      /**
51       * Attribute name of a {@link org.apache.http.HttpRequest} object that
52       * represents the actual HTTP request.
53       */
54      public static final String HTTP_REQUEST     = "http.request";
55  
56      /**
57       * Attribute name of a {@link org.apache.http.HttpResponse} object that
58       * represents the actual HTTP response.
59       */
60      public static final String HTTP_RESPONSE    = "http.response";
61  
62      /**
63       * Attribute name of a {@link org.apache.http.HttpHost} object that
64       * represents the connection target.
65       */
66      public static final String HTTP_TARGET_HOST = "http.target_host";
67  
68      /**
69       * Attribute name of a {@link Boolean} object that represents the
70       * the flag indicating whether the actual request has been fully transmitted
71       * to the target host.
72       */
73      public static final String HTTP_REQ_SENT    = "http.request_sent";
74  
75      public static HttpCoreContext create() {
76          return new HttpCoreContext(new BasicHttpContext());
77      }
78  
79      public static HttpCoreContext adapt(final HttpContext context) {
80          Args.notNull(context, "HTTP context");
81          if (context instanceof HttpCoreContext) {
82              return (HttpCoreContext) context;
83          } else {
84              return new HttpCoreContext(context);
85          }
86      }
87  
88      private final HttpContext context;
89  
90      public HttpCoreContext(final HttpContext context) {
91          super();
92          this.context = context;
93      }
94  
95      public HttpCoreContext() {
96          super();
97          this.context = new BasicHttpContext();
98      }
99  
100     @Override
101     public Object getAttribute(final String id) {
102         return context.getAttribute(id);
103     }
104 
105     @Override
106     public void setAttribute(final String id, final Object obj) {
107         context.setAttribute(id, obj);
108     }
109 
110     @Override
111     public Object removeAttribute(final String id) {
112         return context.removeAttribute(id);
113     }
114 
115     public <T> T getAttribute(final String attribname, final Class<T> clazz) {
116         Args.notNull(clazz, "Attribute class");
117         final Object obj = getAttribute(attribname);
118         if (obj == null) {
119             return null;
120         }
121         return clazz.cast(obj);
122     }
123 
124     public <T extends HttpConnection> T getConnection(final Class<T> clazz) {
125         return getAttribute(HTTP_CONNECTION, clazz);
126     }
127 
128     public HttpConnection getConnection() {
129         return getAttribute(HTTP_CONNECTION, HttpConnection.class);
130     }
131 
132     public HttpRequest getRequest() {
133         return getAttribute(HTTP_REQUEST, HttpRequest.class);
134     }
135 
136     public boolean isRequestSent() {
137         final Boolean b = getAttribute(HTTP_REQ_SENT, Boolean.class);
138         return b != null && b.booleanValue();
139     }
140 
141     public HttpResponse getResponse() {
142         return getAttribute(HTTP_RESPONSE, HttpResponse.class);
143     }
144 
145     public void setTargetHost(final HttpHost host) {
146         setAttribute(HTTP_TARGET_HOST, host);
147     }
148 
149     public HttpHost getTargetHost() {
150         return getAttribute(HTTP_TARGET_HOST, HttpHost.class);
151     }
152 
153 }