280 281 /** 282 * Sets the connection for TLS to use. The TLS connection will be attached 283 * to this connection. 284 * 285 * @param ldapConnection The non-null connection to use. 286 * @param hostname The server's hostname. If null, the hostname used to 287 * open the connection will be used instead. 288 */ 289 public void setConnection(Connection ldapConnection, String hostname) { 290 this.ldapConnection = ldapConnection; 291 this.hostname = (hostname != null) ? hostname : ldapConnection.host; 292 originalInputStream = ldapConnection.inStream; 293 originalOutputStream = ldapConnection.outStream; 294 } 295 296 /* 297 * Returns the default SSL socket factory. 298 * 299 * @return The default SSL socket factory. 300 * @throw IOException If TLS is not supported. 301 */ 302 private SSLSocketFactory getDefaultFactory() throws IOException { 303 304 if (defaultFactory != null) { 305 return defaultFactory; 306 } 307 308 return (defaultFactory = 309 (SSLSocketFactory) SSLSocketFactory.getDefault()); 310 } 311 312 /* 313 * Start the TLS handshake and manipulate the input and output streams. 314 * 315 * @param factory The SSL socket factory to use. 316 * @return The SSL socket. 317 * @throw IOException If an exception occurred while performing the 318 * TLS handshake. 319 */ 320 private SSLSocket startHandshake(SSLSocketFactory factory) 321 throws IOException { 322 323 if (ldapConnection == null) { 324 throw new IllegalStateException("LDAP connection has not been set." 325 + " TLS requires an existing LDAP connection."); 326 } 327 328 if (factory != currentFactory) { 329 // Create SSL socket layered over the existing connection 330 sslSocket = (SSLSocket) factory.createSocket(ldapConnection.sock, 331 ldapConnection.host, ldapConnection.port, false); 332 currentFactory = factory; 333 334 if (debug) { 335 System.out.println("StartTLS: Created socket : " + sslSocket); 336 } 337 } | 280 281 /** 282 * Sets the connection for TLS to use. The TLS connection will be attached 283 * to this connection. 284 * 285 * @param ldapConnection The non-null connection to use. 286 * @param hostname The server's hostname. If null, the hostname used to 287 * open the connection will be used instead. 288 */ 289 public void setConnection(Connection ldapConnection, String hostname) { 290 this.ldapConnection = ldapConnection; 291 this.hostname = (hostname != null) ? hostname : ldapConnection.host; 292 originalInputStream = ldapConnection.inStream; 293 originalOutputStream = ldapConnection.outStream; 294 } 295 296 /* 297 * Returns the default SSL socket factory. 298 * 299 * @return The default SSL socket factory. 300 * @throws IOException If TLS is not supported. 301 */ 302 private SSLSocketFactory getDefaultFactory() throws IOException { 303 304 if (defaultFactory != null) { 305 return defaultFactory; 306 } 307 308 return (defaultFactory = 309 (SSLSocketFactory) SSLSocketFactory.getDefault()); 310 } 311 312 /* 313 * Start the TLS handshake and manipulate the input and output streams. 314 * 315 * @param factory The SSL socket factory to use. 316 * @return The SSL socket. 317 * @throws IOException If an exception occurred while performing the 318 * TLS handshake. 319 */ 320 private SSLSocket startHandshake(SSLSocketFactory factory) 321 throws IOException { 322 323 if (ldapConnection == null) { 324 throw new IllegalStateException("LDAP connection has not been set." 325 + " TLS requires an existing LDAP connection."); 326 } 327 328 if (factory != currentFactory) { 329 // Create SSL socket layered over the existing connection 330 sslSocket = (SSLSocket) factory.createSocket(ldapConnection.sock, 331 ldapConnection.host, ldapConnection.port, false); 332 currentFactory = factory; 333 334 if (debug) { 335 System.out.println("StartTLS: Created socket : " + sslSocket); 336 } 337 } |