[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Wed Mar 17 20:17:39 UTC 2010


gordonp
Wed Mar 17 16:17:39 EDT 2010
Update of /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/services
In directory dev.open-bio.org:/tmp/cvs-serv11211/src/main/ca/ucalgary/services

Modified Files:
	CGIService.java SoapServlet.java WSDLService.java 
Log Message:
Updates related to major overhaul of Moby service wrapping system, Daggoo
moby-live/Java/src/main/ca/ucalgary/services CGIService.java,1.7,1.8 SoapServlet.java,1.7,1.8 WSDLService.java,1.4,1.5
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/services/CGIService.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/services/CGIService.java	2008/03/13 23:06:48	1.7
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/services/CGIService.java	2010/03/17 20:17:39	1.8
@@ -1,5 +1,6 @@
 package ca.ucalgary.services;
 
+import ca.ucalgary.services.util.CGIUtils;
 import ca.ucalgary.services.util.XHTMLForm;
 
 import org.biomoby.service.MobyServlet;
@@ -10,7 +11,6 @@
 import org.apache.commons.httpclient.methods.*;
 import org.apache.commons.httpclient.methods.multipart.*;
 import org.apache.commons.httpclient.params.*;
-import org.apache.commons.httpclient.util.URIUtil;
 
 import java.io.*;
 import java.net.URL;
@@ -68,7 +68,7 @@
 	    if(!(mobyData instanceof MobyDataObject) &&
 	       !(mobyData instanceof MobyDataObjectSet)){
 		throw new MobyException("The Moby parameter '" + paramName + 
-					"' is not a priary input as expected (" +
+					"' is not a primary input as expected (" +
 					"found " + mobyData.getClass().getName() + ")");
 	    }
 	    
@@ -101,16 +101,17 @@
 	    method = new PostMethod(formConfig.getFormAction());
 	    if(XHTMLForm.MULTIPART.toLowerCase().equals(
 			 formConfig.getFormEncodingType().toLowerCase())){
-		((PostMethod) method).setRequestEntity(getMultipartRequest(formDataInstanceMap, 
-									   method.getParams()));
+		((PostMethod) method).setRequestEntity(CGIUtils.getMultipartRequest(formDataInstanceMap, 
+										    method.getParams(),
+										    formConfig.getFormFiles()));
 	    }
 	    else{
-		((PostMethod) method).setRequestBody(getNameValuePairs(formDataInstanceMap));
+		((PostMethod) method).setRequestBody(CGIUtils.getNameValuePairs(formDataInstanceMap));
 	    }
 	}
 	// If not POST, assume GET
 	else{
-	    method = new GetMethod(formConfig.getFormAction()+"?"+getURLQuery(formDataInstanceMap));
+	    method = new GetMethod(formConfig.getFormAction()+"?"+CGIUtils.getURLQuery(formDataInstanceMap));
 	}
 
 	int statusCode;
@@ -170,45 +171,6 @@
 	return createServiceFromConfig(request, formConfig);
     }
 
-    protected MultipartRequestEntity getMultipartRequest(Map<String,byte[]> dataInstances, 
-							 HttpMethodParams params){
-	Part[] parts = new Part[dataInstances.size()];
-	List<String> fileTypeInputs = formConfig.getFormFiles();
-	int i = 0;
-	for(Map.Entry<String,byte[]> formInput: dataInstances.entrySet()){
-	    String formInputName = formInput.getKey();
-	    if(fileTypeInputs.contains(formInputName)){
-		parts[i++] = new FilePart(formInputName, 
-					  new ByteArrayPartSource(formInputName, 
-								  formInput.getValue()));
-	    }
-	    else{
-		parts[i++] = new StringPart(formInputName, new String(formInput.getValue()));
-	    }
-	}
-	return new MultipartRequestEntity(parts, params);
-    }
-
-    protected NameValuePair[] getNameValuePairs(Map<String,byte[]> dataInstances){
-	NameValuePair[] data = new NameValuePair[dataInstances.size()];
-	int i = 0;
-	for(Map.Entry<String,byte[]> formField: dataInstances.entrySet()){
-	    data[i++] = new NameValuePair(formField.getKey(), 
-					  formField.getValue() == null ? "" : new String(formField.getValue()));
-	}
-	return data;
-    }
-
-    protected String getURLQuery(Map<String,byte[]> dataInstances) throws URIException{
-	String[] queryItems = new String[dataInstances.size()];
-	int i = 0;
-	for(Map.Entry<String,byte[]> formField: dataInstances.entrySet()){
-	    queryItems[i++] = URIUtil.encodeWithinQuery(formField.getKey()) + "=" + 
-		URIUtil.encodeWithinQuery(new String(formField.getValue()));
-	}
-	return XHTMLForm.join("+", queryItems);
-    }
-
     public String createInputSpecString(XHTMLForm form){
 	Map<String,String> ins = form.getPrimaryInputs();
 	// Create a reverse map so we can look up where a moby param 

===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/services/SoapServlet.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/services/SoapServlet.java	2009/08/27 21:37:32	1.7
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/services/SoapServlet.java	2010/03/17 20:17:39	1.8
@@ -25,13 +25,8 @@
  * can be extended using stylesheets and registering a DataRecorder (e.g. Daggoo registers PBERecorder to get 
  * its functionality), please see the documentation at http://biomoby.open-bio.org/CVS_CONTENT/moby-live/Java/docs/soapServlet.html
  */
-public class SoapServlet extends HttpServlet{
+public class SoapServlet extends WrappingServlet{
 
-    public static final String DATARECORDER_CONTEXTPARAM = "dataRecorder"; // how users spec a DataRecorder in the web.xml
-
-    public static final String SERVICE_SPEC_PARAM = "service";
-    public static final String WSDL_HTTP_PARAM = "wsdl";
-    public static final String ID_PARAM = "seahawkId";
     private static final String ARRAY_TYPE_SENTINEL = "ar_TyPe";
     private static final String BASIC_TYPE_SENTINEL = "baSIc_TyPe";
     private static final String BASIC_NAME_SENTINEL = "baSIc_naMe";
@@ -50,11 +45,8 @@
     private static final String SQUOTE_OPTION = "single quotes (&apos;...&apos;)";
 
     private static DocumentBuilder docBuilder;
-    private Transformer indentationTransformer;
-
-    private static Logger logger = Logger.getLogger("ca.ucalgary.services.SoapServlet");
 
-    private DataRecorder recorder = null;
+    private static Logger logger = Logger.getLogger(SoapServlet.class.getName());
 
     static{
 	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
@@ -74,146 +66,29 @@
 	    URL u = getClass().getClassLoader().getResource(INDENTATION_XSL_RESOURCE);
 	    if(u != null){
 		// for pretty copying of SOAP response XML
-		indentationTransformer = transformerFactory.newTransformer(new StreamSource(u.openStream()));  
+		setResponseTransformer(transformerFactory.newTransformer(new StreamSource(u.openStream())));  
 	    }
 	    else{
 		// verbatim copy as backup plan
-		indentationTransformer = transformerFactory.newTransformer();  
-	    }
-	    if(recorder != null){
-		recorder.setTransformer(indentationTransformer);
+		setResponseTransformer(transformerFactory.newTransformer());  
 	    }
 	} catch (Exception e){
 	    logger.log(Level.SEVERE,
 		       "Could not create an XSLT transformer: " + e,
 		       e);
 	}
-
-	// See if the user has specified a DataRecorder to be used (customization and/or recording of
-	// user interaction and Web Service response).
-	String dataRecorderClassName = null;
-	javax.servlet.ServletContext context = getServletContext();
-	if(context != null){
-	    if(context.getInitParameter(DATARECORDER_CONTEXTPARAM) != null){
-		dataRecorderClassName = context.getInitParameter(DATARECORDER_CONTEXTPARAM);
-	    }
-	}
-	javax.servlet.ServletConfig config = getServletConfig();
-	if(config != null){
-	    if(config.getInitParameter(DATARECORDER_CONTEXTPARAM) != null){
-		dataRecorderClassName = config.getInitParameter(DATARECORDER_CONTEXTPARAM);
-	    }
-	}
-	if(dataRecorderClassName != null){
-	    DataRecorder dataRecorder = null;
-
-	    try{
-		// This line can throw many different exception if you didn't get the class right! 
-		Class drClass = getClass().getClassLoader().loadClass(dataRecorderClassName);
-		if(drClass == null){
-		    throw new ClassNotFoundException("The DataRecorder class to run (" + 
-						     dataRecorderClassName +
-						     ") was not found, please ensure that the web.xml is up-to-date.");
-		}
-		dataRecorder = (DataRecorder) drClass.newInstance();
-	    } catch(Exception e){
-		System.err.println("The DataRecorder implementing class was not specified properly in the web.xml file:");
-		e.printStackTrace();
-		throw new ServletException("Invalid web.xml, the parameter 'dataRecorder' was not useable");
-	    }
-	    setRecorder(dataRecorder);
-	}
-    }
-
-    public void setRecorder(DataRecorder r){
-	recorder = r;
-	if(indentationTransformer != null){
-	    recorder.setTransformer(indentationTransformer);
-	}
-    }
-
-    public DataRecorder getRecorder(){
-	return recorder;
     }
 
     /**
-     * Gets are for form creation
+     * Post is for service submission (if the wsdl URL is provided)
      */
-    public void doGet(HttpServletRequest request,
-		      HttpServletResponse response)
-	throws ServletException, java.io.IOException{
-
-	if(request.getSession(false) == null && recorder != null){
-	    recorder.startRecording(request); //should set up a session
-	}
-
-	String action = request.getParameter(DataRecorder.PASSTHROUGH_ACTION);
-	if(action != null && action.trim().length() > 0){
-	    // Delegate to the seahawk action recorder for wrapping
-	    recorder.doGet(request, response);
-	    return;
-	}
-
-	String endpoint = request.getParameter(WSDL_HTTP_PARAM);
-	if(recorder != null && recorder.shouldIntercept(request)){
-	    recorder.interceptRequest(request, response);
-	}
-	else if(endpoint == null || endpoint.trim().length() == 0){  // Normal web-browser form fill-in
-	    writeInputForm(request, response);
-	}
-	else{
-	    writeServiceForm(request, response, endpoint);
-	}
-    }
-
-    /**
-     * Post is for service submission (if the wsdl URL is provided), or the PBERecorder (any other case)
-     */
-    public void doPost(HttpServletRequest request,
-		       HttpServletResponse response){
-
-	java.io.PrintStream out = null;
-	try{
-	    out = new java.io.PrintStream(response.getOutputStream());
-	}
-	catch(java.io.IOException ioe){
-	    logger.log(Level.SEVERE, "While getting servlet output stream (for HTML form response to client)", ioe);
-	    return;
-	}
-
-	// Find out the info needed to build the JAX-WS client
-	if(recorder != null && recorder.shouldIntercept(request)){
-	//if(wsdlLoc == null || wsdlLoc.trim().length() == 0){
-	//    if(recorder != null){
-	    recorder.interceptRequest(request, response);
-	    return;
-	}
-	String wsdlLoc = request.getParameter(WSDL_HTTP_PARAM);
-	if(wsdlLoc == null || wsdlLoc.trim().length() == 0){
-	    response.setContentType("text/html");
-	    out.print("<html><head><title>Error</title>"+
-		      "<link type=\"text/css\" rel=\"stylesheet\" href=\"stylesheets/input_ask.css\" />\n"+
-		      "</head><body>No '"+WSDL_HTTP_PARAM+"' parameter (the location of the WSDL document)" +
-		      "was specified in the POST request, and " +
-		      (recorder == null ? "no data recorder is associated with this servlet" : 
-		       "the data recorder did not want to intercept the POST") +"</body></html>");
-	    
-	}
-	URL url = null;
-	try{
-	    url = new URL(wsdlLoc);
-	} catch(Exception e){
-	    out.print("<html><head><title>Error</title>\n"+
-		      "<link type=\"text/css\" rel=\"stylesheet\" href=\"stylesheets/input_ask.css\" />\n"+
-		      "</head><body><h2>The URL specified (" + 
-		      wsdlLoc + ") could not be parsed</h2><br><pre>");
-	    e.printStackTrace(out);
-	    out.print("</pre></body></html>\n");
-	    return;
-	}
+    public void callService(HttpServletRequest request,
+			    HttpServletResponse response,
+			    URL url,  //url of wsdl file
+			    PrintStream out){
 
 	String serviceSpec = request.getParameter(SERVICE_SPEC_PARAM);
-	if(wsdlLoc == null || wsdlLoc.trim().length() == 0){
+	if(serviceSpec == null || serviceSpec.trim().length() == 0){
 	    out.print("<html><head><title>Error</title>\n"+
 		      "<link type=\"text/css\" rel=\"stylesheet\" href=\"stylesheets/input_ask.css\" />\n"+
 		      "</head><body>No '"+SERVICE_SPEC_PARAM+"' parameter (specifying " +
@@ -278,7 +153,7 @@
 	for (Enumeration e = request.getParameterNames(); e.hasMoreElements();){
 	    String paramName = (String) e.nextElement();
 	    if(SERVICE_SPEC_PARAM.equals(paramName) || 
-	       WSDL_HTTP_PARAM.equals(paramName) || 
+	       SRC_PARAM.equals(paramName) || 
 	       ID_PARAM.equals(paramName)){
 		continue;
 	    }
@@ -403,7 +278,7 @@
 		answer = recorder.markupResponse(resultSource, request);
 	    } catch(Exception e){
 		//todo
-		answer = "<pre>Exception in PBE system:\n"+e.toString()+"\n";
+		answer = "<pre>Exception in DataRecorder subsystem:\n"+e.toString()+"\n";
 		for(StackTraceElement ste: e.getStackTrace()){
 		    answer += ste.toString()+"\n";
 		}	    
@@ -414,8 +289,9 @@
 	    // Does two duties: fixes indentation, and outputs to a Java stream we can print easily
 	    ByteArrayOutputStream stringResult = new ByteArrayOutputStream();
 	    try{
-		synchronized(indentationTransformer){
-		    indentationTransformer.transform(resultSource, 
+		Transformer responseTransformer = getResponseTransformer();
+		synchronized(responseTransformer){
+		    responseTransformer.transform(resultSource, 
 						     new javax.xml.transform.stream.StreamResult(stringResult));
 		}
 	    } catch(Exception e){
@@ -440,52 +316,24 @@
 
     // Asks for the WSDL file
     private void writeInputForm(HttpServletRequest request,
-				HttpServletResponse response){
-	java.io.OutputStream out = null;
-	response.setContentType("text/html");
-	try{
-	    out = response.getOutputStream();
-	}
-	catch(java.io.IOException ioe){
-	    logger.log(Level.SEVERE, "While getting servlet output stream (for HTML form response to client)", ioe);
-	    return;
-	}
-
-	try{
-	    out.write("<html><head><title>Generic SOAP Client</title><link type=\"text/css\" rel=\"stylesheet\" href=\"stylesheets/wsdl_ask.css\" /></head>\n".getBytes());
-	    out.write("<body>Enter the URL of the WSDL file below: <form action=''><input name='".getBytes());
-	    out.write((WSDL_HTTP_PARAM+"' type='text' size='50'/>").getBytes());
-	    out.write("</form></body></html>".getBytes());
-	}
-	catch(java.io.IOException ioe){
-	    logger.log(Level.SEVERE, "While printing HTML form to servlet output stream", ioe);
-	    return;
-	}	
+				HttpServletResponse response,
+				PrintStream out){
+	out.print("<html><head><title>Generic SOAP Client</title><link type=\"text/css\" rel=\"stylesheet\" href=\"stylesheets/wsdl_ask.css\" /></head>\n");
+	out.print("<body>Enter the URL of the WSDL file below: <form action=''><input name='");
+	out.print(SRC_PARAM+"' type='text' size='50'/>");
+	out.print("</form></body></html>");
     }
 
     // Presents the WSDL file as a CGI form
-    private void writeServiceForm(HttpServletRequest request,
-				  HttpServletResponse response,
-				  String endpoint){
-	java.io.PrintStream out = null;
-	response.setContentType("text/html");
-	try{
-	    out = new java.io.PrintStream(response.getOutputStream());
-	}
-	catch(java.io.IOException ioe){
-	    log("While getting servlet output stream (for HTML form response to client)", ioe);
-	    return;
-	}
-
-	URL url = null;
-	try{
-	    url = new URL(endpoint);
-	} catch(Exception e){
-	    out.print("<html><head><title>Parsing Error</title>\n"+
-		      "<link type=\"text/css\" rel=\"stylesheet\" href=\"stylesheets/wsdl_ask.css\" />\n" +
-		      "</head><body><h2>The URL specified (" + endpoint + ") could not be parsed</h2><br><pre>");
-	    e.printStackTrace(out);
-	    out.print("</pre></body></html>\n");
+    protected void writeServiceForm(HttpServletRequest request,
+				    HttpServletResponse response,
+				    URL url,
+				    PrintStream out){
+
+	// Normal web-browser form fill-in, ask for the WSDL to wrap
+	// Useful so this servlet can standalone and borker WSDL services.
+	if(url == null){
+	    writeInputForm(request, response, out);
 	    return;
 	}
 
@@ -496,7 +344,7 @@
 		out.print(recorder.getHead(request));
 	    }
 	    // allow the recorder to insert any body events required
-	    out.print("</head>\n<body " + (recorder==null?"":recorder.getBodyAttrs()) + ">\n");
+	    out.print("</head>\n<body " + (recorder==null?"":recorder.getBodyAttrs(request)) + ">\n");
 	    if(recorder != null){
 		out.print(recorder.getBody(request));
 	    }
@@ -1010,7 +858,7 @@
 				    portQName.getNamespaceURI() + " " + portQName.getLocalPart() + " ";
 				out.print("<a name='" + opQName.getLocalPart() + "'></a><div class='operation'><h3>"+
 					  opQName.getLocalPart()+"</h3><form action='' method='post'>\n"+
-                                          "<input type='hidden' name='"+WSDL_HTTP_PARAM+"' value='"+url+"'/>\n");
+                                          "<input type='hidden' name='"+SRC_PARAM+"' value='"+url+"'/>\n");
 				Map<String,QName> partsMap = msg2Parts.get(messageQName);
 				if(partsMap == null){
 				    System.err.println("Got null parts map for message " + messageQName);
@@ -1335,23 +1183,6 @@
     }
 
     /**
-     * Links the form inputs to javascript capture actions, if a recorder is available
-     */
-    private String rec(){
-	if(recorder != null){
-	    return recorder.getOnEventText(); 
-	}
-	return "";
-    }
-
-    private String sub(){
-	if(recorder != null){
-	    return recorder.getOnSubmitText(); 
-	}
-	return "";
-    }
-
-    /**
      * Expand any import or include statements in-place.
      */
     public static void doImports(Document doc, URL baseURL) throws Exception{
@@ -1449,13 +1280,4 @@
 	    }
 	}
     }
-
-    public static class MyNodeList implements NodeList{
-        private Vector<Node> nodes;
-        public MyNodeList(){nodes = new Vector<Node>();}
-        public int getLength(){return nodes.size();}
-        public Node item(int index){return nodes.elementAt(index);}
-        public void add(Node n){nodes.add(n);}
-        public void add(NodeList n){for(int i=0;i<n.getLength();i++){nodes.add(n.item(i));}}
-    };
 }

===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/services/WSDLService.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/services/WSDLService.java	2009/08/17 21:11:47	1.4
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/services/WSDLService.java	2010/03/17 20:17:39	1.5
@@ -21,9 +21,9 @@
 import javax.xml.transform.stream.*;
 import javax.xml.xpath.*;
 //import javax.xml.ws.*;
-import org.apache.axis.AxisFault;
-import org.apache.axis.client.Call;
-import org.apache.axis.client.Service;
+// import org.apache.axis.AxisFault;
+// import org.apache.axis.client.Call;
+// import org.apache.axis.client.Service;
 
 import java.io.*;
 import java.net.HttpURLConnection;
@@ -496,9 +496,12 @@
 	    // may both be useful, so we kept the /@... or /text() suffixes
 	    // to avoid name collision in the parameter hash. To get the result node
 	    // on which MOB rules will be applied, we need to strip these parts off
-	    // don't worry, they appear in the MOB rules, so we get the correct response.
+	    // don't worry, they appear in the MOB rules (unless its a regex), so we get the correct response.
 	    // Here we're just getting the evaluation context for the MOB rule.
-	    String resultXPathString = paramName.replaceAll("/(?:text\\(\\)|@[^/]+)$","");
+	    String resultXPathString = paramName;
+	    if(!outXPathString2IsRegex.get(paramName)){
+		resultXPathString = resultXPathString.replaceAll("/(?:text\\(\\)|@[^/]+)$","");
+	    }
 	    // We will cheat slightly by making any paths e.g. /a/b/c into //a/b/c 
 	    // because the XML doc may actually contain the soap envelope, etc.
 	    if(resultXPathString.indexOf("//") != 0){
@@ -506,10 +509,12 @@
 		resultXPathString = resultXPathString.substring(resultXPathString.indexOf("/")+1);
 	    }
 	    try{
+		//temp: we use IOUtils, which gives root element, not document node.  Lop off first level of xpath
+		resultXPathString = resultXPathString.replaceFirst("^/[^/]+/","");
 		xPath.setNamespaceContext(targetNSContext);
 		outXPathString2XPath.put(paramName,
 					 xPath.compile(resultXPathString));
-		//logger.log(Level.WARNING, "%%%%%%Compiled " + resultXPathString);
+		logger.log(Level.WARNING, "%%%%%%Compiled " + resultXPathString);
 	    } catch(Exception e){
 		logger.log(Level.SEVERE,
 			   "While parsing service result XPath (" + resultXPathString +
@@ -548,7 +553,8 @@
 
      private String fixXPath(String xpath){
 	 //return xpath.replaceAll("/([a-zA-Z_0-9\\-]+)(?=/|$|\\[)", "/"+DEFAULT_NSPREFIX_4_XPATH+":$1");
-	return xpath.replaceAll("/([a-zA-Z_0-9\\-]+)(?=/|$)", "/*[local-name() = '$1']");
+	return xpath.replaceAll("/([a-zA-Z_0-9\\-]+)(?=/|$)", "/*[local-name() = '$1']")
+	    .replaceAll("/([a-zA-Z_0-9\\-]+)\\[", "/*[local-name() = '$1' and ");
      }
 
     public String createInputSpecString(WSDLConfig wsdl){




More information about the MOBY-guts mailing list