ecological-engine-wps-exten.../src/main/java/org/gcube/dataanalysis/geo/wps/client/WPSClient.java

335 lines
12 KiB
Java

package org.gcube.dataanalysis.geo.wps.client;
import java.math.BigInteger;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import net.opengis.wps.x100.CapabilitiesDocument;
import net.opengis.wps.x100.ComplexDataType;
import net.opengis.wps.x100.ExecuteDocument;
import net.opengis.wps.x100.ExecuteResponseDocument;
import net.opengis.wps.x100.ExecuteResponseDocument.ExecuteResponse.ProcessOutputs;
import net.opengis.wps.x100.InputDescriptionType;
import net.opengis.wps.x100.InputType;
import net.opengis.wps.x100.OutputDescriptionType;
import net.opengis.wps.x100.ProcessBriefType;
import net.opengis.wps.x100.ProcessDescriptionType;
import org.apache.xmlbeans.XmlString;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.dataanalysis.ecoengine.datatypes.StatisticalType;
import org.gcube.dataanalysis.geo.wps.mappings.WPS2SM;
import org.n52.wps.client.ExecuteRequestBuilder;
import org.n52.wps.client.WPSClientSession;
import org.w3c.dom.NodeList;
public class WPSClient {
private ProcessBriefType[] processesList;
private String wpsServiceURL;
private InputDescriptionType[] currentInputs;
private OutputDescriptionType[] currentOutputs;
public OutputDescriptionType[] getCurrentOutputs() {
return currentOutputs;
}
public void setCurrentOutputs(OutputDescriptionType[] currentOutputs) {
this.currentOutputs = currentOutputs;
}
private List<StatisticalType> currentInputStatisticalTypes;
private Hashtable<String, StatisticalType> currentOutputStatisticalTypes;
private ProcessDescriptionType currentProcessDescription;
public Hashtable<String, StatisticalType> getCurrentOutputStatisticalTypes() {
return currentOutputStatisticalTypes;
}
public void setCurrentOutputStatisticalTypes(Hashtable<String, StatisticalType> currentOutputStatisticalTypes) {
this.currentOutputStatisticalTypes = currentOutputStatisticalTypes;
}
public String getWpsServiceURL() {
return wpsServiceURL;
}
public void setWpsServiceURL(String wpsServiceURL) {
this.wpsServiceURL = wpsServiceURL;
}
public InputDescriptionType[] getCurrentInputs() {
return currentInputs;
}
public void setCurrentInputs(InputDescriptionType[] currentInputs) {
this.currentInputs = currentInputs;
}
public List<StatisticalType> getCurrentInputStatisticalTypes() {
return currentInputStatisticalTypes;
}
public void setCurrentInputStatisticalTypes(List<StatisticalType> currentStatisticalTypes) {
this.currentInputStatisticalTypes = currentStatisticalTypes;
}
public String getCurrentProcessID() {
return currentProcessID;
}
public void setCurrentProcessID(String currentProcessID) {
this.currentProcessID = currentProcessID;
}
public String getCurrentProcessTitle() {
return currentProcessTitle;
}
public void setCurrentProcessTitle(String currentProcessTitle) {
this.currentProcessTitle = currentProcessTitle;
}
public String getCurrentProcessAbstract() {
return currentProcessAbstract;
}
public void setCurrentProcessAbstract(String currentProcessAbstract) {
this.currentProcessAbstract = currentProcessAbstract;
}
private String currentProcessID;
private String currentProcessTitle;
private String currentProcessAbstract;
// example: http://wps01.i-marine.d4science.org/wps/WebProcessingService
public WPSClient(String wpsServiceURL) throws Exception {
this.wpsServiceURL = wpsServiceURL;
}
public void describeProcess(String processID) throws Exception {
describeProcess(processID, null);
}
public void describeProcess(String processID, URL processDescriptionURL) throws Exception {
WPSClientSession wpsClient = WPSClientSession.getInstance();
try {
ProcessDescriptionType processDescription = wpsClient.getProcessDescription(wpsServiceURL, processID);
this.currentProcessDescription = processDescription;
// processDescription.set(XmlString.Factory.parse(new URL("http://schemas.opengis.net/wps/1.0.0/examples/40_wpsDescribeProcess_response.xml")));
if (processDescriptionURL != null)
processDescription.set(XmlString.Factory.parse(processDescriptionURL));
AnalysisLogger.getLogger().debug(processDescription.toString());
currentProcessID = processDescription.getIdentifier().getStringValue();
currentProcessTitle = processDescription.getTitle().getStringValue();
currentProcessAbstract = processDescription.getAbstract() != null ? processDescription.getAbstract().getStringValue() : "";
AnalysisLogger.getLogger().debug("WPSClient->Process ID:" + currentProcessID);
AnalysisLogger.getLogger().debug("WPSClient->Process Title:" + currentProcessTitle);
AnalysisLogger.getLogger().debug("WPSClient->Process Abstract:" + currentProcessAbstract);
InputDescriptionType[] inputList = processDescription.getDataInputs().getInputArray();
AnalysisLogger.getLogger().debug("WPSClient->Fetching Inputs");
currentInputStatisticalTypes = new ArrayList<StatisticalType>();
for (InputDescriptionType input : inputList) {
StatisticalType stype = WPS2SM.convert2SMType(input);
currentInputStatisticalTypes.add(stype);
AnalysisLogger.getLogger().debug("WPSClient->Converted Into a Statistical Type: " + stype);
}
AnalysisLogger.getLogger().debug("WPSClient->Fetching Outputs");
OutputDescriptionType[] outputList = processDescription.getProcessOutputs().getOutputArray();
currentOutputStatisticalTypes = new Hashtable<String, StatisticalType>();
currentOutputs = outputList;
for (OutputDescriptionType output : outputList) {
AnalysisLogger.getLogger().debug("WPSClient->Output id:" + output.getIdentifier().getStringValue());
if (output.getAbstract() != null)
AnalysisLogger.getLogger().debug("WPSClient->Abstract:" + output.getAbstract().getStringValue());
AnalysisLogger.getLogger().debug("WPSClient->Name:" + output.getTitle().getStringValue());
StatisticalType stype = WPS2SM.convert2SMType(output);
currentOutputStatisticalTypes.put(output.getIdentifier().getStringValue(), stype);
AnalysisLogger.getLogger().debug("WPSClient->Converted Into a Statistical Type: " + stype);
}
currentInputs = inputList;
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
wpsClient.disconnect(wpsServiceURL);
}
}
public void requestGetCapabilities() throws Exception {
WPSClientSession wpsClient = WPSClientSession.getInstance();
wpsClient.connect(wpsServiceURL);
try {
CapabilitiesDocument capabilities = wpsClient.getWPSCaps(wpsServiceURL);
ProcessBriefType[] processList = capabilities.getCapabilities().getProcessOfferings().getProcessArray();
for (ProcessBriefType process : processList) {
AnalysisLogger.getLogger().debug("WPSClient->Process id:" + process.getIdentifier().getStringValue());
AnalysisLogger.getLogger().debug("WPSClient->title:" + process.getTitle().getStringValue());
AnalysisLogger.getLogger().debug("WPSClient->abstract:" + process.getAbstract());
}
this.setProcessesList(processList);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
wpsClient.disconnect(wpsServiceURL);
}
}
public ProcessBriefType[] getProcessesList() {
return processesList;
}
public void setProcessesList(ProcessBriefType[] processesList) {
this.processesList = processesList;
}
public ProcessDescriptionType getProcessDescription() {
return currentProcessDescription;
}
public void setProcessDescription(ProcessDescriptionType processDescription) {
this.currentProcessDescription = processDescription;
}
public ProcessOutputs executeProcess(ExecuteRequestBuilder executeBuilder, ProcessDescriptionType processDescription) throws Exception {
try {
OutputDescriptionType[] odts = processDescription.getProcessOutputs().getOutputArray();
for (OutputDescriptionType odt : odts) {
// executeBuilder.setMimeTypeForOutput("text/xml", "result");
if (odt.isSetComplexOutput())
executeBuilder.setMimeTypeForOutput("text/xml", odt.getIdentifier().getStringValue());
}
} catch (Exception e) {
e.printStackTrace();
AnalysisLogger.getLogger().debug("Execute Process-> Warning, no xml structured objects will be provided");
}
// executeBuilder.setSchemaForOutput("http://schemas.opengis.net/gml/3.1.1/base/feature.xsd", "result");
ExecuteDocument execute = executeBuilder.getExecute();
execute.getExecute().setService("WPS");
WPSClientSession wpsClient = WPSClientSession.getInstance();
try {
wpsClient.connect(wpsServiceURL);
AnalysisLogger.getLogger().debug("Sending:\n" + execute);
Object responseObject = wpsClient.execute(wpsServiceURL, execute);
AnalysisLogger.getLogger().debug("Response:\n" + responseObject);
if (responseObject instanceof ExecuteResponseDocument) {
ExecuteResponseDocument response = (ExecuteResponseDocument) responseObject;
return response.getExecuteResponse().getProcessOutputs();
} else
throw new Exception("" + responseObject);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
wpsClient.disconnect(wpsServiceURL);
}
}
public static void main(String[] args) throws Exception {
AnalysisLogger.setLogger("./cfg/ALog.properties");
WPSClient client = new WPSClient("http://wps01.i-marine.d4science.org/wps/WebProcessingService");
// WPSClient client = new WPSClient("http://geoprocessing.demo.52north.org:8080/wps/WebProcessingService");
client.requestGetCapabilities();
// client.describeProcess("com.terradue.wps_hadoop.processes.examples.async.Async", new URL("file:///C:/Users/coro/Desktop/WorkFolder/Workspace/EcologicalEngineWPSExtension/cfg/test.xml"));
// client.describeProcess("org.n52.wps.extension.GetFuelPriceProcess");
// client.describeProcess("org.n52.wps.server.algorithm.test.DummyTestClass");
// client.describeProcess("org.n52.wps.server.algorithm.coordinatetransform.CoordinateTransformAlgorithm");
// client.describeProcess("org.n52.wps.extension.GetFuelPriceProcess");
client.describeProcess("com.terradue.wps_hadoop.processes.examples.async.Async");
}
public static int calculateBBDimensions(String bbstring) {
String[] bbinput = bbstring.split(",");
int dimcounter = 0;
try {
for (int i = 0; i < bbinput.length; i++) {
Double.parseDouble(bbinput[i]);
dimcounter++;
}
} catch (Exception e) {
AnalysisLogger.getLogger().debug("Dimensions Count: " + dimcounter);
}
return dimcounter;
}
public static void addBoundingBoxInput(org.n52.wps.client.ExecuteRequestBuilder executeBuilder, String identifier, String BBstring) {
ExecuteDocument executor = executeBuilder.getExecute();
InputType input1 = executor.getExecute().getDataInputs().addNewInput();
input1.addNewIdentifier().setStringValue(identifier);
net.opengis.ows.x11.BoundingBoxType bbtype = input1.addNewData().addNewBoundingBoxData();
// bboxInput=46,102,47,103,urn:ogc:def:crs:EPSG:6.6:4326,2
String[] bbinput = BBstring.split(",");
int dimensions = calculateBBDimensions(BBstring);
List lc = new ArrayList<String>();
for (int i = 0; i < dimensions / 2; i++) {
lc.add(bbinput[i]);
}
List uc = new ArrayList<String>();
for (int i = dimensions / 2; i < dimensions; i++) {
uc.add(bbinput[i]);
}
bbtype.setLowerCorner(lc);
bbtype.setUpperCorner(uc);
int crsidx = bbinput[dimensions].indexOf("crs:");
String crs = bbinput[dimensions];
/*
* if (crsidx>=0) crs = bbinput[dimensions].substring(crsidx+4);
*/
bbtype.setCrs(crs);
bbtype.setDimensions(new BigInteger("" + dimensions / 2));
}
public static List<String> retrieveURLsFromWPSResponse(ComplexDataType cdt) {
org.w3c.dom.Node node = cdt.getDomNode();
List<String> urls = getURLFromXML(node);
return urls;
}
private static List<String> getURLFromXML(org.w3c.dom.Node node) {
List<String> urls = new ArrayList<String>();
if (node == null)
return urls;
NodeList listnodes = node.getChildNodes();
int nChildren = listnodes.getLength();
if (nChildren == 0) {
String text = node.getNodeValue();
if (text.startsWith("https:")||text.startsWith("http:") || text.startsWith("ftp:") || text.startsWith("smp:")|| text.startsWith("file:"))
urls.add(text.trim());
} else {
for (int i = 0; i < nChildren; i++) {
List<String> childrenurls = getURLFromXML(listnodes.item(i));
urls.addAll(childrenurls);
}
}
return urls;
}
}