SOAP Header

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)

SOAP Header

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 :

  • Create a java application project
  • Create a java class file
  • Add the code for Header
  • Make Compilation of the file
  • Build the project
  • Run the project

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

  • Take a new Java Application   Project as given in below in Figure1

SOAP Header

    Figure. 1

  • Give the project name as SOAP-Header as given in below in Figure2

SOAP Header

Figure 2

 

  • Click on the finish button. This creates a Java Application project.
Creating the Java File
  • Now create a new java file.
  • Right click on the project
  • Select the new java Class as shown in below  Figure 3

SOAP Header

 Figure 3

  • Give the name of the java class as Header1.
  • Give the package name as pack2 as given below in Fig 4

SOAP Header

   Figure. 4

  • Click on the finish .It will create the file Header1.java
  • Now edit the code of the Header1.java as shown   below.

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 <
  • After creation of the Header1.java and Editing save it .
  • Run the file.Right Click and select run file in Header1.java
  • You will see the output as given below in fig 5.
  • SOAP Header

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