package test; import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; 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 java.util.logging.Logger; 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; public class pknotsrgC { private static Logger log = Logger.global; /** * main Method * */ public static void main(String[] args) { try { // server url final String server = "http://bibiwstest.techfak.uni-bielefeld.de"; // Production: http://bibiwsserv.techfak.uni-bielefeld.de // Test: http://bibitest.techfak.uni-bielefeld.de // wsdl file location final URL wsdl = new URL("http://bibiwstest.techfak.uni-bielefeld.de/pknotsRG/axis/pknotsRGPort?wsdl"); // Production: http://bibiserv.techfak.uni-bielefeld.de/wsdl/pknotsRG.wsdl // Test: http://bibiwstest.techfak.uni-bielefeld.de/pknotsRG/axis/pknotsRGPort?wsdl // parameter to hashtable Hashtable paramshash = parseParameter(args); if (!paramshash.containsKey("F")) { System.err.println("java pknotsrgC -F [-strategy (m|f|l)] [-c ] [-n ] [-p ]"); 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 paramshash.remove("F"); log.info("create Service"); // prepare the call (the same for all called methods) Service ser = new Service( wsdl, new QName( server+"/pknotsRG/axis/pknotsRGPort", "pknotsRGImplementationService")); log.info("create call"); Call call = (Call) ser.createCall(new QName("pknotsRGPort"), "request"); log.info("invoke service"); // call and get id System.out.println("*1*"); String id = (String) call.invoke(new Object[] { hashtable2array(paramshash), in }); System.out.println("*2*"); // 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("pknotsRGPort"), "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()); } } /* * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ following * function(s) used by class * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ /** static method converting an InputStream to a string */ private static String get(InputStream in) throws IOException { StringBuffer buf_s = new StringBuffer(); Reader r = new InputStreamReader(new BufferedInputStream(in)); for (int c = r.read(); c != -1; c = r.read()) { buf_s.append((char) c); } in.close(); return buf_s.toString(); } /** static method converting a DOM Object to a String */ private static String DOM2String(Document dom){ StringWriter strWtr = new StringWriter(); StreamResult strResult = new StreamResult(strWtr); TransformerFactory tfac = TransformerFactory.newInstance(); try { 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(dom.getDocumentElement()), strResult); //DOMSource with document's root element } catch (TransformerException e) { System.err.println("toString Error (TransformerException)"+e.toString()); } return strResult.getWriter().toString(); } /** static method parse Inputparameter into a Hashtable */ private static Hashtable parseParameter(String args []){ Hashtable prop = new Hashtable(); prop.put("strategy",new String()); prop.put("c",new Double(0.0)); prop.put("s",new Boolean(true)); prop.put("n",new Double(0.0)); prop.put("k",new Integer(0)); prop.put("p",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 */ 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; } }