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.hc.core5.http.protocol;
29  
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  import javax.net.ssl.SSLSession;
34  
35  import org.apache.hc.core5.http.EndpointDetails;
36  import org.apache.hc.core5.http.HttpRequest;
37  import org.apache.hc.core5.http.HttpResponse;
38  import org.apache.hc.core5.http.HttpVersion;
39  import org.apache.hc.core5.http.ProtocolVersion;
40  import org.apache.hc.core5.util.Args;
41  
42  /**
43   * Core execution {@link HttpContext}.
44   * <p>
45   * IMPORTANT: This class is NOT thread-safe and MUST NOT be used concurrently by
46   * multiple message exchanges.
47   *
48   * @since 4.3
49   */
50  public class HttpCoreContext implements HttpContext {
51  
52      /**
53       * @deprecated Use getter methods
54       */
55      @Deprecated
56      public static final String CONNECTION_ENDPOINT  = HttpContext.RESERVED_PREFIX + "connection-endpoint";
57  
58      /**
59       * @deprecated Use getter methods
60       */
61      @Deprecated
62      public static final String SSL_SESSION = HttpContext.RESERVED_PREFIX + "ssl-session";
63  
64      /**
65       * @deprecated Use getter methods
66       */
67      @Deprecated
68      public static final String HTTP_REQUEST     = HttpContext.RESERVED_PREFIX + "request";
69  
70      /**
71       * @deprecated Use getter methods
72       */
73      @Deprecated
74      public static final String HTTP_RESPONSE    = HttpContext.RESERVED_PREFIX + "response";
75  
76      public static HttpCoreContext create() {
77          return new HttpCoreContext();
78      }
79  
80      /**
81       * @deprecated Use {@link #cast(HttpContext)}.
82       */
83      @Deprecated
84      public static HttpCoreContext adapt(final HttpContext context) {
85          if (context == null) {
86              return new HttpCoreContext();
87          }
88          if (context instanceof HttpCoreContext) {
89              return (HttpCoreContext) context;
90          }
91          return new HttpCoreContext(context);
92      }
93  
94      /**
95       * Casts the given generic {@link HttpContext} as {@link HttpCoreContext}
96       * or creates a new {@link HttpCoreContext} with the given {@link HttpContext}
97       * as a parent.
98       *
99       * @since 5.3
100      */
101     public static HttpCoreContext cast(final HttpContext context) {
102         if (context instanceof HttpCoreContext) {
103             return (HttpCoreContext) context;
104         }
105         return new HttpCoreContext(context);
106     }
107 
108     private final HttpContext parentContext;
109     private Map<String, Object> map;
110     private ProtocolVersion version;
111     private HttpRequest request;
112     private HttpResponse response;
113     private EndpointDetails endpointDetails;
114     private SSLSession sslSession;
115 
116     public HttpCoreContext(final HttpContext parentContext) {
117         super();
118         this.parentContext = parentContext;
119     }
120 
121     public HttpCoreContext() {
122         super();
123         this.parentContext = null;
124     }
125 
126     /**
127      * Represents the protocol version used by the message exchange.
128      * <p>
129      * This context attribute is expected to be populated by the protocol handler
130      * in the course of request execution.
131      *
132      * @since 5.0
133      */
134     @Override
135     public ProtocolVersion getProtocolVersion() {
136         return this.version != null ? this.version : HttpVersion.HTTP_1_1;
137     }
138 
139     /**
140      * @since 5.0
141      */
142     @Override
143     public void setProtocolVersion(final ProtocolVersion version) {
144         this.version = version;
145     }
146 
147     @Override
148     public Object getAttribute(final String id) {
149         Object o = map != null ? map.get(id) : null;
150         if (o == null && parentContext != null) {
151             o = parentContext.getAttribute(id);
152         }
153         return o;
154     }
155 
156     @Override
157     public Object setAttribute(final String id, final Object obj) {
158         if (map == null) {
159             map = new HashMap<>();
160         }
161         return map.put(id, obj);
162     }
163 
164     @Override
165     public Object removeAttribute(final String id) {
166         if (map != null) {
167             return map.remove(id);
168         } else {
169             return null;
170         }
171     }
172 
173     public <T> T getAttribute(final String id, final Class<T> clazz) {
174         Args.notNull(clazz, "Attribute class");
175         final Object obj = getAttribute(id);
176         if (obj == null) {
177             return null;
178         }
179         return clazz.cast(obj);
180     }
181 
182     /**
183      * Represents current request message head.
184      * <p>
185      * This context attribute is expected to be populated by the protocol handler
186      * in the course of request execution.
187      */
188     public HttpRequest getRequest() {
189         return request;
190     }
191 
192     /**
193      * @since 5.3
194      */
195     public void setRequest(final HttpRequest request) {
196         this.request = request;
197     }
198 
199     /**
200      * Represents current response message head.
201      * <p>
202      * This context attribute is expected to be populated by the protocol handler
203      * in the course of request execution.
204      */
205     public HttpResponse getResponse() {
206         return response;
207     }
208 
209     /**
210      * @since 5.3
211      */
212     public void setResponse(final HttpResponse response) {
213         this.response = response;
214     }
215 
216     /**
217      * Represents current connection endpoint details.
218      * <p>
219      * This context attribute is expected to be populated by the protocol handler
220      * in the course of request execution.
221      * @since 5.0
222      */
223     public EndpointDetails getEndpointDetails() {
224         return endpointDetails;
225     }
226 
227     /**
228      * @since 5.3
229      */
230     public void setEndpointDetails(final EndpointDetails endpointDetails) {
231         this.endpointDetails = endpointDetails;
232     }
233 
234     /**
235      * Represents current TLS session details.
236      * <p>
237      * This context attribute is expected to be populated by the protocol handler
238      * in the course of request execution.
239      * @since 5.0
240      */
241     public SSLSession getSSLSession() {
242         return sslSession;
243     }
244 
245     /**
246      * @since 5.3
247      */
248     public void setSSLSession(final SSLSession sslSession) {
249         this.sslSession = sslSession;
250     }
251 
252     @Override
253     public String toString() {
254         return "HttpCoreContext{" +
255                 "version=" + version +
256                 ", request=" + request +
257                 ", response=" + response +
258                 ", endpointDetails=" + endpointDetails +
259                 ", sslSession=" + sslSession +
260                 '}';
261     }
262 
263 }