Urban pro

View My Profile

Thursday, January 3, 2013

The JAX RPC way of using a SOAP Handler

Back to you guys after a long time. Hope you are doing well. Today we are going  to look in to some important aspects of the JAX RPC way of servicing the web.

So first things first ::

What is JAX RPC :  Jax Rpc is a java standard way of communicating with web services.Java API for XML-based RPC (JAX-RPC) allows a Java application to invoke a Java-based Web Service with a known description while still being consistent with its WSDL description. It can be seen as Java RMIs over Web services.

It works as follows:
  1. A Java program invokes a method on a stub (local object representing the remote service)
  2. The stub invokes routines in the JAX-RPC Runtime System (RS)
  3. The RS converts the remote method invocation into a SOAP message
  4. The RS transmits the message as an HTTP request
The advantage of such a method is that it allows the Web Service to be implemented at server-side as a Servlet or EJB container. Thus, Servlet or EJB applications are made available through Web services.

What is a Service Handler :  When designing a web service that accepts or returns SOAP messages,  the SOAP messages should be processed by message handlers Handlers on incoming messages are invoked before the message is delivered to the web service operation, and handlers on outgoing messages are invoked after the web service operation has completed. The web service itself is unaware of the presence of handlers. SOAP message handlers are sometimes referred to as interceptors.


Now suppose you have a SOAP request like this ::


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ch="urn://mfots.com/xmlmessaging/CH" xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<soapenv:Header>
<ch:MFprofileMnt>
<ch:myID>1458</ch:myID>
<ch:bigID>raptool</ch:bigID>
<ch:matID>5689</ch:matID>
</ch:MFprofileMnt>
</soapenv:Header>
</soapenv:Envelope



Now suppose you have got to create a message handler where you will have to construct the SOAP header MFprofileMnt and its children.
Now how do you do it :: Keep patience we are coming there.
See the code snippet given below :
 Name headerContextName = soapEnvelope.createName("MFprofileMnt", "ch","urn://mfots.com/xmlmessaging/CH");
SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(headerContextName);
// mustUnderstand attribute is used to indicate
// whether the header entry is mandatory or optional for the
// recipient to process.
soapHeaderElement.setMustUnderstand(true);
//Now set the attribute children
// create the first child element and set the value
SOAPElement element1 = soapHeaderElement.addChildElement("myID", "ch");
element1.addTextNode("1458");
//create the second child element and set the value
SOAPElement element2 = soapHeaderElement.addChildElement("bigID", "ch");
element2.addTextNode("raptool");
//create the third child element and set the value
SOAPElement element3 = soapHeaderElement.addChildElement("matID", "ch");
element3.addTextNode("5689");
So what i am doing here is that i am creating a name object where i am feeding the header  name , the prefix and the prefix-namespace-URL . After that in the next line i am setting mustUnderstand as "true", which means the header entry is mandatory.
Now we have got to create the children elements. To do that i am creating 3 SOAPElement objects for the 3 children and adding the child elements using the method addChildElement() and then to pass the value i am using the addTextNode() method and voila you have created the header with its children.

How to get the operation name :


Now suppose you need to get the operation name . Only depending on this you will construct your header . So how do you go about doing this ? This is how :


protected String getMethodName(MessageContext mc)

{

String operationName = null;

try

{

SOAPMessageContext messageContext = (SOAPMessageContext) mc;

// assume the operation name is the first element

// after SOAP:Body element

Iterator i = messageContext.

getMessage().getSOAPPart().getEnvelope().getBody().getChildElements();

while ( i.hasNext() )

{

Object obj = i.next();

if(obj instanceof SOAPElement)

{

SOAPElement e = (SOAPElement) obj;

operationName = e.getElementName().getLocalName();

break;

}

}

}

catch(Exception e)

{

e.printStackTrace();

}

return operationName;

}

Explanation : 

What i am assuming here is that the first element after the soap body tag is my operation name . So for that i am getting the body part of the request by using the getBody() on the messageContext. Then i am getting all the children of the body attribute and after iterating through that list i am only collecting the first element and then breaking away. Now that element is also my operation name. Thus we can get the  operation name  as well.

The purpose of this post is to tell people how to make handlers work in JAX RPC. Since , now a days people use more and more JAX WS , so RPC related stuffs are very less in the internet. Hope this helps some of the folks. Stay tuned , i will come soon again with more exiting stuffs.


No comments:

Post a Comment