Creating SOAPConnection and SOAPMessage Objects

This section describes classes and methods that are needed to create SOAPConnection objects and SOAPMessage objects.

Before writing more SAAJ programs, let's take a closer look at classes and methods that we need to use to create SOAPConnection objects and SOAPMessage objects.

1. javax.xml.soap.SOAPConnectionFactory - A factory class that can be used to create SOAPConnection objects. Only 2 methods are provided in this class:

static SOAPConnectionFactory newInstance() - 
   Creates an instance of the default SOAPConnectionFactory object.

abstract SOAPConnection createConnection() -
   Create a new SOAPConnection object.

Here is how these two method are used to create a SOAPConnection object:

   SOAPConnectionFactory fac = SOAPConnectionFactory.newInstance();
   SOAPConnection con = fac.createConnection();

2. javax.xml.soap.SOAPConnection - A point-to-point connection that a client program can use for sending messages directly to a remote party (represented by a URL, for instance). Only 3 methods are provided in this class:

abstract SOAPMessage call(SOAPMessage request, Object to) -
   Sends the given message to the specified endpoint and blocks until
   it has returned the response.

SOAPMessage get(Object to) -
   Gets a message from a specific endpoint and blocks until it
   receives.

abstract void close() -
   Closes this SOAPConnection object.

The important method in this class is the call() method, which takes a SOAPMessage object as the request, sends it to a SOAP endpoint and returns a SOAPMessage object as the response. The SOAP endpoint, the second argument, is an object of 3 types:

java.lang.String - A URL string like http://herongyang.com/MyService
java.net.URL class offered in the JDK
javax.xml.messaging.URLEndpoint class offered in the Apache Axis

3. javax.xml.soap.MessageFactory - A factory class that can be used to create SOAPMessage objects. Only 3 main methods are provided in this class:

static MessageFactory newInstance() -
   Creates a new MessageFactory object of the default implementation.

static MessageFactory newInstance(String protocol) -
   Creates a new MessageFactory object of the specified 
   implementation. 4 protocols are supported: DYNAMIC_SOAP_PROTOCOL,
   DEFAULT_SOAP_PROTOCOL, SOAP_1_1_PROTOCOL, SOAP_1_2_PROTOCOL.

abstract SOAPMessage createMessage()
   Creates a new empty SOAPMessage object.

Here is how you should create SOAPMesage objects for SOAP 1.1 and SOAP 1.2:

   MessageFactory fac = MessageFactory.newInstance();
   SOAPMessage msg11 = fac.createMessage(); // Default is SOAP 1.1
   SOAPMessage msg12
      = fac.createMessage(SOAPConstants.SOAP_1_2_PROTOCOL);

4. javax.xml.soap.SOAPMessage - The root class for all SOAP messages. Some important methods are:

abstract void writeTo(OutputStream out) -
   Writes this SOAPMessage object to the given output stream.

abstract SOAPPart getSOAPPart() -
   Gets the SOAP part of this SOAPMessage object.

SOAPBody getSOAPBody() -
   Gets the SOAP Body contained in this SOAPMessage object.

SOAPHeader getSOAPHeader() -
   Gets the SOAP Header contained in this SOAPMessage object.

abstract void addAttachmentPart(AttachmentPart AttachmentPart) -
   Adds an attachment to this SOAPMessage object.

abstract Iterator getAttachments() -
   Retrieves all attachments of this SOAPMessage object.

Last update: 2009.

Table of Contents

 About This Book

 Introduction to Web Service

 Introduction to SOAP (Simple Object Access Protocol)

 SOAP Message Structure

 SOAP Message Transmission and Processing

 SOAP Data Model

 SOAP Encoding

 SOAP RPC Presentation

 SOAP Properties Model

 SOAP Message Exchange Patterns

 SOAP HTTP Binding

 SOAP Perl Implementations

 SOAP PHP Implementations

 SOAP Java Implementations

 Perl SOAP::Lite - SOAP Server-Client Communication Module

 Perl Socket Test Program for HTTP and SOAP

 Perl SOAP::Lite for GetSpeech SOAP 1.1 Web Service

 Perl SOAP::Lite 0.710 for SOAP 1.2 Web Services

 Perl SOAP::Lite 0.710 for WSDL

 PHP SOAP Extension Client Programs

 PHP SOAP Extension Server Programs

 Java Socket and HttpURLConnection for SOAP

SAAJ - SOAP with Attachments API for Java

 SAAJ API 1.3 Classes and Interfaces Overview

 SAAJ API and Default Implementation in JDK 1.6.0

 SAAJ API Reference Implementation 1.3.4

 First SOAPConnection Test Program

Creating SOAPConnection and SOAPMessage Objects

 SAAJ SOAPMessage Structure and Classes/Interfaces

 Populating the SOAP Body with Request XML Elements

 Don't Use xml* as namespace Prefix

 addHeader() - Setting SOAPAction Header Line

 Calling GetSpeech SOAP 1.1 with SAAJ

 SOAPConstants.SOAP_1_2_PROTOCOL

 Calling GetSpeech SOAP 1.2 with SAAJ

 SoapUI - SOAP Web Service Testing Tool

 WS-Security - SOAP Message Security Extension

 WS-Security X.509 Certificate Token

 Web Services and SOAP Terminology

 References

 PDF Printing Version