Having your own SOAP Client

Overview:

This blog is the outcome of my support experience with Oracle Weblogic Web service’s stack for quite sometime. After this exercise  we should understand, how Webservice layer talk to a client and also  how we can write our own java client to test a web service irrespective of the implementation i.e., web sphere or .net. Often I use this sample to explain to Customers that the issue we are dealing with is not specific to Weblogic.

Let’s start:

If you have a running Webservice already in any of the container, you can skip the following setup and move directly to “Getting the SOAP Request”

Start Weblogic:

Download and install ( Weblogic 10.3 ) and make sure you have the examples domain which comes along with Weblogic and start Weblogic from that domain. In my local box, I have started Weblogic(startWeblogic) from D:\oracle\bea103\wlserver_10.3\samples\domains\wl_server

Deploy a  sample Web service which comes with Weblogic:

For this example I am deploying jws_basic Webservice example which comes with Weblogic 10.3

steps:

1) Open a cmd and run setExamplesEnv.cmd from the domain

2) Go to the samples folder ( for ex, D:\oracle\bea103\wlserver_10.3\samples\server\examples\src\examples\webservices\jws_basic\simple)

3) and type “ant”, that compile and create all the necessary webservice stuff and deploy it to the samples domain.

4) And now if you go to your weblogic console, you should see a application deployed with the name “webservicesJwsSimpleEar”

5) Now if you run “ant run” on the same cmd you will see output from Web service “[java] Got result: Here is the message: ‘Hi there!'” , to make sure the sample web service we just deployed is working.

Getting the SOAP Request:

There are many ways you can get the SOAP request i) By enabling some debug flags in Weblogic, I am sure all the containers should have some debug flag which spit the SOAP Request ii) You can use any of existing tools SOAP UI or SOAP Sonar ..etc…and I prefer SOAP UI because its java based and it’s free. iii) Even you can form the particular SOAP request based on your WSDL, it is hard as complex object grows, but you can try it out 🙂

In this case, I am going to stick with SOAP UI as I want to provide a non container specific and a simple way to get the SOAP Request.

I am using SOPA UI 2.5, if you have installed please open SOAP UI and go to File and create a new project, after giving some name, add the WSDL URL for your project for ex, http://localhost:7001/jws_basic_simple/SimpleService?WSDL and click OK, that should pull all operations published by that WSDL and select one operation, in my case I just had one operation, it will take a string “mama” and returns “Here is the message: ‘mama'”

Copy the SOAP Request from that and that’s what we are going to try from our Java program.

Simple Java program which executes this SOAP Request for testing purpose:

TestingSOAP.java

——————

import java.io.*;

import java.net.*;

public class TestingSOAP

{
public static void main(String args[]){

TestingSOAP.sayHello();
}

private static void sayHello() {
//our SOAP Request
/** <soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:exam=”http://example.org”&gt;
<soapenv:Header/>
<soapenv:Body>
<exam:sayHello>
<exam:message>mama</exam:message>
</exam:sayHello>
</soapenv:Body>
</soapenv:Envelope>
*/

String message = “<soapenv:Envelope xmlns:soapenv=’http://schemas.xmlsoap.org/soap/envelope/&#8217; xmlns:exam=’http://example.org’>”+
“<soapenv:Header/>”+
“<soapenv:Body>”+
“<exam:sayHello>”+
“<exam:message>mama</exam:message>”+
“</exam:sayHello>”+
“</soapenv:Body>”+
“</soapenv:Envelope>”;

//make a simple HTTP URL connection

HttpURLConnection uc = null;
URL url = null;
byte[] data = message.getBytes();
try {
//Put your URL to connect to the server,in your case it should be “http://localhost:7001/jws_basic_simple/SimpleService?UserName=username&amp;Password=password&#8221;
url = new URL(“http://localhost:7001/jws_basic_simple/SimpleService&#8221;);
//Below the way you set the proxy details, just same as any HTTPURLConnection
//Properties sysProps = System.getProperties();
//sysProps.put( “proxySet”, “true” );
//sysProps.put( “proxyHost”, “proxy.myproxy.com”);
//sysProps.put( “proxyPort”, “3128” );
} catch (MalformedURLException e1) {
System.err.println(“URL EXCEPTION”);
e1.printStackTrace();
}
try {

uc = (HttpURLConnection) url.openConnection();

uc.setDoInput(true);
uc.setDoOutput(true);
uc.setUseCaches(false);
uc.setRequestProperty(“Content-Length”, ” ” + message.length());
uc.setRequestProperty(“Content-Type”, “text/xml; charset=UTF-8”);
//set SOAP Action –> in my case we don’t have one..so it is empty.
//uc.setRequestProperty(“SOAPAction”, “SOAPAction defined in the WSDL”);
uc.setRequestMethod(“POST”);
uc.connect();
DataOutputStream dos = new DataOutputStream(uc.getOutputStream());
dos.write(data, 0, data.length);
dos.flush();
dos.close();
BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String res = null;
while ((res = br.readLine()) != null)
System.out.println(res);
br.close();
} catch (IOException e) {
System.err.println(“From client: ” + e);
e.printStackTrace(System.out);
uc.disconnect();
System.err.println(“Disconnected…”);
}

}

}

Output:

Compiling and running the above java code should have the following results

Conclusion:

Considering we have tons of tools to run Web services, my intention wasn’t creating a new SOAP client tool but to explain how Web services works from the basic level, you can see, still it is a basic HttpURL Connection with SOAP message. All your understanding with Http/TCP layers are still applicable with Web services. If you are a weblogic admin or any Container admin, I hope this code would give a clear understanding to debug any Web service layer issues.

Please feel free to ask any questions associated with this blog.

About sasikumarchellappan

Part of Sustaining Weblogic Development Team of Oracle Weblogic Server.
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Having your own SOAP Client

  1. Mugdha says:

    Helpful Indeed 🙂

  2. Arun says:

    Hi Sasi,
    Thanks for the post…Its very helpful…

    Could you please explain me in detail about ” web and EJB container ” and the transaction flow happens in web logic.

Leave a comment