[MOBY-guts] biomoby commit
Paul Gordon
gordonp at dev.open-bio.org
Tue Jun 9 19:08:32 UTC 2009
gordonp
Tue Jun 9 15:08:32 EDT 2009
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/client
In directory dev.open-bio.org:/tmp/cvs-serv20107/src/main/org/biomoby/client
Modified Files:
MobyRequest.java
Log Message:
Removed Apache library dependencies
moby-live/Java/src/main/org/biomoby/client MobyRequest.java,1.42,1.43
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/MobyRequest.java,v
retrieving revision 1.42
retrieving revision 1.43
diff -u -r1.42 -r1.43
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/MobyRequest.java 2008/12/03 15:21:13 1.42
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/MobyRequest.java 2009/06/09 19:08:32 1.43
@@ -1,6 +1,8 @@
package org.biomoby.client;
import java.io.*;
+import java.net.URL;
+import java.net.HttpURLConnection;
import java.util.*;
import javax.xml.namespace.QName;
@@ -14,10 +16,12 @@
import org.apache.axis.message.MessageElement;
import org.apache.xml.utils.PrefixResolver;
import org.apache.xml.utils.PrefixResolverDefault;
-import org.apache.xpath.XPath;
-import org.apache.xpath.XPathContext;
-import org.apache.xpath.objects.XNodeSet;
-import org.apache.xpath.objects.XObject;
+
+// import org.apache.xpath.XPath;
+// import org.apache.xpath.XPathContext;
+// import org.apache.xpath.objects.XNodeSet;
+// import org.apache.xpath.objects.XObject;
+import javax.xml.xpath.*;
import org.biomoby.shared.*;
import org.biomoby.shared.data.*;
@@ -26,6 +30,7 @@
import org.omg.lsae.notifications.AnalysisEvent;
import org.w3c.dom.*;
+import ca.ucalgary.services.util.IOUtils;
/**
* This class handles the WSDL transaction to request a response
@@ -57,13 +62,10 @@
protected Class stringType;
protected static boolean debug = false;
protected PrintStream debugPS = System.err;
- protected XPathContext xpath_context;
protected String responseString = null;
- private XPath stringEncodedXPath;
- private XPath base64EncodedXPath;
- private XPath simpleChildXPath;
- private XPath collectionChildXPath;
+ private XPathExpression stringEncodedXPath;
+ private XPathExpression base64EncodedXPath;
private String user;
private String password;
@@ -96,32 +98,25 @@
debugPS.println("WARNING: Something is very wrong, could not find Class definition of String: " + classe);
}
- xpath_context = new XPathContext();
- mobyPrefixResolver = new MobyPrefixResolver();
-
// Now compile the XPath statements that will be used fetch data from the server response
try{
- base64EncodedXPath = new XPath("//*[starts-with(substring-after(@"+
- MobyPrefixResolver.XSI1999_PREFIX+
- ":type, ':'), \"base64\") or starts-with(substring-after(@"+
- MobyPrefixResolver.XSI2001_PREFIX+
- ":type, ':'), \"base64\")]", null,
- mobyPrefixResolver, XPath.SELECT);
- stringEncodedXPath = new XPath("//*[substring-after(@"+
- MobyPrefixResolver.XSI1999_PREFIX+
- ":type, ':')=\"string\" or substring-after(@"+
- MobyPrefixResolver.XSI2001_PREFIX+
- ":type, ':')=\"string\"] | //"+
- MobyPrefixResolver.SOAP_ENC_PREFIX+":string", null,
- mobyPrefixResolver, XPath.SELECT);
- simpleChildXPath = new XPath("moby:Simple | Simple", null,
- mobyPrefixResolver, XPath.SELECT);
- collectionChildXPath = new XPath("moby:Collection | Collection", null,
- mobyPrefixResolver, XPath.SELECT);
+ XPath xPath = (new org.apache.xpath.jaxp.XPathFactoryImpl()).newXPath();
+ xPath.setNamespaceContext(new NamespaceContextImpl());
+ base64EncodedXPath = xPath.compile("//*[starts-with(substring-after(@"+
+ MobyPrefixResolver.XSI1999_PREFIX+
+ ":type, ':'), \"base64\") or starts-with(substring-after(@"+
+ MobyPrefixResolver.XSI2001_PREFIX+
+ ":type, ':'), \"base64\")]");
+ stringEncodedXPath = xPath.compile("//*[substring-after(@"+
+ MobyPrefixResolver.XSI1999_PREFIX+
+ ":type, ':')=\"string\" or substring-after(@"+
+ MobyPrefixResolver.XSI2001_PREFIX+
+ ":type, ':')=\"string\"] | //"+
+ MobyPrefixResolver.SOAP_ENC_PREFIX+":string");
}
- catch(TransformerException te){
+ catch(XPathExpressionException xpee){
debugPS.println("Syntax error encountered while compiling XPath " +
- "statements for internal use (code bug?): " + te);
+ "statements for internal use (code bug?): " + xpee);
}
setDebugMode(System.getProperty("moby.debug") != null);
}
@@ -325,12 +320,13 @@
}
else{
String mobyXML = convertMOBYDataToMOBYRequest(inData);
- Call call = getServiceFromWSDL();
- if(user != null && password != null) {
- call.setProperty( Call.USERNAME_PROPERTY, user );
- call.setProperty( Call.PASSWORD_PROPERTY, password );
- }
- mobyDOM = performSOAPRequest(call, mobyXML, contentsXML);
+//PG Call call = getServiceFromWSDL();
+//PG if(user != null && password != null) {
+//PG call.setProperty( Call.USERNAME_PROPERTY, user );
+//PG call.setProperty( Call.PASSWORD_PROPERTY, password );
+//PG }
+//PG mobyDOM = performSOAPRequest(call, mobyXML, contentsXML);
+ mobyDOM = performSOAPRequest(new URL(mobyService.getURL()), mobyService.getName(), mobyXML, contentsXML);
// The following parses the DOM and extracts all the appropriate jMOBY objects to represent the XML in Java
return MobyDataUtils.fromXMLDocument(mobyDOM, mobyService.getServiceType().getRegistry());
}
@@ -486,7 +482,7 @@
* (or uses an internally cached copy from a previous invocation), and sets the variables for the
* SOAP call appropriately so you can consequently call performSOAPRequest.
*/
- protected Call getServiceFromWSDL() throws MobyException, NoSuccessException{
+//PG protected Call getServiceFromWSDL() throws MobyException, NoSuccessException{
// String wsdl = null;
// // Since this is how we retrieve a service from Central, use the same values as the key to the hash
@@ -509,8 +505,8 @@
// lastWsdlCacheKey = wsdlCacheKey; // Keep track of the last invocation
// Get ready to do SOAP
- return setCallFromWSDL(null);
- }
+//PG return setCallFromWSDL(null);
+// }
/**
* Creates the SOAP Call that will be invoked later. This should be based on the WSDL document
@@ -544,32 +540,43 @@
/**
* Calls the invoke() method of the JAX-RPC Call interface.
*/
- protected Element performSOAPRequest(Call soapCall, String mobyInputXML, StringBuffer contentsXMLOutput) throws SOAPException{
+//PG protected Element performSOAPRequest(Call soapCall, String mobyInputXML, StringBuffer contentsXMLOutput) throws SOAPException{
+ protected Element performSOAPRequest(URL endpoint, String method, String mobyInputXML, StringBuffer contentsXMLOutput) throws Exception{
// First, turn the input objects into a MOBY XML request
- String[] mobyXMLInputData = new String[1];
+//PG String[] mobyXMLInputData = new String[1];
//Setup
- mobyXMLInputData[0] = mobyInputXML;
+//PG mobyXMLInputData[0] = mobyInputXML;
- if(debug)
- debugPS.println("returnType just before invoke call is " + soapCall.getReturnType());
- Object returnedObject = null;
- try{
- returnedObject = soapCall.invoke(new QName("http://biomoby.org/",
- mobyService.getName()), mobyXMLInputData);
- }
- catch(Exception e){
- e.printStackTrace();
- //System.err.println("Input: "+mobyInputXML);
- throw new SOAPException("While invoking SOAP Call: " + e);
- }
-
- try{
- if(debug){
- debugPS.println("SOAP Response was:\n");
- debugPS.println(soapCall.getResponseMessage().getSOAPPart().getEnvelope());
- }
- Element resultDom = ((MessageElement) soapCall.getResponseMessage().getSOAPPart().getEnvelope()).getAsDOM();
+ String soapAction = "http://biomoby.org/#" + method;
+ HttpURLConnection conn = (HttpURLConnection) endpoint.openConnection();
+ byte[] payload = ("<ns1:"+method+
+ " soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://biomoby.org/\">"+
+ (mobyInputXML == null ? "" : "<ns1:arg0 xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">"+mobyInputXML.replaceAll("<","<").replaceAll(">",">")+"</ns1:arg0>")+
+ "</ns1:"+method+">").getBytes();
+ IOUtils.writeToConnection(conn, payload, soapAction);
+ Node resultSource = IOUtils.readFromConnection(conn);
+
+//PG if(debug)
+//PG debugPS.println("returnType just before invoke call is " + soapCall.getReturnType());
+//PG Object returnedObject = null;
+//PG try{
+//PG returnedObject = soapCall.invoke(new QName("http://biomoby.org/",
+//PG mobyService.getName()), mobyXMLInputData);
+//PG }
+//PG catch(Exception e){
+//PG e.printStackTrace();
+//PG //System.err.println("Input: "+mobyInputXML);
+//PG throw new SOAPException("While invoking SOAP Call: " + e);
+//PG }
+
+ try{
+//PG if(debug){
+//PG debugPS.println("SOAP Response was:\n");
+//PG debugPS.println(soapCall.getResponseMessage().getSOAPPart().getEnvelope());
+//PG }
+//PG Element resultDom = ((MessageElement) soapCall.getResponseMessage().getSOAPPart().getEnvelope()).getAsDOM();
+ Element resultDom = resultSource instanceof Element ? (Element) resultSource : resultSource.getOwnerDocument().getDocumentElement();
return decodeSOAPMessage(resultDom, contentsXMLOutput, mobyInputXML);
} catch(Exception e){
e.printStackTrace();
@@ -598,36 +605,35 @@
}
NodeList node_list = null;
- XPath responseElementXPath = null;
- try{
- if(async){
- responseElementXPath = new XPath("//"+MobyPrefixResolver.WSRP_PREFIX +
- ":"+AsyncClient.WSRP_MULTI_PROPERTY_TAG_NAME+"Response",
- null, mobyPrefixResolver, XPath.SELECT);
- }
- else{
- responseElementXPath = new XPath("//"+ MobyPrefixResolver.MOBY_TRANSPORT_PREFIX+
- ":"+mobyService.getName()+"Response | //" +
- mobyService.getName()+"Response | " +
- "//"+ MobyPrefixResolver.MOBY_TRANSPORT_PREFIX+
- ":"+mobyService.getName() + " | //" +
- mobyService.getName(),
- null, mobyPrefixResolver, XPath.SELECT);
- }
- }catch(TransformerException te){
- throw new SOAPException("Cannot select SOAP nodes due to exception "+
- "while compiling XPath statement (code bug?):" +te);
+ if(async){
+ node_list = n.getElementsByTagNameNS(MobyPrefixResolver.WSRP_NAMESPACE,
+ AsyncClient.WSRP_MULTI_PROPERTY_TAG_NAME+"Response");
}
- try{
- node_list = runXPath(responseElementXPath, n);
- }catch(TransformerException te){
- throw new SOAPException("Cannot select SOAP nodes due to exception "+
- "while executing XPath statement:" +te);
+ else{
+ if(n.getLocalName().equals(mobyService.getName()+"Response") ||
+ n.getLocalName().equals(mobyService.getName())){
+ node_list = new MobyPrefixResolver.MobyNodeList();
+ ((MobyPrefixResolver.MobyNodeList) node_list).add(n);
+ }
+ if(node_list == null || node_list.getLength() == 0){
+ node_list = n.getElementsByTagNameNS(MobyPrefixResolver.MOBY_TRANSPORT_NAMESPACE,
+ mobyService.getName()+"Response");
+ }
+ if(node_list == null || node_list.getLength() == 0){
+ node_list = n.getElementsByTagNameNS(MobyPrefixResolver.MOBY_TRANSPORT_NAMESPACE,
+ mobyService.getName());
+ }
+ if(node_list == null || node_list.getLength() == 0){
+ node_list = n.getElementsByTagName(mobyService.getName()+"Response");
+ }
+ if(node_list == null || node_list.getLength() == 0){
+ node_list = n.getElementsByTagName(mobyService.getName());
+ }
}
if(node_list == null || node_list.getLength() == 0){
throw new SOAPException("Could not find a response element in SOAP payload (service " +
- mobyService.getName() + ")");
+ mobyService.getName() + ", root element "+n.getNodeName()+")");
}
if(node_list.getLength() > 1){
@@ -663,9 +669,9 @@
try{
node_list = runXPath(base64EncodedXPath, responseNode);
}
- catch(TransformerException te){
+ catch(XPathExpressionException xpee){
throw new SOAPException("Cannot select base64 encoded SOAP nodes due to exception "+
- "while executing XPath statement:" +te);
+ "while executing XPath statement:" +xpee);
}
if(debug && node_list != null){
debugPS.println("There were " + node_list.getLength() +
@@ -694,9 +700,9 @@
try{
node_list = runXPath(stringEncodedXPath, responseNode);
}
- catch(TransformerException te){
+ catch(XPathExpressionException xpee){
throw new SOAPException("Cannot select string encoded SOAP nodes due to exception "+
- "while executing XPath statement:" +te);
+ "while executing XPath statement:" +xpee);
}
// Do concatenation for each plain string part found
@@ -882,9 +888,6 @@
}
} // end for responseNode in responseNodes
- // release resources related to the Xpath execution, since we won't be using this doc anymore
- releaseXPath(n);
-
return domRoot;
}
@@ -1006,25 +1009,9 @@
/**
* A method that sets up the execution environment for and runs a compiled XPath statement against a DOM node
- * You should call releaseXPath when you're done with the results
- * @return the list of Nodes that satisfy the XPath in this Node's context
+ * @return the list of Nodes that satisfy the XPath in this Node's context
*/
- protected NodeList runXPath(XPath xpath, Node n) throws TransformerException{
- NodeList result = null;
- int dtm_node_handle = xpath_context.getDTMHandleFromNode(n);
- PrefixResolver node_prefix_resolver = new PrefixResolverDefault(n);
- XObject xobject = xpath.execute(xpath_context, n, node_prefix_resolver);
- if(xobject instanceof XNodeSet){
- result = ((XNodeSet) xobject).nodelist();
- }
- else if(debug && xobject != null){
- debugPS.println("Output of XPath was not a XNodeSet as expected, found " + xobject.getClass().getName());
- debugPS.flush();
- }
- return result;
- }
-
- protected void releaseXPath(Node n){
- xpath_context.release(xpath_context.getDTM(xpath_context.getDTMHandleFromNode(n)), false);
+ protected NodeList runXPath(XPathExpression xpath, Node n) throws XPathExpressionException{
+ return (NodeList) xpath.evaluate(n, XPathConstants.NODESET);
}
}
More information about the MOBY-guts
mailing list