Using the HTTP Publish-Subscribe Server 12-11
from a client before the pub-sub server gets it and subsequently sends it out to the subscribers to the channel. Reasons for pre-processing the messages include validating
incoming data, gathering monitoring information, tracking the users of the pub-sub application, caching, and so on.
There are two major steps to implementing message filter chains:
■
Section 12.3.3.1, Programming the Message Filter Class
■
Section 12.3.3.2, Configuring the Message Filter Chain
12.3.3.1 Programming the Message Filter Class
Each filter in the chain must have its own user-programmed filter class. The filter class must implement the com.bea.httppubsub.MessageFilter interface. The
MessageFilter interface includes a single method, handleMessageEventMessage; its signature is as follows:
boolean handleMessageEventMessage message; The com.bea.httppubsub.EventMessage interface extends BayeaxMessage,
which is a JavaScript Object Notation JSON see http:www.json.org encoded object. JSON is a lightweight data-interchange format used by the Bayeux protocol.
The EventMessage interface defines two methods, getPayload and setPayload, that programmers use to access and process the incoming messages.
Because the handleMessage method returns boolean, a programmer can interrupt all further processing in the message filter chain by returning false in any
of the filter classes in the chain. This action not only interrupts the filter processing, but also immediately returns the message back to the client that published it, without
sending it on to channel subscribers. This is a great way for programmers to ensure that there is no problem identified in the incoming messages, and, if a problem is
found, to prevent the messages to be published to subscribers.
The following example shows a simple implementation of the MessageFilter interface:
package msgfilters; public static class Filter1 implements MessageFilter {
public boolean handleMessageEventMessage message { String msg = String message.getPayLoad;
message.setPayLoad[ + msg.substring1, msg.length-1; return true;
} }
In the example, the getPayload method gets the String message from the inputted message parameter; this message either comes directly from the client if
Filter1 is the first configured filter in the chain or is the result of another filter class if Filter1 is not the first in the chain. The setPayLoad method resets the
message while performing some data manipulation; in the example, the first character of the message is replaced with a [.
12.3.3.2 Configuring the Message Filter Chain
You configure the message filters in the weblogic-pubsub.xml deployment descriptor of the pub-sub server.
First, you declare the message filters using the wlps:message-filter child element of the root wlps:weblogic-pubsub element. Then you configure a
specific channel by adding a wlps:message-filter element for each filter in the
12-12 Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server
chain. The order in which the filters are configured in the wlps:channel element is the order in which they execute.
The following example shows how to configure message filters in the weblogic-pubsub.xml deployment descriptor; only relevant information is shown.
See the text after the example for an explanation:
?xml version=1.0 encoding=UTF-8? wlps:weblogic-pubsub
xmlns:wlps=http:xmlns.oracle.comweblogicweblogic-pubsub wlps:server-config
... wlps:server-config
wlps:message-filter wlps:message-filter-namefilter1wlps:message-filter-name
wlps:message-filter-classmsgfilters.Fiter1wlps:message-filter-class wlps:message-filter
wlps:message-filter wlps:message-filter-namefilter2wlps:message-filter-name
wlps:message-filter-classmsgfilters.Filter2wlps:message-filter-class wlps:message-filter
wlps:channel wlps:channel-patternfirstchannelwlps:channel-pattern
wlps:message-filterfilter1wlps:message-filter wlps:channel
wlps:channel wlps:channel-patternsecondchannelwlps:channel-pattern
wlps:message-filterfilter2wlps:message-filter wlps:message-filterfilter1wlps:message-filter
wlps:channel wlps:weblogic-pubsub
In the example, two filters are declared using the wlps:message-filter element: filter1 implemented by the msgfilters.Filter1 class and filter2
implemented by the msgfilters.Filter2 class.
The channel with pattern firstchannel is then configured with filter1. At run time, this means that all messages published to the direct subchannels of
firstchannel are first pre-processed by the msgfilters.Filter1 class.
The channel with pattern secondchannel is configured with two filters: filter2 and filter1. The order in which these two filters are configured is
important. At run time, all messages published to the direct subchannels of secondchannel are first intercepted and processed by the msgfilters.Filter2
class, then the result of this processing is sent to msgfilters.Filter1 which then does its own processing, and then the result is sent to the subscribers of the channel.
12.3.4 Updating a Browser Client to Communicate with the Pub-Sub Server