aspose file tools*
The moose likes Web Services and the fly likes sending pdf or any attachment from a java webservice to java client using JAX-WS and SAAJ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "sending pdf or any attachment from a java webservice to java client using JAX-WS and SAAJ" Watch "sending pdf or any attachment from a java webservice to java client using JAX-WS and SAAJ" New topic
Author

sending pdf or any attachment from a java webservice to java client using JAX-WS and SAAJ

Srinath Kannan
Greenhorn

Joined: Jan 11, 2011
Posts: 14

Hi,

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!

Can this be achieved using JAX-WS and SAAJ?


Regards
Srinath
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Hi Srinath,
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!

Would this not be same as uploading pdf except what you were doing on client side now moves to service side and accordingly interface method arguments(request & response) are reversed?


Cheers,
Naren
(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)
Srinath Kannan
Greenhorn

Joined: Jan 11, 2011
Posts: 14

Hi Naren,

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
Srinath
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Hi Srinath,
This question has been asked many times in Javaranch. You may find my reply to this post useful.
Srinath Kannan
Greenhorn

Joined: Jan 11, 2011
Posts: 14

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!
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Hi Srinath,
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?
Srinath Kannan
Greenhorn

Joined: Jan 11, 2011
Posts: 14

Thank you for your comments.

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.
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Hi Srinath,
So maximum size limit is 5MB

Not sure if this is fixed. But, I think it depends on JVM maximum and usage memory.

When the above request is sent, the SOAPHeader is null. Please help me in this regard.

Shouldn't make a difference, but try this
Srinath Kannan
Greenhorn

Joined: Jan 11, 2011
Posts: 14

Hi Naren,

soapMsg.getSOAPHeader() is not working! Still null is returned!

Please help


Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Hi Srikanth,
Unless you really have SOAP Header you won't get null. Check your response in SOAP UI if you have any SOAP Headers coming through.
Sid Misra
Greenhorn

Joined: Jun 07, 2011
Posts: 2

Hi Srinath,

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

Sid
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sending pdf or any attachment from a java webservice to java client using JAX-WS and SAAJ