diff options
Diffstat (limited to 'jetty-client/src/main/java/org/eclipse/jetty/client/SocketConnector.java')
-rw-r--r-- | jetty-client/src/main/java/org/eclipse/jetty/client/SocketConnector.java | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/SocketConnector.java b/jetty-client/src/main/java/org/eclipse/jetty/client/SocketConnector.java new file mode 100644 index 0000000000..275a7b2651 --- /dev/null +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/SocketConnector.java @@ -0,0 +1,87 @@ +// ======================================================================== +// Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +package org.eclipse.jetty.client; + +import java.io.IOException; +import java.io.InterruptedIOException; +import java.net.Socket; + +import javax.net.SocketFactory; +import javax.net.ssl.SSLContext; + +import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.bio.SocketEndPoint; +import org.eclipse.jetty.util.component.AbstractLifeCycle; +import org.eclipse.jetty.util.log.Log; + +class SocketConnector extends AbstractLifeCycle implements HttpClient.Connector +{ + /** + * + */ + private final HttpClient _httpClient; + + /** + * @param httpClient + */ + SocketConnector(HttpClient httpClient) + { + _httpClient = httpClient; + } + + public void startConnection(final HttpDestination destination) throws IOException + { + Socket socket=null; + + if ( destination.isSecure() ) + { + SSLContext sslContext = _httpClient.getSSLContext(); + socket = sslContext.getSocketFactory().createSocket(); + } + else + { + Log.debug("Using Regular Socket"); + socket = SocketFactory.getDefault().createSocket(); + } + + Address address = destination.isProxied() ? destination.getProxy() : destination.getAddress(); + socket.connect(address.toSocketAddress()); + + EndPoint endpoint=new SocketEndPoint(socket); + + final HttpConnection connection=new HttpConnection(_httpClient,endpoint,_httpClient.getHeaderBufferSize(),_httpClient.getRequestBufferSize()); + connection.setDestination(destination); + destination.onNewConnection(connection); + _httpClient.getThreadPool().dispatch(new Runnable() + { + public void run() + { + try + { + connection.handle(); + } + catch (IOException e) + { + if (e instanceof InterruptedIOException) + Log.ignore(e); + else + { + Log.warn(e); + destination.onException(e); + } + } + } + }); + + } +} |