Using WSIF to invoke Web services >

 

Example: Using WSIF to pass SOAP attachments

These example code fragments show you how to use WSIF to pass SOAP messages with attachments.

 

Example

The following code fragment can invoke the service described by the example WSDL in the topic Example: Writing the WSDL extensions for SOAP attachments:

import javax.activation.DataHandler;
. . .
DataHandler dh = new DataHandler(new FileDataSource("myimage.jpg"));
WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
WSIFService service = factory.getService("my.wsdl",null,null,"http://mynamespace","abc");
WSIFOperation op = service.getPort().createOperation("MyOperation");
WSIFMessage in = op.createInputMessage();
in.setObjectPart("attch",dh);
op.executeInputOnlyOperation(in);

The associated type mapping in the DeploymentDescriptor.xml file depends upon your SOAP server. For example if you use Tomcat with SOAP 2.3, then the DeploymentDescriptor.xml file contains the following type mapping:

<isd:mappings>
<isd:map encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
 xmlns:x="http://mynamespace"
 qname="x:datahandler"
 javaType="javax.activation.DataHandler"
 java2XMLClassName="org.apache.soap.encoding.soapenc.MimePartSerializer"
 xml2JavaClassName="org.apache.soap.encoding.soapenc.MimePartSerializer" />
</isd:mappings> 

In this case, the backend service is invoked with the following signature:

public void MyOperation(DataHandler dh);

You can also use stubs to pass attachments into the Web Services Invocation Framework (WSIF):

DataHandler dh = new DataHandler(new FileDataSource("myimage.jpg"));
WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
WSIFService service = factory.getService("my.wsdl",null,null,"http://mynamespace","abc");
MyInterface stub = (MyInterface)service.getStub(MyInterface.class);
stub.MyOperation(dh);

Attachments can also be returned from an operation, but only one attachment can be returned as the return parameter.


Related reference
Example: Writing the WSDL extensions for SOAP attachments SOAP messages with attachments - Working with types and type mappings Passing SOAP messages with attachments using WSIF