I have a problem - i cant find a explanation for the implementation of SOAP with Attachment. I read all the Mail in the Mailinglist but cant find a good explanation. There are always snippets of code. Can we solve this Problem all the time?
So I will tell my solution and hope some clever programmers will correct it and explain the right way to do it.
The point is: We want to send multiple attachments from Service to client side. The attachments can become a size of more then 100 MB. The attachments come from a Database.
Test: Therefore we send the path of a file to the Service, get the file and send it back to the Client (for test reasons). So we will generate
an Interface like this: (for multiple attachments = DataHandler[])??? ********************************************************************** public interface AttachmentService {
public DataHandler getFileAsAttachment(String filename); } **********************************************************************
This Interface will return a DataHandler (only one attachment!). --> I dont know if this is the right solution (DataHandler?)???
We use Java2WSDL to generate the WSDL from the Interface!
The WSDL looks like this:
**********************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="services:AttachmentService" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="services:AttachmentService" xmlns:intf="services:AttachmentService" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:message name="getFileAsAttachmentResponse">
<wsdl:part name="getFileAsAttachmentReturn" type="apachesoap:DataHandler"/>
</wsdl:message>
<wsdl:message name="getFileAsAttachmentRequest">
<wsdl:part name="in0" type="xsd:string"/>
</wsdl:message><wsdl:portType name="AttachmentService">
<wsdl:operation name="getFileAsAttachment" parameterOrder="in0">
<wsdl:input message="impl:getFileAsAttachmentRequest" name="getFileAsAttachmentRequest"/>
<wsdl:output message="impl:getFileAsAttachmentResponse" name="getFileAsAttachmentResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="AttachmentService_ServiceSoapBinding" type="impl:AttachmentService">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getFileAsAttachment">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="getFileAsAttachmentRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="services:AttachmentService" use="encoded"/>
</wsdl:input>
<wsdl:output name="getFileAsAttachmentResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="services:AttachmentService" use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="AttachmentServiceService">
<wsdl:port binding="impl:AttachmentService_ServiceSoapBinding" name="AttachmentService_Service">
<wsdlsoap:address location="http://localhost:8080/axis/services/AttachmentService_Service"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions> **********************************************************************
So my problem is, i think there is something missing in this WSDL!? I dont see anything about "MIME" and so on!!! Can someone tell me whats wrong here????
Now we use WSDL to Java to generate the Classes (Stub, Skeleton, and so on ...)
Then we deploy the Service with the genareted deploy.wsdd from the WSDL2Java
Looks like this:
**********************************************************************
<deployment
xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<!-- Services from AttachmentServiceService WSDL service -->
<service name="AttachmentService_Service" provider="java:RPC" style="rpc" use="encoded">
<parameter name="wsdlTargetNamespace" value="services:AttachmentService"/>
<parameter name="wsdlServiceElement" value="AttachmentServiceService"/>
<parameter name="wsdlServicePort" value="AttachmentService_Service"/>
<parameter name="className" value="webService.attachment.AttachmentService_wsdl.AttachmentService_ServiceSoapBindingSkeleton"/>
<parameter name="wsdlPortType" value="AttachmentService"/>
<parameter name="allowedMethods" value="*"/>
<parameter name="scope" value="Application"/>
</service> </deployment> **********************************************************************
and Implement the ServiceImpl, like this:
**********************************************************************
public javax.activation.DataHandler getFileAsAttachment(String filename) throws java.rmi.RemoteException {
DataHandler dataHandler = null;
try{
FileDataSource dataSource = new FileDataSource(filename);
dataHandler = new DataHandler(dataSource);
}catch(Exception e){}
return dataHandler;
}
**********************************************************************we start to call the Service from our test client, looks like this:
**********************************************************************
public static void main(String[] args) {
String filename = "H:\\Tomcat\\webapps\\axis\\WEB-INF\\web.xml";
String path = "H:\\Tomcat\\webapps\\axis\\WEB-INF\\web1.xml";
try {
/* Verbindung zum Web Service aufbauen
* => �ber den ZeitansageServiceLocator
*/
AttachmentServiceLocator serviceLocator =
new AttachmentServiceLocator ();
AttachmentService service =
(AttachmentService)
serviceLocator.getAttachmentService_Service ();
/* Casten auf den von WSDL2Java generierten
* Stub:
*/
AttachmentService_ServiceSoapBindingStub soapService =
(AttachmentService_ServiceSoapBindingStub) service;
DataHandler data = soapService.getFileAsAttachment(filename);
OutputStream outstream = data.getOutputStream();
ByteArrayOutputStream outBytes
=(ByteArrayOutputStream)outstream;
FileOutputStream out = new FileOutputStream(path);
out.write(outBytes.toByteArray(), 0, outBytes.size());
} catch ( Exception e ) {
System.out.println (
"Es ist folgender Fehler aufgetreten: " + e
);
}
}
**********************************************************************The SOAP message look nice but i think not right for a attachment!
The mesage looks like this - Request:
**********************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:getFileAsAttachment
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="services:AttachmentService">
<in0 xsi:type="xsd:string">H:\Tomcat\webapps\axis\WEB-INF\web.xml</in0>
</ns1:getFileAsAttachment>
</soapenv:Body>
</soapenv:Envelope>
**********************************************************************
And the Response like this:
**********************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:getFileAsAttachmentResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:ns1="services:AttachmentService">
<getFileAsAttachmentReturn href="cid:9BE1C0F08C13A905FB7123C8BB0458FF"
xmlns:ns2="http://xml.apache.org/xml-soap"/>
</ns1:getFileAsAttachmentResponse>
</soapenv:Body>
</soapenv:Envelope>
------=_Part_1_31361704.1075294386156
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-Id: <9BE1C0F08C13A905FB7123C8BB0458FF>
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
<web-app>
<display-name>Apache-Axis</display-name>
<servlet>
<servlet-name>AxisServlet</servlet-name>
<display-name>Apache-Axis Servlet</display-name>
<servlet-class>
org.apache.axis.transport.http.AxisServlet
</servlet-class>
</servlet>
....
</web-app>------=_Part_1_31361704.1075294386156-- **********************************************************************
Is it the right way we solve the Problem, or there other ways - better ways???
I cant get something out the DataHandler of the client side!? why?
How can i use MIME as the Protocol for sending my attachment?
