1 10 11 package org.mule.providers.ssl; 12 13 import org.apache.commons.lang.StringUtils; 14 import org.mule.providers.AbstractConnector; 15 import org.mule.providers.tcp.TcpMessageReceiver; 16 import org.mule.umo.UMOComponent; 17 import org.mule.umo.endpoint.UMOEndpoint; 18 import org.mule.umo.lifecycle.InitialisationException; 19 20 import javax.net.ServerSocketFactory; 21 import javax.net.ssl.SSLContext; 22 import javax.net.ssl.SSLServerSocket; 23 24 import java.io.IOException ; 25 import java.net.InetAddress ; 26 import java.net.ServerSocket ; 27 import java.net.URI ; 28 import java.security.KeyManagementException ; 29 import java.security.NoSuchAlgorithmException ; 30 import java.security.SecureRandom ; 31 32 39 public class SslMessageReceiver extends TcpMessageReceiver 40 { 41 public SslMessageReceiver(AbstractConnector connector, UMOComponent component, UMOEndpoint endpoint) 42 throws InitialisationException 43 { 44 super(connector, component, endpoint); 45 } 46 47 protected ServerSocket createSocket(URI uri) 48 throws IOException , NoSuchAlgorithmException , KeyManagementException 49 { 50 SslConnector cnn; 51 ServerSocketFactory ssf; 52 cnn = (SslConnector)connector; 53 SSLContext sslc = SSLContext.getInstance(cnn.getProtocol().toLowerCase(), cnn.getProvider()); 56 57 sslc.init(cnn.getKeyManagerFactory().getKeyManagers(), cnn.getTrustManagerFactory() 59 .getTrustManagers(), 60 new SecureRandom ()); 62 63 ssf = sslc.getServerSocketFactory(); 64 65 String host = StringUtils.defaultIfEmpty(uri.getHost(), "localhost"); 66 int backlog = cnn.getBacklog(); 67 SSLServerSocket serverSocket; 68 69 InetAddress inetAddress = InetAddress.getByName(host); 70 if (inetAddress.equals(InetAddress.getLocalHost()) || inetAddress.isLoopbackAddress() 71 || host.trim().equals("localhost")) 72 { 73 serverSocket = (SSLServerSocket)ssf.createServerSocket(uri.getPort(), backlog); 74 } 75 else 76 { 77 serverSocket = (SSLServerSocket)ssf.createServerSocket(uri.getPort(), backlog, inetAddress); 78 } 79 serverSocket.setNeedClientAuth(cnn.isRequireClientAuthentication()); 81 return serverSocket; 82 83 } 84 } 85 | Popular Tags |