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  
20  import javax.net.SocketFactory;
21  import javax.net.ssl.SSLContext;
22  
23  import org.mortbay.component.AbstractLifeCycle;
24  import org.mortbay.io.EndPoint;
25  import org.mortbay.io.bio.SocketEndPoint;
26  import org.mortbay.log.Log;
27  
28  class SocketConnector extends AbstractLifeCycle implements HttpClient.Connector
29  {
30      /**
31       * 
32       */
33      private final HttpClient _httpClient;
34  
35      /**
36       * @param httpClient
37       */
38      SocketConnector(HttpClient httpClient)
39      {
40          _httpClient = httpClient;
41      }
42  
43      public void startConnection(final HttpDestination destination) throws IOException
44      {
45          Socket socket=null;
46          
47          if ( destination.isSecure() )
48          {
49              SSLContext sslContext = _httpClient.getSSLContext();
50              socket = sslContext.getSocketFactory().createSocket();
51          }
52          else
53          {
54              Log.debug("Using Regular Socket");
55              socket = SocketFactory.getDefault().createSocket();                
56          }
57         
58          socket.connect(destination.isProxied()?destination.getProxy():destination.getAddress());
59          
60          EndPoint endpoint=new SocketEndPoint(socket);
61          
62          final HttpConnection connection=new HttpConnection(_httpClient,endpoint,_httpClient.getHeaderBufferSize(),_httpClient.getRequestBufferSize());
63          connection.setDestination(destination);
64          destination.onNewConnection(connection);
65          _httpClient.getThreadPool().dispatch(new Runnable()
66          {
67              public void run()
68              {
69                  try
70                  {
71                      connection.handle();
72                  }
73                  catch (IOException e)
74                  {
75                      if (e instanceof InterruptedIOException)
76                          Log.ignore(e);
77                      else
78                      {
79                          Log.warn(e);
80                          destination.onException(e);
81                      }
82                  }
83              }
84          });
85               
86      }
87  }