1135 lines
36 KiB
Java
1135 lines
36 KiB
Java
package org.gcube.portlets.user.dataminermanager.server.smservice;
|
|
|
|
import java.math.BigInteger;
|
|
import java.net.URL;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.Comparator;
|
|
import java.util.HashMap;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
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.OutputDataType;
|
|
import net.opengis.wps.x100.OutputDescriptionType;
|
|
import net.opengis.wps.x100.ProcessBriefType;
|
|
import net.opengis.wps.x100.ProcessDescriptionType;
|
|
import net.opengis.wps.x100.ResponseDocumentType;
|
|
import net.opengis.wps.x100.StatusType;
|
|
import net.opengis.wps.x100.SupportedComplexDataInputType;
|
|
import net.opengis.wps.x100.impl.ExecuteResponseDocumentImpl;
|
|
|
|
import org.apache.xmlbeans.XmlString;
|
|
import org.gcube.portlets.user.dataminermanager.client.bean.ComputationStatus;
|
|
import org.gcube.portlets.user.dataminermanager.client.bean.ComputationStatus.Status;
|
|
import org.gcube.portlets.user.dataminermanager.client.bean.Operator;
|
|
import org.gcube.portlets.user.dataminermanager.client.bean.OperatorCategory;
|
|
import org.gcube.portlets.user.dataminermanager.client.bean.OperatorsClassification;
|
|
import org.gcube.portlets.user.dataminermanager.client.bean.output.FileResource;
|
|
import org.gcube.portlets.user.dataminermanager.client.bean.output.ImagesResource;
|
|
import org.gcube.portlets.user.dataminermanager.client.bean.output.MapResource;
|
|
import org.gcube.portlets.user.dataminermanager.client.bean.output.ObjectResource;
|
|
import org.gcube.portlets.user.dataminermanager.client.bean.output.Resource;
|
|
import org.gcube.portlets.user.dataminermanager.client.bean.parameters.ObjectParameter;
|
|
import org.gcube.portlets.user.dataminermanager.client.bean.parameters.Parameter;
|
|
import org.gcube.portlets.user.dataminermanager.server.is.InformationSystemUtils;
|
|
import org.gcube.portlets.user.dataminermanager.server.smservice.wps.ProcessInformations;
|
|
import org.gcube.portlets.user.dataminermanager.server.smservice.wps.ResponseWPS;
|
|
import org.gcube.portlets.user.dataminermanager.server.smservice.wps.StatWPSClientSession;
|
|
import org.gcube.portlets.user.dataminermanager.server.smservice.wps.WPS2SM;
|
|
import org.gcube.portlets.user.dataminermanager.server.util.ServiceCredential;
|
|
import org.gcube.portlets.user.dataminermanager.shared.Constants;
|
|
import org.gcube.portlets.user.dataminermanager.shared.data.ComputationId;
|
|
import org.gcube.portlets.user.dataminermanager.shared.exception.ServiceException;
|
|
import org.n52.wps.client.ExecuteRequestBuilder;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.w3c.dom.NamedNodeMap;
|
|
import org.w3c.dom.Node;
|
|
import org.w3c.dom.NodeList;
|
|
|
|
/**
|
|
* Client 4 WPS
|
|
*
|
|
*
|
|
* @author Giancarlo Panichi email: <a
|
|
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
|
*
|
|
*/
|
|
public class SClient4WPS extends SClient {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(SClient4WPS.class);
|
|
// private static final int OPERATOR_BRIEF_DESCRIPTION_MAX_LENGHT = 170;
|
|
private static final String OTHERS = "OTHERS";
|
|
|
|
private String wpsToken;
|
|
private String wpsUser;
|
|
// private String wpsServiceURL;
|
|
private String wpsProcessingServlet;
|
|
private String wpsCancelComputationServlet;
|
|
|
|
private ProcessDescriptionType[] processesDescriptionType;
|
|
private ProcessBriefType[] processesBriefs;
|
|
|
|
private HashMap<String, ProcessInformations> process;
|
|
private HashMap<ComputationId, ProcessInformations> runningProcess;
|
|
|
|
private StatWPSClientSession wpsClient;
|
|
|
|
public SClient4WPS(ServiceCredential serviceCredential) throws Exception {
|
|
super();
|
|
process = new HashMap<>();
|
|
runningProcess = new HashMap<>();
|
|
if (serviceCredential == null) {
|
|
logger.error("Error credetials are null!");
|
|
throw new Exception("Error credetials are null!");
|
|
} else {
|
|
String token = serviceCredential.getToken();
|
|
if (token == null || token.isEmpty()) {
|
|
logger.error("Error authorization token invalid: " + token);
|
|
throw new Exception("Error authorization token invalid: "
|
|
+ token);
|
|
} else {
|
|
wpsToken = token;
|
|
}
|
|
|
|
String userName = serviceCredential.getUsername();
|
|
if (userName == null || userName.isEmpty()) {
|
|
logger.error("Error invalid user name: " + userName);
|
|
throw new Exception("Error invalid user name: " + userName);
|
|
} else {
|
|
wpsUser = userName;
|
|
}
|
|
List<String> serviceAddress = InformationSystemUtils
|
|
.retrieveServiceAddress(
|
|
Constants.DATAMINER_SERVICE_CATEGORY,
|
|
Constants.DATA_MINER_SERVICE_NAME,
|
|
serviceCredential.getScope());
|
|
logger.debug("Service Address retrieved:" + serviceAddress);
|
|
if (serviceAddress == null || serviceAddress.size() < 1) {
|
|
logger.error("No DataMiner service address available!");
|
|
throw new Exception("No DataMiner service address available!");
|
|
} else {
|
|
logger.info("DataMiner service address found: "
|
|
+ serviceAddress.get(0));
|
|
wpsProcessingServlet = serviceAddress.get(0);
|
|
|
|
int wpsWebProcessingServiceIndex = wpsProcessingServlet
|
|
.indexOf(Constants.WPSWebProcessingService);
|
|
if (wpsWebProcessingServiceIndex > 0) {
|
|
String wpsServiceUrl = wpsProcessingServlet.substring(0,
|
|
wpsWebProcessingServiceIndex);
|
|
wpsCancelComputationServlet = wpsServiceUrl
|
|
+ Constants.WPSCancelComputationServlet;
|
|
logger.debug("Cancel computation servlet: "
|
|
+ wpsCancelComputationServlet);
|
|
|
|
} else {
|
|
logger.error("Cancel computation servlet not available!");
|
|
throw new Exception(
|
|
"Cancel computation servlet not available!");
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private StatWPSClientSession createWPSClientSession() {
|
|
if (wpsClient == null) {
|
|
wpsClient = new StatWPSClientSession(wpsUser, wpsToken);
|
|
return wpsClient;
|
|
} else {
|
|
return wpsClient;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public List<OperatorsClassification> getOperatorsClassifications()
|
|
throws Exception {
|
|
|
|
LinkedHashMap<String, String> operatorsToCategoriesMap = new LinkedHashMap<>();
|
|
LinkedHashMap<String, List<Operator>> categoriesToOperatorsMap = new LinkedHashMap<>();
|
|
|
|
requestCapability();
|
|
if (processesBriefs == null || processesDescriptionType == null) {
|
|
throw new ServiceException("Algorithms WPS not available!");
|
|
}
|
|
|
|
for (ProcessBriefType processBrief : processesBriefs) {
|
|
String categoryTitle = processBrief.getTitle().getStringValue();
|
|
String categoryName;
|
|
if (categoryTitle == null || categoryTitle.isEmpty()) {
|
|
categoryName = OTHERS;
|
|
} else {
|
|
String[] categorySplitted = categoryTitle.split(":");
|
|
if (categorySplitted.length < 1) {
|
|
categoryName = OTHERS;
|
|
} else {
|
|
categoryName = categorySplitted[0];
|
|
}
|
|
}
|
|
operatorsToCategoriesMap.put(processBrief.getIdentifier()
|
|
.getStringValue(), categoryName);
|
|
}
|
|
|
|
String briefDescription;
|
|
for (ProcessBriefType processDescriptionType : processesDescriptionType) {
|
|
briefDescription = processDescriptionType.getAbstract()
|
|
.getStringValue();
|
|
|
|
String categoryName = operatorsToCategoriesMap
|
|
.get(processDescriptionType.getIdentifier()
|
|
.getStringValue());
|
|
if (categoryName == null || categoryName.isEmpty()) {
|
|
categoryName = OTHERS;
|
|
}
|
|
|
|
List<Operator> listOperators = categoriesToOperatorsMap
|
|
.get(categoryName);
|
|
if (listOperators == null) {
|
|
listOperators = new ArrayList<>();
|
|
}
|
|
|
|
listOperators
|
|
.add(new Operator(processDescriptionType.getIdentifier()
|
|
.getStringValue(), processDescriptionType
|
|
.getTitle().getStringValue(), briefDescription,
|
|
processDescriptionType.getAbstract()
|
|
.getStringValue(), null));
|
|
|
|
categoriesToOperatorsMap.put(categoryName, listOperators);
|
|
|
|
}
|
|
|
|
List<OperatorCategory> categories = new ArrayList<>();
|
|
List<Operator> operators = new ArrayList<>();
|
|
|
|
Comparator<OperatorCategory> categoriesComparator = new Comparator<OperatorCategory>() {
|
|
public int compare(OperatorCategory c1, OperatorCategory c2) {
|
|
return c1.getName().compareTo(c2.getName()); // use your logic
|
|
}
|
|
};
|
|
|
|
Comparator<Operator> operatorsComparator = new Comparator<Operator>() {
|
|
public int compare(Operator c1, Operator c2) {
|
|
return c1.getName().compareTo(c2.getName()); // use your logic
|
|
}
|
|
};
|
|
|
|
for (String categoryName : categoriesToOperatorsMap.keySet()) {
|
|
OperatorCategory category = new OperatorCategory(categoryName,
|
|
categoryName, categoryName);
|
|
List<Operator> listOperators = categoriesToOperatorsMap
|
|
.get(categoryName);
|
|
for (Operator operator : listOperators) {
|
|
operator.setCategory(category);
|
|
}
|
|
Collections.sort(listOperators, operatorsComparator);
|
|
category.setOperators(listOperators);
|
|
operators.addAll(listOperators);
|
|
categories.add(category);
|
|
}
|
|
|
|
Collections.sort(operators, operatorsComparator);
|
|
Collections.sort(categories, categoriesComparator);
|
|
|
|
List<OperatorsClassification> operatorsClass = new ArrayList<>();
|
|
|
|
OperatorsClassification op = new OperatorsClassification(
|
|
Constants.UserClassificationName, categories, operators);
|
|
|
|
operatorsClass.add(op);
|
|
|
|
logger.debug("OperatorClass: " + operatorsClass);
|
|
return operatorsClass;
|
|
}
|
|
|
|
private ProcessInformations describeProcess(String processId)
|
|
throws Exception {
|
|
return describeProcess(processId, null);
|
|
}
|
|
|
|
private ProcessInformations describeProcess(String processId,
|
|
URL processDescriptionURL) throws Exception {
|
|
if (process.containsKey(processId)) {
|
|
return process.get(processId);
|
|
}
|
|
|
|
StatWPSClientSession wpsClient = null;
|
|
try {
|
|
wpsClient = createWPSClientSession();
|
|
|
|
logger.debug("Describe Process WPS URL: " + wpsProcessingServlet);
|
|
ProcessDescriptionType processDescription = null;
|
|
for (int k = 0; k <= 3; k++) {
|
|
try {
|
|
processDescription = wpsClient.getProcessDescription(
|
|
wpsProcessingServlet, processId);
|
|
} catch (Exception e) {
|
|
logger.debug("Retrying with WPS URL: "
|
|
+ wpsProcessingServlet);
|
|
if (k == 3)
|
|
throw e;
|
|
}
|
|
if (processDescription != null)
|
|
break;
|
|
}
|
|
|
|
ProcessInformations processInformations = new ProcessInformations(
|
|
processDescription);
|
|
|
|
if (processDescriptionURL != null)
|
|
processDescription.set(XmlString.Factory
|
|
.parse(processDescriptionURL));
|
|
|
|
logger.debug(processDescription.toString());
|
|
|
|
InputDescriptionType[] inputList = processDescription
|
|
.getDataInputs().getInputArray();
|
|
logger.debug("WPSClient->Fetching Inputs");
|
|
for (InputDescriptionType input : inputList) {
|
|
logger.debug("WPSClient->Input: " + input);
|
|
}
|
|
|
|
OutputDescriptionType[] outputList = processDescription
|
|
.getProcessOutputs().getOutputArray();
|
|
logger.debug("WPSClient->Fetching Outputs");
|
|
for (OutputDescriptionType output : outputList) {
|
|
logger.debug("WPSClient->Output: " + output);
|
|
}
|
|
|
|
processInformations.setInputs(inputList);
|
|
processInformations.setOutputs(outputList);
|
|
|
|
process.put(processId, processInformations);
|
|
|
|
return processInformations;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
throw e;
|
|
} finally {
|
|
wpsClient.disconnect(wpsProcessingServlet);
|
|
}
|
|
}
|
|
|
|
private void requestCapability() throws ServiceException {
|
|
StatWPSClientSession wpsClient = null;
|
|
processesDescriptionType = null;
|
|
processesBriefs = null;
|
|
|
|
try {
|
|
wpsClient = createWPSClientSession();
|
|
|
|
wpsClient.connect(wpsProcessingServlet);
|
|
|
|
processesDescriptionType = wpsClient
|
|
.getAllProcessDescriptions(wpsProcessingServlet);
|
|
|
|
CapabilitiesDocument capabilitiesDocument = wpsClient
|
|
.getWPSCaps(wpsProcessingServlet);
|
|
|
|
processesBriefs = capabilitiesDocument.getCapabilities()
|
|
.getProcessOfferings().getProcessArray();
|
|
return;
|
|
|
|
} catch (Throwable e) {
|
|
e.printStackTrace();
|
|
throw new ServiceException(e.getLocalizedMessage());
|
|
} finally {
|
|
if (wpsClient != null) {
|
|
wpsClient.disconnect(wpsProcessingServlet);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
private String executeProcessAsync(ExecuteRequestBuilder executeBuilder,
|
|
ProcessDescriptionType processDescription) throws Exception {
|
|
try {
|
|
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) {
|
|
logger.debug("Execute Process-> Warning, no xml structured objects will be provided: "
|
|
+ e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
}
|
|
// executeBuilder.setSchemaForOutput("http://schemas.opengis.net/gml/3.1.1/base/feature.xsd",
|
|
// "result");
|
|
|
|
ExecuteDocument execute = executeBuilder.getExecute();
|
|
execute.getExecute().setService("WPS");
|
|
// System.out.println("RESPONSE FORM:"+execute.getExecute().getResponseForm());
|
|
StatWPSClientSession wpsClient = null;
|
|
|
|
wpsClient = createWPSClientSession();
|
|
wpsClient.connect(wpsProcessingServlet);
|
|
logger.debug("Sending: " + execute);
|
|
if (execute.getExecute().getResponseForm() != null) {
|
|
ResponseDocumentType documentType = execute.getExecute()
|
|
.getResponseForm().getResponseDocument();
|
|
documentType.setStoreExecuteResponse(true);
|
|
documentType.setStatus(true);
|
|
documentType.setLineage(false);
|
|
execute.getExecute().getResponseForm()
|
|
.setResponseDocument(documentType);
|
|
}
|
|
Object responseObject = wpsClient.execute(wpsProcessingServlet,
|
|
execute);
|
|
String processLocation = null;
|
|
if (responseObject != null) {
|
|
processLocation = ((ExecuteResponseDocumentImpl) responseObject)
|
|
.getExecuteResponse().getStatusLocation();
|
|
} else
|
|
throw new Exception("Invalid Response from service: "
|
|
+ responseObject);
|
|
logger.debug("Retrieved ProcessLocation: " + processLocation);
|
|
return processLocation;
|
|
|
|
} catch (Exception e) {
|
|
logger.debug("ExecuteProcessAsync: " + e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw e;
|
|
} finally {
|
|
try {
|
|
if (wpsClient != null)
|
|
wpsClient.disconnect(wpsProcessingServlet);
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
private ProcessOutputs retrieveProcessResult(String processLocation)
|
|
throws Exception {
|
|
try {
|
|
logger.debug("RetrieveProcessResult: " + processLocation);
|
|
wpsClient = createWPSClientSession();
|
|
// wpsClient.connectForMonitoring(webProcessingService);
|
|
// wpsClient.connect(url)
|
|
|
|
Object responseObject = null;
|
|
if (processLocation != null && processLocation.length() > 0)
|
|
responseObject = wpsClient.executeViaGET(processLocation, "");
|
|
else
|
|
throw new Exception("Process Location is null!");
|
|
|
|
logger.debug("Response:\n" + responseObject);
|
|
return ((ExecuteResponseDocument) responseObject)
|
|
.getExecuteResponse().getProcessOutputs();
|
|
|
|
} catch (Exception e) {
|
|
logger.debug("RetrieveProcessResult: " + e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw e;
|
|
} finally {
|
|
wpsClient.disconnect(wpsProcessingServlet);
|
|
}
|
|
}
|
|
|
|
private 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) {
|
|
logger.debug("Dimensions Count: " + dimcounter);
|
|
}
|
|
return dimcounter;
|
|
}
|
|
|
|
private 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<String> lc = new ArrayList<String>();
|
|
for (int i = 0; i < dimensions / 2; i++) {
|
|
lc.add(bbinput[i]);
|
|
}
|
|
List<String> 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));
|
|
|
|
}
|
|
|
|
private static LinkedHashMap<String, ResponseWPS> retrieveURLsFromWPSResponse(
|
|
ComplexDataType cdt) {
|
|
org.w3c.dom.Node node = cdt.getDomNode();
|
|
LinkedHashMap<String, ResponseWPS> urls = new LinkedHashMap<>();
|
|
getURLFromXML(node, urls);
|
|
for (String key : urls.keySet()) {
|
|
logger.debug("ResponseWPS Map: " + key + "-->" + urls.get(key));
|
|
}
|
|
return urls;
|
|
}
|
|
|
|
private static String getExceptionText(String exceptionText) {
|
|
try {
|
|
String excText = "ExceptionText>";
|
|
int idx = exceptionText.indexOf(excText);
|
|
if (idx >= 0) {
|
|
String exception = exceptionText.substring(idx
|
|
+ excText.length());
|
|
exception = exception.substring(0, exception.indexOf("</"));
|
|
exception = exception
|
|
.replace("<", "")
|
|
.replace(">", "")
|
|
.replace("/", " ")
|
|
.replace("\\", " ")
|
|
.replaceAll(
|
|
"[\\]\\[!\"#$%&'\\(\\)*+/:;<=>?@\\^_`{\\|}~-]",
|
|
"_").trim();
|
|
exception = exception.replaceAll("[ ]+", " ");
|
|
if (exception.length() > 200)
|
|
exception = exception.substring(0, 200) + "...";
|
|
return exception;
|
|
} else
|
|
return "Process error in WPS Execution";
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return "Backend error in WPS Execution";
|
|
}
|
|
}
|
|
|
|
private static void getURLFromXML(org.w3c.dom.Node node,
|
|
ResponseWPS responseWPS) {
|
|
if (node == null)
|
|
return;
|
|
|
|
logger.debug("Node Name: " + node.getNodeName() + "- Value:"
|
|
+ node.getFirstChild().getNodeValue());
|
|
|
|
if (node.getNodeName() == null) {
|
|
return;
|
|
}
|
|
|
|
String text;
|
|
switch (node.getNodeName()) {
|
|
case "d4science:Data":
|
|
text = node.getFirstChild().getNodeValue();
|
|
responseWPS.setData(text);
|
|
break;
|
|
case "d4science:Description":
|
|
text = node.getFirstChild().getNodeValue();
|
|
responseWPS.setDescription(text);
|
|
break;
|
|
case "d4science:MimeType":
|
|
text = node.getFirstChild().getNodeValue();
|
|
responseWPS.setMimeType(text);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
/* logger.debug("ResponseWPS:"+responseWPS); */
|
|
|
|
}
|
|
|
|
private static void getURLFromXML(org.w3c.dom.Node node,
|
|
LinkedHashMap<String, ResponseWPS> urls) {
|
|
if (node == null)
|
|
return;
|
|
|
|
logger.debug("Node Name: " + node.getNodeName());
|
|
|
|
if (node.getNodeName() == null) {
|
|
return;
|
|
}
|
|
|
|
ResponseWPS responseWPS = null;
|
|
NodeList listnodes = node.getChildNodes();
|
|
int nChildren = listnodes.getLength();
|
|
|
|
switch (node.getNodeName()) {
|
|
case "ogr:Result":
|
|
NamedNodeMap attrs = node.getAttributes();
|
|
Node n = attrs.getNamedItem("fid");
|
|
String key = n.getNodeValue();
|
|
responseWPS = new ResponseWPS();
|
|
urls.put(key, responseWPS);
|
|
if (nChildren == 0) {
|
|
|
|
} else {
|
|
for (int i = 0; i < nChildren; i++) {
|
|
getURLFromXML(listnodes.item(i), responseWPS);
|
|
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
if (nChildren == 0) {
|
|
|
|
} else {
|
|
for (int i = 0; i < nChildren; i++) {
|
|
getURLFromXML(listnodes.item(i), urls);
|
|
|
|
}
|
|
}
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
*
|
|
* NodeList listnodes = node.getChildNodes(); int nChildren =
|
|
* listnodes.getLength();
|
|
*
|
|
* if (nChildren == 0) { String text = node.getNodeValue(); if (text !=
|
|
* null && (text.startsWith("https:") || text.startsWith("http:") ||
|
|
* text.startsWith("ftp:") || text.startsWith("smp:") || text
|
|
* .startsWith("file:"))) urls.add(text.trim()); else if (text != null
|
|
* && text.trim().length() > 0) urls.add(text.trim()); } else { for (int
|
|
* i = 0; i < nChildren; i++) { List<String> childrenurls =
|
|
* getURLFromXML(listnodes.item(i)); urls.addAll(childrenurls);
|
|
*
|
|
* } } return urls;
|
|
*/
|
|
}
|
|
|
|
// TODO
|
|
@Override
|
|
public List<Parameter> getInputParameters(Operator operator)
|
|
throws Exception {
|
|
try {
|
|
logger.info("Parameters of algorithm " + operator.getId());
|
|
|
|
ProcessInformations processInformations;
|
|
try {
|
|
processInformations = describeProcess(operator
|
|
.getId());
|
|
} catch (Throwable e) {
|
|
logger.error("GetParameters: " + e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw new ServiceException(e.getLocalizedMessage());
|
|
}
|
|
|
|
List<Parameter> parameters = new ArrayList<>();
|
|
|
|
Parameter inputParameter;
|
|
for (InputDescriptionType inputDesc : processInformations.getInputs()) {
|
|
inputParameter = WPS2SM.convert2SMType(inputDesc);
|
|
logger.debug("InputParameter: " + inputParameter);
|
|
parameters.add(inputParameter);
|
|
}
|
|
|
|
logger.debug("Parameters: " + parameters);
|
|
return parameters;
|
|
|
|
} catch (Throwable e) {
|
|
|
|
logger.error(e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw new ServiceException(e.getLocalizedMessage());
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public ComputationId startComputation(Operator operator)
|
|
throws Exception {
|
|
ProcessInformations processInformations;
|
|
try {
|
|
processInformations = describeProcess(operator
|
|
.getId());
|
|
} catch (Throwable e) {
|
|
logger.error("GetParameters: " + e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw new ServiceException(e.getLocalizedMessage());
|
|
}
|
|
|
|
LinkedHashMap<String, Parameter> inputParameters = new LinkedHashMap<>();
|
|
|
|
Parameter inputParameter;
|
|
for (InputDescriptionType inputDesc : processInformations.getInputs()) {
|
|
inputParameter = WPS2SM.convert2SMType(inputDesc);
|
|
logger.debug("InputParameter: " + inputParameter);
|
|
inputParameters.put(inputParameter.getName(), inputParameter);
|
|
}
|
|
|
|
List<Parameter> params = operator.getOperatorParameters();
|
|
Map<String, String> userInputs = new LinkedHashMap<>();
|
|
for (Parameter parm : params) {
|
|
userInputs.put(parm.getName(), parm.getValue());
|
|
logger.debug("UserInputs= key:" + parm.getName() + ", value="
|
|
+ parm.getValue());
|
|
}
|
|
|
|
String processUrl = compute(processInformations, userInputs, inputParameters);
|
|
logger.debug("Stated Computation ProcessLocation:" + processUrl);
|
|
|
|
int idIndex = processUrl.lastIndexOf("?id=");
|
|
String id;
|
|
if (idIndex > -1) {
|
|
id = processUrl.substring(idIndex + 4, processUrl.length());
|
|
} else {
|
|
logger.error("Invalid processLocation: " + processUrl);
|
|
throw new ServiceException("Invalid processLocation: " + processUrl);
|
|
}
|
|
ComputationId computationId = new ComputationId(id, processUrl);
|
|
logger.debug("ComputationId: " + computationId);
|
|
|
|
runningProcess.put(computationId, processInformations);
|
|
|
|
return computationId;
|
|
}
|
|
|
|
private String compute(ProcessInformations processInformations, Map<String, String> userInputs,
|
|
Map<String, Parameter> inputParameters) throws Exception {
|
|
try {
|
|
// setup the inputs
|
|
org.n52.wps.client.ExecuteRequestBuilder executeBuilder = new org.n52.wps.client.ExecuteRequestBuilder(
|
|
processInformations.getProcessDescription());
|
|
// for each input
|
|
for (InputDescriptionType input : processInformations.getInputs()) {
|
|
// retrieve the input from the sm config
|
|
String value = userInputs.get(input.getIdentifier()
|
|
.getStringValue());
|
|
|
|
if (value != null && value.trim().length() > 0) {
|
|
/*
|
|
* String[] values = value.split("\\|"); if (values.length
|
|
* == 0) { values = new String[1]; values[0] = value; }
|
|
*/
|
|
// fulfill an input obj for the execution
|
|
if (input.isSetLiteralData()) {
|
|
logger.debug("Configuring Literal: "
|
|
+ input.getIdentifier().getStringValue()
|
|
+ " to: " + value);
|
|
/* for (String v : values) { */
|
|
executeBuilder.addLiteralData(input.getIdentifier()
|
|
.getStringValue(), value);
|
|
/* } */
|
|
} else if (input.isSetBoundingBoxData()) {
|
|
logger.debug("Configuring Bounding Box: "
|
|
+ input.getIdentifier().getStringValue()
|
|
+ " to: " + value);
|
|
|
|
/* for (String v : values) { */
|
|
addBoundingBoxInput(executeBuilder, input
|
|
.getIdentifier().getStringValue(), value);
|
|
/* } */
|
|
} else {
|
|
logger.debug("Configuring Complex: "
|
|
+ input.getIdentifier().getStringValue()
|
|
+ " to: " + value);
|
|
SupportedComplexDataInputType complex = input
|
|
.getComplexData();
|
|
/* for (String v : values) { */
|
|
executeBuilder.addComplexDataReference(input
|
|
.getIdentifier().getStringValue(), value,
|
|
complex.getDefault().getFormat().getSchema(),
|
|
complex.getDefault().getFormat().getEncoding(),
|
|
complex.getDefault().getFormat().getMimeType());
|
|
/* } */
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
// Submit the execution
|
|
String statusLocation = executeProcessAsync(executeBuilder,
|
|
processInformations.getProcessDescription());
|
|
logger.debug("Starting Process: " + statusLocation);
|
|
return statusLocation;
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
String exceptiontext = getExceptionText(e.getLocalizedMessage());
|
|
logger.debug("WPSProcess->Returning Exception to the SM:"
|
|
+ exceptiontext);
|
|
throw new Exception(exceptiontext);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public ComputationStatus getComputationStatus(ComputationId computationId)
|
|
throws Exception {
|
|
try {
|
|
logger.debug("GetComputationStatus(): ComputationId="
|
|
+ computationId);
|
|
wpsClient = createWPSClientSession();
|
|
wpsClient.connectForMonitoring(wpsProcessingServlet);
|
|
|
|
Object responseObject = null;
|
|
if (computationId == null || computationId.getUrlId() == null
|
|
|| computationId.getUrlId().isEmpty()) {
|
|
throw new Exception("Process Location is null!");
|
|
} else {
|
|
ComputationStatus computationStatus = null;
|
|
try {
|
|
|
|
responseObject = wpsClient.executeViaGET(
|
|
computationId.getUrlId(), "");
|
|
|
|
logger.debug("ComputationStatus ResponseObject:"
|
|
+ responseObject);
|
|
|
|
if (responseObject instanceof ExecuteResponseDocumentImpl) {
|
|
if (((ExecuteResponseDocumentImpl) responseObject)
|
|
.getExecuteResponse() == null) {
|
|
logger.debug("WPS FAILURE OR PAUSED: ExecuteResponse is null");
|
|
computationStatus = new ComputationStatus(
|
|
Status.FAILED, 100f);
|
|
|
|
} else {
|
|
StatusType statusType = ((ExecuteResponseDocumentImpl) responseObject)
|
|
.getExecuteResponse().getStatus();
|
|
if (statusType == null) {
|
|
logger.debug("WPS FAILURE OR PAUSED: Status Type is null");
|
|
computationStatus = new ComputationStatus(
|
|
Status.FAILED, 100f);
|
|
} else {
|
|
|
|
int status = statusType.getProcessStarted() == null ? -1
|
|
: statusType.getProcessStarted()
|
|
.getPercentCompleted();
|
|
/*
|
|
* String processStarted =
|
|
* statusType.getProcessStarted() == null ? null
|
|
* :
|
|
* statusType.getProcessStarted().getStringValue
|
|
* ();
|
|
*/
|
|
String failure = statusType.getProcessFailed() == null ? null
|
|
: statusType.getProcessFailed()
|
|
.getExceptionReport()
|
|
.toString();
|
|
String accepted = statusType
|
|
.getProcessAccepted() == null ? null
|
|
: statusType.getProcessAccepted();
|
|
String success = statusType
|
|
.getProcessSucceeded() == null ? null
|
|
: statusType.getProcessSucceeded();
|
|
String paused = statusType.getProcessPaused() == null ? null
|
|
: statusType.getProcessPaused()
|
|
.getStringValue();
|
|
|
|
if ((failure != null && failure.length() > 0)
|
|
|| (paused != null && paused.length() > 0)) {
|
|
logger.debug("WPS FAILURE: " + failure
|
|
+ " OR PAUSED: " + paused);
|
|
computationStatus = new ComputationStatus(
|
|
Status.FAILED, 100f);
|
|
} else {
|
|
if (accepted != null
|
|
&& accepted.length() > 0) {
|
|
logger.debug("WPS ACCEPTED");
|
|
computationStatus = new ComputationStatus(
|
|
Status.PENDING, 0f);
|
|
} else {
|
|
if (success != null
|
|
&& success.length() > 0) {
|
|
logger.debug("WPS SUCCESS");
|
|
computationStatus = new ComputationStatus(
|
|
Status.COMPLETE, 100f);
|
|
} else {
|
|
if (status >= 0) {
|
|
Float statusd = (float) status;
|
|
try {
|
|
statusd = Float
|
|
.parseFloat(statusType
|
|
.getProcessStarted()
|
|
.getStringValue());
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
logger.debug("WPS STATUS:"
|
|
+ statusd);
|
|
computationStatus = new ComputationStatus(
|
|
Status.RUNNING, statusd);
|
|
} else {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
logger.debug("ComputationStatus: " + computationStatus);
|
|
return computationStatus;
|
|
} else {
|
|
logger.error("Error in ResponceObject: "
|
|
+ responseObject);
|
|
logger.error("WPS FAILURE");
|
|
computationStatus = new ComputationStatus(
|
|
Status.FAILED, 100f);
|
|
return computationStatus;
|
|
}
|
|
} catch (Throwable e) {
|
|
logger.error("WPS FAILURE: " + e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
computationStatus = new ComputationStatus(Status.FAILED,
|
|
100f);
|
|
return computationStatus;
|
|
|
|
}
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
logger.error("MonitorProcess: " + e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw e;
|
|
} finally {
|
|
wpsClient.disconnect(wpsProcessingServlet);
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public Resource getResourceByComputationId(ComputationId computationId)
|
|
throws Exception {
|
|
Map<String, Resource> resources = retrieveOutput(computationId);
|
|
MapResource mapResource = new MapResource("mapResource", "Resources",
|
|
"Resources", resources);
|
|
|
|
return mapResource;
|
|
}
|
|
|
|
private Map<String, Resource> retrieveOutput(ComputationId computationId)
|
|
throws Exception {
|
|
Map<String, Resource> outputResource = new LinkedHashMap<>();
|
|
Map<String, Parameter> outputParameters = new LinkedHashMap<>();
|
|
ProcessInformations processInformations=runningProcess.get(computationId);
|
|
|
|
Parameter outputParameter;
|
|
for (OutputDescriptionType outputDesc : processInformations.getOutputs()) {
|
|
outputParameter = WPS2SM.convert2SMType(outputDesc);
|
|
logger.debug("OutputParameter: " + outputParameter);
|
|
outputParameters.put(outputParameter.getName(), outputParameter);
|
|
}
|
|
|
|
retrieveProcessOutput(computationId.getUrlId(), outputParameters, outputResource);
|
|
|
|
return outputResource;
|
|
|
|
}
|
|
|
|
private void retrieveProcessOutput(String processLocation,
|
|
Map<String, Parameter> outputParameters,
|
|
Map<String, Resource> outputResource) throws Exception {
|
|
ProcessOutputs outs = retrieveProcessResult(processLocation);
|
|
logger.debug("Process Executed");
|
|
// retrieve the output objs
|
|
if (outs == null)
|
|
throw new Exception(
|
|
"Error during the execution of the WPS process: returned an empty document");
|
|
else {
|
|
OutputDataType[] outputData = outs.getOutputArray();
|
|
|
|
for (OutputDataType out : outputData) {
|
|
String outputID = out.getIdentifier().getStringValue();
|
|
String value = "";
|
|
if (out.getData().isSetLiteralData()) {
|
|
value = out.getData().getLiteralData().getStringValue();
|
|
Parameter paramLiteral = outputParameters.get(outputID);
|
|
if (paramLiteral != null) {
|
|
paramLiteral.setValue(value);
|
|
logger.debug("Assigning value: " + value
|
|
+ " to output named: " + outputID);
|
|
Resource resource = new ObjectResource(outputID,
|
|
paramLiteral.getName(),
|
|
paramLiteral.getDescription(),
|
|
paramLiteral.getValue());
|
|
outputResource.put(outputID, resource);
|
|
}
|
|
} else {
|
|
if (out.getData().isSetComplexData()) {
|
|
if (out.getReference() != null) {
|
|
value = out.getReference().getHref();
|
|
Parameter paramComplexData = outputParameters
|
|
.get(outputID);
|
|
if (paramComplexData != null) {
|
|
paramComplexData.setValue(value);
|
|
logger.debug("Assigning value: " + value
|
|
+ " to output named: " + outputID);
|
|
Resource resource = new ObjectResource(
|
|
outputID, paramComplexData.getName(),
|
|
paramComplexData.getDescription(),
|
|
paramComplexData.getValue());
|
|
outputResource.put(outputID, resource);
|
|
}
|
|
} else
|
|
// remove the element name, which is not useful
|
|
outputParameters.remove(outputID);
|
|
|
|
ComplexDataType cdt = out.getData().getComplexData();
|
|
LinkedHashMap<String, ResponseWPS> urls = retrieveURLsFromWPSResponse(cdt);
|
|
|
|
for (String key : urls.keySet()) {
|
|
logger.debug("Adding OBJ:" + key);
|
|
ResponseWPS responseWPS = urls.get(key);
|
|
ObjectParameter objP = new ObjectParameter(key,
|
|
responseWPS.getDescription(),
|
|
String.class.getName(), " ");
|
|
objP.setValue(responseWPS.getData());
|
|
logger.debug("ObjectParameter: " + objP);
|
|
outputParameters.put(key, objP);
|
|
if (responseWPS != null
|
|
&& responseWPS.getMimeType() != null) {
|
|
Resource resource;
|
|
switch (responseWPS.getMimeType()) {
|
|
case "image/bmp":
|
|
case "image/gif":
|
|
case "image/jpeg":
|
|
case "image/png":
|
|
resource = new ImagesResource(key,
|
|
responseWPS.getDescription(),
|
|
responseWPS.getDescription(),
|
|
responseWPS.getData(),
|
|
responseWPS.getMimeType());
|
|
outputResource.put(key, resource);
|
|
break;
|
|
case "text/csv":
|
|
if (responseWPS.getData() != null
|
|
&& !responseWPS.getData().isEmpty()) {
|
|
if (responseWPS.getData().startsWith(
|
|
"http:")
|
|
|| responseWPS.getData()
|
|
.startsWith("https:")
|
|
|| responseWPS.getData()
|
|
.startsWith("smp:")) {
|
|
resource = new FileResource(key,
|
|
responseWPS
|
|
.getDescription(),
|
|
responseWPS
|
|
.getDescription(),
|
|
responseWPS.getData(),
|
|
responseWPS.getMimeType());
|
|
outputResource.put(key, resource);
|
|
} else {
|
|
resource = new ObjectResource(key,
|
|
responseWPS
|
|
.getDescription(),
|
|
responseWPS
|
|
.getDescription(),
|
|
responseWPS.getData());
|
|
outputResource.put(key, resource);
|
|
}
|
|
} else {
|
|
resource = new ObjectResource(key,
|
|
responseWPS.getDescription(),
|
|
responseWPS.getDescription(),
|
|
responseWPS.getData());
|
|
outputResource.put(key, resource);
|
|
}
|
|
|
|
break;
|
|
default:
|
|
if (responseWPS.getData() != null
|
|
&& !responseWPS.getData().isEmpty()) {
|
|
if (responseWPS.getData().startsWith(
|
|
"http:")
|
|
|| responseWPS.getData()
|
|
.startsWith("https:")
|
|
|| responseWPS.getData()
|
|
.startsWith("smp:")) {
|
|
resource = new FileResource(key,
|
|
responseWPS
|
|
.getDescription(),
|
|
responseWPS
|
|
.getDescription(),
|
|
responseWPS.getData(),
|
|
responseWPS.getMimeType());
|
|
outputResource.put(key, resource);
|
|
} else {
|
|
resource = new ObjectResource(key,
|
|
responseWPS
|
|
.getDescription(),
|
|
responseWPS
|
|
.getDescription(),
|
|
responseWPS.getData());
|
|
outputResource.put(key, resource);
|
|
}
|
|
} else {
|
|
resource = new ObjectResource(key,
|
|
responseWPS.getDescription(),
|
|
responseWPS.getDescription(),
|
|
responseWPS.getData());
|
|
outputResource.put(key, resource);
|
|
}
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
} else {
|
|
value = out.getData().getLiteralData().getStringValue();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public String cancelComputation(ComputationId computationId)
|
|
throws Exception {
|
|
try {
|
|
wpsClient = createWPSClientSession();
|
|
|
|
String result = wpsClient.cancelComputation(
|
|
wpsCancelComputationServlet, computationId.getId());
|
|
|
|
return result;
|
|
|
|
} catch (Throwable e) {
|
|
e.printStackTrace();
|
|
throw new ServiceException(e.getLocalizedMessage());
|
|
} finally {
|
|
if (wpsClient != null) {
|
|
wpsClient.disconnect(wpsProcessingServlet);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public ComputationId resubmitComputation(ComputationId computationId)
|
|
throws Exception {
|
|
// TODO
|
|
return null;
|
|
}
|
|
|
|
}
|