KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > http > HttpsMessageReceiver


1 /*
2  * $Id: HttpsMessageReceiver.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.http;
12
13 import org.apache.commons.lang.StringUtils;
14 import org.mule.providers.AbstractConnector;
15 import org.mule.umo.UMOComponent;
16 import org.mule.umo.endpoint.UMOEndpoint;
17 import org.mule.umo.lifecycle.InitialisationException;
18
19 import javax.net.ServerSocketFactory;
20 import javax.net.ssl.SSLContext;
21 import javax.net.ssl.SSLServerSocket;
22
23 import java.io.IOException JavaDoc;
24 import java.net.InetAddress JavaDoc;
25 import java.net.ServerSocket JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.security.KeyManagementException JavaDoc;
28 import java.security.NoSuchAlgorithmException JavaDoc;
29 import java.security.SecureRandom JavaDoc;
30
31 /**
32  * <code>HttpsMessageReceiver</code> is a Https server implementation used to
33  * receive incoming requests over https
34  *
35  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
36  * @version $Revision: 3798 $
37  */

38
39 public class HttpsMessageReceiver extends HttpMessageReceiver
40 {
41     public HttpsMessageReceiver(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         HttpsConnector cnn;
51         ServerSocketFactory ssf;
52         cnn = (HttpsConnector)connector;
53         // An SSLContext is an environment for implementing JSSE
54
// It is used to create a ServerSocketFactory
55
SSLContext sslc = SSLContext.getInstance(cnn.getSslType(), 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
Popular Tags