import java.io.File; import java.io.IOException; import java.io.StringWriter; 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.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.rpc.ServiceException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.apache.axis.AxisFault; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException; /** * Example webservice client using Dialign webservice. This client use the * request/response methods dealing with sequenceML as input and alignmentML as * output. * * @author Jan Krueger * */ public class DialignWSC { /** * @param args */ public static void main(String[] args) { try { /* declare addresslocation for service */ final String server = "bibiwsserv.techfak.uni-bielefeld.de"; //final String server = "bibiwstest.techfak.uni-bielefeld.de"; /* declare where to find the describing WSDL */ final URL wsdl = new URL("http://bibiserv.techfak.uni-bielefeld.de/wsdl/DIALIGN.wsdl"); //final URL wsdl = new URL("http://bibitest.techfak.uni-bielefeld.de/wsdl/DIALIGN.wsdl"); /* parameter to hashtable */ Hashtable paramshash = parseParameter(args); /* check for file parameter */ if (!paramshash.containsKey("F")) { System.err.println("java "+DialignWSC.class.getSimpleName()+" -F \n"+ "\t[-sequenceMode (n|nt)]\n"+ "\t[-threshold 0 <= int <= 10]\n"+ "\t[-stars 0 <= int <= 15"); System.exit(0); } /* read Document from File */ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder docbuilder = factory.newDocumentBuilder(); Document in = docbuilder.parse(new File((String) paramshash.get("F"))); /* remove file parameter */ paramshash.remove("F"); /* prepare the call (the same for all called methods) */ Service ser = new Service(wsdl, new QName("http://" + server + "/DIALIGN/axis/DIALIGNPort", "DIALIGNImplementationService")); Call call = (Call) ser.createCall(new QName("DIALIGNPort"), "request"); /* call and get id */ String id = (String) call.invoke( new Object[] { in, hashtable2array(paramshash)} ); /* 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("DIALIGNPort"), "response"); // call and get result as DOM Tree(if finished) Document result = (Document) call.invoke(new Object[] { id }); // convert Dom Document to String StreamResult strResult = new StreamResult(new StringWriter()); TransformerFactory tfac = TransformerFactory.newInstance(); Transformer t = tfac.newTransformer(); t.setOutputProperty("encoding", "iso-8859-1"); t.setOutputProperty("indent", "yes"); t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); t.transform(new DOMSource(result.getDocumentElement()), strResult); // and print it to STDOUT System.out.println(strResult.getWriter().toString()); // 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 + ")"); } catch (TransformerException e) { System.err.println("Document can't tranformed into a String "); } } /* 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.err.println("ws remote error (" + e.toString() + ")"); */ 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); } catch (ParserConfigurationException e) { System.err.println(e.toString()); } catch (SAXException e) { System.err.println(e.toString()); } } /** * 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("sequenceMode", new String()); prop.put("threshold", new Integer(0)); prop.put("stars", new Integer(0)); prop.put("F", new String()); Hashtable ret = new Hashtable(); String key = ""; for (int i = 0; i < args.length; ++i) { String current = args[i]; /* found key */ if (current.startsWith("-")) { /* 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)); } } /* remove -- */ key = current.replaceAll("-", ""); } 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; } }