File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services and the fly likes problem with SOAP attachments using JAXM Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "problem with SOAP attachments using JAXM" Watch "problem with SOAP attachments using JAXM" New topic
Author

problem with SOAP attachments using JAXM

Ste Lamb
Greenhorn

Joined: Feb 05, 2003
Posts: 14
I am trying to pass different files using a SOAP with attachments (JAXM). It works fine for an xml file as the attchment (for example) but any other type of file is causing the following error:
"java.net.SocketException: Unexpected end of file from server"
The code I am using to pass create the attachment is:
File outputFile = new File("C:/myfile.bmp");
//Build the call:-
//Get a connection
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection connSOAP = scf.createConnection();
//Create a message
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();
//Access the elements of the message
SOAPPart part = msg.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
SOAPHeader header = envelope.getHeader();
SOAPBody body = envelope.getBody();
//Add a new SOAP element
Name name = envelope.createName("loadFile");
SOAPElement child = body.addBodyElement(name);
//Add content
name = envelope.createName("filename");
SOAPElement filename = child.addChildElement(name);
filename.addTextNode(outputFile.getAbsolutePath());
//Add attachment
URL fileurl = outputFile.toURL();
DataHandler dh = new DataHandler(fileurl);

AttachmentPart attachment = msg.createAttachmentPart(dh);
attachment.setContentId("test_file");

msg.addAttachmentPart(attachment);

Any ideas as to where I'm going wrong? Or is this a known bug?
Any help, very much appreciated.
Cheers
Ste
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
That sounds suspiciously like it's not base-64 encoding the file. Could you show what's flowing over the wire to us (perhaps using Bill Brogden's handy snoop tool for SOAP)...
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Ste Lamb
Greenhorn

Joined: Feb 05, 2003
Posts: 14
Hi Kyle
not exactly certain of what you mean, but below is the actual dump stack I get. If it isn't encoding it like you say, how do I go about encoding it? Alternatively how about I write the file to a byte array and attach that? Would that work.. although how to write a whole file to an an array I'm not sure...
Any advice appreciated...
Thanks Kyle

java.net.SocketException: Unexpected end of file from server
boolean sun.net.www.http.HttpClient.parseHTTPHeader(sun.net.www.MessageHeader, sun.net.ProgressEntry)
HttpClient.java:708
boolean sun.net.www.http.HttpClient.parseHTTP(sun.net.www.MessageHeader, sun.net.ProgressEntry)
HttpClient.java:613
boolean sun.net.www.http.HttpClient.parseHTTPHeader(sun.net.www.MessageHeader, sun.net.ProgressEntry)
HttpClient.java:706
boolean sun.net.www.http.HttpClient.parseHTTP(sun.net.www.MessageHeader, sun.net.ProgressEntry)
HttpClient.java:613
java.io.InputStream sun.net.www.protocol.http.HttpURLConnection.getInputStream()
HttpURLConnection.java:506
int java.net.HttpURLConnection.getResponseCode()
HttpURLConnection.java:230
javax.xml.soap.SOAPMessage com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.post(javax.xml.soap.SOAPMessage, java.net.URL)
HttpSOAPConnection.java:274
java.lang.Object com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection$PriviledgedPost.run()
HttpSOAPConnection.java:142
java.lang.Object java.security.AccessController.doPrivileged(java.security.PrivilegedExceptionAction)
native code
javax.xml.soap.SOAPMessage com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(javax.xml.soap.SOAPMessage, java.lang.Object)
HttpSOAPConnection.java:115
boolean uk.gov.pro.arc.ClientInterfaces.LoadProcess.loadFiles()
LoadProcess.java:509
void uk.gov.pro.arc.ClientInterfaces.TestclientInterfaces.main(java.lang.String[])
TestclientInterfaces.java:81
java.security.PrivilegedActionException: javax.xml.soap.SOAPException: Message send failed
javax.xml.soap.SOAPMessage com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.post(javax.xml.soap.SOAPMessage, java.net.URL)
HttpSOAPConnection.java:289
java.lang.Object com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection$PriviledgedPost.run()
HttpSOAPConnection.java:142
java.lang.Object java.security.AccessController.doPrivileged(java.security.PrivilegedExceptionAction)
native code
javax.xml.soap.SOAPMessage com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(javax.xml.soap.SOAPMessage, java.lang.Object)
HttpSOAPConnection.java:115
boolean uk.gov.pro.arc.ClientInterfaces.LoadProcess.loadFiles()
LoadProcess.java:509
void uk.gov.pro.arc.ClientInterfaces.TestclientInterfaces.main(java.lang.String[])
TestclientInterfaces.java:81
javax.xml.soap.SOAPException: java.security.PrivilegedActionException <<javax.xml.soap.SOAPException: Message send failed>>
javax.xml.soap.SOAPMessage com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(javax.xml.soap.SOAPMessage, java.lang.Object)
HttpSOAPConnection.java:121
boolean uk.gov.pro.arc.ClientInterfaces.LoadProcess.loadFiles()
LoadProcess.java:509
void uk.gov.pro.arc.ClientInterfaces.TestclientInterfaces.main(java.lang.String[])
TestclientInterfaces.java:81
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Seems to me that if you are getting this:

Then the client isn't even getting through the headers before something odd happens.
I ran into the difficulty of figuring this kind of problem out when doing my SOAP book. I wrote a utility that lets you trap both sides of the client-server text stream and even alter and resend messages. With that you can see exactly what the server is sending back.
You can download it from a link at the bottom of this page.
Bill
Ste Lamb
Greenhorn

Joined: Feb 05, 2003
Posts: 14
hi Kyle/William
I tried a different method using HTTP Post to get the file across and was coming up with a similar problem. This time I converted the file to a byte array before sending it. Unfotunately the same error was happening.
I resolved this problem by taking on board what you suggested Kyle which was to encode the byte array to base64 before sending it. And it worked!
I suspect but haven't tried yet that this is the same problem that is happening with the JAXM SOAP connection. When I've tried it I'll let you know.
Its strange that this isn't documented anywhere. I would have thought it encodes it automatically for you?
I guess though that most of this sort of thing is done direct from a JSP which encodes it automatically???
Thanks for your help...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with SOAP attachments using JAXM