Just another (occasional) brain dump on technology and the technology industry...

About Me

My photo
I enjoy wine, laptops, walks, bicycling with the kids and long drives. I am constanly reading. In my heart I am a teacher a salesman and a technologist.

Friday, February 15, 2008

SOAP With Attachments in Java CAPS, Client

In a previous article I showed how to use SAAJ in Java CAPS to consume SwA messages. This article adds the client side code to generate an appropriate SOAP message and to invoke the "Web Service" in CAPS.

The code is as follows:






01 package tech.axl.s11wa.saaj13.client;
02 
03 
04 import java.io.FileInputStream;
05 import java.io.IOException;
06 import java.net.MalformedURLException;
07 import java.net.URL;
08 import java.util.logging.Level;
09 import java.util.logging.Logger;
10 import javax.xml.soap.AttachmentPart;
11 import javax.xml.soap.MessageFactory;
12 import javax.xml.soap.SOAPConnection;
13 import javax.xml.soap.SOAPConnectionFactory;
14 import javax.xml.soap.SOAPConstants;
15 import javax.xml.soap.SOAPException;
16 import javax.xml.soap.SOAPFactory;
17 import javax.xml.soap.SOAPMessage;
18 
19 
20 public class _Client {
21 
22     public static void main(String[] args) {
23         _Client c_ = new _Client();
24         int helloIndex_ = 1;
25         int worldIndex_ = 1;
26         if (args.length >= 2) {
27             helloIndex_ = Integer.parseInt(args[0]);
28             worldIndex_ = Integer.parseInt(args[1]);
29         }
30         c_.callSOAP11WithAttachmentsEndpoint(helloIndex_, worldIndex_);
31     }
32 
33     private void callSOAP11WithAttachmentsEndpoint(final int helloIndex, final int worldIndex) {
34         try {
35             MessageFactory mf_ = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
36             SOAPMessage message_ = mf_.createMessage()
37 
38             //
39             AttachmentPart attachment0_ = message_.createAttachmentPart();
40             attachment0_.setRawContent(new FileInputStream("Hello.txt")"text/plain");
41             attachment0_.setContentId("hello_in_multiple_languages");
42             message_.addAttachmentPart(attachment0_);
43             AttachmentPart attachment1_ = message_.createAttachmentPart();
44             attachment1_.setRawContent(new FileInputStream("World.txt")"text/plain");
45             attachment1_.setContentId("world_in_multiple_languages");
46             message_.addAttachmentPart(attachment1_);
47 
48             //
49             SOAPFactory factory_ = SOAPFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
50             message_.getSOAPBody().addBodyElement(factory_.createName(
51                     "helloIndex""dhpoc""http://www.discovery.co.za/poc")).setValue(String.valueOf(helloIndex));
52             message_.getSOAPBody().addBodyElement(factory_.createName(
53                     "worldIndex""dhpoc""http://www.discovery.co.za/poc")).setValue(String.valueOf(worldIndex));
54 
55             //
56             message_.saveChanges();
57 
58             SOAPConnectionFactory scf_ = SOAPConnectionFactory.newInstance();
59             SOAPConnection c_ = scf_.createConnection();
60             SOAPMessage response_ = c_.call(message_, new URL(
61                     "http://localhost:28280/dpSwAWithSAAJ13_servlet_SwAWithSAAJ13/SwAWithSAAJ13"));
62             if (null != response_) {
63                 response_.writeTo(System.out);
64             else {
65                 Logger.getLogger(_Client.class.getName()).log(Level.WARNING, "Could not retrieve SOAP response");
66             }
67         catch (SOAPException ex) {
68             Logger.getLogger(_Client.class.getName()).log(Level.SEVERE, null, ex);
69         catch (MalformedURLException ex) {
70             Logger.getLogger(_Client.class.getName()).log(Level.SEVERE, null, ex);
71         catch (IOException ex) {
72             Logger.getLogger(_Client.class.getName()).log(Level.SEVERE, null, ex);
73         }
74     }
75 
76 }




I used tcpmon to redirect the call to the appropriate destination to see the payload on the wire.

Have fun!

No comments: