KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > transport > http > ssl > basic > 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.basic;
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       HTTPSInvokerTestServer.SampleInvocationHandler invocationHandler = new HTTPSInvokerTestServer.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       ServerSocketFactory serverSocketFactory = null;
69
70       // since doing basic (using default ssl server socket factory)
71
// need to set the system properties to the keystore and password
72
String JavaDoc keyStoreFilePath = this.getClass().getResource("../.keystore").getFile();
73       System.setProperty("javax.net.ssl.keyStore", keyStoreFilePath);
74       System.setProperty("javax.net.ssl.keyStorePassword", "opensource");
75
76       SSLSocketBuilder server = new SSLSocketBuilder();
77       serverSocketFactory = server.createSSLServerSocketFactory();
78
79       return serverSocketFactory;
80    }
81
82    protected void setUp() throws Exception JavaDoc
83    {
84       setupServer();
85    }
86
87    protected void tearDown() throws Exception JavaDoc
88    {
89       if(connector != null)
90       {
91          connector.stop();
92          connector.destroy();
93       }
94    }
95
96
97    /**
98     * Simple invocation handler implementation.
99     */

100    public static class SampleInvocationHandler implements ServerInvocationHandler
101    {
102
103
104       /**
105        * called to handle a specific invocation
106        *
107        * @param invocation
108        * @return
109        * @throws Throwable
110        */

111       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
112       {
113          // Print out the invocation request
114
System.out.println("Invocation request is: " + invocation.getParameter());
115          if(NULL_RETURN_PARAM.equals(invocation.getParameter()))
116          {
117             return null;
118          }
119          else if(invocation.getParameter() instanceof ComplexObject)
120          {
121             return OBJECT_RESPONSE_VALUE;
122          }
123          else
124          {
125             // Just going to return static string as this is just simple example code.
126
return RESPONSE_VALUE;
127          }
128       }
129
130       /**
131        * Adds a callback handler that will listen for callbacks from
132        * the server invoker handler.
133        *
134        * @param callbackHandler
135        */

136       public void addListener(InvokerCallbackHandler callbackHandler)
137       {
138          // NO OP as do not handling callback listeners in this example
139
}
140
141       /**
142        * Removes the callback handler that was listening for callbacks
143        * from the server invoker handler.
144        *
145        * @param callbackHandler
146        */

147       public void removeListener(InvokerCallbackHandler callbackHandler)
148       {
149          // NO OP as do not handling callback listeners in this example
150
}
151
152       /**
153        * set the mbean server that the handler can reference
154        *
155        * @param server
156        */

157       public void setMBeanServer(MBeanServer JavaDoc server)
158       {
159          // NO OP as do not need reference to MBeanServer for this handler
160
}
161
162       /**
163        * set the invoker that owns this handler
164        *
165        * @param invoker
166        */

167       public void setInvoker(ServerInvoker invoker)
168       {
169          // NO OP as do not need reference back to the server invoker
170
}
171
172    }
173
174 }
Popular Tags