import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.rmi.RemoteException; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceException; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import org.apache.axis.AxisFault; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.xerces.dom.DOMImplementationImpl; import org.w3c.dom.*; import org.xml.sax.*; import java.io.*; import java.util.*; /** * @author Kai Loewenthal */ public class RNAshapesCRequestResponse { /** main method */ public static void main(String[] args) { if(args.length < 1){ System.err.println("You have to specify a SequenceML-file as argument!\n Usage : java RNAshapesCRequestResponse "); System.exit(0); } try { /* wsdl location of rnahybrid ws */ final URL wsdl = new URL( "http://bibiserv.techfak.uni-bielefeld.de/wsdl/RNAshapes.wsdl"); /* Creating DOM from the XML (ARGS[0]) */ System.out.println("Opening xml file"); File file = new File( args[0] ); InputSource src = new InputSource( new FileInputStream( file ) ); System.out.println("Building DOM from xml"); org.apache.xerces.parsers.DOMParser prsr = new org.apache.xerces.parsers.DOMParser(); prsr.parse( src ); Document sequenceDoc = prsr.getDocument(); /* sample parameter array */ final Object[] parameter = { "mode", "a", "shape", new Integer(5), "energy_mode", "e", "energy", new Double(2.3)}; /* statuscode */ int statuscode; /* statusdescription */ String statusdescription; /* contains result of request */ String result; /* * create a new Service Object */ Service service = new Service( wsdl, new QName( "http://bibiwsserv.techfak.uni-bielefeld.de/RNAshapes/axis/RNAshapesPort", "RNAshapesImplementationService")); /* create a Call object using method "request" */ Call call = (Call) service.createCall(new QName("RNAshapesPort"), "request"); /* call and get id */ String id = (String) call.invoke(new Object[] { null, sequenceDoc, parameter }); /* print id to STDERR - for test purpose only */ System.err.println("get ID :: " + id); /* set statuscode and description to 601,submitted */ statuscode = 601; statusdescription = "submitted"; /* create a Call object using method "response" */ call = (Call) service.createCall(new QName("RNAshapesPort"), "response"); /* * implement a simple polling to get result of request; A statuscode * between 600 and 700 means that the job is still calculating; and * we have to try again */ while ((statuscode > 600) && (statuscode < 700)) { try { // print result to STDOUT Document out = (Document) call.invoke(new Object[] { id }); ///////////////// //Output the XML //set up a transformer TransformerFactory transfac = TransformerFactory .newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); //create string from xml tree StringWriter sw = new StringWriter(); StreamResult res = new StreamResult(sw); DOMSource source = new DOMSource(out); trans.transform(source, res); String xmlString = sw.toString(); //print xml System.out.println("Here's the xml:\n\n" + xmlString); // set statuscode and description to finished statuscode = 600; statusdescription = "finished ok"; } catch (RemoteException e) { Object[] tmp = parseFault((AxisFault) e); statuscode = ((Integer) tmp[0]).intValue(); statusdescription = (String) tmp[1]; // print a dot to STDERR and wait five seconds System.err.println(statuscode + "(" + statusdescription + ") polling..."); Thread.sleep(5000); } catch (Exception allother) { allother.printStackTrace(); } } /* * In the case an error occurred (statuscode >= 700) print * statuscode and description to STDERR */ if (statuscode >= 700) { System.err.println("An error occurred calling RNAshapes WebService :"); System.err.println("StatusCode :" + statuscode); System.err.println("Description :" + statusdescription); } } catch (MalformedURLException e) { System.err.println("A malformed URL Exception occurred:\n" + e.getMessage()); } catch (ServiceException e) { System.err.println("A Service Exception occurred:\n" + e.getMessage()); } catch (RemoteException e) { Object[] tmp = parseFault((AxisFault) e); System.err .println("An error occurred calling RNAshapes WebService :"); System.err.println("StatusCode :" + ((Integer) tmp[0])); System.err.println("Description :" + (String) tmp[1]); } catch (InterruptedException e) { System.err.println("A Interrupted Exception occurred:\n" + e.getMessage()); } catch (Exception e) { System.err.println("Some other exception occured:\n" + e.getMessage()); } } /** method, that provides parsing AxisFault */ public static Object[] parseFault(AxisFault e) { Element root = e.lookupFaultDetail(new QName( "http://hobit.sourceforge.net/xsds/hobitStatuscode.xsd", "hobitStatuscode")); if (root == null) { System.err.println("WebService remote error \n\nmessage:\n" + e.getMessage() + "\n\n"); System.exit(1); } String description = root.getLastChild().getFirstChild().getNodeValue(); Integer statuscode = new Integer(Integer.parseInt(root.getFirstChild() .getFirstChild().getNodeValue())); return new Object[] { statuscode, description }; } }