View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
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       * @param httpClient
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  }