View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.client;
20  
21  import java.io.IOException;
22  import java.io.InterruptedIOException;
23  import java.net.Socket;
24  
25  import javax.net.SocketFactory;
26  
27  import org.eclipse.jetty.io.Connection;
28  import org.eclipse.jetty.io.EndPoint;
29  import org.eclipse.jetty.io.bio.SocketEndPoint;
30  import org.eclipse.jetty.util.component.AbstractLifeCycle;
31  import org.eclipse.jetty.util.log.Log;
32  import org.eclipse.jetty.util.log.Logger;
33  
34  class SocketConnector extends AbstractLifeCycle implements HttpClient.Connector
35  {
36      private static final Logger LOG = Log.getLogger(SocketConnector.class);
37  
38      /**
39       *
40       */
41      private final HttpClient _httpClient;
42  
43      /**
44       * @param httpClient
45       */
46      SocketConnector(HttpClient httpClient)
47      {
48          _httpClient = httpClient;
49      }
50  
51      public void startConnection(final HttpDestination destination) throws IOException
52      {
53          Socket socket= destination.isSecure()
54              ?_httpClient.getSslContextFactory().newSslSocket()
55              :SocketFactory.getDefault().createSocket();
56  
57          socket.setSoTimeout(0);
58          socket.setTcpNoDelay(true);
59  
60          Address address = destination.isProxied() ? destination.getProxy() : destination.getAddress();
61          socket.connect(address.toSocketAddress(), _httpClient.getConnectTimeout());
62  
63          final EndPoint endpoint=new SocketEndPoint(socket);
64  
65          final AbstractHttpConnection connection=new BlockingHttpConnection(_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                  finally
97                  {
98                      try
99                      {
100                         destination.returnConnection(connection,true);
101                     }
102                     catch (IOException e)
103                     {
104                         LOG.debug(e);
105                     }
106                 }
107             }
108         });
109 
110     }
111 }