You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by Brosca Diana <Di...@atosorigin.com> on 2005/12/06 15:12:13 UTC

Web service with Attachment with

Hello 

I have a question for you.

How I can implement a web service that has a pdf  in attachment?

Can I use the WSDL2Java tool for generate the class that implements the
web service?

I'm using Axis 1.2RC

Thanks for your help

Diana


Re: Web service with Attachment with

Posted by Wade Chandler <hw...@yahoo.com>.
--- Brosca Diana <Di...@atosorigin.com> wrote:

> Hello 
> 
> I have a question for you.
> 
> How I can implement a web service that has a pdf  in
> attachment?
> 
> Can I use the WSDL2Java tool for generate the class
> that implements the
> web service?
> 
> I'm using Axis 1.2RC
> 
> Thanks for your help
> 
> Diana
> 
> 
I have types which contain elements named content
which are of the xsd:base64Binary type.  WSDL2Java
made these elements of type Object (if not you should
be able to change it).  I can add attachments in two
different ways.

1) simply write my attachment out to a byte array and
set the "content" equal to the byte[] instance.  

or

2) I can use the activation framework and
javax.activation.DataHandler and
javax.activation.FileDataSource and make an attachment
this way.  Axis will place an attachment reference
automatically for me for this Object since it is a
data handler.

So, in simpler terms...lets say I have a class created
from the wsdl created type:
<complexType name="NodeDocument">
<sequence>
<element name="name" type="xsd:string" default="Your
document name"/>
<element name="type" type="xsd:string"/>
<element name="content" type="xsd:base64Binary"/>
</sequence>
</complexType>

The simple java less some axis stuff will be:
public class NodeDocument implements Serializable{
String type = null;
String name = null;
Object content = null;

public String getType(){
   return type;
}
public void setType(String type){
   this.type = type;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public Object getContent(){
return this.content;
}
public void setContent(Object content){
this.content = content;
}
}//end NodeDocument class

Case 1 create a byte array:
byte[] content = "some kind of string data in the form
of attachment".getBytes();
doc.setContent(content);

Case 2 create a DataHandler attached to a temp file or
file:
javax.activation.FileDataSource fds = new
javax.activation.FileDataSource("some path to a
file");
javax.activation.DataHandler dh = new
javax.activation.DataHandler(fds);
doc.setContent(dh);

Now I've left out the parts where Axis is calling the
objects, but this is how simple it is.  In both cases
I can use attachments.  For the data handler an actual
"Attachment" will be used.  Imagine you have an array
of these NodeDocuments in your return message.  You
will end up setting the array with multiple documents,
and each document will be converted to an attachment
or it will converted to a base64 array from the byte
array.  Axis handles this stuff automatically for you.

When you get a message with attachments you will first
have to unpack the attachment from an AttachmentPart. 
So basically you'll have to add code to check to see
at any stage if content is of type
AttachementPart(apache type), DataHandler, or byte[]
if you are going to be able to read it.  I like using
DataHandler even for byte content and using the Apache
ManagedMemoryDataSource type to make it all easier to
use by using a single consistent type (DataSource).

Wade