Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
HydraExpress Web Service Development Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

15.4 Creating Your Own Handlers

This section includes a simple tutorial for creating your own handlers. It uses an example in the <installdir>\tutorials\webservices\Handlers directory. This directory contains provided client and server implementations, two handler classes, and configuration files required to configure the handlers.

On the client, we will add two handlers to the proxy: a request handler and a transport handler.

On the server, the same handler will take the SOAP header and perform special processing before adding a response header back in for the response message.

Creating a customized handler is a simple task, requiring the following basic steps:

15.4.1 Generate Code

Before we create a custom handler, we'll generate code from the supplied handlers.wsdl. Open a command prompt and change to the directory <installdir>\tutorials\webservices\Handlers. Then generate code as following:

HydraExpress will generate code based on handlers.wsdl into a directory HandlersExample. For basic information on how to generate code, see Chapter 3, "Creating a Web Service."

Now we're ready to create a handler.

15.4.2 Write the Handler Implementation

The two handlers included in this example are SoapSecurityHandler, a request handler, and StringReverseHandler, a transport handler. Both derive from base class rwsf::MessageHandlerImp.

rwsf::MessageHandlerImp is the body class of a handle/body pair. You derive from this class and implement the method invoke(). This method takes a rwsf::CallInfo object containing any data or information to be included with the message.

15.4.2.1 Creating a Request Handler

First, we'll create the request handler. A request handler processes the message body itself. Take a look at the included class SoapSecurityHandler.

//1Derives from rwsf::MessageHandlerImp.
//2If isClient is true, this is the client and not the server. If it's the client, let's add a request SOAP header containing security data.
//3Or, if this is the server, get the client's SOAP header and add it to the response header.

15.4.2.2 Creating a Transport Handler

A transport handler processes the message after it is created and before it is transported. Let's look at the transport handler class, StringReverseHandler.

//1Derives from rwsf::MessageHandlerImp.
//2If isRequest is true, this is a request from a client, so get it. Otherwise, this is a response from the server, so get that.
//3Reverse the bytes in the message. This just demonstrates how to change the message before it is sent over the transport.
//4If this is a request, add the changed message to the request; otherwise, add it to the response.

Now that you've created your handlers, they are ready to add to the client proxy.

15.4.2.3 Aborting Handler Processing

If you wish to stop the further processing of a message by the message processing layer (for instance, based on a SOAP header it contains) you may do so.

From your handler implementation, simply invoke

This method aborts the further processing of a message.

15.4.3 Add the Handlers to the Proxy

The client proxy is itself a handler, and also supports chaining handlers. For general information on how to use the client proxy, see Chapter 7, "Developing Clients."

To add new handlers to the proxy, just invoke the appropriate "add handler" methods on the proxy after you call the make() method. Following is an excerpt from the supplied implementation of the proxy, HandlersClient.cpp.

15.4.4 Register the Handlers

There are two ways to register your new handlers. The easiest way is to define them in the server implementation, using the macro RWSF_DEFINE_MESSAGE_HANDLER. This method is the easiest because it requires no other changes. Following is an excerpt from the supplied server implementation, HandlersImp.cpp.

//1Include the handler header files.
//2Use the RWSF_DEFINE_MESSAGE_HANDLER macro to register your handlers.

The disadvantage of the above method is that it ties the handlers to the service implementation when really they should be independent of it. One way to achieve a decoupling of the handlers from the service implementation is by declaring the RWSF_DEFINE_MESSAGE_HANDLER macros in the implementation files for the handlers themselves and compiling the handlers into a separate library.

To complete the registration of your handlers, simply add them to the handlers_objects.xml file. This file is loaded by the Agent at startup, making all objects in it available to any running service. Let's take a look at the relevant lines in the supplied handlers_objects.xml:

//1Add a naming-obj element for each handler. The element must include a naming-name and a naming-class attribute.
//2The naming-class attribute is made up of library.createClass where "library" is the name of the service and the shared library (or dll on Windows), and "Class" is the name of the class to instantiate.

15.4.5 Configure the Handlers

To configure and chain your handlers, add them to handlers_handlers.xml in the appropriate chain.

//1Add the request handler by its name attribute before the skeleton.
//2Add the transport handler.

15.4.6 Compile and Deploy the Service

Before building this example, copy all provided sample files from <installdir>\tutorials\webservices\Handlers to the new HandlersExample directory, allowing the provided files to overwrite the generated files, as follows:

HandlersClient.cpp

Copy to HandlersExample\app\client

HandlersImp.cpp

Copy to HandlersExample\app\server

SoapSecurityHandler.h
StringReverseHandler.h

Copy to HandlersExample\include

handlers_handlers.xml
handlers_objects.xml

Copy to HandlersExample\conf

To compile the client and server, run nmake (Windows) or make (Linux or UNIX) on the makefile.

To deploy the Agent, run nmake (Windows) or make (Linux or UNIX) for the deploy target, and then start the server.


If the Agent is already running, you must first stop it before you deploy your service, using the rwsfserver stop command.

.

Win32

%RWSF_HOME%\bin\rwsfserver stop

nmake deploy

%RWSF_HOME%\bin\rwsfserver start

UNIX/Linux

$RWSF_HOME/bin/rwsfserver stop

make deploy

$RWSF_HOME/bin/rwsfserver start

For more information on compiling and deploying, see Chapter 23, "Options for Compiling."

Change directories to HandlersExample\bin, and run the client by entering:

The server should return:



Previous fileTop of DocumentContentsIndex pageNext file

©2004-2007 Copyright Quovadx, Inc. All Rights Reserved.
Quovadx and Rogue Wave are registered trademarks of Quovadx, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.