This topic has not yet been written. The content below is from the topic description.
Test HTML page Test HTML page HTTP invocation This http invocation sample (found in the org.jboss.remoting.samples.http package), demonstrates how the http invoker can be used for a variety of http based invocations. This time, will start with the server side. The SimpleServer class is much like the one from the previous simple invocation example, except that instead of using the 'socket' transport, will be using the 'http' transport. Also, instead of using the SampleInvocationHandler class as the handler, will be using the WebInvocationHandler (code shown below). public class WebInvocationHandler implements ServerInvocationHandler { // Pre-defined returns to be sent back to client based on type of request. public static final String RESPONSE_VALUE = "This is the return to simple text based http invocation."; public static final ComplexObject OBJECT_RESPONSE_VALUE = new ComplexObject(5, "dub", false); public static final String HTML_PAGE_RESPONSE = " " + " HTTP/Servlet Test HTML page This is a simple page served for test." + " Should show up in browser or via invoker client"; // Different request types that client may make public static final String NULL_RETURN_PARAM = "return_null"; public static final String OBJECT_RETURN_PARAM = "return_object"; public static final String STRING_RETURN_PARAM = "return_string"; /** * called to handle a specific invocation * * @param invocation * @return * @throws Throwable */ public Object invoke(InvocationRequest invocation) throws Throwable { // Print out the invocation request System.out.println("Invocation request from client is: " + invocation.getParameter()); if(NULL_RETURN_PARAM.equals(invocation.getParameter())) { return null; } else if(invocation.getParameter() instanceof ComplexObject) { return OBJECT_RESPONSE_VALUE; } else if(STRING_RETURN_PARAM.equals(invocation.getParameter())) { Map responseMetadata = invocation.getReturnPayload(); responseMetadata.put(HTTPMetadataConstants.RESPONSE_CODE, new Integer(207)); responseMetadata.put(HTTPMetadataConstants.RESPONSE_CODE_MESSAGE, "Custom response code and message from remoting server"); // Just going to return static string as this is just simple example code. return RESPONSE_VALUE; } else { return HTML_PAGE_RESPONSE; } } The most interesting part of the WebInvocationHandler is its invoke() method implementation. First it will check to see what the request parameter was from the InvocationRequest and based on what the value is, will return different responses. The first check is to see if the client passed a request to return a null value. The second will check to see if the request parameter from the client was of type ComplexObject. If so, return the pre-built ComplexObject that was created as a static variable. After that, will check to see if the request parameter was for returning a simple String. Notice in this block, will set the desired response code and message to be returned to the client. In this case, are setting the response code to be returned to 207 and the response message to "Custom response code and message from remoting server". These are non-standard code and message, but can be anything desired. Last, if have not found a matching invocation request parameter, will just return some simple html. Now onto the client side for making the calls to this handler, which can be found in SimpleClient (code shown below). public class SimpleClient { // Default locator values private static String transport = "http"; private static String host = "localhost"; private static int port = 5400; public void makeInvocation(String locatorURI) throws Throwable { // create InvokerLocator with the url type string // indicating the target remoting server to call upon. InvokerLocator locator = new InvokerLocator(locatorURI); System.out.println("Calling remoting server with locator uri of: " + locatorURI); Client remotingClient = new Client(locator); // make invocation on remoting server and send complex data object // by default, the remoting http client invoker will use method type of POST, // which is needed when ever sending objects to the server. So no metadata map needs // to be passed to the invoke() method. Object response = remotingClient.invoke(new ComplexObject(2, "foo", true), null); System.out.println("\nResponse from remoting http server when making http POST request and sending a complex data object:\n" + response); Map metadata = new HashMap(); // set the metadata so remoting client knows to use http GET method type metadata.put("TYPE", "GET"); // not actually sending any data to the remoting server, just want to get its response response = remotingClient.invoke((Object) null, metadata); System.out.println("\nResponse from remoting http server when making GET request:\n" + response); // now set type back to POST and send a plain text based request metadata.put("TYPE", "POST"); response = remotingClient.invoke(WebInvocationHandler.STRING_RETURN_PARAM, metadata); System.out.println("\nResponse from remoting http server when making http POST request and sending a text based request:\n" + response); // notice are getting custom response code and message set by web invocation handler Integer responseCode = (Integer) metadata.get(HTTPMetadataConstants.RESPONSE_CODE); String responseMessage = (String) metadata.get(HTTPMetadataConstants.RESPONSE_CODE_MESSAGE); System.out.println("Response code from server: " + responseCode); System.out.println("Response message from server: " + responseMessage); } This SimpleClient, like the one before in the simple invocation example, starts off by creating an InvokerLocator and remoting Client instance, except is using http transport instead of socket. The first invocation made is to send a newly constructed ComplexObject. If remember from the WebInvocationHandler above, will expect this invocation to return a different ComplexObject, which can be seen in the following system output line. The next invocation to be made is a simple http GET request. To do this, must first let the remoting client know that the method type needs to be changed from the default, which is POST, to be GET. Then make the invocation with a null payload (since not wanting to send any data, just get data in response) and the metadata map just populated with the GET type. This invocation request will return a response of html. Then, will change back to being a POST type request and will pass a simple String as the payload to the invocation request. This will return a simple String as the response from the WebInvocationHandler. Afterward, will see the specific response code and message printed to standard output, as well as the exception itself. To run this example, can compile all the classes in the package, then first run the SimpleServer and then the SimpleClient. Or can go to the examples directory and run the ant target 'run-http-server' and then in another console window run the ant target 'run-http-client'. For example: ant run-http-server and then: ant run-http-client The output when running the SimpleClient should look like: Response from remoting http server when making http POST request and sending a complex data object: ComplexObject (i = 5, s = dub, b = false, bytes.length = 0) Response from remoting http server when making GET request: HTTP/Servlet Test HTML page This is a simple page served for test. Should show up in browser or via invoker client Response from remoting http server when making http POST request and sending a text based request: This is the return to simple text based http invocation. Response code from server: 207 Response message from server: Custom response code and message from remoting server Notice that the first response is the ComplexObject from the static variable returned within WebInvocationHandler. The next response is html and then simple text from the WebInvocationHandler. Can see the specific response code and message set in the WebInvocationHandler. The output from the SimpleServer should look like: Starting remoting server with locator uri of: http://localhost:5400 Jan 26, 2006 11:39:53 PM org.apache.coyote.http11.Http11BaseProtocol init INFO: Initializing Coyote HTTP/1.1 on http-127.0.0.1-5400 Jan 26, 2006 11:39:53 PM org.apache.coyote.http11.Http11BaseProtocol start INFO: Starting Coyote HTTP/1.1 on http-127.0.0.1-5400 Invocation request from client is: ComplexObject (i = 2, s = foo, b = true, bytes.length = 0) Invocation request from client is: null Invocation request from client is: return_string First the information for the http server invoker is written, which includes the locator uri used to start the server and the output from starting the Tomcat connector. Then will see the invocation parameter passed for each client request. Since the SimpleServer should still be running, can open a web browser and enter the locator uri, http://localhost:5400. This should cause the browser to render the html returned from the WebInvocationHandler.