Sunday 27 May 2007

Web Service client with Spring-WS and DuckTyping

This week I have tested Spring-WS, I'm interesting in Xpath support and I have done a simple client of BookService.
The BookService is a test of JSR 181 with XFire (perhaps I'll explain the implementation in other post).

One example of SOAP request/response:

SOAP Request:

<ns1:findBook xmlns:ns1="http://dahernan.net/BookService">
<ns1:isbn>12567</ns1:isbn>
</ns1:findBook>


SOAP Response:

<ns1:findBookResponse xmlns:ns1="http://dahernan.net/BookService">
<ns1:Book>
<author xmlns="http://model.dahernan.net">Matt Raible</author>
<isbn xmlns="http://model.dahernan.net">12567</isbn>
<price xmlns="http://model.dahernan.net">12.22</price>
<title xmlns="http://model.dahernan.net">Spring Live</title>
</ns1:Book>
</ns1:findBookResponse>


So, let's see the Spring-WS Client:

public class BookServiceClient {

private static final String MESSAGE =
"<ns1:findbook ns1="\"http://dahernan.net/BookService\"">" +
"<ns1:isbn>12567</ns1:isbn>" +
"</ns1:findbook>";

private WebServiceTemplate webServiceTemplate;

private XPathExpression xpathExp;

// ... getters and setters

public String getAuthor() {
StreamSource source = new StreamSource(new StringReader(MESSAGE));
DOMResult result = new DOMResult();
webServiceTemplate.sendAndReceive(source, result);
String author = xpathExp.evaluateAsString(result.getNode());

return author;
}


It's based in WebServiceTemplate (I like the Spring templates :), and XPathExpresion, the request is constant.
The response, first I build a DOM with response and next I evaluate the XPath Expresion.

And the Spring wiring.

<bean id="bookServiceClient" class="net.dahernan.example.BookServiceClient">
<property name="webServiceTemplate" ref="webServiceTemplate"/>
<property name="xpathExp" ref="xpathExp"/>
</bean>

<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<property name="defaultUri" value="http://localhost:8080/xfire_test/services/BookService"/>
<property name="messageSender">
<bean class="org.springframework.ws.transport.http.HttpUrlConnectionMessageSender"/>
</property>
</bean>

<bean id="xpathExp" class="org.springframework.xml.xpath.XPathExpressionFactoryBean">
<property name="expression" value="/ns1:findBookResponse/ns1:Book/dh:author"/>
<property name="namespaces">
<props>
<prop key="ns1">http://dahernan.net/BookService</prop>
<prop key="dh">http://model.dahernan.net</prop>
</props>
</property>
</bean>


The last step, is do a small test.

public class BookServiceClientTest extends AbstractDependencyInjectionSpringContextTests{

protected static Logger logger = Logger.getLogger(BookServiceClientTest.class);

protected String[] getConfigLocations() {
return new String [] {"ws-beans.xml"};
}

public void testClient(){
BookServiceClient client = (BookServiceClient) this.applicationContext.getBean("bookServiceClient");
String author = client.getAuthor();
logger.info("Author: " + author);
}

}


All, without stubs, It looks like great.

Wednesday 16 May 2007

Things that I'd like to test

Now I'm testing Mule, because I'm interesting in ESB (it's fantastic I hope spend some time writing a post about Mule). And when I'm tired, I read about Scrum and Agile.

There are a lot of things that I'd like to test but I don't have enough time. Here a bunch of things:
My delicious network and my feeds make that the list grows. I have to spend some time in an idea that make me rich.

Sunday 6 May 2007

Arid Framework or Simplifying Spring XML

Chris Richardson (author of Pojos in Action) in his blog tell us about Simplifying Spring XML with Arid Framework. This is a very interesting idea, and it's on the same line as actual trends about "Convention over Configuration".

This approach to the problem, it's based in autowire and custom namespace in Spring 2.0 XML.

I don't like especially autowire by type, it's useful for test proposes, but when you have two classes that implements the same interface, you must disabled. I see beginners developers crash with this, many times in test classes.

Another very useful thing about classic Spring XML, it's that you have a mirror of the architecture. You can use Spring IDE or another tool to see the real architecture, not an obsolete UML diagram. With annotations or conventions it's more difficult to see the architecture.

In my humble opinion, with expert developers Arid framework and other solutions about Convention over Configuration can be fantastic, but with beginners the best approach is the classic style.