Let's try sending binary attachments without using MTOM.
Client code,
Using MTOM,
Using the same wsdl, lets modify it to support 2 more functions. Also when both client and server is generated, select "Enable MTOM".
The wsdl is then modified to add xmime:expectedContentTypes.
Client code,
<!--?xml version="1.0" encoding="UTF-8"?--><wsdl:definitions name="Resource" targetNamespace="http://server.com/Resource/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.com/Resource/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<xsd:schema targetNamespace="http://server.com/Resource/">
<xsd:element name="sendImage">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="0" name="in" type="xsd:base64Binary"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="sendImageResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="0" name="out" type="xsd:base64Binary"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="sendImageRequest">
<wsdl:part element="tns:sendImage" name="parameters"/>
</wsdl:message>
<wsdl:message name="sendImageResponse">
<wsdl:part element="tns:sendImageResponse" name="parameters"/>
</wsdl:message>
<wsdl:portType name="Resource">
<wsdl:operation name="sendImage">
<wsdl:input message="tns:sendImageRequest"/>
<wsdl:output message="tns:sendImageResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ResourceSOAP" type="tns:Resource">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sendImage">
<soap:operation soapAction="http://server.com/Resource/sendImage"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Resource">
<wsdl:port binding="tns:ResourceSOAP" name="ResourceSOAP">
<soap:address location="http://localhost:9089/WSResServer/Resource"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
@javax.jws.WebService (endpointInterface="com.server.Resource", targetNamespace="http://server.com/Resource/", serviceName="Resource", portName="ResourceSOAP", wsdlLocation="WEB-INF/wsdl/Resource.wsdl")
public class ResourceSOAPImpl{
public byte[] sendImage(byte[] in) {
byte[] fileData = null;
System.out.println("WS Server");
System.out.println(new String(in));
try {
File file = new File("");
FileInputStream fileIn = new FileInputStream(file);
fileData = new byte[(int) file.length()];
System.out.println(file.length());
fileIn.read(fileData);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fileData;
}
}
Client code,
System.out.println("Client calling WS");
ResourceSOAPProxy proxy = new ResourceSOAPProxy();
proxy._getDescriptor().setEndpoint("http://localhost:9089/WSResServer/Resource");
byte[] request = "Hello from Client".getBytes();
byte[] result = proxy.sendImage(request);
System.out.println(result);
FileOutputStream fileOut =
new FileOutputStream("C:\\myImg.jpg");
fileOut.write(result);
fileOut.close();
Using MTOM,
Using the same wsdl, lets modify it to support 2 more functions. Also when both client and server is generated, select "Enable MTOM".
The wsdl is then modified to add xmime:expectedContentTypes.
<?xml version="1.0" encoding="UTF-8"?><wsdl:definitions name="Resource" targetNamespace="http://server.com/Resource/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.com/Resource/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xmime="http://www.w3.org/2005/05/xmlmime" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<xsd:schema targetNamespace="http://server.com/Resource/">
<xsd:element name="sendImage">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="0" name="in" type="xsd:base64Binary" xmime:expectedContentTypes="image/jpeg"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="sendImageResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="0" name="out" type="xsd:base64Binary" xmime:expectedContentTypes="image/jpeg"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="sendPDF">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="0" name="in" type="xsd:base64Binary" xmime:expectedContentTypes="*/*">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="sendPDFResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="0" name="out" type="xsd:base64Binary" xmime:expectedContentTypes="*/*">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="sendWord">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="0" name="in" type="xsd:base64Binary">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="sendWordResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="0" name="out" type="xsd:base64Binary">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="sendImageRequest">
<wsdl:part element="tns:sendImage" name="parameters"/>
</wsdl:message>
<wsdl:message name="sendImageResponse">
<wsdl:part element="tns:sendImageResponse" name="parameters"/>
</wsdl:message>
<wsdl:message name="sendPDFRequest">
<wsdl:part element="tns:sendPDF" name="parameters"/>
</wsdl:message>
<wsdl:message name="sendPDFResponse">
<wsdl:part element="tns:sendPDFResponse" name="parameters"/>
</wsdl:message>
<wsdl:message name="sendWordRequest">
<wsdl:part element="tns:sendWord" name="parameters"/>
</wsdl:message>
<wsdl:message name="sendWordResponse">
<wsdl:part element="tns:sendWordResponse" name="parameters"/>
</wsdl:message>
<wsdl:portType name="Resource">
<wsdl:operation name="sendImage">
<wsdl:input message="tns:sendImageRequest"/>
<wsdl:output message="tns:sendImageResponse"/>
</wsdl:operation>
<wsdl:operation name="sendPDF">
<wsdl:input message="tns:sendPDFRequest"/>
<wsdl:output message="tns:sendPDFResponse"/>
</wsdl:operation>
<wsdl:operation name="sendWord">
<wsdl:input message="tns:sendWordRequest"/>
<wsdl:output message="tns:sendWordResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ResourceSOAP" type="tns:Resource">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sendImage">
<soap:operation soapAction="http://server.com/Resource/sendImage"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="sendPDF">
<soap:operation soapAction="http://server.com/Resource/sendPDF"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="sendWord">
<soap:operation soapAction="http://server.com/Resource/sendWord"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Resource">
<wsdl:port binding="tns:ResourceSOAP" name="ResourceSOAP">
<soap:address location="http://localhost:9089/WSRes2Server/Resource"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
@javax.jws.WebService (endpointInterface="com.server.Resource", targetNamespace="http://server.com/Resource/", serviceName="Resource", portName="ResourceSOAP", wsdlLocation="WEB-INF/wsdl/Resource.wsdl")
@javax.xml.ws.BindingType (value=javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_MTOM_BINDING)
public class ResourceSOAPImpl{
public Image sendImage(Image in) {
try {
File file = new File("C:\\Users\\Atechian\\Desktop\\Redbooks_Sample_Code\\7835code\\webservices\\mtom\\BlueHills.jpg");
BufferedImage bi = new BufferedImage(in.getWidth(null),
in.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
g2d.drawImage(in, 0, 0, null);
ImageIO.write(bi, "jpeg", file);
} catch (Exception e) {
e.printStackTrace();
}
return in;
}
public DataHandler sendPDF(DataHandler in) {
try {
FileOutputStream fileOut = new FileOutputStream(
new File("C:\\Users\\Atechian\\Desktop\\Redbooks_Sample_Code\\7835code\\webservices\\mtom\\JAX-WS.pdf"));
BufferedInputStream fileIn = new BufferedInputStream(in.getInputStream());
while (fileIn.available() != 0) {
fileOut.write(fileIn.read());
}
} catch (Exception e) {
e.printStackTrace();
}
return in;
}
public byte[] sendWord(byte[] in) {
try {
FileOutputStream fileOut = new FileOutputStream
(new File("C:\\Users\\Atechian\\Desktop\\Redbooks_Sample_Code\\7835code\\webservices\\mtom\\RAD-intro.doc"));
fileOut.write(in);
} catch (Exception e) {
e.printStackTrace();
}
return in;
}
Client code,
System.out.println("Client calling WS");
ResourceSOAPProxy proxy = new ResourceSOAPProxy();
proxy._getDescriptor().setEndpoint("http://localhost:9089/WSResServer/Resource");
File file = new File("C:\\input.jpg");
BufferedImage bimg = ImageIO.read(file);
Image result = proxy.sendImage(bimg);
System.out.println(result);
BufferedImage bi = (BufferedImage)result;
File f = new File("C:\\myImg.jpg");
ImageIO.write(bi, "jpg", f);
No comments:
Post a Comment