Home

 

Implementing the JavaBean skeleton

Before we test the sample JSP client, we have to implement the generated Java Bean skeleton. The Web services stores the received document on the local hard drive, then passes the same document back to the client. Do these steps:

Examine the generated skeleton class ProcessDocumentPortBindingImpl. We can see that sendWordFile is mapped to byte[], sendPDFFile is mapped to javax.activation.DataHandler, and sendImage is mapped to java.awt.Image, as we expected.

Copy/paste the code into ProcessDocumentPortImpl.java from C:\7672code\webservice\mtom (Example | 8-23).

Example 18-23 ProcessDocumentPortBindingImpl.java

package com.ibm.rad75.mtom;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.activation.DataHandler;
import javax.imageio.ImageIO;
@javax.jws.WebService
	(endpointInterface="com.ibm.rad75.mtom.ProcessDocumentDelegate",
	targetNamespace="http://mtom.rad75.ibm.com/",
	serviceName="ProcessDocumentService", portName="ProcessDocumentPort")
@javax.xml.ws.BindingType
		(value=javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_MTOM_BINDING)
public class ProcessDocumentPortBindingImpl{
	public byte[] sendWordFile(byte[] arg0) {
		try {
			FileOutputStream fileOut = new FileOutputStream
				(new File("C:/7672code/webservices/mtomresult/RAD-intro.doc"));
			fileOut.write(arg0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return arg0;
	}
	public Image sendImage(Image arg0) {
		try {
			File file = new File
				("C:/7672code/webservices/mntomresult/BlueHills.jpg");
			BufferedImage bi = new BufferedImage(arg0.getWidth(null), 
								arg0.getHeight(null), BufferedImage.TYPE_INT_RGB);
			Graphics2D g2d = bi.createGraphics();
			g2d.drawImage(arg0, 0, 0, null);
			ImageIO.write(bi, "jpeg", file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return arg0;
	}
	public DataHandler sendPDFFile(DataHandler arg0) {
		try {
			FileOutputStream fileOut = new FileOutputStream(new File(
				"C:/7672code/webservices/mtoresult/JAX-WS.pdf"));
			BufferedInputStream fileIn = new BufferedInputStream
														(arg0.getInputStream());
			while (fileIn.available() != 0) {
				fileOut.write(fileIn.read());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return arg0;
	}
}

Examine the code listed in Example | 8-23.

The sendWordFile method takes a byte[] as input and stores the binary data as C:/7672code/webservices/mtomresult/RAD-intro.doc.
The sendImage method takes an image as input and stores the binary data as C:/7672code/WebServices/mtomresult/BlueHills.jpg.
The sendPDFFile method takes a DataHandler as input and stores the data in C:/7672code/WebServices/mtomresult/JAX-WS.pdf.
All the three methods return the received data to the client after storing it on the local drive.
ibm.com/redbooks