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