Thursday, March 21, 2013

JAXWS Handler injection using Spring

I'll try to keep this post less verbose and let the code speak for itself.

In order inject the handler using Spring, we need the below mentioned default handler resolver class and the configuration.

DefaultHandlerResolver
package handler;

import java.util.List;

import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.PortInfo;

@SuppressWarnings("rawtypes")
public class DefaultHandlerResolver implements HandlerResolver {
 private List<Handler> handlerList;

 public List<Handler> getHandlerChain(PortInfo portInfo) {
  return handlerList;
 }

 public void setHandlerList(List<Handler> handlerList) {
  this.handlerList = handlerList;
 }
}


applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 <!-- Web Service custom properties (Timeouts in millis)  -->
 <util:map id="jaxwsCustomProperties">
  <entry key="com.sun.xml.ws.connect.timeout">
   <value type="java.lang.Integer">15000</value>
  </entry> 
  <entry key="com.sun.xml.ws.request.timeout">
   <value type="java.lang.Integer">15000</value>
  </entry>
 </util:map>

 <bean id="handlerResolver" class="handler.DefaultHandlerResolver">
  <property name="handlerList">
   <list>
    <bean class="handler.SOAPRequestResponseSpitter" />
   </list>
  </property>
 </bean>

 <bean id="calculatorServicePortType" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
  <property name="serviceInterface" value="service.Calculator" />
  <property name="wsdlDocumentUrl" value="CalculatorService.wsdl" />
  <property name="namespaceUri" value="http://service/" />
  <property name="serviceName" value="CalculatorService" />
  <property name="endpointAddress" value="http://localhost:7001/CalculatorServiceWebApp/CalculatorService" />
  <property name="customProperties" ref="jaxwsCustomProperties" />
  <property name="handlerResolver" ref="handlerResolver"/>
 </bean>
</beans>


Once the above files are ready, then run the following in the main method of your test class or JUnit:
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
Calculator calculator = (Calculator) context.getBean("calculatorServicePortType");
System.out.println("Calculated result [" + calculator.add(2, 3) +"]");


The output should look like: Outbound message:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Header/>
  <S:Body>
    <ns2:add xmlns:ns2="http://service/">
      <a>2</a>
      <b>3</b>
    </ns2:add>
  </S:Body>
</S:Envelope>
Inbound message:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
 <S:Header/>
 <S:Body>
  <ns2:addResponse xmlns:ns2="http://service/">
   <return>5</return>
  </ns2:addResponse>
 </S:Body>
</S:Envelope>
Calculated result [5]

No comments:

Post a Comment