PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wsdlextensions.ldap/src/com/pymma/jbi/ldapbc/util/LDAPSocketFactory.java

https://bitbucket.org/openesb/netbeans-soa
Java | 111 lines | 79 code | 17 blank | 15 comment | 0 complexity | a325ca4d0684710469463732f48e9b0c MD5 | raw file
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package com.pymma.jbi.ldapbc.util;
  7. import java.io.IOException;
  8. import java.net.InetAddress;
  9. import java.net.Socket;
  10. import java.net.UnknownHostException;
  11. import java.security.NoSuchAlgorithmException;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import javax.net.SocketFactory;
  15. import javax.net.ssl.SSLContext;
  16. import javax.net.ssl.SSLParameters;
  17. import javax.net.ssl.SSLSocket;
  18. import javax.net.ssl.SSLSocketFactory;
  19. /**
  20. *
  21. * @author polperez
  22. * LDAPSocketfactory is a bespoke socket factory that uses a bespoke context factory.
  23. * The class design comes from the Java bug solution provided here
  24. * https://bitbucket.org/atlassian/cwd-4444-java-bug-reproducer/src/master/src/main/java/Working.java
  25. * Here in the code, we don't use the SSLContext.getDefault() to get a SSL Context
  26. * We use the static method LDAPSocketFactory.setSSLContext().
  27. * In that case, we don't impact the global SSL context.
  28. */
  29. public class LDAPSocketFactory extends SSLSocketFactory {
  30. SSLSocketFactory sf ;
  31. private static SSLContext sslContext = null ;
  32. public LDAPSocketFactory () throws NoSuchAlgorithmException {
  33. sf = sslContext.getSocketFactory();
  34. }
  35. @Override
  36. public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
  37. SSLSocket s = (SSLSocket) sf.createSocket(host,port);
  38. SSLParameters paramters = s.getSSLParameters();
  39. paramters.setEndpointIdentificationAlgorithm("LDAPS");
  40. s.setSSLParameters(paramters);
  41. return s ;
  42. }
  43. @Override
  44. public Socket createSocket(String host, int port, InetAddress localhost, int localport) throws IOException, UnknownHostException {
  45. SSLSocket s = (SSLSocket) sf.createSocket(host,port,localhost,localport);
  46. SSLParameters paramters = s.getSSLParameters();
  47. paramters.setEndpointIdentificationAlgorithm("LDAPS");
  48. s.setSSLParameters(paramters);
  49. return s ;
  50. }
  51. @Override
  52. public Socket createSocket(InetAddress host, int port) throws IOException {
  53. SSLSocket s = (SSLSocket) sf.createSocket(host,port);
  54. SSLParameters paramters = s.getSSLParameters();
  55. paramters.setEndpointIdentificationAlgorithm("LDAPS");
  56. s.setSSLParameters(paramters);
  57. return s ;
  58. }
  59. @Override
  60. public Socket createSocket(InetAddress host, int port, InetAddress localhost, int localport) throws IOException {
  61. SSLSocket s = (SSLSocket) sf.createSocket(host,port,localhost,localport);
  62. SSLParameters paramters = s.getSSLParameters();
  63. paramters.setEndpointIdentificationAlgorithm("LDAPS");
  64. s.setSSLParameters(paramters);
  65. return s ;
  66. }
  67. @Override
  68. public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
  69. SSLSocket sslSocket = (SSLSocket) sf.createSocket(s, host, port, autoClose);
  70. SSLParameters paramters = sslSocket.getSSLParameters();
  71. paramters.setEndpointIdentificationAlgorithm("LDAPS");
  72. sslSocket.setSSLParameters(paramters);
  73. return sslSocket ;
  74. }
  75. public static SocketFactory getDefault () {
  76. try {
  77. return new LDAPSocketFactory();
  78. } catch (NoSuchAlgorithmException ex) {
  79. Logger.getLogger(LDAPSocketFactory.class.getName()).log(Level.SEVERE, null, ex);
  80. throw new RuntimeException();
  81. }
  82. }
  83. public static void setSSLContext (SSLContext context) {
  84. sslContext = context ;
  85. }
  86. @Override
  87. public String[] getDefaultCipherSuites() {
  88. return null;
  89. }
  90. @Override
  91. public String[] getSupportedCipherSuites() {
  92. return null ;
  93. }
  94. }