ManagedClientConnection.java revision 069490a5ca2fd1988d29daf45d892f47ad665115
1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/ManagedClientConnection.java $
3 * $Revision: 672969 $
4 * $Date: 2008-06-30 18:09:50 -0700 (Mon, 30 Jun 2008) $
5 *
6 * ====================================================================
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements.  See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership.  The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License.  You may obtain a copy of the License at
14 *
15 *   http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied.  See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 * ====================================================================
24 *
25 * This software consists of voluntary contributions made by many
26 * individuals on behalf of the Apache Software Foundation.  For more
27 * information on the Apache Software Foundation, please see
28 * <http://www.apache.org/>.
29 *
30 */
31
32package org.apache.http.conn;
33
34import java.io.IOException;
35import java.util.concurrent.TimeUnit;
36
37import javax.net.ssl.SSLSession;
38
39import org.apache.http.HttpClientConnection;
40import org.apache.http.HttpInetConnection;
41import org.apache.http.HttpHost;
42import org.apache.http.params.HttpParams;
43import org.apache.http.protocol.HttpContext;
44
45import org.apache.http.conn.routing.HttpRoute;
46
47
48
49/**
50 * A client-side connection with advanced connection logic.
51 * Instances are typically obtained from a connection manager.
52 *
53 * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
54 *
55 *
56 * <!-- empty lines to avoid svn diff problems -->
57 * @version   $Revision: 672969 $
58 *
59 * @since 4.0
60 */
61public interface ManagedClientConnection extends
62    HttpClientConnection, HttpInetConnection, ConnectionReleaseTrigger {
63
64
65    /**
66     * Indicates whether this connection is secure.
67     * The return value is well-defined only while the connection is open.
68     * It may change even while the connection is open.
69     *
70     * @return  <code>true</code> if this connection is secure,
71     *          <code>false</code> otherwise
72     */
73    boolean isSecure()
74        ;
75
76
77    /**
78     * Obtains the current route of this connection.
79     *
80     * @return  the route established so far, or
81     *          <code>null</code> if not connected
82     */
83    HttpRoute getRoute()
84        ;
85
86
87    /**
88     * Obtains the SSL session of the underlying connection, if any.
89     * If this connection is open, and the underlying socket is an
90     * {@link javax.net.ssl.SSLSocket SSLSocket}, the SSL session of
91     * that socket is obtained. This is a potentially blocking operation.
92     * <br/>
93     * <b>Note:</b> Whether the underlying socket is an SSL socket
94     * can not necessarily be determined via {@link #isSecure}.
95     * Plain sockets may be considered secure, for example if they are
96     * connected to a known host in the same network segment.
97     * On the other hand, SSL sockets may be considered insecure,
98     * for example depending on the chosen cipher suite.
99     *
100     * @return  the underlying SSL session if available,
101     *          <code>null</code> otherwise
102     */
103    SSLSession getSSLSession()
104        ;
105
106
107    /**
108     * Opens this connection according to the given route.
109     *
110     * @param route     the route along which to open. It will be opened to
111     *                  the first proxy if present, or directly to the target.
112     * @param context   the context for opening this connection
113     * @param params    the parameters for opening this connection
114     *
115     * @throws IOException      in case of a problem
116     */
117    void open(HttpRoute route, HttpContext context, HttpParams params)
118        throws IOException
119        ;
120
121
122    /**
123     * Indicates that a tunnel to the target has been established.
124     * The route is the one previously passed to {@link #open open}.
125     * Subsequently, {@link #layerProtocol layerProtocol} can be called
126     * to layer the TLS/SSL protocol on top of the tunnelled connection.
127     * <br/>
128     * <b>Note:</b> In HttpClient 3, a call to the corresponding method
129     * would automatically trigger the layering of the TLS/SSL protocol.
130     * This is not the case anymore, you can establish a tunnel without
131     * layering a new protocol over the connection.
132     *
133     * @param secure    <code>true</code> if the tunnel should be considered
134     *                  secure, <code>false</code> otherwise
135     * @param params    the parameters for tunnelling this connection
136     *
137     * @throws IOException  in case of a problem
138     */
139    void tunnelTarget(boolean secure, HttpParams params)
140        throws IOException
141        ;
142
143
144    /**
145     * Indicates that a tunnel to an intermediate proxy has been established.
146     * This is used exclusively for so-called <i>proxy chains</i>, where
147     * a request has to pass through multiple proxies before reaching the
148     * target. In that case, all proxies but the last need to be tunnelled
149     * when establishing the connection. Tunnelling of the last proxy to the
150     * target is optional and would be indicated via {@link #tunnelTarget}.
151     *
152     * @param next      the proxy to which the tunnel was established.
153     *                  This is <i>not</i> the proxy <i>through</i> which
154     *                  the tunnel was established, but the new end point
155     *                  of the tunnel. The tunnel does <i>not</i> yet
156     *                  reach to the target, use {@link #tunnelTarget}
157     *                  to indicate an end-to-end tunnel.
158     * @param secure    <code>true</code> if the connection should be
159     *                  considered secure, <code>false</code> otherwise
160     * @param params    the parameters for tunnelling this connection
161     *
162     * @throws IOException  in case of a problem
163     */
164    void tunnelProxy(HttpHost next, boolean secure, HttpParams params)
165        throws IOException
166        ;
167
168
169    /**
170     * Layers a new protocol on top of a {@link #tunnelTarget tunnelled}
171     * connection. This is typically used to create a TLS/SSL connection
172     * through a proxy.
173     * The route is the one previously passed to {@link #open open}.
174     * It is not guaranteed that the layered connection is
175     * {@link #isSecure secure}.
176     *
177     * @param context   the context for layering on top of this connection
178     * @param params    the parameters for layering on top of this connection
179     *
180     * @throws IOException      in case of a problem
181     */
182    void layerProtocol(HttpContext context, HttpParams params)
183        throws IOException
184        ;
185
186
187    /**
188     * Marks this connection as being in a reusable communication state.
189     * The checkpoints for reuseable communication states (in the absence
190     * of pipelining) are before sending a request and after receiving
191     * the response in it's entirety.
192     * The connection will automatically clear the checkpoint when
193     * used for communication. A call to this method indicates that
194     * the next checkpoint has been reached.
195     * <br/>
196     * A reusable communication state is necessary but not sufficient
197     * for the connection to be reused.
198     * A {@link #getRoute route} mismatch, the connection being closed,
199     * or other circumstances might prevent reuse.
200     */
201    void markReusable()
202        ;
203
204
205    /**
206     * Marks this connection as not being in a reusable state.
207     * This can be used immediately before releasing this connection
208     * to prevent it's reuse. Reasons for preventing reuse include
209     * error conditions and the evaluation of a
210     * {@link org.apache.http.ConnectionReuseStrategy reuse strategy}.
211     * <br/>
212     * <b>Note:</b>
213     * It is <i>not</i> necessary to call here before writing to
214     * or reading from this connection. Communication attempts will
215     * automatically unmark the state as non-reusable. It can then
216     * be switched back using {@link #markReusable markReusable}.
217     */
218    void unmarkReusable()
219        ;
220
221
222    /**
223     * Indicates whether this connection is in a reusable communication state.
224     * See {@link #markReusable markReusable} and
225     * {@link #unmarkReusable unmarkReusable} for details.
226     *
227     * @return  <code>true</code> if this connection is marked as being in
228     *          a reusable communication state,
229     *          <code>false</code> otherwise
230     */
231    boolean isMarkedReusable()
232        ;
233
234    /**
235     * Assigns a state object to this connection. Connection managers may make
236     * use of the connection state when allocating persistent connections.
237     *
238     * @param state The state object
239     */
240    void setState(Object state)
241        ;
242
243    /**
244     * Returns the state object associated with this connection.
245     *
246     * @return The state object
247     */
248    Object getState()
249        ;
250
251    /**
252     * Sets the duration that this connection can remain idle before it is
253     * reused. The connection should not be used again if this time elapses. The
254     * idle duration must be reset after each request sent over this connection.
255     * The elapsed time starts counting when the connection is released, which
256     * is typically after the headers (and any response body, if present) is
257     * fully consumed.
258     */
259    void setIdleDuration(long duration, TimeUnit unit);
260
261} // interface ManagedClientConnection
262