[an error occurred while processing this directive]

SOAP Header

                         

Project Requirement

Make a project in netbeans.In it develop an application that gives create a SOAP message along with several headers (use SOAP 1.1 here)

Solution :

SOAP Header is the optional Element in SOAP Message . It contains data for application in XML form.For example, transaction semantics, authentication information, and so on.It can be specified as the content of a SOAPHeader object.

Create a java application project

SOAP Header

                                                    Figure. 1

SOAP Header

Figure 2

 

Creating the Java File

SOAP Header

   Figure 3

SOAP Header

         Figure. 4

package pack2;
import javax.xml.soap.*;
import java.util.*;
import javax.xml.namespace.QName;
public class Header1 {

    public static void main(String[] args) {
           String version = "1.2";
        System.out.println("SOAP version is " + version);

        if (!(version.equals("1.1") || version.equals("1.2"))) {
            System.err.println("Value must be 1.1 or 1.2");
            System.exit(1);
        }

        try {
            MessageFactory messageFactory = null;
            if (version.equals("1.1")) {
                messageFactory = MessageFactory.newInstance();
            } else {
                messageFactory = MessageFactory.newInstance(
                            SOAPConstants.SOAP_1_2_PROTOCOL);
            }
            SOAPMessage message = messageFactory.createMessage();
            SOAPHeader header = message.getSOAPHeader();
            String nameSpace = "ns";
            String nameSpaceURI = "http://www.roseindia.net";
            QName train1 = new QName(nameSpaceURI, "trainDesk", nameSpace);
            SOAPHeaderElement orderHeader = header.addHeaderElement(train1);

            if (version.equals("1.1")) {
                orderHeader.setActor("http://www.roseindia.net/training");
            } else {
                orderHeader.setRole("http://www.roseindia.net/training");
            }

            QName ejb = new QName(nameSpaceURI, "ejbDesk", nameSpace);
            SOAPHeaderElement shippingHeader = header.addHeaderElement(
                        ejb);

            if (version.equals("1.1")) {
                shippingHeader.setActor("http://www.roseindia.net/ejb/");
            } else {
                shippingHeader.setRole("http://www.roseindia.net/ejb/");
            }
            QName xml = new QName(
                        nameSpaceURI,
                        "xmlDesk",
                        nameSpace);
            SOAPHeaderElement confirmationHeader = header.addHeaderElement(
                        xml);

            if (version.equals("1.1")) {
                confirmationHeader.setActor("http://www.roseindia.net/xml/");
            } else {
                confirmationHeader.setRole("http://www.roseindia.net/xml/");
            }

            confirmationHeader.setMustUnderstand(true);
            QName struts = new QName(nameSpaceURI, "strutsDesk", nameSpace);
            SOAPHeaderElement billingHeader = header.addHeaderElement(struts);

            if (version.equals("1.1")) {
                billingHeader.setActor("http://www.roseindia.net/struts/");
            } else {
                billingHeader.setRole("http://www.roseindia.net/struts/");
            }
            if (version.equals("1.2")) {
                billingHeader.setRelay(true);
            }
            SOAPBody body = message.getSOAPBody();
            message.saveChanges();
            System.out.println("\n----- Request Message ----\n");
            message.writeTo(System.out);
            System.out.println();
            Iterator allHeaders = header.examineAllHeaderElements();
            while (allHeaders.hasNext()) {
                SOAPHeaderElement headerElement = (SOAPHeaderElement) allHeaders
                    .next();
                QName headerName = headerElement.getElementQName();
                System.out.println("\nHeader name is " + headerName.toString());

                if (version.equals("1.1")) {
                    System.out.println("Actor is " + headerElement.getActor());
                } else {
                    System.out.println("Role is " + headerElement.getRole());
                }

                System.out.println(
                        "mustUnderstand is "
                        + headerElement.getMustUnderstand());

                if (version.equals("1.2")) {
                    System.out.println("relay is " + headerElement.getRelay());
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Running the file <

Figure. 5

APIs used in this project

MessageFactory:- It is a factory for creating SOAPMessage objects
A MessageFactory object  is created by  newInstance () method in SAAJ client file.
SOAPMessage:- It is the root class for all SOAP messages.
A SOAPMessage object consists of a SOAP part and optionally one or more attachment parts. The SOAP part for a SOAPMessage object is a SOAPPart object.  SOAP part contains information used for message routing and identification. All data in the SOAP Part of a message must be in XML format. 
SOAPHeader:- SOAP Header is the optional Element. It contains data for application in XML form.For example, transaction semantics, authentication information, and so on, can be specified as the content of a SOAPHeader object
SOAPBody :- It is the required Element of the SOAP.It has the main message contents in the XML form.
Qname:- This class  represents a qualified name as defined in the XML specifications
SOAPHeaderElement:-This object represents the contents in the SOAP header part of the SOAP envelope.It is used after SOAP Header.So the children of a SOAPHeader object can be represented only as SOAPHeaderElement objects. A SOAPHeaderElement object can have other SOAPElement objects as its children.

Download Code

                         
[an error occurred while processing this directive]