Wednesday, August 13, 2008

Web Service Message Level Implementations

If you're like me and prefer working with the raw XML in your web service implementations instead of dealing with the mess of generated data binding code, this article will show you how to do so using JAX-WS and XPath. In this example, we use this approach to implement a WS-Notification consumer service that receives WSDM-based messages for monitoring the health and performance of services.

JAX-WS has the Provider interface that you can implement to get access to the raw XML message. You have the choice of implementing Provider<Source>, Provider<DataSource>, or Provider<SOAPMessage>. We'll be using the Provider<Source> in this example. The invoke(Source) method is what gets called to process a service request. We'll break down the implementation of that method step-by-step.

First we send the source through a Transformer to get a DOM tree that we can use for XPath processing:

DOMResult dom = new DOMResult();
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.transform(source, dom);

Next, we instantiate an XPathFactory and create an XPath that we'll use to process the DOM tree against. The third line calls the setNamespaceContext() method and uses a custom class I created that provides a mapping of namespace URI's to prefixes and vice-versa. You can find more information on how to implement such a class here. We need this because we'll be using namespace prefixes in our expressions.

XPathFactory xpf = XPathFactory.newInstance();
XPath xp = xpf.newXPath();
xp.setNamespaceContext(MyNamespaceContext.getInstance());

Now we'll process our first expression to extract the NotificationMessage elements from the message, passing in the XPath expression for the NotificationMessage and our DOM tree to evaluate the expression against. A WS-Notification message may contain multiple NotificationMessages so we get a NodeList in return.

NodeList resultList = (NodeList)xp.evaluate("/wsnt:Notify/wsnt:NotificationMessage", dom.getNode(), XPathConstants.NODESET);

Then we'll iterate through each item in the NodeList, i.e. each NotificationMessage, and process another expression against it to extract the ResourceId element and print it out.
String exprPrefix = "";
int len = resultList.getLength();
for (int i=1;i<=len;i++) {
String expr1 = "/wsnt:Notify/wsnt:NotificationMessage[" + i
+ "]" + "/wsnt:Message/muws1:ManagementEvent"
+ "/muws1:SourceComponent/muws1:ResourceId";
String resourceId = xp.evaluate(expr1, dom.getNode());
System.out.println("Resource ID: " + resourceId);
}
The entire source code listing can be found here. You'll notice that the class has the following two annotations:

@WebServiceProvider()
@ServiceMode(value=Service.Mode.PAYLOAD)

The first one tells JAX-WS that this class is a Provider endpoint. The second one says that we only want to work with the message at the payload level, i.e. we don't need to access the message headers. In addition to these annotations, we have to customize the WSDL to mark the port as a Provider interface. This can be done by embedding the customizations directly within the WSDL or through a separate customization file. We'll do it using a separate provider-customize.xml file--you can find this file here.


Bookmark and Share

12 comments:

人妻 said...

Hな人妻たちの社交場、割り切った付き合いも当然OK!欲求不満のエロ人妻たちを好みに合わせてご紹介します。即会い、幼な妻、セレブ、熟女、SM妻、秘密、以上6つのジャンルから遊んでみたい女性を選んでください

サイドビジネス said...

1日5万円~が手に入るサイドビジネスのご案内です。男狂いのセレブ女性はネットで知り合った男を次々に金の力で食い散らかしています。そんな女性を手玉にとって大金を稼いでみませんか

Hチェッカー said...

みんなで楽しめるHチェッカー!簡単な設問に答えるだけであなたの隠されたH度数がわかっちゃいます!あの人のムッツリ度もバレちゃう診断を今すぐ試してみよう

家出 said...

最近流行の家出掲示板では、各地のネットカフェ等を泊り歩いている家出少女のメッセージが多数書き込みされています。彼女たちはお金がないので掲示板で知り合った男性の家にでもすぐに泊まりに行くようです。あなたも書き込みに返事を返してみませんか

高収入アルバイト said...

性欲を持て余し、欲求不満になっている女性を金銭の対価を得て、癒して差し上げるお仕事です。参加にあたり用紙、学歴等は一切問いません。高収入アルバイトに興味のある方はぜひどうぞ

高級チェリー said...

童貞を奪ってみたい女性たちは、男性にとって「初体験」という一生に一度だけの、特別なイベントを共に心に刻み込むことを至上の喜びにしているのです。そんな童貞好きな女性たちと高級チェリーで最高のSEXをしてみませんか

困っています。 said...

最近寂しくて困っています。夜一人で寝るのが凄く寂しいです…隣で添い寝してくれる男性いませんか?見た目とか特に気にしません。優しくて一緒にいてくれる方大歓迎☆一緒に布団で温まりましょう♪shart.enamorado.de-me@docomo.ne.jp

副収入 said...

一晩の割り切ったお付き合いで副収入が得られるサイトのご案内です。アルバイト感覚での挑戦もできる、安心の無料登録システムを採用しておりますので、興味のある方は当サイトをぜひご覧ください

スタービーチ said...

復活、スタービーチ!日本最大の友達探しサイトがついに復活、進化を遂げた新生スタビをやってみませんか?理想のパートナー探しの手助け、合コンパーティー等も随時開催しています。楽しかった頃のスタビを体験しよう

モテる度チェッカー said...

なかなか彼氏、彼女が出来ない君達の深層心理を徹底解明♪みんなでモテる度チェックをやって結果交換も自由、合コンや休み時間はモテる度チェックで暇つぶし!次にモテ期が訪れる瞬間をズバリ診断しちゃいます

¥倶楽部 said...

出会ぃも今は¥倶楽部!オンナがオトコを買う時代になりました。当サイトでは逆援希望の女性が男性を自由に選べるシステムを採用しています。経済的に成功を収めた女性ほど金銭面は豊かですが愛に飢えているのです。いますぐTOPページからどうぞ

野外露出 said...

今迄は野外露出がマイナスイメージと囚われがちですが、実際は開放的な気分になり有名人のヌーディストが、オープンになる事を推奨してるぐらいです。このサイトをキッカケに知り合った娘達と野外で楽しみませんか