KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > transport > http > ssl > custom > HTTPSInvokerTestServer


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.remoting.transport.http.ssl.custom;
8
9 import java.io.IOException JavaDoc;
10 import java.security.KeyManagementException JavaDoc;
11 import java.security.KeyStoreException JavaDoc;
12 import java.security.NoSuchAlgorithmException JavaDoc;
13 import java.security.UnrecoverableKeyException JavaDoc;
14 import java.security.cert.CertificateException JavaDoc;
15 import javax.management.MBeanServer JavaDoc;
16 import javax.net.ServerSocketFactory;
17 import org.jboss.jrunit.extensions.ServerTestCase;
18 import org.jboss.remoting.InvocationRequest;
19 import org.jboss.remoting.InvokerLocator;
20 import org.jboss.remoting.ServerInvocationHandler;
21 import org.jboss.remoting.ServerInvoker;
22 import org.jboss.remoting.callback.InvokerCallbackHandler;
23 import org.jboss.remoting.security.SSLSocketBuilder;
24 import org.jboss.remoting.transport.Connector;
25 import org.jboss.remoting.transport.http.ssl.HTTPSServerInvoker;
26 import org.jboss.test.remoting.transport.http.ssl.SSLInvokerConstants;
27 import org.jboss.test.remoting.transport.web.ComplexObject;
28
29 /**
30  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
31  */

32 public class HTTPSInvokerTestServer extends ServerTestCase implements SSLInvokerConstants
33 {
34    // String to be returned from invocation handler upon client invocation calls.
35
public static final String JavaDoc RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
36    public static final ComplexObject OBJECT_RESPONSE_VALUE = new ComplexObject(5, "dub", false);
37
38    public static final String JavaDoc NULL_RETURN_PARAM = "return_null";
39    public static final String JavaDoc OBJECT_RETURN_PARAM = "return_object";
40
41    private Connector connector = null;
42
43    public void setupServer() throws Exception JavaDoc
44    {
45       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
46       InvokerLocator locator = new InvokerLocator(locatorURI);
47       System.out.println("Starting remoting server with locator uri of: " + locatorURI);
48       connector = new Connector();
49       connector.setInvokerLocator(locator.getLocatorURI());
50       connector.create();
51
52       ServerSocketFactory svrSocketFactory = createServerSocketFactory();
53       HTTPSServerInvoker httpsSvrInvoker = (HTTPSServerInvoker) connector.getServerInvoker();
54       httpsSvrInvoker.setServerSocketFactory(svrSocketFactory);
55
56       SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
57       // first parameter is sub-system name. can be any String value.
58
connector.addInvocationHandler("sample", invocationHandler);
59
60       connector.start();
61
62    }
63
64    private ServerSocketFactory createServerSocketFactory()
65          throws NoSuchAlgorithmException JavaDoc, KeyManagementException JavaDoc, IOException JavaDoc,
66                 CertificateException JavaDoc, UnrecoverableKeyException JavaDoc, KeyStoreException JavaDoc
67    {
68
69       ServerSocketFactory serverSocketFactory = null;
70
71       SSLSocketBuilder server = new SSLSocketBuilder();
72       server.setUseSSLServerSocketFactory(false);
73
74       server.setSecureSocketProtocol("SSL");
75       server.setKeyManagementAlgorithm("SunX509");
76
77       server.setKeyStoreType("JKS");
78       String JavaDoc keyStoreFilePath = this.getClass().getResource("../.keystore").getFile();
79       server.setKeyStoreURL(keyStoreFilePath);
80       server.setKeyStorePassword("opensource");
81       /*
82        * This is optional since if not set, will use
83        * the key store password (and are the same in this case)
84        */

85       //server.setKeyPassword("opensource");
86

87       serverSocketFactory = server.createSSLServerSocketFactory();
88
89       return serverSocketFactory;
90    }
91
92    protected void setUp() throws Exception JavaDoc
93    {
94       setupServer();
95    }
96
97    protected void tearDown() throws Exception JavaDoc
98    {
99       if(connector != null)
100       {
101          connector.stop();
102          connector.destroy();
103       }
104    }
105
106
107    /**
108     * Simple invocation handler implementation.
109     */

110    public static class SampleInvocationHandler implements ServerInvocationHandler
111    {
112
113
114       /**
115        * called to handle a specific invocation
116        *
117        * @param invocation
118        * @return
119        * @throws Throwable
120        */

121       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
122       {
123          // Print out the invocation request
124
System.out.println("Invocation request is: " + invocation.getParameter());
125          if(NULL_RETURN_PARAM.equals(invocation.getParameter()))
126          {
127             return null;
128          }
129          else if(invocation.getParameter() instanceof ComplexObject)
130          {
131             return OBJECT_RESPONSE_VALUE;
132          }
133          else
134          {
135             // Just going to return static string as this is just simple example code.
136
return RESPONSE_VALUE;
137          }
138       }
139
140       /**
141        * Adds a callback handler that will listen for callbacks from
142        * the server invoker handler.
143        *
144        * @param callbackHandler
145        */

146       public void addListener(InvokerCallbackHandler callbackHandler)
147       {
148          // NO OP as do not handling callback listeners in this example
149
}
150
151       /**
152        * Removes the callback handler that was listening for callbacks
153        * from the server invoker handler.
154        *
155        * @param callbackHandler
156        */

157       public void removeListener(InvokerCallbackHandler callbackHandler)
158       {
159          // NO OP as do not handling callback listeners in this example
160
}
161
162       /**
163        * set the mbean server that the handler can reference
164        *
165        * @param server
166        */

167       public void setMBeanServer(MBeanServer JavaDoc server)
168       {
169          // NO OP as do not need reference to MBeanServer for this handler
170
}
171
172       /**
173        * set the invoker that owns this handler
174        *
175        * @param invoker
176        */

177       public void setInvoker(ServerInvoker invoker)
178       {
179          // NO OP as do not need reference back to the server invoker
180
}
181
182    }
183
184 }
Popular Tags