KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > md5attach > MD5AttachTest


1 package test.md5attach;
2
3 import org.apache.axis.MessageContext;
4 import org.apache.axis.client.Call;
5 import org.apache.axis.utils.Options;
6
7 /**
8  * A convenient little test program which will send a message as is to
9  * the server. Useful for debugging interoperability problems or
10  * handling of ill-formed messages that are hard to reproduce programmatically.
11  *
12  * Accepts the standard options, followed by a list of files containing
13  * the contents to be sent.
14  */

15 public class MD5AttachTest {
16     static void main(String JavaDoc[] args) throws Exception JavaDoc {
17         Options opts = new Options(args);
18         String JavaDoc action = opts.isValueSet('a');
19
20         Call call = new Call(opts.getURL());
21         if (action != null) {
22             call.setProperty(Call.SOAPACTION_USE_PROPERTY, new Boolean JavaDoc(true));
23             call.setProperty(Call.SOAPACTION_URI_PROPERTY, action);
24         }
25
26         args = opts.getRemainingArgs();
27
28         if (null == args || args.length != 1) {
29             System.err.println("Must specify file to send as an attachment!");
30             System.exit(8);
31         }
32
33         //Create the attachment.
34
javax.activation.DataHandler JavaDoc dh = new javax.activation.DataHandler JavaDoc(new javax.activation.FileDataSource JavaDoc(args[0]));
35
36         org.apache.axis.message.SOAPEnvelope env = new org.apache.axis.message.SOAPEnvelope();
37
38         //Build the body elements.
39
javax.xml.parsers.DocumentBuilderFactory JavaDoc dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
40         javax.xml.parsers.DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
41         org.w3c.dom.Document JavaDoc doc = db.newDocument();
42         org.w3c.dom.Element JavaDoc methodElement = doc.createElementNS("foo", "foo:MD5Attach");
43         org.w3c.dom.Element JavaDoc paramElement = doc.createElementNS("foo", "foo:thefile");
44         long startTime = System.currentTimeMillis();
45         methodElement.appendChild(paramElement);
46         paramElement.appendChild(doc.createTextNode("" + startTime));
47
48
49         org.apache.axis.message.SOAPBodyElement sbe = new org.apache.axis.message.SOAPBodyElement(methodElement);
50         env.addBodyElement(sbe);
51
52         org.apache.axis.Message msg = new org.apache.axis.Message(env);
53
54         //Add the attachment content to the message.
55
org.apache.axis.attachments.Attachments attachments = msg.getAttachmentsImpl();
56         org.apache.axis.Part attachmentPart = attachments.createAttachmentPart(dh);
57         String JavaDoc href = attachmentPart.getContentId();
58         //Have the parameter element set an href attribute to the attachment.
59
paramElement.setAttribute(org.apache.axis.Constants.ATTR_HREF, href);
60         env.clearBody();
61         env.addBodyElement(sbe);
62
63         ((org.apache.axis.SOAPPart)msg.getSOAPPart()).setSOAPEnvelope(env);
64
65         call.setRequestMessage(msg);
66         //go on now....
67
call.invoke();
68
69         MessageContext mc = call.getMessageContext();
70         // System.out.println(mc.getResponseMessage().getAsString());
71

72         env = mc.getResponseMessage().getSOAPEnvelope();
73         sbe = env.getFirstBody();
74         org.w3c.dom.Element JavaDoc sbElement = sbe.getAsDOM();
75         //get the first level accessor ie parameter
76
org.w3c.dom.Node JavaDoc n = sbElement.getFirstChild();
77         for (; n != null && !(n instanceof org.w3c.dom.Element JavaDoc); n = n.getNextSibling()) ;
78         paramElement = (org.w3c.dom.Element JavaDoc) n;
79
80         org.w3c.dom.Node JavaDoc respNode = paramElement.getFirstChild();
81         long elapsedTime = -1;
82         if (respNode != null && respNode instanceof org.w3c.dom.Text JavaDoc) {
83
84             String JavaDoc respStr = ((org.w3c.dom.Text JavaDoc) respNode).getData();
85             String JavaDoc timeStr = null;
86             String JavaDoc MD5String = null;
87             java.util.StringTokenizer JavaDoc st = new java.util.StringTokenizer JavaDoc(respStr);
88             while (st.hasMoreTokens()) {
89                 String JavaDoc s = st.nextToken().trim();
90                 if (s.startsWith("elapsedTime="))
91                     timeStr = s.substring(12);
92                 else if (s.startsWith("MD5=")) MD5String = s.substring(4);
93             }
94             if (timeStr != null) {
95                 long time = Long.parseLong(timeStr);
96                 timeStr = (time / 100.0) + " sec.";
97             } else {
98                 timeStr = "Unknown";
99             }
100             System.out.println("The time to send was:" + timeStr);
101             if (MD5String == null) {
102                 System.err.println("Sorry no MD5 data was received.");
103             } else {
104                 System.out.println("Calculating MD5 for local file...");
105                 java.security.MessageDigest JavaDoc md = java.security.MessageDigest.getInstance("MD5");
106                 byte[] MD5received = org.apache.axis.encoding.Base64.decode(MD5String);
107                 java.io.InputStream JavaDoc attachmentStream = dh.getInputStream();
108                 int bread = 0;
109                 byte[] buf = new byte[64 * 1024];
110                 do {
111                     bread = attachmentStream.read(buf);
112                     if (bread > 0) {
113                         md.update(buf, 0, bread);
114                     }
115                 } while (bread > -1);
116                 attachmentStream.close();
117                 buf = null;
118                 //Add the mime type to the digest.
119
String JavaDoc contentType = dh.getContentType();
120                 if (contentType != null && contentType.length() != 0) {
121                     md.update(contentType.getBytes("US-ASCII"));
122                 }
123                 byte[] MD5orginal = md.digest();
124                 if (java.util.Arrays.equals(MD5orginal, MD5received)) {
125                     System.out.println("All is well with Axis's attachment support!");
126                     System.exit(0);
127                 } else {
128                     System.err.println("Miss match in MD5");
129                 }
130             }
131         } else {
132             System.err.println("Sorry no returned data.");
133         }
134         System.err.println("You've found a bug:\"http://nagoya.apache.org/bugzilla/\"");
135         System.exit(8);
136     }
137
138 }
139
Popular Tags