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  import javax.net.SocketFactory;
19  import javax.net.ssl.SSLContext;
20  
21  import org.eclipse.jetty.io.Connection;
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          socket.setSoTimeout(_httpClient.getSoTimeout());
58          socket.setTcpNoDelay(true);
59  
60          Address address = destination.isProxied() ? destination.getProxy() : destination.getAddress();
61          socket.connect(address.toSocketAddress(), _httpClient.getConnectTimeout());
62  
63          EndPoint endpoint=new SocketEndPoint(socket);
64  
65          final HttpConnection connection=new HttpConnection(_httpClient.getRequestBuffers(),_httpClient.getResponseBuffers(),endpoint);
66          connection.setDestination(destination);
67          destination.onNewConnection(connection);
68          _httpClient.getThreadPool().dispatch(new Runnable()
69          {
70              public void run()
71              {
72                  try
73                  {
74                      Connection con = connection;
75                      while(true)
76                      {
77                          final Connection next = con.handle();
78                          if (next!=con)
79                          {
80                              con=next;
81                              continue;
82                          }
83                          break;
84                      }
85                  }
86                  catch (IOException e)
87                  {
88                      if (e instanceof InterruptedIOException)
89                          Log.ignore(e);
90                      else
91                      {
92                          Log.debug(e);
93                          destination.onException(e);
94                      }
95                  }
96              }
97          });
98  
99      }
100 }