Using OGSA-DAI from Groovy (advanced)
If you need to use more of the features of the normal OGSA-DAI client API then it is possible to create a perform document and send it to the OGSA-DAI service. The response that comes back is an XML document. The normal OGSA-DAI API automatically takes the response document and uses it to populate the components of the original request with their status and any result sets. Unfortunately, this does not happen automatically using the GRIA API so a bit of extra coding is required!
This tutorial expects that you have already run the basic tutorial and therefore have the correct table in the database. You need a subscription with read/write access.
The GRIA 5.3 OGSA-DAI Service was shipped with the performWithRule method missing from the service's PBAC security policy. This means that the method is not available to anyone by default. To enable the method, visit the "Access control" page of the OGSA-DAI Service and download the policy for the "ogsadai-db-subscription" and add these lines at line 113:
<operation name="performWithRule"> <process-role name="owner"/> <process-role name="delegate"/> </operation>
Save the new policy file, undeploy the old one and upload the new one instead.
import java.sql.ResultSet
import uk.ac.soton.itinnovation.grid.types.ConversationID
import uk.ac.soton.itinnovation.grid.comms.ogsadai.OgsaDaiSubscription
import uk.ac.soton.itinnovation.grid.types.MatchRule
import uk.org.ogsadai.client.toolkit.activity.ActivityRequest
import uk.org.ogsadai.client.toolkit.activity.sql.SQLUpdate
import uk.org.ogsadai.client.toolkit.activity.sql.SQLQuery
import uk.org.ogsadai.client.toolkit.activity.sql.WebRowSet
import uk.org.ogsadai.types.ExtensibilityType
import uk.org.ogsadai.common.xml.XMLUtilities;
import uk.org.ogsadai.converters.webrowset.resultset.WebRowSetToResultSet
import org.apache.axis.message.MessageElement
// create proxy to service
ogsadaiService = serviceFactory.createServiceProxy("https://YOUR.MACHINE:PORT/gria-ogsadai-service/services/OgsaDaiServiceI2?wsdl")
// print all resources we can access and (arbitrarily) choose the last subscription
resources = ogsadaiService.getResources()
resources.each {
println(ConversationID.getLabel(it) + ": " + ConversationID.getURLReferenceFromEPR(it))
println(ConversationID.getResourceType(it))
if (ConversationID.getResourceType(it) == "http://www.it-innovation.soton.ac.uk/grid/resource/ogsadai-db-subscription") {
subEPR = it
}
}
println("Choosing this subscription: " + ConversationID.getURLReferenceFromEPR(subEPR))
subscription = proxyFactory.createProxy(subEPR, OgsaDaiSubscription)
The MatchRule is required to secure any OGSA-DAI session resources created by the processing of the perform document. Any sessions created will only be accessible by client matching the MatchRule.
// create a MatchRule matching the current user myMatchRule = new MatchRule(idp.getIdentity().getMatchPattern(), "owner", false) createSQL = "CREATE TABLE test (testcol int)" insertSQL = "INSERT INTO test (testcol) VALUES (43)" selectSQL = "SELECT * FROM test" // make OGSA-DAI request object for updating table sqlUpdate = new SQLUpdate(insertSQL) activityRequest = new ActivityRequest(); activityRequest.add(sqlUpdate) // make it the right type for the GRIA API request = new ExtensibilityType() requestElements = new MessageElement[1] requestElements[0] = new MessageElement(activityRequest.getDocument().getDocumentElement()) request.set_any(requestElements) // print the request document //println XMLUtilities.xmlDOMToString(activityRequest.getDocument()) responseRaw = subscription.performWithRule(myMatchRule, request) // print the response document //responseElement = responseRaw.get_any()[0] //println XMLUtilities.xmlDOMToString(responseElement.getAsDocument()) // make OGSA-DAI request object for querying table sqlQuery = new SQLQuery(selectSQL) rowSet = new WebRowSet(sqlQuery.getOutput()) activityRequest = new ActivityRequest(); activityRequest.add(sqlQuery) activityRequest.add(rowSet) // make it the right type for the GRIA API request = new ExtensibilityType() requestElements = new MessageElement[1] requestElements[0] = new MessageElement(activityRequest.getDocument().getDocumentElement()) request.set_any(requestElements) // print the request document //println XMLUtilities.xmlDOMToString(activityRequest.getDocument()) responseRaw = subscription.performWithRule(myMatchRule, request) // print the request document //responseElement = responseRaw.get_any()[0] //println XMLUtilities.xmlDOMToString(responseElement.getAsDocument())
In this simple case, the final "result" element of the response document contains the WebRowSet. We can extract this and populate the WebRowSet object we used in the request with the results.
// process the response to get a java.sql.ResultSet
responseElement = responseRaw.get_any()[0]
responseDoc = responseElement.getAsDocument()
resultNodes = responseDoc.getElementsByTagNameNS("http://ogsadai.org.uk/namespaces/2005/10/types", "result")
// in this case, the WebRowSet is the last of the result nodes
webRowSetData = resultNodes.item(resultNodes.getLength() - 1).getFirstChild().getNodeValue()
converter = new WebRowSetToResultSet(new StringReader(webRowSetData))
converter.setResultSetType(ResultSet.TYPE_FORWARD_ONLY)
resultSet = converter.getResultSet()
// print the results
println "Results:"
while (resultSet.next()) {
println resultSet.getInt(1)
}
Of course, if you are doing many of these queries then the standard code for constructing the perform document and dealing with the result set should be factored out into helper methods.
