michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.conn.scheme; michael@0: michael@0: import java.io.IOException; michael@0: import java.net.InetAddress; michael@0: import java.net.InetSocketAddress; michael@0: import java.net.Socket; michael@0: import java.net.SocketTimeoutException; michael@0: import java.net.UnknownHostException; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.Immutable; michael@0: michael@0: import ch.boye.httpclientandroidlib.conn.ConnectTimeoutException; michael@0: import ch.boye.httpclientandroidlib.params.HttpConnectionParams; michael@0: import ch.boye.httpclientandroidlib.params.HttpParams; michael@0: michael@0: /** michael@0: * The default class for creating plain (unencrypted) sockets. michael@0: *

michael@0: * The following parameters can be used to customize the behavior of this michael@0: * class: michael@0: *

michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @SuppressWarnings("deprecation") michael@0: @Immutable michael@0: public class PlainSocketFactory implements SocketFactory, SchemeSocketFactory { michael@0: michael@0: private final HostNameResolver nameResolver; michael@0: michael@0: /** michael@0: * Gets the default factory. michael@0: * michael@0: * @return the default factory michael@0: */ michael@0: public static PlainSocketFactory getSocketFactory() { michael@0: return new PlainSocketFactory(); michael@0: } michael@0: michael@0: @Deprecated michael@0: public PlainSocketFactory(final HostNameResolver nameResolver) { michael@0: super(); michael@0: this.nameResolver = nameResolver; michael@0: } michael@0: michael@0: public PlainSocketFactory() { michael@0: super(); michael@0: this.nameResolver = null; michael@0: } michael@0: michael@0: /** michael@0: * @param params Optional parameters. Parameters passed to this method will have no effect. michael@0: * This method will create a unconnected instance of {@link Socket} class michael@0: * using default constructor. michael@0: * michael@0: * @since 4.1 michael@0: */ michael@0: public Socket createSocket(final HttpParams params) { michael@0: return new Socket(); michael@0: } michael@0: michael@0: public Socket createSocket() { michael@0: return new Socket(); michael@0: } michael@0: michael@0: /** michael@0: * @since 4.1 michael@0: */ michael@0: public Socket connectSocket( michael@0: final Socket socket, michael@0: final InetSocketAddress remoteAddress, michael@0: final InetSocketAddress localAddress, michael@0: final HttpParams params) throws IOException, ConnectTimeoutException { michael@0: if (remoteAddress == null) { michael@0: throw new IllegalArgumentException("Remote address may not be null"); michael@0: } michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("HTTP parameters may not be null"); michael@0: } michael@0: Socket sock = socket; michael@0: if (sock == null) { michael@0: sock = createSocket(); michael@0: } michael@0: if (localAddress != null) { michael@0: sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params)); michael@0: sock.bind(localAddress); michael@0: } michael@0: int connTimeout = HttpConnectionParams.getConnectionTimeout(params); michael@0: int soTimeout = HttpConnectionParams.getSoTimeout(params); michael@0: michael@0: try { michael@0: sock.setSoTimeout(soTimeout); michael@0: sock.connect(remoteAddress, connTimeout); michael@0: } catch (SocketTimeoutException ex) { michael@0: throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out"); michael@0: } michael@0: return sock; michael@0: } michael@0: michael@0: /** michael@0: * Checks whether a socket connection is secure. michael@0: * This factory creates plain socket connections michael@0: * which are not considered secure. michael@0: * michael@0: * @param sock the connected socket michael@0: * michael@0: * @return false michael@0: * michael@0: * @throws IllegalArgumentException if the argument is invalid michael@0: */ michael@0: public final boolean isSecure(Socket sock) michael@0: throws IllegalArgumentException { michael@0: michael@0: if (sock == null) { michael@0: throw new IllegalArgumentException("Socket may not be null."); michael@0: } michael@0: // This check is performed last since it calls a method implemented michael@0: // by the argument object. getClass() is final in java.lang.Object. michael@0: if (sock.isClosed()) { michael@0: throw new IllegalArgumentException("Socket is closed."); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: /** michael@0: * @deprecated Use {@link #connectSocket(Socket, InetSocketAddress, InetSocketAddress, HttpParams)} michael@0: */ michael@0: @Deprecated michael@0: public Socket connectSocket( michael@0: final Socket socket, michael@0: final String host, int port, michael@0: final InetAddress localAddress, int localPort, michael@0: final HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException { michael@0: InetSocketAddress local = null; michael@0: if (localAddress != null || localPort > 0) { michael@0: // we need to bind explicitly michael@0: if (localPort < 0) { michael@0: localPort = 0; // indicates "any" michael@0: } michael@0: local = new InetSocketAddress(localAddress, localPort); michael@0: } michael@0: InetAddress remoteAddress; michael@0: if (this.nameResolver != null) { michael@0: remoteAddress = this.nameResolver.resolve(host); michael@0: } else { michael@0: remoteAddress = InetAddress.getByName(host); michael@0: } michael@0: InetSocketAddress remote = new InetSocketAddress(remoteAddress, port); michael@0: return connectSocket(socket, remote, local, params); michael@0: } michael@0: michael@0: }