Urban pro

View My Profile
Showing posts with label web service. Show all posts
Showing posts with label web service. Show all posts

Tuesday, September 24, 2013

JAX WS SSL Configuration to solve wsdl certificate problems

Hi friends,

Back to you after a long time. This past 2 months , i had been crazy busy and hence, did  not  have enough time to discuss anything.

Today , i am going to discuss a very important topic. This is regarding the invocation of  a JAX-WS or JAX-RPC  service.

PROBLEM :
Some time back i was trying to invoke a JAX WS service from a stand alone java application (an application having a java class with the main method and having no application server at all). The problem was, every time i was trying to invoke any service operation, it was giving me a SSL CONFIGURATION EXCEPTION .

REASON :
Now the reason behind it was that, every service call requires to validated . So how is that done ? It's done with the help of certificates. So what i  did was that, i got a hold of those certificates(the service i was using needed 2 of them) and integrated them with my java environment to make the WSDL hoster realize that it was a valid invocation.

SOLUTION (THE JAVA WAY):
My certificate names were cert1.cer and cert2.cer . And this how i integrated them in my  java  environment(In use RAD-Rational Application Developer) :

1. Place the below cry certificates in the path "C:\Program Files\IBM\SDP\jdk\jre\lib\security"

   cert1.cer, cert2.cer 

2. cacerts is the trusStore file. It's present in :
   C:/Program Files/IBM/SDP/jdk/jre/lib/security/cacerts

3. In command prompt perform the below execution :
C:\Program Files\IBM\SDP\jdk\jre\lib\security>"C:\Program Files\IBM\SDP\jdk\jre\bin\keytool" -import -alias cert1 -file cert1.cer -keystore cacerts

4. If it asks keystore password, mention changeit, which is the default keystore password

Enter keystore password: changeit

Trust this certificate? [no]:  yes
Certificate was added to keystore

5. Peform the steps 3 and 4 for the second certificate(cert2.cer).

And , i thought that was the end of it, but apparently it was not to be so. It seems , you have to configure the
"javax.net.ssl"  in your program to make it work, and this is how you have got to do it :

System.setProperty("javax.net.ssl.trustStore", "C:\\Program Files\\IBM\\SDP\\jdk\\jre\\lib\\security\\cacerts");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

Now,the problem will be that the above piece of code will configure only the truststore, but that's not all; you have got to configure  the keystore as well. So for this , you have gotta add the following lines of code :

System.setProperty("javax.net.ssl.keyStore", "C:\\Program Files\\IBM\\SDP\\jdk\\jre\\lib\\security\\cacerts");
System.setProperty("javax.net.ssl.keyStorePassword", "changeit");

And voila, everything will start working for you.
In case you are wondering how to add the certificates pro grammatically instead of adding them manually, then you can use this link : http://stackoverflow.com/questions/18764122/how-to-integrate-wsdl-certificates-to-the-cacerts-file-in-jdk-jre-security-folde

THE UNIX WAY( no--check--certificates) :

Now if you are writing a shell script and you are invoking a particular operation of the web service , then you can actually tell the host not to validate the invocation for any certificates.
In my case, i normally use the CURL  command to do my bidding  , though other times it's the WGET command. So for example , my invocation of a service operation using a schell script will look something like this :

#! /bin/sh
ENDPOINT="http://mathertel.de/AJAXEngine/S01_AsyncSamples/CalcService.asmx"
VALUE=1234567890
if [ -n "${1}" ]; then
    VALUE=${1}
fi

curl --silent \
     --data \
     @- \
     --header 'Content-Type: application/soap+xml; charset=utf-8' \
     --user-agent "" \
     ${ENDPOINT} <<EOF | xmllint --format -
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <CalcPrimeFactors xmlns="http://www.mathertel.de/S01_AsyncSamples/">
      <inputText>${VALUE}</inputText>
    </CalcPrimeFactors>
  </soap12:Body>
</soap12:Envelope>
EOF

DON'T BE AFRAID  or alarmed , this is not a complicated code. Curl's just a command(you have to have it installed though in your linux distro), where you pass the end point and the request structure . Once that is done , this will give an out put , which for brevity , we can parse it using XMLLINT,  which is a command that parses the response in XML format. In case, you do not want to be validated for certificates, you just need to add the option no--check--certificates to the command invocation and you will get the response without   any  certificate hassles .

In my case, the output looks like this :

RESPONSE FROM THE SERVICE :
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <CalcPrimeFactorsResponse xmlns="http://www.mathertel.de/S01_AsyncSamples/">
      <CalcPrimeFactorsResult>2 3 3 5 3607 3803</CalcPrimeFactorsResult>
    </CalcPrimeFactorsResponse>
  </soap:Body>
</soap:Envelope>

Hope this will be  fun to know for you all as it was for me.
Good bye.

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.