PlainSocketFactory.java revision 07b7bb333f41e90af0a72a462fed847378641d21
1/* 2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/scheme/PlainSocketFactory.java $ 3 * $Revision: 659194 $ 4 * $Date: 2008-05-22 11:33:47 -0700 (Thu, 22 May 2008) $ 5 * 6 * ==================================================================== 7 * Licensed to the Apache Software Foundation (ASF) under one 8 * or more contributor license agreements. See the NOTICE file 9 * distributed with this work for additional information 10 * regarding copyright ownership. The ASF licenses this file 11 * to you under the Apache License, Version 2.0 (the 12 * "License"); you may not use this file except in compliance 13 * with the License. You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, 18 * software distributed under the License is distributed on an 19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 20 * KIND, either express or implied. See the License for the 21 * specific language governing permissions and limitations 22 * under the License. 23 * ==================================================================== 24 * 25 * This software consists of voluntary contributions made by many 26 * individuals on behalf of the Apache Software Foundation. For more 27 * information on the Apache Software Foundation, please see 28 * <http://www.apache.org/>. 29 * 30 */ 31 32package org.apache.http.conn.scheme; 33 34import java.io.IOException; 35import java.net.InetAddress; 36import java.net.InetSocketAddress; 37import java.net.Socket; 38 39import org.apache.http.params.HttpConnectionParams; 40import org.apache.http.params.HttpParams; 41 42/** 43 * The default class for creating sockets. 44 * 45 * @author <a href="mailto:rolandw at apache.org">Roland Weber</a> 46 * @author Michael Becke 47 */ 48public final class PlainSocketFactory implements SocketFactory { 49 50 /** 51 * The factory singleton. 52 */ 53 private static final 54 PlainSocketFactory DEFAULT_FACTORY = new PlainSocketFactory(); 55 56 private final HostNameResolver nameResolver; 57 58 /** 59 * Gets the singleton instance of this class. 60 * @return the one and only plain socket factory 61 */ 62 public static PlainSocketFactory getSocketFactory() { 63 return DEFAULT_FACTORY; 64 } 65 66 public PlainSocketFactory(final HostNameResolver nameResolver) { 67 super(); 68 this.nameResolver = nameResolver; 69 } 70 71 72 public PlainSocketFactory() { 73 this(null); 74 } 75 76 // non-javadoc, see interface org.apache.http.conn.SocketFactory 77 public Socket createSocket() { 78 return new Socket(); 79 } 80 81 // non-javadoc, see interface org.apache.http.conn.SocketFactory 82 public Socket connectSocket(Socket sock, String host, int port, 83 InetAddress localAddress, int localPort, 84 HttpParams params) 85 throws IOException { 86 87 if (host == null) { 88 throw new IllegalArgumentException("Target host may not be null."); 89 } 90 if (params == null) { 91 throw new IllegalArgumentException("Parameters may not be null."); 92 } 93 94 if (sock == null) 95 sock = createSocket(); 96 97 if ((localAddress != null) || (localPort > 0)) { 98 99 // we need to bind explicitly 100 if (localPort < 0) 101 localPort = 0; // indicates "any" 102 103 InetSocketAddress isa = 104 new InetSocketAddress(localAddress, localPort); 105 sock.bind(isa); 106 } 107 108 int timeout = HttpConnectionParams.getConnectionTimeout(params); 109 110 InetSocketAddress remoteAddress; 111 if (this.nameResolver != null) { 112 remoteAddress = new InetSocketAddress(this.nameResolver.resolve(host), port); 113 } else { 114 remoteAddress = new InetSocketAddress(host, port); 115 } 116 117 sock.connect(remoteAddress, timeout); 118 119 return sock; 120 121 } // connectSocket 122 123 124 /** 125 * Checks whether a socket connection is secure. 126 * This factory creates plain socket connections 127 * which are not considered secure. 128 * 129 * @param sock the connected socket 130 * 131 * @return <code>false</code> 132 * 133 * @throws IllegalArgumentException if the argument is invalid 134 */ 135 public final boolean isSecure(Socket sock) 136 throws IllegalArgumentException { 137 138 if (sock == null) { 139 throw new IllegalArgumentException("Socket may not be null."); 140 } 141 // This class check assumes that createSocket() calls the constructor 142 // directly. If it was using javax.net.SocketFactory, we couldn't make 143 // an assumption about the socket class here. 144 if (sock.getClass() != Socket.class) { 145 throw new IllegalArgumentException 146 ("Socket not created by this factory."); 147 } 148 // This check is performed last since it calls a method implemented 149 // by the argument object. getClass() is final in java.lang.Object. 150 if (sock.isClosed()) { 151 throw new IllegalArgumentException("Socket is closed."); 152 } 153 154 return false; 155 156 } // isSecure 157 158 159 /** 160 * Compares this factory with an object. 161 * There is only one instance of this class. 162 * 163 * @param obj the object to compare with 164 * 165 * @return iff the argument is this object 166 */ 167 @Override 168 public boolean equals(Object obj) { 169 return (obj == this); 170 } 171 172 /** 173 * Obtains a hash code for this object. 174 * All instances of this class have the same hash code. 175 * There is only one instance of this class. 176 */ 177 @Override 178 public int hashCode() { 179 return PlainSocketFactory.class.hashCode(); 180 } 181 182} 183