import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.rmi.RemoteException; import java.util.Hashtable; import java.util.Iterator; import java.util.Set; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceException; import org.apache.axis.AxisFault; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.w3c.dom.Element; /** * Example webservice client using RNAfold webservice. This client use the * request_orig/response_orig methods dealing with FASTA as input and * dotbracket as output. * * @author Jan Krueger * */ public class RNAfoldCOrig { /** * main method * * @param args */ public static void main(String[] args) { try { /* declare addresslocation for service */ final String server = "http://bibiwsserv.techfak.uni-bielefeld.de"; /* declare where to find the describing WSDL */ final URL wsdl = new URL("http://bibiserv.techfak.uni-bielefeld.de/wsdl/RNAfold.wsdl"); /* parameter to hashtable */ Hashtable paramshash = parseParameter(args); if (!paramshash.containsKey("F")) { System.err.println("java RNAfoldCOrig -F [-T ] \n"); System.exit(0); } /* read sequence from file */ File infile = new File((String) paramshash.get("F")); BufferedReader reader = new BufferedReader(new FileReader(infile)); String line; StringBuffer sequence = new StringBuffer(); while ((line = reader.readLine()) != null) { sequence.append(line + "\n"); } reader.close(); /* remove File parameter */ paramshash.remove("F"); /* prepare the call (the same for all called methods) */ Service ser = new Service(wsdl, new QName( server+"/RNAfold/axis/RNAfoldPort", "RNAfoldImplementationService")); Call call = (Call) ser.createCall(new QName("RNAfoldPort"), "request_orig"); /* call and get id */ String id = (String) call.invoke(new Object[] {hashtable2array(paramshash), sequence.toString() }); /* print id on STDOUT */ System.err.println("get id - '" + id + "'"); int statuscode = 601; while ((statuscode > 600) && (statuscode < 700)) { try { Thread.sleep(2500); call = (Call) ser.createCall(new QName("RNAfoldPort"), "response_orig"); // call and get result as DOM Tree(if finished) String result = (String) call.invoke(new Object[] { id }); // and print it to STDOUT System.out.println(result); // finished statuscode = 600; } catch (InterruptedException e) { System.err.println("process can't sleep!"); } catch (RemoteException e) { // on error WS will throw a soapfault as hobitstatuscode Element root = ((AxisFault) e).lookupFaultDetail(new QName( "http://hobit.sourceforge.net/xsds/hobitStatuscode.xsd", "hobitStatuscode")); if (root == null) { System.err.println("ws remote error (no Hobitstatuscode): " + e.toString()); System.exit(1); } String description = root.getLastChild().getFirstChild().getNodeValue(); statuscode = Integer.parseInt(root.getFirstChild().getFirstChild().getNodeValue()); // print Statusinformation to STDERR System.err.println("(" + statuscode + " - " + description + ")"); } } /* error handling with proper information for the user */ } catch (RemoteException e) { /* on error WS will throw a soapfault as hobitstatuscode */ Element root = ((AxisFault) e).lookupFaultDetail(new QName( "http://hobit.sourceforge.net/xsds/hobitStatuscode.xsd", "hobitStatuscode")); if (root == null) { System.err.println("ws remote error (no Hobitstatuscode): " + e.toString()); System.exit(1); } String description = root.getLastChild().getFirstChild().getNodeValue(); String code = root.getFirstChild().getFirstChild().getNodeValue(); System.out.println("Statuscode: " + code); System.out.println("Description: " + description); /* * Using this kind of Webservice there is only one one field for * giving back a error message. When an axception occours, the * client side of Axis will throw an RemoteException which includes * the class name of the thrown exception. There is no way to get * more information like the original stacktrace !!! */ System.exit(1); } catch (MalformedURLException e) { System.err.println("failed (" + e.toString() + ")"); System.exit(1); } catch (ServiceException e) { System.err.println("Service unavailable (" + e.toString() + ")"); System.exit(1); } catch (IOException e) { System.err.println("can't read sequence file " + args[0]); System.exit(1); } } /* * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ following * function(s) used by class * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ /** * static method parse inputparameter into a Hashtable * * @param String [] - * argument list from cmdline * * @return a Hashtable containing all allowed arguments in a key/value style */ private static Hashtable parseParameter(String args[]) { Hashtable prop = new Hashtable(); prop.put("F", new String()); prop.put("T", new Double(0.0)); Hashtable ret = new Hashtable(); String key = ""; for (int i = 0; i < args.length; ++i) { String current = args[i]; // found key if (current.startsWith("-")) { // remove -- key = current.replaceAll("-", ""); // check - maybe boolean if ((key != null) && (prop.get(key) != null)) { Class c = (prop.get(key)).getClass(); if ((c.getName()).equals("java.lang.Boolean")) { ret.put(key, new Boolean(true)); } } } else { /* found value check if current key exists in prop */ if (prop.get(key) != null) { /* get Class of value */ Class c = (prop.get(key)).getClass(); if ((c.getName()).equals("java.lang.Integer")) { ret.put(key, new Integer(Integer.parseInt(current))); } else if ((c.getName()).equals("java.lang.String")) { ret.put(key, current); } else if ((c.getName()).equals("java.lang.Double")) { ret.put(key, new Double(Double.parseDouble(current))); } } key = null; } } return ret; } /** * convert a Hashtable to an ObjectArray * * @param - * Hashtable * * @return - Objectarray of the Hashtable parameter * */ private static Object[] hashtable2array(Hashtable ht) { Set keys = ht.keySet(); Object ret[] = new Object[keys.size() * 2]; int counter = 0; for (Iterator i = keys.iterator(); i.hasNext();) { Object key = i.next(); ret[counter] = key; counter++; ret[counter] = ht.get(key); counter++; } return ret; } }