SOAP with Attachments API for Java

In this section we will develop SOAP application having a text attachment.

SOAP with Attachments API for Java

  

Project Requirement
In this section we will develop SOAP application having   a text attachment.
Then it retrieves the content of the attachment file and display it on the screen

Solution

  • Make a java application project
  • Create a java class
  • Write the source code
  • Make a attatchment text file
  • Compile the code
  • Build project
  • Execute the application
Create a java application
  • Open  the Netbeans
  • Take a new Java application project as shown below in Figure.1

Attachments API for Java
Figure 1

  • Give the project  name as SOAP-saaj as given below in Figure. 2

Attachments API for Java

Figure. 2

  • This creates the java project.

Creating the java Class

  • Right Click on the project
  • Select NewàJava Class as shown below in Figure. 3.

 Attachments API for Java

   Figure. 3

  • Give the file name as attach1.java as shown below in Figure. 4.
  • This creates the attatch1.java.

Attachments API for Java

Figure. 4

Change the content of java file
Now make the changes in the code of attach1.java in  file as code  given below.

package pack1;
import java.io.*;
import java.util.Iterator;
import javax.xml.soap.*;

public class attatch1 {
  public static void main(String[] args) {
   FileReader fr = null;
  BufferedReader br = null;
  String line = "";
  try {
  MessageFactory messageFactory = MessageFactory.newInstance();
  SOAPMessage message = messageFactory.createMessage();
  SOAPHeader header = message.getSOAPHeader();
  SOAPBody body = message.getSOAPBody();
  header.detachNode();
  AttachmentPart attachment1 = message.createAttachmentPart();

  fr = new FileReader(new File("rose1.txt"));
  br = new BufferedReader(fr);

  String stringContent = "";
  line = br.readLine();

  while (line != null) {
    stringContent = stringContent.concat(line);
  stringContent = stringContent.concat("\n");
  line = br.readLine();
  }

  attachment1.setContent(stringContent, "text/plain");
  attachment1.setContentId("attached_text");

  message.addAttachmentPart(attachment1);
  Iterator iterator = message.getAttachments();

   while (iterator.hasNext()) {
  AttachmentPart attached = (AttachmentPart) iterator.next();

  String id = attached.getContentId();
  String type = attached.getContentType();
  System.out.println(
  "Attachment " + id + " has content type " + type);

  if (type.equals("text/plain")) {
  Object content = attached.getContent();
  System.out.println("Attachment contains:\n" + content);
  }
  }
  } catch (FileNotFoundException e) {
  System.out.println("File not found: " + e.toString());
  System.exit(1);
  } catch (IOException e) {
  System.out.println("I/O exception: " + e.toString());
  System.exit(1);
  }
  catch (ArrayIndexOutOfBoundsException e)
  {
  System.out.println("array exception" +e.toString());
  }
  catch (Exception ex) {
  ex.printStackTrace();
  } 
  }
}

  • Now in the code makes modification as code given below.

package pack1;
import java.io.*;
import java.util.Iterator;
import javax.xml.soap.*;

public class attatch1 {
  public static void main(String[] args) {
   FileReader fr = null;
  BufferedReader br = null;
  String line = "";

  try {
    MessageFactory messageFactory = MessageFactory.newInstance();
  SOAPMessage message = messageFactory.createMessage();
  SOAPHeader header = message.getSOAPHeader();
  SOAPBody body = message.getSOAPBody();
  header.detachNode();
  
  AttachmentPart attachment1 = message.createAttachmentPart();

  fr = new FileReader(new File("rose1.txt"));
  br = new BufferedReader(fr);

  String stringContent = "";
  line = br.readLine();

  while (line != null) {
  stringContent = stringContent.concat(line);
  stringContent = stringContent.concat("\n");
  line = br.readLine();
  }

  attachment1.setContent(stringContent, "text/plain");
  attachment1.setContentId("attached_text");

  message.addAttachmentPart(attachment1);
  Iterator iterator = message.getAttachments();

   while (iterator.hasNext()) {
    AttachmentPart attached = (AttachmentPart) iterator.next();

  String id = attached.getContentId();
  String type = attached.getContentType();
  System.out.println(
  "Attachment " + id + " has content type " + type);

  if (type.equals("text/plain")) {
  Object content = attached.getContent();
  System.out.println("Attachment contains:\n" + content);
  }
  }
  }
   catch (FileNotFoundException e) {
  System.out.println("File not found: " + e.toString());
  System.exit(1);
  }

  catch (IOException e) {
  System.out.println("I/O exception: " + e.toString());
  System.exit(1);
  }
  catch (ArrayIndexOutOfBoundsException e) {
  System.out.println("array exception" +e.toString());
  }
  catch (Exception ex) {
  ex.printStackTrace();
  } 
  }
}

Creating  text attatchment file

  • Now Right Click on the project folder
  • Select  the Project properties
  • Copy the project folder location as given in Figure 5 below.
  • Open it in Window Explorer

Attachments API for Java

Figure. 5

  • In this folder make a rose1.txt   file  which have been given as the argument name in the above code
  • Give some content as

Hi
Welcome to the SAAJ Application
This is the Example of Attached Text Message.
Best Regards
RoseIndia

Running the project
  • Now run the file.
  • Right Click and select run file
  • It executes and give the output as  given below in Figure 6.

Attachments API for Java

Figure 6

API used in the program
   This file uses  the  file handling API  such as FileReader  and
   BufferedReader and Web Service API  as given below

  • 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  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.
  • AttachmentPart :-It  is  a single attachment to a SOAPMessage object. A SOAPMessage object may contain zero, one, or many AttachmentPart objects. Each AttachmentPart object consists of two parts, application-specific content and associated MIME headers.

Download Code

Advertisements

Share on Google+Share on Google+

SOAP with Attachments API for Java

Posted on: October 21, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: SOAP with Attachments API for Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Arun
January 18, 2012
SAAJ with Web Services Issue

I have downloaded the code given above and it works fine as a normal Java application. But what I need is to generate a SOAP attachment with XML file in a Web service and transfer the SOAP message to another Web service. I work with Netbeans IDE. When I try to deploy the above given logic as a Web Service onto a Glassfish server, I get the following error. "Deploying application in domain failed; Deployment Error -- Exception occured in the wsgen process javax.xml.ws.WebServiceException: Unable to create JAXBContext" I have tried using javax.jar file in the Web application library, but only end up with deployment failure. Kindly help me in fixing this problem. Thanks in advance.

Ads

 

Ads