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          Address address = destination.isProxied() ? destination.getProxy() : destination.getAddress();
    59          socket.connect(address.toSocketAddress());
    60
    61          EndPoint endpoint=new SocketEndPoint(socket);
    62
    63          final HttpConnection connection=new HttpConnection(_httpClient,endpoint,_httpClient.getHeaderBufferSize(),_httpClient.getRequestBufferSize());
    64          connection.setDestination(destination);
    65          destination.onNewConnection(connection);
    66          _httpClient.getThreadPool().dispatch(new Runnable()
    67          {
    68              public void run()
    69              {
    70                  try
    71                  {
    72                      connection.handle();
    73                  }
    74                  catch (IOException e)
    75                  {
    76                      if (e instanceof InterruptedIOException)
    77                          Log.ignore(e);
    78                      else
    79                      {
    80                          Log.warn(e);
    81                          destination.onException(e);
    82                      }
    83                  }
    84              }
    85          });
    86
    87      }
    88  }