Yes Andrew solution is more elegant but I'm not sure that what you send in a 
parameter of a ws method can be stream like the attachments are. Could somebody 
from the axis developpers confirm this?

I need to test that in any case.

In my example I'm not explicitly sending AttachmentsPart nor DataHandler so that's why it's compatible with other languages. I'm attaching that to the SOAP message and somehow AXIS takes care of sending the right binary using the activation package (like if you would send email with attachment) ;)

But once again I'm not sure if it's the best way but it works just fine ;)

Cheers,
Patrick.



Praveen Peddi wrote:
OK. Andrew's solution is surely more elegant than Patrick's provided that Andrew's solution also sends the content as attachement (I mean sends ths stream rather than all the bytes at once). But again, In Andrew's server side code, he is checking as an instance of either byte[], DataHandler and AttachmentsPart. byte[] I think is ruled in my case since I don't load all bytes in memory. DataHandler as we have discussed cannot be used by .NET and other non-java clients. The only thing left is AttachmentPart. Isn't AttachmentPart specific to Axis. I did see Patrick also used it in his code (both java and .NET client). But can it be used by any non-Java client (for example we have .NET, MSSOAP tool kit,java and PHP clients)? Do you guys think I can use AttachmentPart in method argument (ofcourse as xsd:anyType, just like how Andrew used it) and it should work fine with all clients?

Andrew: BTW whats the java type of content in your java code. How does your NodeDocument class look like?

Praveen

----- Original Message ----- From: "BLIS Webmaster (Patrick Houbaux)" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, January 07, 2005 6:13 PM
Subject: Re: Best way to send attachments



Andrew,

Does it mean that you send the attachment as a part of a java bean you sent in your a ws method parameter?

I did not think it was possible at all.
Interesting I should try this out as well ;)

Cheers,
Patrick.

ANDREW MICONE wrote:

Sure, the here's a Java code snippet. Unfortunately, the .NET side is a black box to me, so I don't have any code to share. This will take the code generated by WSDL2Java and pull out the attachments from the "content" field which is typed as an xsd:anyType:

public static byte[] convertToBytes(NodeDocument doc)
{
Object content = null;
byte dataOUT[] = null;
content = doc.getContent();
if(content instanceof byte[])
dataOUT = (byte[])content;
else
if(content instanceof DataHandler)
{
DataHandler dhData = (DataHandler)content;
dataOUT = getBytesFromStream(dhData);
} else
if(content instanceof AttachmentPart)
{
AttachmentPart attch = (AttachmentPart)content;
dataOUT = getBytesFromStream(attch.getActivationDataHandler());
} else
{
throw new IllegalArgumentException("Could not convert to bytes from: " + content + " Argument type is unknown!");
}
return dataOUT;
}


    private static byte[] getBytesFromStream(DataHandler data)
    {
        InputStream in = null;
        byte out[] = null;
        try
        {
            in = data.getInputStream();
            if(in != null)
            {
                out = new byte[in.available()];
                in.read(out);
            } else
            {
                out = new byte[0];
            }
        }
        catch(IOException ex)
        {
            System.err.println("Could not get Bytes.");
            ex.printStackTrace();
        }
        return out;
    }


[EMAIL PROTECTED] 01/07/05 03:30PM >>>


Does it mean that the following method should work fine if I change DataHandler to Object? Does it work w/o any changes in the method implementation?

public String createOrUpdateContentObjectWithAttachments(String sessionID,
String containerID, String xmlString, String customerID, String contentID, DataHandler source, String sourceFileName,
DataHandler thumb, String thumbFileName);


input argument DataHandler is sent stream. How would the client and server know that it has to send the stream for that specific argument. You are just sending xsd:AnyType.

Andy, do you mind sending your code. Or atleast the snippet of the method that takes attachment as input argument?. I would appreciate if could also send your .NET snippet.

Thannks
Praveen

----- Original Message ----- From: "ANDREW MICONE" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, January 07, 2005 5:22 PM
Subject: RE: Best way to send attachments



Right, it's passed in either directly or by reference as xsd:anyType and then the receiver has to type the anyType to determine whether its base64encoded, SwA, or DIME. -- Andy



[EMAIL PROTECTED] 01/07/05 03:10PM >>>


Nothing in that wsdl fragment indicates that there will be any MIME or
DIME based attachments.

Cheers
Simon

-----Original Message-----
From: ANDREW MICONE [mailto:[EMAIL PROTECTED] Sent: Friday, January 07, 2005 12:14 PM
To: [EMAIL PROTECTED] Subject: RE: Best way to send attachments


Here's an example of a WSDL snippet that is consumed by both .NET and Axis that handles attachments and interoperates between the two. This is from a service in production:

<complexType name="NodeDocument">
<sequence>
<element name="name" nillable="true" type="xsd:string"/>
<element name="type" nillable="true" type="xsd:string"/>
<element name="content" nillable="true" type="xsd:anyType"/>
</sequence>
</complexType>
<complexType name="ArrayofDoc">
<complexContent>
<restriction base="soapenc:Array">
<attribute ref="soapenc:arrayType" wsdl:arrayType="tns1:NodeDocument[]"/>
</restriction>
</complexContent>
</complexType>


BTW, I didn't write it, I just implemented it. -- Andy


[EMAIL PROTECTED] 01/07/05 12:26PM >>>


I don't believe there is a way to define this in wsdl so that both .Net and Java(axis) can consume the wsdl. Someone please correct me if I am wrong. My clients just have to understand that certain methods have filles attached. I also allow them to set a request parameter do define whether the attachment should be set to Dime or Mime encoding (the service is Axis).


Raul

-----Original Message-----
From: BLIS Webmaster (Patrick Houbaux)
[mailto:[EMAIL PROTECTED] Sent: Friday, January 07, 2005 1:12 PM
To: [EMAIL PROTECTED] Subject: Re: Best way to send attachments


I have no problem sending attachements to .NET client.

I have a RPC web service (I guess it works for other web service style), and here is the methodologie:

Let's assume you have a web service supposed to send some attachments, the idea is to add the attachment to the SOAP message before the web service method returns on the server side (please note the following is using AXIS 1.1, but it is almost the same with the latest version of AXIS, the AXIS API has changed a bit).

1- get the response message from the message context:
//... org.apache.axis.MessageContext msgContext= org.apache.axis.MessageContext.getCurrentContext();
org.apache.axis.Message rspMsg= msgContext.getResponseMessage();


2 - Set the attachment type to be sent as DIME

rspMsg.getAttachmentsImpl().setSendType(org.apache.axis.attach
ments.Atta
chments.SEND_TYPE_DIME);

3- Let's assume you want to send a file

java.io.File fileToAddAsAttachment = new java.io.File("<the path to your file>");

4- Add the file to attachment of the response message

javax.activation.DataHandler dh=new
javax.activation.DataHandler(new
javax.activation.FileDataSource(fileToAddAsAttachment));
org.apache.axis.attachments.AttachmentPart part = new org.apache.axis.attachments.AttachmentPart(dh);
rspMsg.addAttachmentPart(part);


5- Return your method

The drawback with that is I haven't figured out how to declare (with
java2wsdl) the attachment in the WSDL so you have to document your web service or inform your clients they have to expect some attachments when they call your method.


On the .NET client side, the method is the following:

1- Call the web service method

2- Just after the previous call returned, get the SOAP Response message context
SoapContext rspContext = service.ResponseSoapContext;


3- Get the DIME attachements, loop on them and write in a file what you find there:
DimeAttachmentCollection attachments = rspContext.Attachments;
for (int i=0; i<attachments.Count; i++)
{
Stream str = attachments[i].Stream;
FileStream fs = new FileStream("<the file name where you want to save the attachment>",FileMode.Create,FileAccess.Write);
((MemoryStream)str).WriteTo(fs);
str.Close();
fs.Close();
}


That's all, that works perfectly for me ... hope it helps.

Cheers,
Patrick.



Vy Ho wrote:

All of the reples make no sense whatsover to me.

The original poster makes a very clear question that how to send attachments using soap way that works with many environments. For example, Axis and .Net.

To rephrase this, I would say how to create a Wsdl that works with both axis and .net. Currently, using the DataHandler in


the wsdl (or

generating the wsdl from java code with DataHandler) would not work with other environment. I haven't tried this, but looking at the definition of DataHandler (package name), and its namespace in the wsdl, you can tell it comes from apache, not some Soap standard, unless Apache is the official standard used for attachment.

It's funny to read a bunch of replies that have little


answer value to


the original question.




Reply via email to