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