2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.test.remoting.transport.http.ssl.custom;
25
import java.io.ByteArrayInputStream;
26
import javax.management.MBeanServer;
27
import javax.management.MBeanServerFactory;
28
import javax.management.ObjectName;
29
import javax.xml.parsers.DocumentBuilderFactory;
30
import org.jboss.jrunit.extensions.ServerTestCase;
31
import org.jboss.remoting.InvocationRequest;
32
import org.jboss.remoting.ServerInvocationHandler;
33
import org.jboss.remoting.ServerInvoker;
34
import org.jboss.remoting.callback.InvokerCallbackHandler;
35
import org.jboss.remoting.security.SSLServerSocketFactoryService;
36
import org.jboss.remoting.security.SSLSocketBuilder;
37
import org.jboss.remoting.security.ServerSocketFactoryMBean;
38
import org.jboss.remoting.transport.Connector;
39
import org.jboss.remoting.transport.coyote.ssl.RemotingSSLImplementation;
40
import org.jboss.test.remoting.transport.http.ssl.SSLInvokerConstants;
41
import org.jboss.test.remoting.transport.web.ComplexObject;
42
import org.w3c.dom.Document;
45
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
47
public class HTTPSInvokerTestServer extends ServerTestCase implements SSLInvokerConstants
49
// String to be returned from invocation handler upon client invocation calls.
50
public static final String RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
51
public static final ComplexObject OBJECT_RESPONSE_VALUE = new ComplexObject(5, "dub", false);
52
public static final ComplexObject LARGE_OBJECT_RESPONSE_VALUE = new ComplexObject(5, "dub", false, 7568);
54
public static final String NULL_RETURN_PARAM = "return_null";
55
public static final String OBJECT_RETURN_PARAM = "return_object";
57
private Connector connector = null;
59
public void setupServer() throws Exception
62
String locatorURI = transport + "://" + host + ":" + port;
63
InvokerLocator locator = new InvokerLocator(locatorURI);
64
System.out.println("Starting remoting server with locator uri of: " + locatorURI);
65
connector = new Connector();
66
connector.setInvokerLocator(locator.getLocatorURI());
69
ServerSocketFactory svrSocketFactory = createServerSocketFactory();
70
HTTPSServerInvoker httpsSvrInvoker = (HTTPSServerInvoker) connector.getServerInvoker();
71
httpsSvrInvoker.setServerSocketFactory(svrSocketFactory);
73
SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
74
// first parameter is sub-system name. can be any String value.
75
connector.addInvocationHandler("sample", invocationHandler);
80
MBeanServer server = MBeanServerFactory.createMBeanServer();
82
// create and register socket server factory service
83
ServerSocketFactoryMBean serverSocketFactoryMBean = createServerSocketFactoryMBean();
84
String socketFactoryObjName = "test:type=serversocketfactory";
85
server.registerMBean(serverSocketFactoryMBean, new ObjectName(socketFactoryObjName));
87
connector = new Connector();
88
server.registerMBean(connector, new ObjectName("test:type=connector,transport=coyote"));
89
StringBuffer buf = new StringBuffer();
90
buf.append("<?xml version=\"1.0\"?>\n");
91
buf.append("<config>");
92
buf.append("<invoker transport=\"" + transport + "\">");
93
buf.append("<attribute name=\"SSLImplementation\">" + RemotingSSLImplementation.class.getName() + "</attribute>");
94
buf.append("<attribute name=\"serverSocketFactory\">" + socketFactoryObjName + "</attribute>");
95
buf.append("<attribute name=\"serverBindAddress\">" + host + "</attribute>");
96
buf.append("<attribute name=\"serverBindPort\">" + port + "</attribute>");
97
buf.append("</invoker>");
98
buf.append("<handlers>");
99
buf.append(" <handler subsystem=\"sample\">" + HTTPSInvokerTestServer.SampleInvocationHandler.class.getName() + "</handler>\n");
100
buf.append("</handlers>");
101
buf.append("</config>");
102
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(buf.toString().getBytes()));
103
//connector.setInvokerLocator(locator.getLocatorURI());
104
connector.setConfiguration(xml.getDocumentElement());
111
private ServerSocketFactoryMBean createServerSocketFactoryMBean() throws Exception
113
ServerSocketFactoryMBean serverSocketFactoryMBean = null;
115
SSLSocketBuilder socketBuilder = new SSLSocketBuilder();
116
socketBuilder.setUseSSLServerSocketFactory(false);
118
socketBuilder.setSecureSocketProtocol("SSL");
119
socketBuilder.setKeyStoreAlgorithm("SunX509");
121
socketBuilder.setKeyStoreType("JKS");
122
String keyStoreFilePath = this.getClass().getResource("../.keystore").getFile();
123
socketBuilder.setKeyStoreURL(keyStoreFilePath);
124
socketBuilder.setKeyStorePassword("unit-tests-server");
126
* This is optional since if not set, will use
127
* the key store password (and are the same in this case)
129
//server.setKeyPassword("unit-tests-server");
131
SSLServerSocketFactoryService sslServerSocketFactoryService = new SSLServerSocketFactoryService();
132
sslServerSocketFactoryService.setSSLSocketBuilder(socketBuilder);
133
sslServerSocketFactoryService.start();
134
serverSocketFactoryMBean = sslServerSocketFactoryService;
136
return serverSocketFactoryMBean;
140
private ServerSocketFactory createServerSocketFactory()
141
throws NoSuchAlgorithmException, KeyManagementException, IOException,
142
CertificateException, UnrecoverableKeyException, KeyStoreException
145
ServerSocketFactory serverSocketFactory = null;
147
SSLSocketBuilder server = new SSLSocketBuilder();
148
server.setUseSSLServerSocketFactory(false);
150
server.setSecureSocketProtocol("SSL");
151
server.setKeyManagementAlgorithm("SunX509");
153
server.setKeyStoreType("JKS");
154
String keyStoreFilePath = this.getClass().getResource("../.keystore").getFile();
155
server.setKeyStoreURL(keyStoreFilePath);
156
server.setKeyStorePassword("unit-tests-server");
158
// This is optional since if not set, will use
159
// the key store password (and are the same in this case)
160
//server.setKeyPassword("unit-tests-server");
162
serverSocketFactory = server.createSSLServerSocketFactory();
164
return serverSocketFactory;
168
protected void setUp() throws Exception
173
protected void tearDown() throws Exception
175
if(connector != null)
182
public static void main(String[] args)
184
HTTPSInvokerTestServer server = new HTTPSInvokerTestServer();
188
Thread.currentThread().sleep(600000);
198
* Simple invocation handler implementation.
200
public static class SampleInvocationHandler implements ServerInvocationHandler
205
* called to handle a specific invocation
211
public Object invoke(InvocationRequest invocation) throws Throwable
213
// Print out the invocation request
214
System.out.println("Invocation request is: " + invocation.getParameter());
215
if(NULL_RETURN_PARAM.equals(invocation.getParameter()))
219
else if(invocation.getParameter() instanceof ComplexObject)
221
ComplexObject obj = (ComplexObject) invocation.getParameter();
222
if(obj.getSize() > 1024)
224
return LARGE_OBJECT_RESPONSE_VALUE;
228
return OBJECT_RESPONSE_VALUE;
233
// Just going to return static string as this is just simple example code.
234
return RESPONSE_VALUE;
239
* Adds a callback handler that will listen for callbacks from
240
* the server invoker handler.
242
* @param callbackHandler
244
public void addListener(InvokerCallbackHandler callbackHandler)
246
// NO OP as do not handling callback listeners in this example
250
* Removes the callback handler that was listening for callbacks
251
* from the server invoker handler.
253
* @param callbackHandler
255
public void removeListener(InvokerCallbackHandler callbackHandler)
257
// NO OP as do not handling callback listeners in this example
261
* set the mbean server that the handler can reference
265
public void setMBeanServer(MBeanServer server)
267
// NO OP as do not need reference to MBeanServer for this handler
271
* set the invoker that owns this handler
275
public void setInvoker(ServerInvoker invoker)
277
// NO OP as do not need reference back to the server invoker
b'\\ No newline at end of file'