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!

Tuesday, January 05, 2010

Metro : Sniffing the traffic with JAX-WS, Netbeans and Metro

I often use Fiddler to view / log browser traffic.

From the web site: "Fiddler is a Web Debugging Proxy which logs all HTTP(S) traffic between your computer and the Internet. Fiddler allows you to inspect all HTTP(S) traffic, set breakpoints, and "fiddle" with incoming or outgoing data. Fiddler includes a powerful event-based scripting subsystem, and can be extended using any .NET language."

However, it doesn't work when you want to look at Java web service (SOAP) traffic generated by the JAX-WS support in Netbeans (i.e. Metro).

I have a number of command line tools that I use to generate various types of traffic with a command line like:


java -jar SomeProgram.jar


To get Fiddler to work, you need to change the command line to:


java -DproxySet=true -DproxyHost=127.0.0.1 -DproxyPort=8888 -jar SomeProgram.jar


Fiddler uses port 8888 as default.

Enjoy!

Tuesday, October 20, 2009

Blogger : Syntax highlighting for code

I hate the way Blogger displays code , XML etc, inside the "blockqoute" construct.

Then I came across this:

How to add syntax highlight to Blogger.

This uses Syntax Highlighter.

Just to add:

You don't need to host the files. You can use:

http://syntaxhighlighter.googlecode.com/svn/trunk/

e.g. the link in the article would become:
http://syntaxhighlighter.googlecode.com/svn/trunk/Styles/SyntaxHighlighter.css

Update: As per the comment:

More info. available here:
http://mlawire.blogspot.com/2009/07/blogger-syntax-highlighting.html

Warning: There are a number of posts around on how to do this - all slightly different - use at your own risk.

Enjoy!

Log4J : Path to properties file

For Glassfish, we normally put the log4j.properties in the "domain / config" directory.

To enable Glassfish to find it, add the following to the Glassfish JVM settings. Easiest way is to use the Admin console i.e. Application Server / JVM Settings / JVM Options.

Add this line:

-Dlog4j.configuration=file:///${com.sun.aas.instanceRoot}/config/log4j.properties

Enjoy!

Netbeans : Generating getters and setters for a Java class

Using Netbeans 6.5.

Unlike Eclipse (where the link to do this is obvious), in Netbeans you will find this under:

Refactor / Encapsulate Fields

Enjoy!

Metro : JAX WS catalogue

One of the problems with using Netbeans to generate web services using Glassfish / Metro is that when you want to create a new Web service client, you are asked to select the web service which is normally on your local machine.

This means that the web service has an address like http://localhost:8080 and it then becomes a problem when you deploy to other environments.

One way around this is to use a catalog to resolve the addresses at run time. The file is called jax-ws-catalog.xml and it's placed in the META-INF directory.

It looks like e.g.

 
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog" prefer="system">
<rewriteSystem
systemIdStartString="http://localhost:8080/"
rewritePrefix="http://some url:some port/"/>
<rewriteURI
uriStartString="http://localhost:8080/"
rewritePrefix="http://some url:some port/" />
</catalog>


This searches for any URL that starts with "http://localhost:8080/" and replaces this with "http://some url:some port/" e.g.

http://localhost:8080/webserver/WebService

becomes

http://some url:some port/webserver/WebService

The ant build files in Netbeans use the -catalog option to point to a file called catalog.xml in the project root directory. You can apply the same principles to this file to resolve addresses at compile time.

Some useful links:

Using a JAX-WS Catalog

How to write an XML catalog file

Global URI re-writing with jax-ws-catalog

Enjoy!

Friday, August 07, 2009

Netbeans : New Web Service client error

I use Netbeans 6.7.

In the web applications project where you are trying to add a web service call to e.g. a servlet, click "New / Web Service Client" and select the project where the actual web service lives. When you click Finish, you get the error.

"wsimport doesn't support the "xnocompile" attribute"

To get round this:

In the project where you are trying to add the web service client, right-click the web service under the "Web Service Reference" sub-tree.

Select "Edit Web Service Attributes".

Click "WsImport Options" tab.

Select the xnocompile line and Remove then OK.

Right-click again on the web service under the "Web Service Reference" sub-tree.

Select "Refresh Client"

The client should now be created.

These a good tutorial about Netbeans web services
here

Enjoy!

Netbeans : Display Web Service Designer and Axis2

This is displayed (I use Netbeans 6.7) when you right click on a web applications project and select "New / Web Service".

You then get two tabs - Source and Design.

However, you do NOT see these tabs if you have previously loaded the Axis2 support plugin.

Enjoy!

Thursday, July 09, 2009

Eclipse : Problems with web.xml

If you use Eclipse with the Sysdeo plugin you sometimes get problems with web.xml when you add more servlet mappings. The message is:

The content of element type "web-app" must match "(icon?,display-
name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*,servlet-
mapping*,session-config?,mime-mapping*,welcome-file-list?,error-page*,taglib*,resource-env-
ref*,resource-ref*,security-constraint*,login-config?,security-role*,env-entry*,ejb-ref*,ejb-local-
ref*)".


e.g. you may have


web-app
servlet A
servlet-name
servlet-class
/servlet

servlet-mapping A
servlet-name
url-pattern
/servlet-mapping

servlet B
servlet-name
servlet-class
/servlet

servlet-mapping B
servlet-name
url-pattern
/servlet-mapping

/web-app


The problem is in the order. You can't intermix servlet and servlet-mapping. All the servlets have to come first followed by all the servlet-mappings. So the above would look like:


web-app
servlet A
servlet-name
servlet-class
/servlet

servlet B
servlet-name
servlet-class
/servlet

servlet-mapping A
servlet-name
url-pattern
/servlet-mapping

servlet-mapping B
servlet-name
url-pattern
/servlet-mapping

/web-app


You can check this by : Right click inside the xml window - Validate.

Enjoy!

Tuesday, July 07, 2009

Notepad++ : Error with nppcm.dll

Use this freebie program a lot as a simple text editor on Windows XP. I used the right context click plugin which one day suddenly came up with:

""Error creating process: nppcm.dll needs to be in the same directory than notepad++.exe"

Mr Google to the rescue and found the answer.

There is a file in the root of the hard drive called 'Program' - no extension.

Delete this file and all is sweetness and light!

Enjoy!