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.conn;
29  
30  import java.io.IOException;
31  import java.util.concurrent.TimeUnit;
32  
33  import javax.net.ssl.SSLSession;
34  
35  import org.apache.http.HttpClientConnection;
36  import org.apache.http.HttpHost;
37  import org.apache.http.params.HttpParams;
38  import org.apache.http.protocol.HttpContext;
39  
40  import org.apache.http.conn.routing.HttpRoute;
41  
42  /**
43   * A client-side connection with advanced connection logic.
44   * Instances are typically obtained from a connection manager.
45   *
46   * @since 4.0
47   */
48  public interface ManagedClientConnection extends
49      HttpClientConnection, HttpRoutedConnection, ConnectionReleaseTrigger {
50  
51      /**
52       * Indicates whether this connection is secure.
53       * The return value is well-defined only while the connection is open.
54       * It may change even while the connection is open.
55       *
56       * @return  <code>true</code> if this connection is secure,
57       *          <code>false</code> otherwise
58       */
59      boolean isSecure();
60  
61      /**
62       * Obtains the current route of this connection.
63       *
64       * @return  the route established so far, or
65       *          <code>null</code> if not connected
66       */
67      HttpRoute getRoute();
68  
69      /**
70       * Obtains the SSL session of the underlying connection, if any.
71       * If this connection is open, and the underlying socket is an
72       * {@link javax.net.ssl.SSLSocket SSLSocket}, the SSL session of
73       * that socket is obtained. This is a potentially blocking operation.
74       * <br/>
75       * <b>Note:</b> Whether the underlying socket is an SSL socket
76       * can not necessarily be determined via {@link #isSecure}.
77       * Plain sockets may be considered secure, for example if they are
78       * connected to a known host in the same network segment.
79       * On the other hand, SSL sockets may be considered insecure,
80       * for example depending on the chosen cipher suite.
81       *
82       * @return  the underlying SSL session if available,
83       *          <code>null</code> otherwise
84       */
85      SSLSession getSSLSession();
86  
87      /**
88       * Opens this connection according to the given route.
89       *
90       * @param route     the route along which to open. It will be opened to
91       *                  the first proxy if present, or directly to the target.
92       * @param context   the context for opening this connection
93       * @param params    the parameters for opening this connection
94       *
95       * @throws IOException      in case of a problem
96       */
97      void open(HttpRoute route, HttpContext context, HttpParams params)
98          throws IOException;
99  
100     /**
101      * Indicates that a tunnel to the target has been established.
102      * The route is the one previously passed to {@link #open open}.
103      * Subsequently, {@link #layerProtocol layerProtocol} can be called
104      * to layer the TLS/SSL protocol on top of the tunnelled connection.
105      * <br/>
106      * <b>Note:</b> In HttpClient 3, a call to the corresponding method
107      * would automatically trigger the layering of the TLS/SSL protocol.
108      * This is not the case anymore, you can establish a tunnel without
109      * layering a new protocol over the connection.
110      *
111      * @param secure    <code>true</code> if the tunnel should be considered
112      *                  secure, <code>false</code> otherwise
113      * @param params    the parameters for tunnelling this connection
114      *
115      * @throws IOException  in case of a problem
116      */
117     void tunnelTarget(boolean secure, HttpParams params)
118         throws IOException;
119 
120     /**
121      * Indicates that a tunnel to an intermediate proxy has been established.
122      * This is used exclusively for so-called <i>proxy chains</i>, where
123      * a request has to pass through multiple proxies before reaching the
124      * target. In that case, all proxies but the last need to be tunnelled
125      * when establishing the connection. Tunnelling of the last proxy to the
126      * target is optional and would be indicated via {@link #tunnelTarget}.
127      *
128      * @param next      the proxy to which the tunnel was established.
129      *                  This is <i>not</i> the proxy <i>through</i> which
130      *                  the tunnel was established, but the new end point
131      *                  of the tunnel. The tunnel does <i>not</i> yet
132      *                  reach to the target, use {@link #tunnelTarget}
133      *                  to indicate an end-to-end tunnel.
134      * @param secure    <code>true</code> if the connection should be
135      *                  considered secure, <code>false</code> otherwise
136      * @param params    the parameters for tunnelling this connection
137      *
138      * @throws IOException  in case of a problem
139      */
140     void tunnelProxy(HttpHost next, boolean secure, HttpParams params)
141         throws IOException;
142 
143     /**
144      * Layers a new protocol on top of a {@link #tunnelTarget tunnelled}
145      * connection. This is typically used to create a TLS/SSL connection
146      * through a proxy.
147      * The route is the one previously passed to {@link #open open}.
148      * It is not guaranteed that the layered connection is
149      * {@link #isSecure secure}.
150      *
151      * @param context   the context for layering on top of this connection
152      * @param params    the parameters for layering on top of this connection
153      *
154      * @throws IOException      in case of a problem
155      */
156     void layerProtocol(HttpContext context, HttpParams params)
157         throws IOException;
158 
159     /**
160      * Marks this connection as being in a reusable communication state.
161      * The checkpoints for reuseable communication states (in the absence
162      * of pipelining) are before sending a request and after receiving
163      * the response in its entirety.
164      * The connection will automatically clear the checkpoint when
165      * used for communication. A call to this method indicates that
166      * the next checkpoint has been reached.
167      * <br/>
168      * A reusable communication state is necessary but not sufficient
169      * for the connection to be reused.
170      * A {@link #getRoute route} mismatch, the connection being closed,
171      * or other circumstances might prevent reuse.
172      */
173     void markReusable();
174 
175     /**
176      * Marks this connection as not being in a reusable state.
177      * This can be used immediately before releasing this connection
178      * to prevent its reuse. Reasons for preventing reuse include
179      * error conditions and the evaluation of a
180      * {@link org.apache.http.ConnectionReuseStrategy reuse strategy}.
181      * <br/>
182      * <b>Note:</b>
183      * It is <i>not</i> necessary to call here before writing to
184      * or reading from this connection. Communication attempts will
185      * automatically unmark the state as non-reusable. It can then
186      * be switched back using {@link #markReusable markReusable}.
187      */
188     void unmarkReusable();
189 
190     /**
191      * Indicates whether this connection is in a reusable communication state.
192      * See {@link #markReusable markReusable} and
193      * {@link #unmarkReusable unmarkReusable} for details.
194      *
195      * @return  <code>true</code> if this connection is marked as being in
196      *          a reusable communication state,
197      *          <code>false</code> otherwise
198      */
199     boolean isMarkedReusable();
200 
201     /**
202      * Assigns a state object to this connection. Connection managers may make
203      * use of the connection state when allocating persistent connections.
204      *
205      * @param state The state object
206      */
207     void setState(Object state);
208 
209     /**
210      * Returns the state object associated with this connection.
211      *
212      * @return The state object
213      */
214     Object getState();
215 
216     /**
217      * Sets the duration that this connection can remain idle before it is
218      * reused. The connection should not be used again if this time elapses. The
219      * idle duration must be reset after each request sent over this connection.
220      * The elapsed time starts counting when the connection is released, which
221      * is typically after the headers (and any response body, if present) is
222      * fully consumed.
223      */
224     void setIdleDuration(long duration, TimeUnit unit);
225 
226 }