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 javax.net.ssl.SSLSession;
31  
32  import org.apache.hc.core5.http.EndpointDetails;
33  import org.apache.hc.core5.http.HttpRequest;
34  import org.apache.hc.core5.http.HttpResponse;
35  import org.apache.hc.core5.http.ProtocolVersion;
36  import org.apache.hc.core5.util.Args;
37  
38  /**
39   * Implementation of {@link HttpContext} that provides convenience
40   * setters for user assignable attributes and getter for readable attributes.
41   *
42   * @since 4.3
43   */
44  public class HttpCoreContext implements HttpContext {
45  
46      /**
47       * Attribute name of a {@link EndpointDetails} object that
48       * represents the actual connection endpoint details.
49       */
50      public static final String CONNECTION_ENDPOINT  = HttpContext.RESERVED_PREFIX + "connection-endpoint";
51  
52      /**
53       * Attribute name of a {@link SSLSession} object that
54       * represents the actual connection endpoint details.
55       */
56      public static final String SSL_SESSION = HttpContext.RESERVED_PREFIX + "ssl-session";
57  
58      /**
59       * Attribute name of a {@link HttpRequest} object that
60       * represents the actual HTTP request.
61       */
62      public static final String HTTP_REQUEST     = HttpContext.RESERVED_PREFIX + "request";
63  
64      /**
65       * Attribute name of a {@link HttpResponse} object that
66       * represents the actual HTTP response.
67       */
68      public static final String HTTP_RESPONSE    = HttpContext.RESERVED_PREFIX + "response";
69  
70      public static HttpCoreContext create() {
71          return new HttpCoreContext();
72      }
73  
74      public static HttpCoreContext adapt(final HttpContext context) {
75          if (context == null) {
76              return new HttpCoreContext();
77          }
78          if (context instanceof HttpCoreContext) {
79              return (HttpCoreContext) context;
80          }
81          return new HttpCoreContext(context);
82      }
83  
84      private final HttpContext context;
85  
86      public HttpCoreContext(final HttpContext context) {
87          super();
88          this.context = context;
89      }
90  
91      public HttpCoreContext() {
92          super();
93          this.context = new BasicHttpContext();
94      }
95  
96      /**
97       * @since 5.0
98       */
99      @Override
100     public ProtocolVersion getProtocolVersion() {
101         return this.context.getProtocolVersion();
102     }
103 
104     /**
105      * @since 5.0
106      */
107     @Override
108     public void setProtocolVersion(final ProtocolVersion version) {
109         this.context.setProtocolVersion(version);
110     }
111 
112     @Override
113     public Object getAttribute(final String id) {
114         return context.getAttribute(id);
115     }
116 
117     @Override
118     public void setAttribute(final String id, final Object obj) {
119         context.setAttribute(id, obj);
120     }
121 
122     @Override
123     public Object removeAttribute(final String id) {
124         return context.removeAttribute(id);
125     }
126 
127     public <T> T getAttribute(final String attribname, final Class<T> clazz) {
128         Args.notNull(clazz, "Attribute class");
129         final Object obj = getAttribute(attribname);
130         if (obj == null) {
131             return null;
132         }
133         return clazz.cast(obj);
134     }
135 
136     /**
137      * @since 5.0
138      */
139     public SSLSession getSSLSession() {
140         return getAttribute(SSL_SESSION, SSLSession.class);
141     }
142 
143     /**
144      * @since 5.0
145      */
146     public EndpointDetails getEndpointDetails() {
147         return getAttribute(CONNECTION_ENDPOINT, EndpointDetails.class);
148     }
149 
150     public HttpRequest getRequest() {
151         return getAttribute(HTTP_REQUEST, HttpRequest.class);
152     }
153 
154     public HttpResponse getResponse() {
155         return getAttribute(HTTP_RESPONSE, HttpResponse.class);
156     }
157 
158 }