package mime;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.StringWriter;
import java.io.IOException;
import javax.xml.rpc.Stub;
import javax.naming.InitialContext;
import java.awt.image.BufferedImage;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.ImageFormatException;
public class Client
{
private static final float JPEG_IMAGE_QUALITY = 1.0F;
public static void main(String[] args) throws Exception
{
| // get the initial context
| InitialContext ctx = new InitialContext();
|
| // lookup the service for Types
| MIMEService svc = (MIMEService)
| ctx.lookup("xmlrpc:soap:mime.MIMEService");
|
| // get the Types stub
| MIMEPortType mimeport = (MIMEPortType) svc.getMIMEPort();
|
| // set the end point address
| if (args.length > 0)
| ((Stub)mimeport)._setProperty(
| "javax.xml.rpc.service.endpoint.address", args[0]);
|
| echoMimeString(mimeport);
| echoMimeImage(mimeport);
| System.exit(0);
}
public static void echoMimeString(MIMEPortType stub)
throws Exception
{
| System.out.println("\n\n********** EchoMimeString **********");
|
| // Open and read the file LICENSE.html
| FileInputStream fis = new FileInputStream("LICENSE.html");
| StringWriter sw = new StringWriter();
| int c;
| while((c = fis.read()) != -1) sw.write(c);
|
| //
| String content = sw.toString();
| System.out.println("sending content********************");
| System.out.println(content);
| System.out.println("********************");
| System.out.println("\n\n");
|
| //
| String result = stub.echoMIMEString(content);
| System.out.println("recieved content********************");
| System.out.println(result);
| System.out.println("********************");
}
public static void echoMimeImage(MIMEPortType stub)
throws Exception
{
| System.out.println("\n\n********** EchoMimeImage **********");
| // read in the image file extend.jpg
| Toolkit tk = Toolkit.getDefaultToolkit();
| Image image = tk.getImage("eXtend.jpg");
|
| // send and receive the image
| System.out.println("sending image********************");
| Image result = stub.echoMIMEImage(image);
| System.out.println("received image********************");
|
| // write the contents of image to file
| FileOutputStream os = new FileOutputStream("echo.jpg");
| BufferedImage bImage = null;
| MediaTracker tracker = new MediaTracker(new Frame());
| try {
| | tracker.addImage(result, 0);
| | tracker.waitForID(0);
| | bImage = new BufferedImage(result.getWidth(null),
| | result.getHeight(null), BufferedImage.TYPE_INT_RGB);
| | Graphics2D g = bImage.createGraphics();
| | g.drawImage(result, 0, 0, null);
| |
| | JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
| | JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bImage);
| | param.setQuality(JPEG_IMAGE_QUALITY, false);
| | encoder.setJPEGEncodeParam(param);
| | encoder.encode(bImage);
| | os.flush();
| | os.close();
| | System.out.println("Wrote image to echo.jpg");
| } catch (InterruptedException ie) {
| | ie.printStackTrace();
| | throw new IOException("Error: while getting image contents - " +
| | ie.getMessage());
| } catch(IOException iex) {
| | iex.printStackTrace();
| | throw new IOException("Error: while writing JPEG image - " +
| | iex.getMessage());
| } catch(ImageFormatException fex) {
| | fex.printStackTrace();
| | throw new IOException("Error: while encoding JPEG image - " +
| | fex.getMessage());
| }
}
}
package mime;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.StringWriter;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.ImageFormatException;
import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.Service;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceFactory;
import com.sssw.jbroker.web.encoding.DefaultTypeMappingRegistry;
public class CallClient
{
private static final float JPEG_IMAGE_QUALITY = 1.0F;
private final static String URI = "urn:MIMEService";
public static void main(String[] args) throws Exception
{
| System.setProperty(ServiceFactory.SERVICEFACTORY_PROPERTY,
| "com.sssw.jbroker.web.xml.rpc.ServiceFactoryDelegate");
|
| // create a Novell exteNd WSSDK service factory
| ServiceFactory factory = ServiceFactory.newInstance();
|
| //create the dynamic service
| QName qname = new QName(URI, "MIMEService");
| Service svc = (Service) factory.createService(qname);
|
| //create the dynamic call
| QName portType = new QName(URI, "MIMEPort");
| Call call = svc.createCall(portType);
|
| QName input = new QName(URI, "echoMIMEImage");
| QName output = new QName(URI, "echoMIMEResponse");
|
| //add input parameter
| call.addParameter("img_in", input, java.awt.Image.class, ParameterMode.IN);
|
| //set return type
| javax.xml.namespace.QName r = new javax.xml.namespace.QName("", "img_out");
| call.setReturnType(r, java.awt.Image.class);
|
| //set call properties
| call.setTargetEndpointAddress(args.length > 0 ? args[0] :
| "http://localhost:9090/mime");
|
| call.setProperty(Call.SOAPACTION_URI_PROPERTY,
| "urn:MIMEService/echoMIMEImage");
|
| call.setProperty(Call.OPERATION_STYLE_PROPERTY, "rpc");
| call.setProperty(Call.ENCODINGSTYLE_URI_PROPERTY,
| "http://schemas.xmlsoap.org/soap/encoding/");
|
| call.setPortTypeName(portType);
| call.setOperationName(input);
|
| //set attachments array property
| call.setProperty(com.sssw.jbroker.web.portable.Stub.ATTACHMENTS,
| new String[] {"image/jpeg", "image/jpeg"});
|
| //create input parameters for attachment
| Toolkit tk = Toolkit.getDefaultToolkit();
| Image image = tk.getImage("eXtend.jpg");
|
| Object[] params = { image };
|
|
| //invoke the operation
| Image result = (Image) call.invoke(params);
|
| System.out.println("writing echoed image to file echo.jpg");
| writeImageToFile(result);
| System.exit(0);
|
}
public static void writeImageToFile(Image result) throws Exception
{
| // write the contents of image to file
| FileOutputStream os = new FileOutputStream("echo.jpg");
| BufferedImage bImage = null;
| MediaTracker tracker = new MediaTracker(new Frame());
| try {
| | tracker.addImage(result, 0);
| | tracker.waitForID(0);
| | bImage = new BufferedImage(result.getWidth(null),
| | result.getHeight(null), BufferedImage.TYPE_INT_RGB);
| | Graphics2D g = bImage.createGraphics();
| | g.drawImage(result, 0, 0, null);
| |
| | JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
| | JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bImage);
| | param.setQuality(JPEG_IMAGE_QUALITY, false);
| | encoder.setJPEGEncodeParam(param);
| | encoder.encode(bImage);
| | os.flush();
| | os.close();
| | System.out.println("Wrote image to echo.jpg");
| } catch (InterruptedException ie) {
| | ie.printStackTrace();
| | throw new IOException("Error: while getting image contents - " +
| | ie.getMessage());
| } catch(IOException iex) {
| | iex.printStackTrace();
| | throw new IOException("Error: while writing JPEG image - " +
| | iex.getMessage());
| } catch(ImageFormatException fex) {
| | fex.printStackTrace();
| | throw new IOException("Error: while encoding JPEG image - " +
| | fex.getMessage());
| }
}
}