1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jetty.client;
14
15 import java.io.IOException;
16 import java.io.InterruptedIOException;
17 import java.net.Socket;
18
19 import javax.net.SocketFactory;
20 import javax.net.ssl.SSLContext;
21
22 import org.eclipse.jetty.io.EndPoint;
23 import org.eclipse.jetty.io.bio.SocketEndPoint;
24 import org.eclipse.jetty.util.component.AbstractLifeCycle;
25 import org.eclipse.jetty.util.log.Log;
26
27 class SocketConnector extends AbstractLifeCycle implements HttpClient.Connector
28 {
29
30
31
32 private final HttpClient _httpClient;
33
34
35
36
37 SocketConnector(HttpClient httpClient)
38 {
39 _httpClient = httpClient;
40 }
41
42 public void startConnection(final HttpDestination destination) throws IOException
43 {
44 Socket socket=null;
45
46 if ( destination.isSecure() )
47 {
48 SSLContext sslContext = _httpClient.getSSLContext();
49 socket = sslContext.getSocketFactory().createSocket();
50 }
51 else
52 {
53 Log.debug("Using Regular Socket");
54 socket = SocketFactory.getDefault().createSocket();
55 }
56
57 Address address = destination.isProxied() ? destination.getProxy() : destination.getAddress();
58 socket.connect(address.toSocketAddress());
59
60 EndPoint endpoint=new SocketEndPoint(socket);
61
62 final HttpConnection connection=new HttpConnection(_httpClient.getRequestBuffers(),_httpClient.getResponseBuffers(),endpoint);
63 connection.setDestination(destination);
64 destination.onNewConnection(connection);
65 _httpClient.getThreadPool().dispatch(new Runnable()
66 {
67 public void run()
68 {
69 try
70 {
71 connection.handle();
72 }
73 catch (IOException e)
74 {
75 if (e instanceof InterruptedIOException)
76 Log.ignore(e);
77 else
78 {
79 Log.debug(e);
80 destination.onException(e);
81 }
82 }
83 }
84 });
85
86 }
87 }