Showing posts with label SOAP. Show all posts
Showing posts with label SOAP. Show all posts

Thursday, January 28, 2010

Metro : Printing / dumping out the contents of a SOAP packet

I've used Metro a lot and I've always battled to have decent logging.

In the web service, I normally log all the inputs and the outputs and all the exceptions. Wouldn't it be easier if you could just log all the requests and responses which would guarantee that you had all the information all of the time?

I just couldn't figure out how.

Until I came across "MessageDumpingFeature". You find this in the Glassfish \lib directory in the webservices-rt.jar file.

If you've generated web services using Metro, you find the code below very familiar. All you really need to do is replace:

service.getWebServiceName();

with

service.getWebServiceName(messageDumper);

The code looks like:

 

import com.sun.xml.ws.assembler.MessageDumpingFeature;

... snip ...

messageDumper = new MessageDumpingFeature();

ServiceName service = new ServiceName();
WebServiceName port = service.getWebServiceName(messageDumper);
// TODO initialize WS operation arguments here
java.lang.String xxx = "yyy";
java.lang.String yyy = "zzz"
// TODO process result here
ResultName result = port.doWebService(xxx, yyy);

String request = messageDumper.nextMessage();
String response = messageDumper.nextMessage();

System.out.println (request);
System.out.println (response);

... snip ...

} catch (Exception ex) {
String request = messageDumper.nextMessage();
String response = messageDumper.nextMessage();

System.out.println (request);
System.out.println (response);
}


It prints out both normal responses and SOAP Faults.

A request would be dumped like:

 
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:doWebService xmlns:ns2="http://namespace/">
<xxx>yyy</xxx>
<yyy>zzz</yyy>
</ns2:doWebService>
</S:Body>
</S:Envelope>


A valid response would be dumped like:

 
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:doWebService xmlns:ns2="http://namespace/">
<return>
<aaa>bbb</aaa>
<bbb>ccc</bbb>
</return>
</ns2:doWebService>
</S:Body>
</S:Envelope>


A SOAP fault would be dumped like:

 
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
<faultcode>S:Server</faultcode>
<faultstring>SomeErrorString</faultstring>
<detail>
<ns2:SomeException xmlns:ns2="http://namespace/">
<message>SomeErrorMessage</message>
</ns2:SomeException>
</detail>
</S:Fault>
</S:Body>
</S:Envelope>


Of course, instead of dumping out the request / response, it could be written to a file instead.

Enjoy!

Thursday, January 07, 2010

SOAPUI : Adding a wsdl

Assume a WSDL like:

http://host:port/ApplicationServer/Service

Sometimes when you are adding a WSDL like the above you get an error:

SOAPUIException: Error inporting wsdl

Try qualifying the URL i.e.

http://host:port/ApplicationServer/Service?wsdl

Enjoy!

Wednesday, January 06, 2010

SOAPUI : NoClassDefFoundError

Using SOAPUI 3.0.1 to test web services created using Netbeans, JAX-WS and Metro.

When I try and run the TestRunner, I get:


java.lang.NoClassDefFoundError: org/apache/commons/cli/CommandLineParser


My solution was to edit soapui.bat (in the /bin directory) and add:

set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_16

at the top.

Then, instead of running soapui.exe from the desktop shortcut, I open a command prompt and run soapui.bat from there manually.

Problem solved.

Enjoy!

Thursday, April 19, 2007

SOAP : Asynchronous web service

Had to implement an asynchronous web service which did not need to be real-time so no response was needed.

Had me a bit puzzled until I realised that a web service can return void!

i.e. in ASP:

[WebMethod]
public void webMethod(Some parameter)

compiles and runs no problem.

So what does it return?

The WSDL shows:


s:element name="webMethodResponse"
s:complexType /
/s:element


So it actually returns null!

Enjoy!

Monday, March 19, 2007

SOAP : WSCF - Web Services Contract First

"Contract first" involves creating the WSDL first and then using that to generate the code. There aren't many tools to do this.

Essentially, this approach involves modelling the messages, data and interfaces at the start and then publishing them as a contract before coding the implementation begins. Typically, modelling of the data and messages is done using a XML Schema (i.e. XSD files) and the contract is formalised using the Web Services Description Language (WSDL files).

thinktecture offer their free tool to do this which is nicely integrated with Visual Studio (2003 and 2005) and also allows a command line interface.

Get it from here.

Documentation and walk-through here.

Two MSDN articles which cover this are:

here and

here

Enjoy!

Friday, February 17, 2006

SOAP : TCPMON - trace SOAP messages

This can also be done with Ethereal but that requires some TCP/IP knowledge.

Download tcpmon.jar from here

and run as:

java -jar tcpmon.jar

This will bring up the screen with:

Local Port: 8080
Server Name: 127.0.0.1
Server Port: 80

Assume that the web service address that the client uses is

http://a.b.c.d:9090

Change this in the client to

http://localhost:8080

Then change the Server Name to a.b.c.d and change the Server Port to 9090.

Then "Add Monitor"

So now your client will send to port 8080 and tcpmon will pick this up and redirect to a.b.c.d:9090 thus trapping the messages and then displaying them

Enjoy!

Wednesday, November 09, 2005

SOAP : Debugging Axis with Tomcat and Eclipse

Using Tomcat 5.0 and Eclipse 3.1.

In your Tomcat/bin directory, there is a file called catalina.bat. From the command prompt, just run catalina to see the options. You'll see one option called:

jpda start Start Catalina under JPDA debugger

That's the one you need. I had endless problems running this until I changed the relevant part of the bat file to:

%_EXECJAVA% -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %ACTION%

i.e. the address and transport are specified in the command line rather than in environment options.

Now start Tomcat with this option.

In Eclipse, the project is set up in the normal way with java classes etc.

Breakpoint where you want the code to stop inside Tomcat (e.g. inside a web service)
and then select Run / Debug / Remote Java Application.

You only need to worry about the Connect tab. "Connection Type" is "Standard (Socket Attach)", host is e.g. "localhost" and the port is the port specified in the command line above under the "address" option i.e. 8000. Then press the "Debug" button.

Now invoke whatever to force your Tomcat code to execute e.g. running a web service client which invokes the web service you want to debug.

Bingo - Eclipse will catch the breakpoint and change to the Debug perspective.

Enjoy!

Thursday, November 03, 2005

SOAP : Compiling Axis WSDL2Java

So you've run Axis WSDL2Java and got all the Java files in their package and are battling to compile it all.

Don't sweat.

Add the following construct to the command line for WSDL2Java:

--buildFile

This constructs the build.xml file for you and then it's as simple as running "Ant".

Enjoy!

SOAP : Administer Axis

The "Administer Axis" feature is disabled by default for security reasons. To enable it, navigate to:

C:\Tomcat 5.0\webapps\axis\WEB-INF\web.xml

(This assumes that Axis is running inside Tomcat 5.0)

Then uncomment the section with the comment:

-- uncomment this if you want the admin servlet --

Enjoy!

SOAP : Watch the obvious

From the Axis SOAP install documentation:

set AXIS_HOME=/usr/axis
set AXIS_LIB=$AXIS_HOME/lib
set AXISCLASSPATH=%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery.jar;
%AXIS_LIB%\commons-logging.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;
%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar

So you cut and paste and make a .bat file that does this and nothing works. Pull out hair. Hang on, lets have a look at the \lib directory. Ah!

It's not "commons-logging.jar" but rather "commons-logging-1.0.4.jar" and so on.

Sometimes it's the obvious that screws you!

Enjoy!

Friday, September 23, 2005

SOAP : "Dynamic" web service

Using SOAP and web services, this is something I have found useful.

Basically, the original question was: "Is it possible to allow an application to access webservices without specifically adding a web reference in the project? In other words, can an application be created which allows the 'user' to specify the URL, and then call whatever webmethods may exist on that server?"

This may be a bit of a hack but hey "If the shoe fits ....".

What you could do is have dummy stubs

e.g. int Method1 (int)
string Method2 (string)

and so on and code each differently on the server side.

So Method1 on URL1 will do something completely different to Method1 on URL2.

The important thing is that the WSDL signatures are correct.

Enjoy!

Monday, April 04, 2005

SOAP : Tracing SOAP Web Services using extensions

Ever wanted to log the actual content of the SOAP messages arising out of Web Services on your device.

You can do this with SOAP Trace Extensions.

See here:

Fun with SOAP Extensions - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspnet/html/asp03222001.asp

Enjoy!