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.Connection;
23  import org.eclipse.jetty.io.EndPoint;
24  import org.eclipse.jetty.io.bio.SocketEndPoint;
25  import org.eclipse.jetty.util.component.AbstractLifeCycle;
26  import org.eclipse.jetty.util.log.Log;
27  import org.eclipse.jetty.util.log.Logger;
28  
29  class SocketConnector extends AbstractLifeCycle implements HttpClient.Connector
30  {
31      private static final Logger LOG = Log.getLogger(SocketConnector.class);
32  
33      /**
34       *
35       */
36      private final HttpClient _httpClient;
37  
38      /**
39       * @param httpClient
40       */
41      SocketConnector(HttpClient httpClient)
42      {
43          _httpClient = httpClient;
44      }
45  
46      public void startConnection(final HttpDestination destination) throws IOException
47      {
48          Socket socket=null;
49  
50          if ( destination.isSecure() )
51          {
52              SSLContext sslContext = _httpClient.getSSLContext();
53              socket = sslContext.getSocketFactory().createSocket();
54          }
55          else
56          {
57              LOG.debug("Using Regular Socket");
58              socket = SocketFactory.getDefault().createSocket();
59          }
60  
61          socket.setSoTimeout(0);
62          socket.setTcpNoDelay(true);
63  
64          Address address = destination.isProxied() ? destination.getProxy() : destination.getAddress();
65          socket.connect(address.toSocketAddress(), _httpClient.getConnectTimeout());
66  
67          EndPoint endpoint=new SocketEndPoint(socket);
68  
69          final HttpConnection connection=new HttpConnection(_httpClient.getRequestBuffers(),_httpClient.getResponseBuffers(),endpoint);
70          connection.setDestination(destination);
71          destination.onNewConnection(connection);
72          _httpClient.getThreadPool().dispatch(new Runnable()
73          {
74              public void run()
75              {
76                  try
77                  {
78                      Connection con = connection;
79                      while(true)
80                      {
81                          final Connection next = con.handle();
82                          if (next!=con)
83                          {
84                              con=next;
85                              continue;
86                          }
87                          break;
88                      }
89                  }
90                  catch (IOException e)
91                  {
92                      if (e instanceof InterruptedIOException)
93                          LOG.ignore(e);
94                      else
95                      {
96                          LOG.debug(e);
97                          destination.onException(e);
98                      }
99                  }
100             }
101         });
102 
103     }
104 }