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