~ubuntu-branches/ubuntu/oneiric/libjboss-remoting-java/oneiric

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/http/ssl/custom/HTTPSInvokerTestServer.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
*
 
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.
 
11
*
 
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.
 
16
*
 
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.
 
21
*/
 
22
 
 
23
package org.jboss.test.remoting.transport.http.ssl.custom;
 
24
 
 
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;
 
43
 
 
44
/**
 
45
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
 
46
 */
 
47
public class HTTPSInvokerTestServer extends ServerTestCase implements SSLInvokerConstants
 
48
{
 
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);
 
53
 
 
54
   public static final String NULL_RETURN_PARAM = "return_null";
 
55
   public static final String OBJECT_RETURN_PARAM = "return_object";
 
56
 
 
57
   private Connector connector = null;
 
58
 
 
59
   public void setupServer() throws Exception
 
60
   {
 
61
      /*
 
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());
 
67
      connector.create();
 
68
 
 
69
      ServerSocketFactory svrSocketFactory = createServerSocketFactory();
 
70
      HTTPSServerInvoker httpsSvrInvoker = (HTTPSServerInvoker) connector.getServerInvoker();
 
71
      httpsSvrInvoker.setServerSocketFactory(svrSocketFactory);
 
72
 
 
73
      SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
 
74
      // first parameter is sub-system name.  can be any String value.
 
75
      connector.addInvocationHandler("sample", invocationHandler);
 
76
 
 
77
      connector.start();
 
78
      */
 
79
 
 
80
      MBeanServer server = MBeanServerFactory.createMBeanServer();
 
81
 
 
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));
 
86
 
 
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());
 
105
      connector.create();
 
106
      connector.start();
 
107
 
 
108
 
 
109
   }
 
110
 
 
111
   private ServerSocketFactoryMBean createServerSocketFactoryMBean() throws Exception
 
112
   {
 
113
      ServerSocketFactoryMBean serverSocketFactoryMBean = null;
 
114
 
 
115
      SSLSocketBuilder socketBuilder = new SSLSocketBuilder();
 
116
      socketBuilder.setUseSSLServerSocketFactory(false);
 
117
 
 
118
      socketBuilder.setSecureSocketProtocol("SSL");
 
119
      socketBuilder.setKeyStoreAlgorithm("SunX509");
 
120
 
 
121
      socketBuilder.setKeyStoreType("JKS");
 
122
      String keyStoreFilePath = this.getClass().getResource("../.keystore").getFile();
 
123
      socketBuilder.setKeyStoreURL(keyStoreFilePath);
 
124
      socketBuilder.setKeyStorePassword("unit-tests-server");
 
125
      /*
 
126
       * This is optional since if not set, will use
 
127
       * the key store password (and are the same in this case)
 
128
       */
 
129
      //server.setKeyPassword("unit-tests-server");
 
130
 
 
131
      SSLServerSocketFactoryService sslServerSocketFactoryService = new SSLServerSocketFactoryService();
 
132
      sslServerSocketFactoryService.setSSLSocketBuilder(socketBuilder);
 
133
      sslServerSocketFactoryService.start();
 
134
      serverSocketFactoryMBean = sslServerSocketFactoryService;
 
135
 
 
136
      return serverSocketFactoryMBean;
 
137
   }
 
138
 
 
139
/*
 
140
   private ServerSocketFactory createServerSocketFactory()
 
141
         throws NoSuchAlgorithmException, KeyManagementException, IOException,
 
142
                CertificateException, UnrecoverableKeyException, KeyStoreException
 
143
   {
 
144
 
 
145
      ServerSocketFactory serverSocketFactory = null;
 
146
 
 
147
      SSLSocketBuilder server = new SSLSocketBuilder();
 
148
      server.setUseSSLServerSocketFactory(false);
 
149
 
 
150
      server.setSecureSocketProtocol("SSL");
 
151
      server.setKeyManagementAlgorithm("SunX509");
 
152
 
 
153
      server.setKeyStoreType("JKS");
 
154
      String keyStoreFilePath = this.getClass().getResource("../.keystore").getFile();
 
155
      server.setKeyStoreURL(keyStoreFilePath);
 
156
      server.setKeyStorePassword("unit-tests-server");
 
157
 
 
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");
 
161
 
 
162
      serverSocketFactory = server.createSSLServerSocketFactory();
 
163
 
 
164
      return serverSocketFactory;
 
165
   }
 
166
*/
 
167
 
 
168
   protected void setUp() throws Exception
 
169
   {
 
170
      setupServer();
 
171
   }
 
172
 
 
173
   protected void tearDown() throws Exception
 
174
   {
 
175
      if(connector != null)
 
176
      {
 
177
         connector.stop();
 
178
         connector.destroy();
 
179
      }
 
180
   }
 
181
 
 
182
   public static void main(String[] args)
 
183
   {
 
184
      HTTPSInvokerTestServer server = new HTTPSInvokerTestServer();
 
185
      try
 
186
      {
 
187
         server.setUp();
 
188
         Thread.currentThread().sleep(600000);
 
189
      }
 
190
      catch(Exception e)
 
191
      {
 
192
         e.printStackTrace();
 
193
      }
 
194
 
 
195
   }
 
196
 
 
197
   /**
 
198
    * Simple invocation handler implementation.
 
199
    */
 
200
   public static class SampleInvocationHandler implements ServerInvocationHandler
 
201
   {
 
202
 
 
203
 
 
204
      /**
 
205
       * called to handle a specific invocation
 
206
       *
 
207
       * @param invocation
 
208
       * @return
 
209
       * @throws Throwable
 
210
       */
 
211
      public Object invoke(InvocationRequest invocation) throws Throwable
 
212
      {
 
213
         // Print out the invocation request
 
214
         System.out.println("Invocation request is: " + invocation.getParameter());
 
215
         if(NULL_RETURN_PARAM.equals(invocation.getParameter()))
 
216
         {
 
217
            return null;
 
218
         }
 
219
         else if(invocation.getParameter() instanceof ComplexObject)
 
220
         {
 
221
            ComplexObject obj = (ComplexObject) invocation.getParameter();
 
222
            if(obj.getSize() > 1024)
 
223
            {
 
224
               return LARGE_OBJECT_RESPONSE_VALUE;
 
225
            }
 
226
            else
 
227
            {
 
228
               return OBJECT_RESPONSE_VALUE;
 
229
            }
 
230
         }
 
231
         else
 
232
         {
 
233
            // Just going to return static string as this is just simple example code.
 
234
            return RESPONSE_VALUE;
 
235
         }
 
236
      }
 
237
 
 
238
      /**
 
239
       * Adds a callback handler that will listen for callbacks from
 
240
       * the server invoker handler.
 
241
       *
 
242
       * @param callbackHandler
 
243
       */
 
244
      public void addListener(InvokerCallbackHandler callbackHandler)
 
245
      {
 
246
         // NO OP as do not handling callback listeners in this example
 
247
      }
 
248
 
 
249
      /**
 
250
       * Removes the callback handler that was listening for callbacks
 
251
       * from the server invoker handler.
 
252
       *
 
253
       * @param callbackHandler
 
254
       */
 
255
      public void removeListener(InvokerCallbackHandler callbackHandler)
 
256
      {
 
257
         // NO OP as do not handling callback listeners in this example
 
258
      }
 
259
 
 
260
      /**
 
261
       * set the mbean server that the handler can reference
 
262
       *
 
263
       * @param server
 
264
       */
 
265
      public void setMBeanServer(MBeanServer server)
 
266
      {
 
267
         // NO OP as do not need reference to MBeanServer for this handler
 
268
      }
 
269
 
 
270
      /**
 
271
       * set the invoker that owns this handler
 
272
       *
 
273
       * @param invoker
 
274
       */
 
275
      public void setInvoker(ServerInvoker invoker)
 
276
      {
 
277
         // NO OP as do not need reference back to the server invoker
 
278
      }
 
279
 
 
280
   }
 
281
 
 
282
}
 
 
b'\\ No newline at end of file'