View Javadoc

1   //========================================================================
2   //Copyright 2006-2007 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //Licensed under the Apache License, Version 2.0 (the "License");
5   //you may not use this file except in compliance with the License.
6   //You may obtain a copy of the License at
7   //http://www.apache.org/licenses/LICENSE-2.0
8   //Unless required by applicable law or agreed to in writing, software
9   //distributed under the License is distributed on an "AS IS" BASIS,
10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  //See the License for the specific language governing permissions and
12  //limitations under the License.
13  //========================================================================
14  package org.mortbay.jetty.client;
15  
16  import java.io.IOException;
17  import java.io.InterruptedIOException;
18  import java.net.Socket;
19  import javax.net.SocketFactory;
20  import javax.net.ssl.SSLContext;
21  
22  import org.mortbay.component.AbstractLifeCycle;
23  import org.mortbay.io.EndPoint;
24  import org.mortbay.io.bio.SocketEndPoint;
25  import org.mortbay.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());
62  
63          EndPoint endpoint=new SocketEndPoint(socket);
64  
65          final HttpConnection connection=new HttpConnection(_httpClient,endpoint,_httpClient.getHeaderBufferSize(),_httpClient.getRequestBufferSize());
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.handle();
75                  }
76                  catch (IOException e)
77                  {
78                      if (e instanceof InterruptedIOException)
79                          Log.ignore(e);
80                      else
81                      {
82                          Log.warn(e);
83                          destination.onException(e);
84                      }
85                  }
86              }
87          });
88  
89      }
90  }