Source code: org/apache/http/impl/DefaultHttpProxyConnection.java
1 /*
2 * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/impl/DefaultHttpProxyConnection.java $
3 * $Revision: 376961 $
4 * $Date: 2006-02-11 11:32:50 +0100 (Sat, 11 Feb 2006) $
5 *
6 * ====================================================================
7 *
8 * Copyright 1999-2006 The Apache Software Foundation
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ====================================================================
22 *
23 * This software consists of voluntary contributions made by many
24 * individuals on behalf of the Apache Software Foundation. For more
25 * information on the Apache Software Foundation, please see
26 * <http://www.apache.org/>.
27 *
28 */
29
30 package org.apache.http.impl;
31
32 import java.io.IOException;
33 import java.net.InetAddress;
34 import java.net.Socket;
35
36 import org.apache.http.HttpHost;
37 import org.apache.http.HttpProxyConnection;
38 import org.apache.http.Scheme;
39 import org.apache.http.ProxyHost;
40 import org.apache.http.io.SecureSocketFactory;
41 import org.apache.http.io.SocketFactory;
42 import org.apache.http.params.HttpParams;
43
44 /**
45 * Default implementation of a client-side connection through a proxy.
46 *
47 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
48 *
49 * @version $Revision: 376961 $
50 *
51 * @since 4.0
52 */
53 public class DefaultHttpProxyConnection
54 extends DefaultHttpClientConnection implements HttpProxyConnection {
55
56 private volatile HttpHost tunneltarget = null;
57 private volatile boolean secure = false;
58
59 public DefaultHttpProxyConnection(final ProxyHost proxyhost, final InetAddress localAddress) {
60 super(proxyhost, localAddress);
61 }
62
63 public DefaultHttpProxyConnection(final ProxyHost proxyhost) {
64 this(proxyhost, null);
65 }
66
67 public void close() throws IOException {
68 this.tunneltarget = null;
69 this.secure = false;
70 super.close();
71 }
72
73 public void tunnelTo(final HttpHost targetHost, final HttpParams params)
74 throws IOException {
75 if (targetHost == null) {
76 throw new IllegalArgumentException("Target host may not be null");
77 }
78 if (params == null) {
79 throw new IllegalArgumentException("HTTP parameters may not be null");
80 }
81 if (this.tunneltarget != null) {
82 throw new IllegalStateException("Secure tunnel to " +
83 this.tunneltarget + " is already active");
84 }
85 assertOpen();
86 Scheme protocol = targetHost.getScheme();
87 SocketFactory socketfactory = protocol.getSocketFactory();
88 if (socketfactory instanceof SecureSocketFactory) {
89 Socket socket = ((SecureSocketFactory)socketfactory)
90 .createSocket(
91 this.socket,
92 targetHost.getHostName(),
93 targetHost.getPort(),
94 true);
95 bind(socket, params);
96 this.secure = true;
97 } else {
98 this.secure = false;
99 }
100 this.tunneltarget = targetHost;
101 }
102
103 public HttpHost getTunnelTarget() {
104 return this.tunneltarget;
105 }
106
107 public boolean isTunnelActive() {
108 return this.tunneltarget != null;
109 }
110
111 public boolean isSecure() {
112 return this.secure;
113 }
114 }