/* Rajarshi Guha * 03/03/2006 * * * Based on code written by Marty Hall */ import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; import org.apache.axis.AxisFault; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import org.apache.axis.encoding.Base64; import java.util.Vector; import java.util.Iterator; import java.io.*; public class CDKsdgClient { private Call call; private QName qn; private boolean requestError; private String requestErrorMessage; // Initialize the Axis setup public CDKsdgClient(String host) { String endpoint = "http://localhost:8080/axis/services/CDKsdg"; if (host != null) { endpoint = host; } try { Service service = new Service(); call = (Call) service.createCall(); call.setTargetEndpointAddress( new java.net.URL(endpoint) ); } catch (Throwable t) { handleError("CDKsdgClient (constructor) had an Exception.",t); } } // Return true in an error was found public boolean error() { return requestError; } // Return the error message public String getErrorText() { String msg; if (requestError) { msg = requestErrorMessage; } else { msg = ""; } return msg; } public String getsdg(String smiles, int width, int height, double scale) { try { call.removeAllParameters(); call.setOperationName( "getSDG" ); call.addParameter( "smiles", XMLType.XSD_STRING, ParameterMode.IN); call.addParameter( "width", XMLType.XSD_INTEGER, ParameterMode.IN); call.addParameter( "height", XMLType.XSD_INTEGER, ParameterMode.IN); call.addParameter( "scale", XMLType.XSD_DOUBLE, ParameterMode.IN); call.setReturnType( XMLType.XSD_STRING ); String ret = (String)call.invoke( new Object[] { smiles, new Integer(width), new Integer(height), new Double(scale)}); if (ret == null) { System.out.println("Error in SOAP call"); } System.out.println("CDKsdgClient got result : "); return ret; } catch (Throwable t) { handleError("CDKsdgClient (getSDG) had an Exception.",t); return (""); } } // Set error flags, print stack trace public void handleError(String text, Throwable t) { requestError = true; requestErrorMessage = t.toString(); System.out.println(text); System.out.println(t); t.printStackTrace(); return; } // Test program public static void main(String [] args) { if (args.length < 2) { System.out.println("Usage: java CDKsdgClient SMILES [host]"); System.out.println("\nSMILES should be valid SMILES strings"); System.out.println("If host is not specified then http://localhost:8080/axis/services/CDKsdg is used\n"); System.exit(0); } String host = null; if (args.length == 2) { host = args[1]; } CDKsdgClient sr = new CDKsdgClient(host); String pic = sr.getsdg(args[0], 0,0,0); Base64 decoder = new Base64(); byte[] bytes = decoder.decode(pic); try { FileOutputStream fos = new FileOutputStream("img.jpg"); fos.write(bytes); fos.close(); } catch (Exception e) { e.printStackTrace(); } } }