Announcement Announcement Module
Collapse
No announcement yet.
JSESSIONID and setting cookie for WebServiceTemplate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    Spring Webservice with SOAP

    Hi ,
    i did some R&D in Spring webservices then after i got a idea to get the proper response from the service provide... in below i mentioned some code please try to follow...............




    public String clientRequestWebService(final String productName, String xigniteBody) throws WebServiceTemplateException {

    final StreamSource xigniteBodySource = new StreamSource(new StringReader(xigniteBody));
    StreamResult result = new StreamResult(System.out);
    log.debug("Resutl ::::: >>>>>>>> "+ result);

    if(webServiceTemplate == null) {
    throw new WebServiceTemplateException("Web Service Template Shouldn't be Null");
    }

    MyCommonsHttpMessageSender messageSender = new MyCommonsHttpMessageSender();

    try {
    messageSender.createConnection(new URI("http://www.xxxx.com/xCurrencies.asmx"));
    } catch (IOException e) {
    e.printStackTrace();
    } catch (URISyntaxException e) {
    e.printStackTrace();
    }

    webServiceTemplate.setMessageSender(messageSender) ;
    webServiceTemplate.setDefaultUri("http://www.xxxx.com/xCurrencies.asmx?op="+ productName);

    boolean flag = webServiceTemplate.sendSourceAndReceiveToResult( xigniteBodySource, new WebServiceMessageCallback() {
    @Override
    public void doWithMessage(WebServiceMessage arg0) throws IOException, TransformerException {

    SaajSoapMessage saajSoapMessage = ( SaajSoapMessage )arg0;
    saajSoapMessage.getSaajMessage();
    saajSoapMessage.setSoapAction( "http://www.xxxx.com/services/"+ productName);

    StringSource headerSource = new StringSource(getRequestHeader());
    SoapHeader soapHeader = ((SoapMessage) arg0).getSoapHeader();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(headerSource, soapHeader.getResult());
    }
    }, result );

    log.debug("webServiceTemplate.sendSourceAndReceive ToResult Flag : "+ flag);

    return null;
    }

    public String getRequestHeader() {

    StringBuffer sbHeader = new StringBuffer();
    sbHeader.append("<Header xmlns=\"http://www.xignite.com/services/\">");
    sbHeader.append("<Username>");
    sbHeader.append("xx");
    sbHeader.append("</Username>");
    sbHeader.append("<Password> ");
    sbHeader.append( "xxx" );
    sbHeader.append("</Password>");
    sbHeader.append("</Header>");

    log.debug("Header Content : "+ sbHeader.toString());
    return sbHeader.toString();
    }

    public class MyCommonsHttpMessageSender extends org.springframework.ws.transport.http.CommonsHttpM essageSender {
    @Override
    public WebServiceConnection createConnection(URI uri) throws IOException {
    CommonsHttpConnection connection = (CommonsHttpConnection) super.createConnection(uri);
    return connection;
    }
    }

    Comment


    • #17
      If you want to set other HTTP cookie than JSESSIONID then below solution will definitely help. What I am trying to do is to call a webservice which need authentication cookie set in HTTP header, not in SOAP message header.
      Spring's webServiceTemplate.marshalSendAndReceive(request) method internally uses HttpComponentsMessageSender to send the SOAP message over the network and this further uses WebServiceConnection to make http connection with the server. All you have to do is to write your own custom HttpComponentsMessageSender and set the cookie inside postMethod.

      Custome sender code:

      Code:

      package com.swap.ws.sender;

      import java.io.IOException;
      import java.net.URI;

      import javax.annotation.Resource;

      import org.apache.http.client.methods.HttpPost;
      import org.apache.log4j.Logger;
      import org.springframework.stereotype.Service;
      import org.springframework.ws.transport.WebServiceConnect ion;
      import org.springframework.ws.transport.http.HttpComponen tsConnection;

      /**
      *
      * @author swapnil Z
      */
      @Service("urlMessageSender")
      public class CustomHttpComponentsMessageSender extends
      org.springframework.ws.transport.http.HttpComponen tsMessageSender {
      private static Logger _logger = Logger.getLogger("");


      @Override
      public WebServiceConnection createConnection(URI uri) throws IOException {
      String cookie = null;
      HttpComponentsConnection conn = (HttpComponentsConnection) super
      .createConnection(uri);
      HttpPost postMethod = conn.getHttpPost();
      cookie = "<Your Custom Cookie>";

      postMethod.addHeader("Cookie", cookie);

      return conn;
      }
      }



      Spring Configuration :

      <bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMe ssageFactory" />

      <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshalle r">
      <property name="contextPath" value="com.swap.provision" />
      </bean>

      <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServi ceTemplate">
      <constructor-arg ref="messageFactory" />
      <property name="marshaller" ref="marshaller"></property>
      <property name="unmarshaller" ref="marshaller"></property>
      <property name="messageSender" ref="urlMessageSender"/>
      <property name="defaultUri" value=<Server URL> />
      </bean>


      After this I simply get bean webServiceTemplate and call marshalSendAndReceive method. So every request will have its custom cookie set before making HTTP call.

      Comment

      Working...
      X