KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > ssl > SslMessageReceiver


1 /*
2  * $Id: SslMessageReceiver.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

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 JavaDoc;
25 import java.net.InetAddress JavaDoc;
26 import java.net.ServerSocket JavaDoc;
27 import java.net.URI JavaDoc;
28 import java.security.KeyManagementException JavaDoc;
29 import java.security.NoSuchAlgorithmException JavaDoc;
30 import java.security.SecureRandom JavaDoc;
31
32 /**
33  * <code>SslMessageReceiver</code> acts like a tcp server to receive socket
34  * requests using SSL.
35  *
36  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
37  * @version $Revision: 3798 $
38  */

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 JavaDoc uri)
48         throws IOException JavaDoc, NoSuchAlgorithmException JavaDoc, KeyManagementException JavaDoc
49     {
50         SslConnector cnn;
51         ServerSocketFactory ssf;
52         cnn = (SslConnector)connector;
53         // An SSLContext is an environment for implementing JSSE
54
// It is used to create a ServerSocketFactory
55
SSLContext sslc = SSLContext.getInstance(cnn.getProtocol().toLowerCase(), cnn.getProvider());
56
57         // Initialize the SSLContext to work with our key managers
58
sslc.init(cnn.getKeyManagerFactory().getKeyManagers(), cnn.getTrustManagerFactory()
59             .getTrustManagers(),
60         // TODO provide more secure seed (othen than the default one)
61
new SecureRandom JavaDoc());
62
63         ssf = sslc.getServerSocketFactory();
64
65         String JavaDoc host = StringUtils.defaultIfEmpty(uri.getHost(), "localhost");
66         int backlog = cnn.getBacklog();
67         SSLServerSocket serverSocket;
68
69         InetAddress JavaDoc 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         // Authenticate the client?
80
serverSocket.setNeedClientAuth(cnn.isRequireClientAuthentication());
81         return serverSocket;
82
83     }
84 }
85
Popular Tags