Skip to content

Instantly share code, notes, and snippets.

@kdelfour
Created December 23, 2014 15:40
Show Gist options
  • Save kdelfour/b2a449a1bac23e3baec8 to your computer and use it in GitHub Desktop.
Save kdelfour/b2a449a1bac23e3baec8 to your computer and use it in GitHub Desktop.

Revisions

  1. kdelfour created this gist Dec 23, 2014.
    175 changes: 175 additions & 0 deletions ASimpleSOAPClient.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,175 @@
    package com.kdstudio.snippets.soap.client;

    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.InputStream;

    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.soap.MessageFactory;
    import javax.xml.soap.MimeHeaders;
    import javax.xml.soap.SOAPBody;
    import javax.xml.soap.SOAPConnection;
    import javax.xml.soap.SOAPConnectionFactory;
    import javax.xml.soap.SOAPElement;
    import javax.xml.soap.SOAPEnvelope;
    import javax.xml.soap.SOAPException;
    import javax.xml.soap.SOAPMessage;
    import javax.xml.soap.SOAPPart;

    import org.apache.log4j.Logger;
    import org.w3c.dom.Document;
    import org.xml.sax.SAXException;

    /**
    * This is an example of a simple SOAP Client class to send request body to a
    * SOAP Server.
    *
    * Useful when you want to test a SOAP server and you don't want to generate all
    * SOAP client class from the WSDL.
    *
    * @author kdelfour
    */
    public class ASimpleSOAPClient {

    // Default logger
    private static final Logger LOG = Logger.getLogger(ASimpleSOAPClient.class);

    // The SOAP server URI
    private String uriSOAPServer;
    // The SOAP connection
    private SOAPConnection soapConnection = null;

    // If you want to add namespace to the header, follow this constant
    private static final String PREFIX_NAMESPACE = "ns";
    private static final String NAMESPACE = "http://other.namespace.to.add.to.header";

    /**
    * A constructor who create a SOAP connection
    *
    * @param url
    * the SOAP server URI
    */
    public ASimpleSOAPClient(final String url) {
    this.uriSOAPServer = url;

    try {
    createSOAPConnection();
    } catch (UnsupportedOperationException | SOAPException e) {
    LOG.error(e);
    }
    }

    /**
    * Send a SOAP request for a specific operation
    *
    * @param xmlRequestBody
    * the body of the SOAP message
    * @param operation
    * the operation from the SOAP server invoked
    * @return a response from the server
    * @throws SOAPException
    * @throws ParserConfigurationException
    * @throws IOException
    * @throws SAXException
    */
    public String sendMessageToSOAPServer(String xmlRequestBody,
    String operation) throws SOAPException, SAXException, IOException,
    ParserConfigurationException {

    // Send SOAP Message to SOAP Server
    final SOAPElement stringToSOAPElement = stringToSOAPElement(xmlRequestBody);
    final SOAPMessage soapResponse = soapConnection.call(
    createSOAPRequest(stringToSOAPElement, operation),
    uriSOAPServer);

    // Print SOAP Response
    LOG.info("Response SOAP Message : " + soapResponse.toString());
    return soapResponse.toString();
    }

    /**
    * Create a SOAP connection
    *
    * @throws UnsupportedOperationException
    * @throws SOAPException
    */
    private void createSOAPConnection() throws UnsupportedOperationException,
    SOAPException {

    // Create SOAP Connection
    SOAPConnectionFactory soapConnectionFactory;
    soapConnectionFactory = SOAPConnectionFactory.newInstance();
    soapConnection = soapConnectionFactory.createConnection();
    }

    /**
    * Create a SOAP request
    *
    * @param body
    * the body of the SOAP message
    * @param operation
    * the operation from the SOAP server invoked
    * @return the SOAP message request completed
    * @throws SOAPException
    */
    private SOAPMessage createSOAPRequest(SOAPElement body, String operation)
    throws SOAPException {

    final MessageFactory messageFactory = MessageFactory.newInstance();
    final SOAPMessage soapMessage = messageFactory.createMessage();
    final SOAPPart soapPart = soapMessage.getSOAPPart();

    // SOAP Envelope
    final SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration(PREFIX_NAMESPACE, NAMESPACE);

    // SOAP Body
    final SOAPBody soapBody = envelope.getBody();
    soapBody.addChildElement(body);

    // Mime Headers
    final MimeHeaders headers = soapMessage.getMimeHeaders();
    LOG.info("SOAPAction : " + uriSOAPServer + operation);
    headers.addHeader("SOAPAction", uriSOAPServer + operation);

    soapMessage.saveChanges();

    /* Print the request message */
    LOG.info("Request SOAP Message :" + soapMessage.toString());
    return soapMessage;
    }

    /**
    * Transform a String to a SOAP element
    *
    * @param xmlRequestBody
    * the string body representation
    * @return a SOAP element
    * @throws SOAPException
    * @throws SAXException
    * @throws IOException
    * @throws ParserConfigurationException
    */
    private SOAPElement stringToSOAPElement(String xmlRequestBody)
    throws SOAPException, SAXException, IOException,
    ParserConfigurationException {

    // Load the XML text into a DOM Document
    final DocumentBuilderFactory builderFactory = DocumentBuilderFactory
    .newInstance();
    builderFactory.setNamespaceAware(true);
    final InputStream stream = new ByteArrayInputStream(
    xmlRequestBody.getBytes());
    final Document doc = builderFactory.newDocumentBuilder().parse(stream);

    // Use SAAJ to convert Document to SOAPElement
    // Create SoapMessage
    final MessageFactory msgFactory = MessageFactory.newInstance();
    final SOAPMessage message = msgFactory.createMessage();
    final SOAPBody soapBody = message.getSOAPBody();

    // This returns the SOAPBodyElement that contains ONLY the Payload
    return soapBody.addDocument(doc);
    }
    }
    40 changes: 40 additions & 0 deletions pom.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dkstudio</groupId>
    <artifactId>snippets</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Simple SOAP Client</name>
    <description>A simple SOAP Client class to send request body to a SOAP Server.
    Useful when you want to test a SOAP server and you don't want to generate all SOAP client class from the WSDL.</description>

    <developers>
    <developer>
    <id>delfour.k</id>
    <name>Kevin DELFOUR</name>
    <email>[email protected]</email>
    <organization>DK</organization>
    <organizationUrl>http://kevin.delfour.eu</organizationUrl>
    </developer>
    </developers>

    <organization>
    <name>DK</name>
    <url>http://kevin.delfour.eu</url>
    </organization>

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.build.sourceVersion>1.7</project.build.sourceVersion>
    </properties>

    <dependencies>
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    </dependency>
    </dependencies>

    </project>