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          return context instanceof HttpCoreContext
82                          ? (HttpCoreContext) context
83                          : new HttpCoreContext(context);
84      }
85  
86      private final HttpContext context;
87  
88      public HttpCoreContext(final HttpContext context) {
89          super();
90          this.context = context;
91      }
92  
93      public HttpCoreContext() {
94          super();
95          this.context = new BasicHttpContext();
96      }
97  
98      @Override
99      public Object getAttribute(final String id) {
100         return context.getAttribute(id);
101     }
102 
103     @Override
104     public void setAttribute(final String id, final Object obj) {
105         context.setAttribute(id, obj);
106     }
107 
108     @Override
109     public Object removeAttribute(final String id) {
110         return context.removeAttribute(id);
111     }
112 
113     public <T> T getAttribute(final String attribname, final Class<T> clazz) {
114         Args.notNull(clazz, "Attribute class");
115         final Object obj = getAttribute(attribname);
116         if (obj == null) {
117             return null;
118         }
119         return clazz.cast(obj);
120     }
121 
122     public <T extends HttpConnection> T getConnection(final Class<T> clazz) {
123         return getAttribute(HTTP_CONNECTION, clazz);
124     }
125 
126     public HttpConnection getConnection() {
127         return getAttribute(HTTP_CONNECTION, HttpConnection.class);
128     }
129 
130     public HttpRequest getRequest() {
131         return getAttribute(HTTP_REQUEST, HttpRequest.class);
132     }
133 
134     public boolean isRequestSent() {
135         final Boolean b = getAttribute(HTTP_REQ_SENT, Boolean.class);
136         return b != null && b.booleanValue();
137     }
138 
139     public HttpResponse getResponse() {
140         return getAttribute(HTTP_RESPONSE, HttpResponse.class);
141     }
142 
143     public void setTargetHost(final HttpHost host) {
144         setAttribute(HTTP_TARGET_HOST, host);
145     }
146 
147     public HttpHost getTargetHost() {
148         return getAttribute(HTTP_TARGET_HOST, HttpHost.class);
149     }
150 
151 }