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