Welcome!

Weblogic Authors: Yeshim Deniz, Elizabeth White, Michael Meiner, Michael Bushong, Avi Rosenthal

Related Topics: Weblogic

Weblogic: Article

Using SOAP Message Handlers with WebLogic 7

Using SOAP Message Handlers with WebLogic 7

WebLogic Server provides mechanisms to expose standard J2EE components and Java classes as Web services. It typically processes an RPC-style Web service by determining the operation to invoke from the SOAP message, deserializing the operation's parameters in the SOAP message to Java, and then invoking the back-end component.

Sometimes, however, this behavior is not sufficient and a Web service needs access to the SOAP message. SOAP message handlers can intercept SOAP messages in both the request and response to do additional processing of the SOAP message. SOAP message handlers are defined by the Java API for XML-based RPC (JAX-RPC). Handlers enable programmatic access to the SOAP message and are often used to process SOAP message headers. SOAP message headers may contain information about such things as transactions, security, and routing. Processing specific to these headers can be done in SOAP message handlers. Handlers can also be used to add functionality such as caching, encryption and decryption, logging, and auditing to a Web service. Handlers may also be used to process SOAP attachments.

Each WebLogic Web service consists of one or more operations. Operations can be implemented using a combination of back-end components and SOAP message handlers. WebLogic supports Web service operations that are implemented with back-end components only, back-end components and SOAP message handlers, and SOAP message handlers only.

SOAP message handlers are assembled into handler chains - ordered lists of SOAP message handlers that can be applied to the service client or the service endpoint. A WebLogic Web service can optionally define one or more handler chains. These handler chains are then associated with appropriate Web service operations.

Internally, WebLogic Web service operations are implemented as handler chains. The handler chain begins with an authentication handler, followed by any user-defined handlers, then optionally by a component invocation handler that can invoke a back-end component.

Message Handler Life Cycle
SOAP message handlers must be implemented as stateless instances. The JAX-RPC runtime system considers all instances of a particular handler class to be equivalent. This allows the JAX-RPC runtime system to pool handler instances, although this is not required.

The life cycle of a handler consists of two states. A handler either does not exist, or is in a ready state. A handler instance has two life-cycle methods. The init() and destroy() methods allow initialization of the handler when the handler instance is created, and cleanup when the handler instance is destroyed.

Once a handler is in its ready state, its various handle() methods can be invoked. On a service client the handleRequest() method is invoked before the actual SOAP message is sent, and the handleResponse() method is invoked before the actual response is received. On a service endpoint, the handleRequest() method is invoked before the actual service endpoint is invoked, and the handleResponse() method is invoked before the actual response is sent to the client. On a service client the handleFault() method is called before receiving a SOAP fault, and on the service endpoint the handleFault() method is called before generating a SOAP fault.

Designing the Message Handler
When you design SOAP message handlers you must decide the number of handlers, the order in which the handlers will be executed, and whether to invoke a back-end component.

Any arbitrary number of handlers can be configured on both the service client and the service endpoint. The order in which handlers are invoked is significant. For example, consider a handler chain on a service endpoint that is configured with an encryption handler and a caching handler that inspects the SOAP message payload. In order for the caching handler to be able to read and process the message, the encryption and decryption handler must have already decrypted the SOAP message.

A SOAP operation in a WebLogic Web service may or may not be designed to invoke a back-end component. However, a message handler can change this behavior at runtime. A handler can prevent back-end components from being invoked by returning false from its handleRequest() method. In fact, this prevents any of the rest of the handler chain from being invoked. Further processing of the handler chain can also be prevented by returning false from the handleResponse() and handleFault() methods. For example, consider a caching handler. If the incoming request has cached results, the cached results could be returned and further processing of the handler stopped. The following code shows an example of this:

public boolean handleRequest(MessageContext context) {
SOAPMessageContext smc = (SOAPMessageContext)context;
if (Cache.exists(smc.getMessage())) {
smc.setMessage(Cache.get(smc.getMessage()));
return false;
}
return true;
}

SOAP message handlers in a handler chain can also share state with each other. Each of the handle() methods in a handler class accepts a MessageContext as a parameter. The MessageContext instance that is passed to the handle() methods is shared among all handler instances in the handler chain for a single request, response, or fault processing on a service endpoint. Listing 1 shows two handlers that share a property named "myProperty". Handler1 sets this property, and Handler2 uses this property.

Features such as aborting the handler chain processing by returning false in a handle() method and sharing state among handlers introduce interdependencies between handlers in a handler chain. These interdependencies should be planned for and designed in advance because they affect how the SOAP operation is processed.

Implementing the Message Handler
A JAX-RPC handler must implement the javax.xml.rpc.handler.Handler interface. To make handler development easier, JAX-RPC provides the javax.xml.rpc.handler.Generic Handler abstract class. This class provides default implementations of the life-cycle methods and the different handle methods. Only the methods required for the specific handler implementation should be overridden.

The life-cycle methods of a handler should be used to initialize and clean up resources within a handler instance. If a handler makes use of a resource such as a database connection, this can be established in the init() method and cleaned up in the destroy() method.

Handlers can also obtain configuration information when they are initialized. This configuration information is passed as a part of the HandlerInfo object that is passed to the init() method of the handler instance. For example, a logging handler may be given information such as severity level and log directory in its configuration. This would be obtained as follows:

public void init(HandlerInfo handlerInfo) {
Map handlerConfig = handlerInfo.getHandlerConfig();
String severityLevel = (String)handlerConfig.get("severityLevel");
String logDirectory = (String)handlerInfo.get("logDirectory");
}

The handleRequest() and handleResponse() methods may be invoked in a different context depending on whether the handler is deployed on a service endpoint or a service client. This may be important for handlers such as encryption handlers. On the service client the handleRequest() method should encrypt the message and the handleResponse() method should decrypt the message. However, on a service endpoint, handleRequest() should decrypt the message, and handleResponse() should encrypt the message. This can be handled by creating client handlers and service handlers. However, since the behavior is essentially the same, this can also be accomplished by passing configuration information to the handler to tell it whether it is on a service client or service endpoint. (Source code listing EncryptDecryptHandler.java shows an example of this; the source code for this article can be found at www.sys-con.com/weblogic/sourcec.cfm).

Configuring the Message Handler
WebLogic Web services are configured using a deployment descriptor file. This file is named web-services.xml and is deployed in the WEB-INF directory of the WAR file that is hosting the Web services. Web service operations, back-end components that implement the Web service, and handler chains associated with the Web service are specified in this file.

This file is relevant to SOAP message handlers in that it is used to define handler chains, pass configuration information to handlers, and associate handler chains with Web service operations. Handlers and handler chains are defined in the handler-chains section of the Web service deployment descriptor. The handler-chains section contains one or more handler-chain definitions, each of which contains one or more handler definitions. Listing 2 is an example of a handler-chains section of a web-services.xml file. (A complete example is shown in the source code file web-services.xml.} This deployment descriptor defines a single handler chain named "MyHandlerChain" that contains a single handler. The information contained in the handler elements is used to create the javax.xml.rpc.HandlerInfo instance that is passed to the init() method of the handler instance. The parameter names and values defined in the init-params section of the descriptor are put into a Map, which is available through the getHandlerConfig() method of the HandlerInfo instance. This allows a handler instance to obtain configuration information at deployment time.

Using SOAP Message Handlers on the Client
The web-services.xml file is used to configure message handler chains and associate them with SOAP operations on the server. However, handlers can also be used on the service client. The JAX-RPC specification does not specify any standard packaging and deployment model. This work is currently under development in the J2EE 1.4 specification and JSR 109. Since there is no current standard client container for JAX-RPC Web services, SOAP message handlers must be configured programmatically on the service client.

In order to associate a handler chain with a Web service, the developer must programmatically crate the handler chain. A handler chain is simply a list of HandlerInfo objects. Listing 3 shows how to configure a log handler on a client. (A complete listing is shown in the source code Main.java.)

Packaging the Web Service
WebLogic Web services are packaged as standard J2EE applications. The following steps should be taken to assemble a WebLogic Web service that uses message handlers:
1.   Create web-services.xml file
2.   Compile handler classes
3.   Build Web application: Place web-services.xml file into WEB-INF directory of the Web application and the handler classes into WEB-INF/classes.
4.   Build an enterprise application: The Web services Web application should be included in the enterprise application.
5.   Deploy the enterprise application to WebLogic Server: The source code file build.xml shows an example of an Ant script to assemble a Web service that invokes a handler chain and a that component.

Summary
SOAP message handlers provide a standard, flexible method for implementing Web Services. They allow additional processing when exposing a back-end component as a Web service without requiring modification of the back end component. SOAP message handlers can be very useful when implementing WebLogic Web services.

Reference

  • SOAP 1.1 specification: http://www.w3.org/TR/2000/NOTE-SOAP-20000508
  • JAX-RPC 1.0 specification: http://java.sun.com/xml/downloads/jaxrpc.html#jaxrpcspec
  • JSR 109 - Implementing Enterprise Web Services: ftp://www6.software.ibm.com/software/ developer/library/ws-jsr109-proposed.pdf
  • JSR 151 - J2EE 1.4: http://java.sun.com/j2ee/j2ee-1_4-pfd-spec.pdf
  • More Stories By Michael Gilbode

    Mike is an architect for Anexinet Corp. in Philadelphia, PA. Mike is experienced in large scale J2EE projects built on the BEA WebLogic Platform.

    Comments (0)

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


    @ThingsExpo Stories
    SYS-CON Events announced today that Avere Systems, a leading provider of enterprise storage for the hybrid cloud, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Avere delivers a more modern architectural approach to storage that doesn't require the overprovisioning of storage capacity to achieve performance, overspending on expensive storage media for inactive data or the overbui...
    SYS-CON Events announced today that Avere Systems, a leading provider of hybrid cloud enablement solutions, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Avere Systems was created by file systems experts determined to reinvent storage by changing the way enterprises thought about and bought storage resources. With decades of experience behind the company’s founders, Avere got its ...
    Amazon is pursuing new markets and disrupting industries at an incredible pace. Almost every industry seems to be in its crosshairs. Companies and industries that once thought they were safe are now worried about being “Amazoned.”. The new watch word should be “Be afraid. Be very afraid.” In his session 21st Cloud Expo, Chris Kocher, a co-founder of Grey Heron, will address questions such as: What new areas is Amazon disrupting? How are they doing this? Where are they likely to go? What are th...
    As hybrid cloud becomes the de-facto standard mode of operation for most enterprises, new challenges arise on how to efficiently and economically share data across environments. In his session at 21st Cloud Expo, Dr. Allon Cohen, VP of Product at Elastifile, will explore new techniques and best practices that help enterprise IT benefit from the advantages of hybrid cloud environments by enabling data availability for both legacy enterprise and cloud-native mission critical applications. By rev...
    Recently, REAN Cloud built a digital concierge for a North Carolina hospital that had observed that most patient call button questions were repetitive. In addition, the paper-based process used to measure patient health metrics was laborious, not in real-time and sometimes error-prone. In their session at 21st Cloud Expo, Sean Finnerty, Executive Director, Practice Lead, Health Care & Life Science at REAN Cloud, and Dr. S.P.T. Krishnan, Principal Architect at REAN Cloud, will discuss how they b...
    SYS-CON Events announced today that SkyScale will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. SkyScale is a world-class provider of cloud-based, ultra-fast multi-GPU hardware platforms for lease to customers desiring the fastest performance available as a service anywhere in the world. SkyScale builds, configures, and manages dedicated systems strategically located in maximum-security...
    High-velocity engineering teams are applying not only continuous delivery processes, but also lessons in experimentation from established leaders like Amazon, Netflix, and Facebook. These companies have made experimentation a foundation for their release processes, allowing them to try out major feature releases and redesigns within smaller groups before making them broadly available. In his session at 21st Cloud Expo, Brian Lucas, Senior Staff Engineer at Optimizely, will discuss how by using...
    In this strange new world where more and more power is drawn from business technology, companies are effectively straddling two paths on the road to innovation and transformation into digital enterprises. The first path is the heritage trail – with “legacy” technology forming the background. Here, extant technologies are transformed by core IT teams to provide more API-driven approaches. Legacy systems can restrict companies that are transitioning into digital enterprises. To truly become a lead...
    SYS-CON Events announced today that Daiya Industry will exhibit at the Japanese Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Ruby Development Inc. builds new services in short period of time and provides a continuous support of those services based on Ruby on Rails. For more information, please visit https://github.com/RubyDevInc.
    As businesses evolve, they need technology that is simple to help them succeed today and flexible enough to help them build for tomorrow. Chrome is fit for the workplace of the future — providing a secure, consistent user experience across a range of devices that can be used anywhere. In her session at 21st Cloud Expo, Vidya Nagarajan, a Senior Product Manager at Google, will take a look at various options as to how ChromeOS can be leveraged to interact with people on the devices, and formats th...
    SYS-CON Events announced today that Yuasa System will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Yuasa System is introducing a multi-purpose endurance testing system for flexible displays, OLED devices, flexible substrates, flat cables, and films in smartphones, wearables, automobiles, and healthcare.
    Organizations do not need a Big Data strategy; they need a business strategy that incorporates Big Data. Most organizations lack a road map for using Big Data to optimize key business processes, deliver a differentiated customer experience, or uncover new business opportunities. They do not understand what’s possible with respect to integrating Big Data into the business model.
    Enterprises have taken advantage of IoT to achieve important revenue and cost advantages. What is less apparent is how incumbent enterprises operating at scale have, following success with IoT, built analytic, operations management and software development capabilities – ranging from autonomous vehicles to manageable robotics installations. They have embraced these capabilities as if they were Silicon Valley startups. As a result, many firms employ new business models that place enormous impor...
    SYS-CON Events announced today that Taica will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Taica manufacturers Alpha-GEL brand silicone components and materials, which maintain outstanding performance over a wide temperature range -40C to +200C. For more information, visit http://www.taica.co.jp/english/.
    SYS-CON Events announced today that Dasher Technologies will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Dasher Technologies, Inc. ® is a premier IT solution provider that delivers expert technical resources along with trusted account executives to architect and deliver complete IT solutions and services to help our clients execute their goals, plans and objectives. Since 1999, we'v...
    SYS-CON Events announced today that MIRAI Inc. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. MIRAI Inc. are IT consultants from the public sector whose mission is to solve social issues by technology and innovation and to create a meaningful future for people.
    SYS-CON Events announced today that TidalScale, a leading provider of systems and services, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. TidalScale has been involved in shaping the computing landscape. They've designed, developed and deployed some of the most important and successful systems and services in the history of the computing industry - internet, Ethernet, operating s...
    SYS-CON Events announced today that IBM has been named “Diamond Sponsor” of SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California.
    Join IBM November 1 at 21st Cloud Expo at the Santa Clara Convention Center in Santa Clara, CA, and learn how IBM Watson can bring cognitive services and AI to intelligent, unmanned systems. Cognitive analysis impacts today’s systems with unparalleled ability that were previously available only to manned, back-end operations. Thanks to cloud processing, IBM Watson can bring cognitive services and AI to intelligent, unmanned systems. Imagine a robot vacuum that becomes your personal assistant tha...
    SYS-CON Events announced today that TidalScale will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. TidalScale is the leading provider of Software-Defined Servers that bring flexibility to modern data centers by right-sizing servers on the fly to fit any data set or workload. TidalScale’s award-winning inverse hypervisor technology combines multiple commodity servers (including their ass...