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.web;
25
import java.io.OutputStream;
26
import java.net.HttpURLConnection;
28
import java.util.HashMap;
30
import java.util.Properties;
32
import javax.net.ssl.HttpsURLConnection;
34
import junit.framework.TestCase;
36
import org.apache.log4j.Logger;
37
import org.jboss.remoting.Client;
38
import org.jboss.remoting.InvokerLocator;
39
import org.jboss.remoting.transport.http.HTTPMetadataConstants;
40
import org.jboss.remoting.transport.http.WebServerError;
41
import org.jboss.remoting.transport.web.WebUtil;
44
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
46
public abstract class WebInvokerTestClient extends TestCase
48
protected static Logger log = Logger.getLogger(WebInvokerTestClient.class);
50
public abstract String getLocatorURI();
52
public void testPostInvocation() throws Exception
54
testPostInvocationSub(true);
55
testPostInvocationSub(false);
60
* If raw == true, set content-type appropriately.
61
* If raw == false, all content will be binary and well be treated as binary.
63
public void testPostInvocationSub(boolean raw) throws Exception
65
Client remotingClient = null;
69
InvokerLocator locator = new InvokerLocator(getLocatorURI());
70
log.debug("Calling remoting server with locator uri of: " + getLocatorURI());
72
remotingClient = new Client(locator);
73
remotingClient.connect();
75
Map metadata = new HashMap();
80
// The following use of two versions of Client.RAW is to account for the
81
// fact that the value of Client.RAW changed from Remoting 1.4.x to
82
// Remoting 2.0.0. This test is used as part of the version compatibility
84
metadata.put("rawPayload", Boolean.TRUE);
85
metadata.put("RAW_PAYLOAD", Boolean.TRUE);
88
metadata.put("TYPE", "POST");
90
Properties headerProps = new Properties();
94
headerProps.put(HTTPMetadataConstants.REMOTING_CONTENT_TYPE, HTTPMetadataConstants.REMOTING_CONTENT_TYPE_STRING);
98
headerProps.put("Content-Type", WebUtil.BINARY);
99
headerProps.put(HTTPMetadataConstants.REMOTING_CONTENT_TYPE, HTTPMetadataConstants.REMOTING_CONTENT_TYPE_NON_STRING);
102
addHeaders(headerProps);
103
metadata.put("HEADER", headerProps);
106
Properties headerProps1 = new Properties(headerProps);
107
HashMap onewayMetadata1 = new HashMap(metadata);
108
onewayMetadata1.put("HEADER", headerProps1);
109
remotingClient.invokeOneway("Do something", onewayMetadata1, true);
111
Properties headerProps2 = new Properties(headerProps);
112
HashMap onewayMetadata2 = new HashMap(metadata);
113
onewayMetadata2.put("HEADER", headerProps2);
114
remotingClient.invokeOneway("Do something", onewayMetadata2, false);
117
// test with null return expected
118
Object response = null;
119
response = remotingClient.invoke(WebInvocationHandler.NULL_RETURN_PARAM, metadata);
120
log.debug("First response should be null and was: " + response);
121
assertNull(response);
123
response = remotingClient.invoke("Do something", metadata);
124
log.debug("Second response should be " + WebInvocationHandler.HTML_PAGE_RESPONSE + " and was: " + response);
125
assertEquals(WebInvocationHandler.HTML_PAGE_RESPONSE, response);
129
headerProps.put("Content-Type", WebUtil.BINARY);
131
response = remotingClient.invoke(new ComplexObject(2, "foo", true), metadata);
132
log.debug("Third response should be " + WebInvocationHandler.OBJECT_RESPONSE_VALUE + " and was: " + response);
133
assertEquals(WebInvocationHandler.OBJECT_RESPONSE_VALUE, response);
135
response = remotingClient.invoke(new ComplexObject(2, "foo", true, 3000), metadata);
136
log.debug("Fourth response should be " + WebInvocationHandler.LARGE_OBJECT_RESPONSE_VALUE + " and was: " + response);
137
assertEquals(WebInvocationHandler.LARGE_OBJECT_RESPONSE_VALUE, response);
141
headerProps.put("Content-Type", "application/soap+xml");
143
response = remotingClient.invoke(WebInvocationHandler.STRING_RETURN_PARAM, metadata);
144
log.debug("Fifth response should be " + WebInvocationHandler.RESPONSE_VALUE + " and was: " + response);
145
assertEquals(WebInvocationHandler.RESPONSE_VALUE, response);
147
checkUserAgent(remotingClient, metadata);
149
makeExceptionInvocation(remotingClient, metadata);
152
catch (Throwable throwable)
154
throw new Exception(throwable);
158
if (remotingClient != null)
160
remotingClient.disconnect();
165
protected void checkUserAgent(Client remotingClient, Map metadata)
169
String remotingUserAgentValue = "JBossRemoting - ";
170
response = remotingClient.invoke(WebInvocationHandler.USER_AGENT_PARAM, metadata);
171
log.debug("Sixth response start with " + remotingUserAgentValue + " and was: " + response);
172
boolean correctUserAgent = ((String) response).startsWith(remotingUserAgentValue);
173
assertTrue("User-Agent should be begin with " + remotingUserAgentValue + " but was " + response, correctUserAgent);
176
protected void makeExceptionInvocation(Client remotingClient, Map metadata)
180
Object response = null;
184
log.debug("making exception invocation");
185
response = remotingClient.invoke(WebInvocationHandler.THROW_EXCEPTION_PARAM, metadata);
186
assertTrue("Should have thrown WebServerError and not made it to here.", false);
188
catch (Exception error)
190
log.debug("exception: " + error + " " + error.getMessage());
191
// having to check class name instead of just catching type WebServerError so
192
// can use for backwards compatibility tests since WebServerError is new since 2.0.0.CR1.
194
if (getLocatorURI().indexOf("dont-return-exception=true") >= 0)
196
assertTrue("Did not get WebServerError", error instanceof WebServerError);
197
assertNotNull(error.getMessage());
198
assertTrue(error.getMessage().indexOf("Error occurred processing invocation request. ") >= 0);
200
else if (Boolean.TRUE.equals(metadata.get("rawPayload")) ||
201
Boolean.TRUE.equals(metadata.get("RAW_PAYLOAD")))
203
assertTrue("Did not get WebServerError", error instanceof WebServerError);
204
assertNotNull(error.getMessage());
205
log.debug("message: " + error.getMessage());
206
log.debug("message type: " + error.getMessage().getClass());
207
assertTrue(error.getMessage().startsWith("Error received when calling on web server."));
211
assertTrue("Did not get WebTestException", error instanceof WebTestException);
215
metadata.put(HTTPMetadataConstants.NO_THROW_ON_ERROR, "true");
216
response = remotingClient.invoke(WebInvocationHandler.THROW_EXCEPTION_PARAM, metadata);
217
if (response instanceof Exception)
219
log.debug("Return from invocation is of type Exception as expected.");
220
assertTrue("Received exception return as expected.", true);
224
log.info("Did not get Exception type returned as expected.");
225
assertTrue("Should have received Exception as return.", false);
227
metadata.remove(HTTPMetadataConstants.NO_THROW_ON_ERROR);
230
protected void checkContentType() throws Exception
232
log.debug("check_content_type: " + System.getProperty("check_content_type"));
233
String s = System.getProperty("check_content_type", "true");
234
boolean doCheck = Boolean.valueOf(s).booleanValue();
237
log.debug("skipping content type check");
241
String urlString = getLocatorURI();
242
int pos = urlString.indexOf("servlet");
245
urlString = "http" + urlString.substring("servlet".length());
247
pos = urlString.indexOf("sslservlet");
250
urlString = "https" + urlString.substring("sslservlet".length());
253
URL url = new URL(urlString);
254
OutputStream os = null;
255
String contentType = null;
257
if (urlString.startsWith("https"))
259
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
260
conn.setDoOutput(true);
261
conn.setDoInput(true);
262
conn.addRequestProperty(HTTPMetadataConstants.REMOTING_CONTENT_TYPE, HTTPMetadataConstants.REMOTING_CONTENT_TYPE_STRING);
263
os = conn.getOutputStream();
264
byte[] requestBytes = WebInvocationHandler.SET_CONTENT_TYPE.getBytes();
265
os.write(requestBytes);
266
contentType = conn.getContentType();
270
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
271
conn.setDoOutput(true);
272
conn.setDoInput(true);
273
conn.addRequestProperty(HTTPMetadataConstants.REMOTING_CONTENT_TYPE, HTTPMetadataConstants.REMOTING_CONTENT_TYPE_STRING);
274
os = conn.getOutputStream();
275
byte[] requestBytes = WebInvocationHandler.SET_CONTENT_TYPE.getBytes();
276
os.write(requestBytes);
277
contentType = conn.getContentType();
280
// Verify that content-type is the value set in the ServerInvocationHandler.
281
log.debug("content-type: " + contentType);
282
assertEquals(WebInvocationHandler.CONTENT_TYPE, contentType);
285
public void testGetInvocation() throws Exception
287
testGetInvocationSub(true);
288
testGetInvocationSub(false);
291
protected void testGetInvocationSub(boolean raw) throws Exception
293
Client remotingClient = null;
297
InvokerLocator locator = new InvokerLocator(getLocatorURI());
298
log.debug("Calling remoting server with locator uri of: " + getLocatorURI());
300
remotingClient = new Client(locator);
301
remotingClient.connect();
303
Map metadata = new HashMap();
306
metadata.put(Client.RAW, Boolean.TRUE);
308
metadata.put("TYPE", "GET");
310
Object response = null;
312
// test with null return expected
313
response = remotingClient.invoke((Object) null, metadata);
314
log.debug("Response should be " + WebInvocationHandler.HTML_PAGE_RESPONSE + " and was: " + response);
315
assertEquals(WebInvocationHandler.HTML_PAGE_RESPONSE, response);
317
response = remotingClient.invoke((Object) null, metadata);
318
log.debug("Response should be " + WebInvocationHandler.HTML_PAGE_RESPONSE + " and was: " + response);
319
assertEquals(WebInvocationHandler.HTML_PAGE_RESPONSE, response);
321
catch (Throwable throwable)
323
throw new Exception(throwable);
327
if (remotingClient != null)
329
remotingClient.disconnect();
334
protected void addHeaders(Properties headerProps)
336
//NO OP - for overriding by sub-classes.
b'\\ No newline at end of file'