Java A Simple SAAJ client

SAAJ webservice client example.

SAAJ

SOAP with Attachments API for Java provides the standard way to send XML documents over the network for the java platform.
We can send direct SOAP messages instead using any API's like jax-ws , Axis etc.

We will send soap request to a public webservice and get the response out of the service.

For experiment purpose we use the weather service webservice available at the endpoint http://wsf.cdyne.com/WeatherWS/Weather.asmx

Below is the SOAP message to send to this webservice.

SOAP message

<SOAP-ENV:Envelope
        xmlns:SOAP-ENV="
http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:xsi="
http://www.w3.org/1999/XMLSchema-instance"
        xmlns:xsd="
http://www.w3.org/1999/XMLSchema">
    <SOAP-ENV:Header />
    <SOAP-ENV:Body>
        <ns1:GetCityForecastByZIP xmlns:ns1='http://ws.cdyne.com/WeatherWS/'>
   <ns1:ZIP>29602</ns1:ZIP>
 </ns1:GetCityForecastByZIP>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

SAAJClient.java

/**
 * Simple SAAJ client for a publically available Webservice.
 * The service is WEATHER service. 
 */
package com.service;

import java.io.StringReader;
import java.net.Authenticator;
import java.net.URL;

import javax.xml.soap.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

/**
 * @author bala
 *
 */
public class SaajClient {
 
 // SOAP message as string, you can read it from file, n/w source etc

 private static String soapMessage = " " +
   "   " +
   "29602  ";
 
 private static final String endPoint = "http://wsf.cdyne.com/WeatherWS/Weather.asmx";
 
 public static void main(String[] args) throws Exception {
  
  setProxy(); // set proxy if you are behind the firewall 

  SOAPMessage message = sendMessage();
  
  readResponse(message);
 }

 private static SOAPMessage sendMessage() throws Exception {

  SOAPConnectionFactory sFactory = SOAPConnectionFactory.newInstance();
  SOAPConnection sConnection = sFactory.createConnection();
  
  MessageFactory messageFactory = MessageFactory.newInstance();
  SOAPMessage message = messageFactory.createMessage();

  SOAPPart soapPart = message.getSOAPPart();
  SOAPEnvelope envelope = soapPart.getEnvelope();

  // To read the SOAP message from file, here we use the string variable, so this is commented out.
  /*StreamSource preppedMsgSrc = new StreamSource(new FileInputStream("file_location"));
  soapPart.setContent(preppedMsgSrc);*/
  
  soapPart.setContent(new StreamSource(new StringReader(soapMessage)));
  
  System.out.println("Content set!");

  message.saveChanges();

  String destination = endPoint;
  
  URL url = new URL(destination);
    
  SOAPMessage reply  = sConnection.call(message, url);
    
  System.out.println("Request sent!");

  sConnection.close();
  
  System.out.println("connection closed!");
  
  return reply;
 }

 private static void readResponse(SOAPMessage message) throws Exception {

  System.out.println("message here ;" + message);
  TransformerFactory transformerFactory = TransformerFactory.newInstance();
  Transformer transformer = transformerFactory.newTransformer();
  Source sourceContent = message.getSOAPPart().getContent();
  
  StreamResult result = new StreamResult(System.out);

  transformer.transform(sourceContent, result);

 }
 
}

Running above code will give following output in the console. This output is the webservice response.
Response from the webservice is looks like below.

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="
http://www.w3.org/2001/XMLSchema">
 <soap:Body>
  <GetCityForecastByZIPResponse
   xmlns="
http://ws.cdyne.com/WeatherWS/">
   <GetCityForecastByZIPResult>
    <Success>true</Success>
    <ResponseText>City Found</ResponseText>  
   </GetCityForecastByZIPResult>
  </GetCityForecastByZIPResponse>
 </soap:Body>
</soap:Envelope>

Comments


  1. Bei der Auswahl eines Systems ist es ratsam, auf Standards wie FIDO2 oder passwortlose Lösungen nach ISO 27001 zu achten. Diese gewährleisten Kompatibilität und Rechtssicherheit im Ernstfall. Die technischen Vorgaben des §7 des IT-Sicherheitsgesetzes fordern zudem eine regelmäßige Aktualisierung der Sicherheitsmaßnahmen inklusive Authentifizierungsverfahren. Im Jahr 2024 haben zahlreiche Anbieter ihre Produkte verbessert; Unternehmen sollten daher regelmäßig prüfen, ob ihre Systeme noch den aktuellen Normen entsprechen. Durch gezielte Weiterbildungen im Bereich cybersecurity weiterbildung können Mitarbeitende die neuesten Entwicklungen sofort umsetzen und Risiken minimieren..

    ReplyDelete

Post a Comment

Popular posts from this blog

Log4j multiple WAR files in single EAR configuration

Java NIO2 - Watching a directory for changes. FileWatcherService.

Ubuntu / kubuntu - Laptop key board cursor jumping issue