This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am looking for an good example which shows how to send an soap attachment [pdf file] from a java webservice to a java client program. The java client program should download the pdf file from webservice. I have already developed a sample webservice using JAX-WS. Now i wanted to modify the webservice to send attachments to client. Can this be done using SAAJ?
I tried searching, almost 90 % of the examples shows file upload..i.e sending attachments from client to webservice...but i need an example for file download!
Yes its exactly the reverse. Client will request an attachment [pdf/document] by sending the file name in SOAP request. Webservice will identify the document and send it as an attachment back in SOAP response.
I am new to webservice implementations. I have conceptual understanding and i have implemented an simple webservice using JAX-WS API and its working fine, but i want modify it for sending attachments to client. I am familiar with bottom-up approach.
After searching in web, i found that an attachment can be sent as MIME part or binary stream. Like returning byte array or DataHandler object! I dunno whether MIME part or binary is recommended or which one is easy?
If you could help me with clear simple example or code snippet using JAX-WS and SAAJ, then i can easily catch up.
Thanks Naren. After going through many posts under webservices category, i successfully created the webservice using DataHandler object which appears as byte array in client java program [in wsdl the return type is xs:base64Binary]. But there is a new problem now!
I did this implementation using notepad and used wsgen and wsimport tools available with java 6 sdk. After publishing the webservice using java 6 lightweight sever and when i used the wsimport to generate the classes for client program the datahandler object was represented as byte array. I used FileOutputStream to write byte array to a file with pdf extension, it worked fine download pdf operation worked.
Then i tried the same migrating to web application using eclipse ide, this time the following are the new things i did
created sun-jaxws.xml file
declaring WSServletContextListener in web.xml
using jax-ws libraries [copied some 22 jar files to WEB-INF/lib directory]
Service was successfully published. I used wsimport to generate class files for client program, but this time DataHandler object was represented as DataHandler and not byte array.
While writing the data in object output stream i am getting stream corrupt exception.
I have the following questions
1. Why java return type for the operation changes when the service is published in web application? How to solve streamcorrupt exception?
2. sending attachments using datahandler or byte array [return type is xs:base64Binary]! Is this a recommended/effective approach?
3. is there any restrictions in terms of file size, because the file contents are transferred in soap message? if so what is the maximum file size supported?
4. what is advantage of other approach like using SAAJ, MTOM, Attachment with content id and mime type tags?
5. when i tested this in SOAP UI, after getting the response in the response tab/screen i see the binary data, but in the bottom of screen Attachments are not sensed.i.e SOAP UI is not recognizing the binary attachment data as attachment!
After publishing the webservice using java 6 lightweight sever and when i used the wsimport to generate the classes for client program the datahandler object was represented as byte array.
I used wsimport to generate class files for client program, but this time DataHandler object was represented as DataHandler and not byte array.
How does the generation of client classes differ in the above two contexts? I think you should be using same WSDL file to generate client classes, don't you?
Comments for your questions.
1. I don't think it should happen unless you use different web service client generation or web service creation tools.
2. Implementing SwA (usage of DataHandler) is not supported by WS Basic Profile (1.1 or 2.0). You must check if it is supported by Attachments Profile 1.1 before interoperating your web service from other than java language.
Implementing "In-line" or MTOM attachments is though interoperable (as it conforms to the WS Basic Profile), there are attachment content size limitations.
3. If you use "In-line" or MTOM attachments, then there are obviously restrictions on attachment content, which I presume due to JVM memory restrictions. But, using SwA though there are size restrictions you can atleast send bigger files compared to former approach.
4. If you are using MTOM messaging, you don't need to necessarily use SAAJ. JAX-RPC/ JAX-WS should work fine.
5. Have you tried using SOAPUI Pro, which has got some additional features of representing SOAP message in different formats?
I have resolved the stream corrupt exception. Using disassembler i found that there is method in InputStream class which returns byte array. I passed this byte to FileOutputStream it worked.
I also tested transferring pdf, it worked till 5 MB. Above 5MB i started getting OutOfMemory Exception. So maximum size limit is 5MB.
Now i wanted to read user name, password and time stamp token from SOAPHeader for authentication. So i created SOAPHandler and put the following code in the handleMessage method, but SOAPHeader is null.
SOAP request is
When the above request is sent, the SOAPHeader is null. Please help me in this regard.
I am working on the same kind of requirement.We are using Axis 1.4 and we need to return a pdf file as part of a soap attachment in the response.I am pretty new to web services and have learned a lot of the basics in the last week but i am still trying to find a working sample of a webservice that sends a file as a attachment in the response.
If you have some sample code can you share it.It will be a great help.Thanks