1 21 22 27 28 package com.sun.mail.util; 29 30 import java.security.*; 31 import java.net.*; 32 import java.io.*; 33 import java.lang.reflect.*; 34 import java.util.Properties ; 35 import javax.net.*; 36 import javax.net.ssl.*; 37 38 47 public class SocketFetcher { 48 49 private SocketFetcher() { 51 } 52 53 98 public static Socket getSocket(String host, int port, Properties props, 99 String prefix, boolean useSSL) 100 throws IOException { 101 102 if (prefix == null) 103 prefix = "socket"; 104 if (props == null) 105 props = new Properties (); String s = props.getProperty(prefix + ".connectiontimeout", null); 107 int cto = -1; 108 if (s != null) { 109 try { 110 cto = Integer.parseInt(s); 111 } catch (NumberFormatException nfex) { } 112 } 113 114 Socket socket = null; 115 String sfClass = 116 props.getProperty(prefix + ".socketFactory.class", null); 117 String timeout = props.getProperty(prefix + ".timeout", null); 118 String localaddrstr = props.getProperty(prefix + ".localaddress", null); 119 InetAddress localaddr = null; 120 if (localaddrstr != null) 121 localaddr = InetAddress.getByName(localaddrstr); 122 String localportstr = props.getProperty(prefix + ".localport", null); 123 int localport = 0; 124 if (localportstr != null) { 125 try { 126 localport = Integer.parseInt(localportstr); 127 } catch (NumberFormatException nfex) { } 128 } 129 130 if (sfClass != null && sfClass.length() > 0) { 131 int sfPort = -1; 133 boolean fb = false; 134 String fallback = 135 props.getProperty(prefix + ".socketFactory.fallback", null); 136 fb = fallback == null || (!fallback.equalsIgnoreCase("false")); 137 String sfPortStr = 138 props.getProperty(prefix + ".socketFactory.port", null); 139 if (sfPortStr != null) { 140 try { 141 sfPort = Integer.parseInt(sfPortStr); 142 } catch (NumberFormatException nfex) { } 143 } 144 145 try { 146 ClassLoader cl = getContextClassLoader(); 147 Class clsSockFact = null; 148 if (cl != null) { 149 try { 150 clsSockFact = cl.loadClass(sfClass); 151 } catch (ClassNotFoundException cex) { } 152 } 153 if (clsSockFact == null) 154 clsSockFact = Class.forName(sfClass); 155 Method mthGetDefault = clsSockFact.getMethod("getDefault", 157 new Class []{}); 158 SocketFactory sf = (SocketFactory) 159 mthGetDefault.invoke(new Object (), new Object []{}); 160 if (sfPort == -1) 162 sfPort = port; 163 socket = createSocket(localaddr, localport, 164 host, sfPort, cto, sf, useSSL); 165 } catch (SocketTimeoutException sex) { 166 throw sex; 167 } catch (Exception ex) { 168 if (!fb) { 169 if (ex instanceof InvocationTargetException) { 170 Throwable t = 171 ((InvocationTargetException)ex).getTargetException(); 172 if (t instanceof Exception ) 173 ex = (Exception )t; 174 } 175 if (ex instanceof IOException) 176 throw (IOException)ex; 177 IOException ioex = new IOException( 178 "Couldn't connect using \"" + sfClass + 179 "\" socket factory to host, port: " + 180 host + ", " + sfPort + 181 "; Exception: " + ex); 182 ioex.initCause(ex); 183 throw ioex; 184 } 185 } 186 } 187 188 if (socket == null) 189 socket = createSocket(localaddr, localport, 190 host, port, cto, null, useSSL); 191 192 int to = -1; 193 if (timeout != null) { 194 try { 195 to = Integer.parseInt(timeout); 196 } catch (NumberFormatException nfex) { } 197 } 198 if (to >= 0) 199 socket.setSoTimeout(to); 200 201 return socket; 202 } 203 204 public static Socket getSocket(String host, int port, Properties props, 205 String prefix) throws IOException { 206 return getSocket(host, port, props, prefix, false); 207 } 208 209 215 private static Socket createSocket(InetAddress localaddr, int localport, 216 String host, int port, int cto, 217 SocketFactory sf, boolean useSSL) 218 throws IOException { 219 Socket socket; 220 221 if (sf != null) 222 socket = sf.createSocket(); 223 else if (useSSL) 224 socket = SSLSocketFactory.getDefault().createSocket(); 225 else 226 socket = new Socket(); 227 if (localaddr != null) 228 socket.bind(new InetSocketAddress(localaddr, localport)); 229 if (cto >= 0) 230 socket.connect(new InetSocketAddress(host, port), cto); 231 else 232 socket.connect(new InetSocketAddress(host, port)); 233 return socket; 234 } 235 236 240 public static Socket startTLS(Socket socket) throws IOException { 241 InetAddress a = socket.getInetAddress(); 242 String host = a.getHostName(); 243 int port = socket.getPort(); 244 246 try { 247 SSLSocketFactory ssf = 248 (SSLSocketFactory)SSLSocketFactory.getDefault(); 249 socket = ssf.createSocket(socket, host, port, true); 250 251 257 ((SSLSocket)socket).setEnabledProtocols(new String [] {"TLSv1"}); 258 } catch (Exception ex) { 259 if (ex instanceof InvocationTargetException) { 260 Throwable t = 261 ((InvocationTargetException)ex).getTargetException(); 262 if (t instanceof Exception ) 263 ex = (Exception )t; 264 } 265 if (ex instanceof IOException) 266 throw (IOException)ex; 267 IOException ioex = new IOException("Exception in startTLS: host " + 269 host + ", port " + port + "; Exception: " + ex); 270 ioex.initCause(ex); 271 throw ioex; 272 } 273 return socket; 274 } 275 276 281 private static ClassLoader getContextClassLoader() { 282 return (ClassLoader ) 283 AccessController.doPrivileged(new PrivilegedAction() { 284 public Object run() { 285 ClassLoader cl = null; 286 try { 287 cl = Thread.currentThread().getContextClassLoader(); 288 } catch (SecurityException ex) { } 289 return cl; 290 } 291 }); 292 } 293 } 294 | Popular Tags |