Book Image

Spring Web Services 2 Cookbook

By : Hamidreza Sattari, Shameer Kunjumohamed
Book Image

Spring Web Services 2 Cookbook

By: Hamidreza Sattari, Shameer Kunjumohamed

Overview of this book

<p>Spring Web Services is a product of the Spring community focused on creating document-driven Web services.<br /><br />Spring Web Services aims to facilitate contract-first SOAP service development, allowing for the creation of flexible web services using one of the many ways to manipulate XML payloads.<br /><br />This comprehensive guide -- which provides professional expertise on a variety of technical topics right from setting-up a contract-first Web-Service, creating client of a Web-Service to serialization, monitoring, testing and security using Spring-WS -- helps you enhance your skills in Spring Web Services.<br /><br />Spring Web Services 2 Cookbook includes a wide variety of recipes that covers most important topics used in real-world applications. It is a well-rounded guide covering a lot of ground in the Spring Web Services domain using systematic arranged chapters and focused recipes.<br /><br />The book begins with setting up a contract first Web Service over various protocols such as JMS, XMPP, and Email. The next chapter targets creating clients for SOAP Web Services. We then learn how to test and monitor the Web Service using tools like soapUI and TCPMon. Building on, logging, tracing and exception handling are detailed in the subsequent chapter. The book then covers marshalling and unmarshalling using different technologies like JAXB2, XMLBeans, JibX, XStream, MooseXML etc. Securing WebServices through authentication, authorization, encryption and decryption and digital signature using Spring-WS features based on XWSS and WSS4J Libraries is outlined in the next chapter two chapters. The book then tackles development of RESTful Web Services. Finally, Setting up Web Services using Spring Remoting based on various technologies like HTTP , RMI, JMS, JAXWS and a Web Service using Apache CXF on JAX-WS front-end are explained.<br /><br />This book will help relatively new developers in accelerating their learning process and experienced developers in expanding their skills sets of Spring Web Services.</p>
Table of Contents (17 chapters)
Spring Web Services 2 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Setting up Spring-WS on XMPP transport


HTTP is most often used as a Web-Service transport protocol. However, it is not able to meet the asynchronous communication requirements.

Web-Service on XMPP transport is capable of asynchronous communication in which a client doesn't need to wait for a response from a service; instead, the service sends the response to the client when the process is completed. Spring-WS 2.0 includes XMPP (Jabber) support in which a Web-Service can communicate over the XMPP protocol. In this recipe, setting up a Spring-WS on XMPP transport is presented. Since there is no external HTTP server, a test class is used to load the application context.

Getting ready

In this recipe, the project's name is LiveRestaurant_R-1.7, which has the following Maven dependencies:

  • spring-ws-core-2.0.1.RELEASE.jar

  • spring-ws-support-2.0.1.RELEASE.jar

  • spring-test-3.0.5.RELEASE.jar

  • junit-4.7.jar

  • xmlunit-1.1.jar

  • smack-3.1.0.jar

How to do it...

  1. Create an endpoint (SamplePlayLoadEndPoint).

  2. Configure connection to the XMPP server in the application context (applicationContext.xml).

  3. Configure the message receiver in the application context.

  4. Run the following command:

    mvn clean package
    

    The following is the response received:

    <placeOrderRequest xmlns="..."><id>9999</id></placeOrderRequest>
    ...
     for request
     ...<placeOrderRequest xmlns="...."><id>9999</id></placeOrderReque
    t>...
    

How it works...

In the application context, the messageFactory bean is responsible for creating the incoming and outgoing SOAP messages. The messageReceiver bean acts as a server, using a connection (to XMPP server:google talk), and listens to the host on a specific service with a username and password.

    <bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/>
    <bean id="connection" class="org.springframework.ws.transport.xmpp.support.XmppConnectionFactoryBean">
        <property name="host" value="talk.google.com"/>
        <property name="username" value="[email protected]"/>
        <property name="password" value="yourPassword"/>
        <property name="serviceName" value="gmail.com"/>
    </bean>

    <bean id="messagingReceiver" class="org.springframework.ws.transport.xmpp.XmppMessageReceiver">
        <property name="messageFactory" ref="messageFactory"/>
        <property name="connection" ref="connection"/>
        <property name="messageReceiver" ref="messageDispatcher"/>
    </bean>

Once the message is sent by the client, it will be forwarded to the endpoint (SamplePlayLoadEndPoint that is configured within messageDispatcher) by the message dispatcher and the response will be returned to the client:

  <bean id="messageDispatcher"
    class="org.springframework.ws.soap.server.SoapMessageDispatcher">
    <property name="endpointMappings">
     <bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping"> 
        <property name="defaultEndpoint"> <bean class="com.packtpub.liverestaurant.service.endpoint.SamplePlayLoadEndPoint"/> 
      </property> </bean>
    </property>
  </bean>

Webservicetemplate is used here as a client; it will be discussed in the next chapter.

SamplePlayLoadEndPoint just receives a request and returns a response:

public class SamplePlayLoadEndPoint implements PayloadEndpoint {
  static Logger logger = Logger.getLogger(SamplePlayLoadEndPoint.class);
  public Source invoke(Source request) throws Exception {
      return  request;
    }

A test class is included in the project to load the application context, set up the XMPP Web-Service server, and test the Web-Service.

See also

The Creating Web-Service client on XMPP transport recipe discussed in Chapter 2, Building Clients for SOAP Web-Services.