KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xmlrpc > secure > SecureWebServer


1 /*
2  * Copyright 1999,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.xmlrpc.secure;
19
20
21 import java.io.FileInputStream JavaDoc;
22 import java.net.InetAddress JavaDoc;
23 import java.net.ServerSocket JavaDoc;
24 import java.security.KeyStore JavaDoc;
25
26 import javax.net.ssl.SSLServerSocket;
27 import javax.net.ssl.SSLServerSocketFactory;
28
29 import org.apache.xmlrpc.WebServer;
30 import org.apache.xmlrpc.XmlRpc;
31 import org.apache.xmlrpc.XmlRpcServer;
32
33 import com.sun.net.ssl.KeyManagerFactory;
34 import com.sun.net.ssl.SSLContext;
35
36 /**
37  * A minimal web server that exclusively handles XML-RPC requests
38  * over a secure channel.
39  *
40  * Standard security properties must be set before the SecureWebserver
41  * can be used. The SecurityTool takes care of retrieving these
42  * values, but the parent application must set the necessary
43  * values before anything will work.
44  *
45  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
46  * @version $Id: SecureWebServer.java,v 1.7 2005/04/22 10:25:58 hgomez Exp $
47  */

48 public class SecureWebServer
49     extends WebServer
50     implements SecurityConstants
51 {
52     /**
53      * Creates a secure web server configured to run on the specified
54      * port number.
55      *
56      * @param int port number of secure web server.
57      * @see #SecureWebServer(int, InetAddress)
58      */

59     public SecureWebServer(int port)
60     {
61         this(port, null);
62     }
63
64     /**
65      * Creates a secure web server configured to run on the specified
66      * port number and IP address.
67      *
68      * @param int port number of the secure web server
69      * @param addr The IP address to bind to.
70      * @see org.apache.xmlrpc.WebServer#WebServer(int, InetAddress)
71      */

72     public SecureWebServer(int port, InetAddress JavaDoc addr)
73     {
74         super(port, addr);
75     }
76
77
78     /**
79      * Creates a secure web server at the specified port number and IP
80      * address.
81      */

82     public SecureWebServer(int port, InetAddress JavaDoc addr, XmlRpcServer xmlrpc)
83     {
84         super(port, addr, xmlrpc);
85     }
86
87     /**
88      * @see org.apache.xmlrpc.WebServer#createServerSocket(int port, int backlog, InetAddress add)
89      */

90     protected ServerSocket JavaDoc createServerSocket(int port, int backlog, InetAddress JavaDoc add)
91         throws Exception JavaDoc
92     {
93         SecurityTool.setup();
94     
95         SSLContext context = SSLContext.getInstance(SecurityTool.getSecurityProtocol());
96           
97         KeyManagerFactory keyManagerFactory =
98             KeyManagerFactory.getInstance(SecurityTool.getKeyManagerType());
99             
100         KeyStore JavaDoc keyStore = KeyStore.getInstance(SecurityTool.getKeyStoreType());
101             
102         keyStore.load(new FileInputStream JavaDoc(SecurityTool.getKeyStore()),
103             SecurityTool.getKeyStorePassword().toCharArray());
104             
105         keyManagerFactory.init(keyStore, SecurityTool.getKeyStorePassword().toCharArray());
106             
107         context.init(keyManagerFactory.getKeyManagers(), null, null);
108         SSLServerSocketFactory sslSrvFact = context.getServerSocketFactory();
109         return (SSLServerSocket) sslSrvFact.createServerSocket(port);
110     }
111
112     /**
113      * This <em>can</em> be called from command line, but you'll have to
114      * edit and recompile to change the server port or handler objects.
115      *
116      * @see org.apache.xmlrpc.WebServer#addDefaultHandlers()
117      */

118     public static void main(String JavaDoc[] argv)
119     {
120         int p = determinePort(argv, 10000);
121         XmlRpc.setKeepAlive (true);
122         SecureWebServer webserver = new SecureWebServer (p);
123
124         try
125         {
126             webserver.addDefaultHandlers();
127             webserver.start();
128         }
129         catch (Exception JavaDoc e)
130         {
131             System.err.println("Error running secure web server");
132             e.printStackTrace();
133             System.exit(1);
134         }
135     }
136 }
137
Popular Tags