2016-09-14 14:27:54 +02:00
|
|
|
package org.gcube.portlets.user.dataminermanager.server.dmservice.wps;
|
2016-04-06 18:28:32 +02:00
|
|
|
|
2016-06-08 13:02:54 +02:00
|
|
|
import java.io.BufferedReader;
|
2016-04-06 18:28:32 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2016-06-08 13:02:54 +02:00
|
|
|
import java.io.InputStreamReader;
|
2016-05-19 19:35:48 +02:00
|
|
|
import java.io.Serializable;
|
2016-04-06 18:28:32 +02:00
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2016-06-08 13:02:54 +02:00
|
|
|
import java.nio.charset.Charset;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.StandardOpenOption;
|
2016-04-06 18:28:32 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.zip.GZIPInputStream;
|
|
|
|
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
|
|
|
|
import net.opengis.ows.x11.ExceptionReportDocument;
|
|
|
|
import net.opengis.ows.x11.OperationDocument.Operation;
|
|
|
|
import net.opengis.wps.x100.CapabilitiesDocument;
|
|
|
|
import net.opengis.wps.x100.ExecuteDocument;
|
|
|
|
import net.opengis.wps.x100.ExecuteResponseDocument;
|
|
|
|
import net.opengis.wps.x100.ProcessBriefType;
|
|
|
|
import net.opengis.wps.x100.ProcessDescriptionType;
|
|
|
|
import net.opengis.wps.x100.ProcessDescriptionsDocument;
|
|
|
|
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
|
import org.apache.xmlbeans.XmlException;
|
|
|
|
import org.apache.xmlbeans.XmlObject;
|
|
|
|
import org.apache.xmlbeans.XmlOptions;
|
|
|
|
import org.n52.wps.client.ClientCapabiltiesRequest;
|
|
|
|
import org.n52.wps.client.WPSClientException;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.w3c.dom.Document;
|
|
|
|
import org.w3c.dom.Node;
|
|
|
|
import org.xml.sax.SAXException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2017-05-12 11:58:15 +02:00
|
|
|
* @author Giancarlo Panichi
|
|
|
|
*
|
2016-04-06 18:28:32 +02:00
|
|
|
*
|
|
|
|
*/
|
2016-09-14 14:27:54 +02:00
|
|
|
public class SClient4WPSSession implements Serializable {
|
2016-05-19 19:35:48 +02:00
|
|
|
|
|
|
|
private static final long serialVersionUID = -1387670579312851370L;
|
2017-05-12 12:32:43 +02:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(SClient4WPSSession.class);
|
2016-04-06 18:28:32 +02:00
|
|
|
private static final String OGC_OWS_URI = "http://www.opengeospatial.net/ows";
|
|
|
|
private static String SUPPORTED_VERSION = "1.0.0";
|
|
|
|
|
|
|
|
// private static StatWPSClientSession session;
|
2016-05-19 19:35:48 +02:00
|
|
|
private HashMap<String, CapabilitiesDocument> loggedServices;
|
2016-04-06 18:28:32 +02:00
|
|
|
private XmlOptions options = null;
|
|
|
|
|
|
|
|
// a Map of <url, all available process descriptions>
|
2016-05-19 19:35:48 +02:00
|
|
|
public HashMap<String, ProcessDescriptionsDocument> processDescriptions;
|
2016-04-06 18:28:32 +02:00
|
|
|
private String user;
|
|
|
|
private String password;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a WPS client session.
|
|
|
|
*
|
2017-05-12 12:32:43 +02:00
|
|
|
* @param user
|
|
|
|
* user
|
|
|
|
* @param password
|
|
|
|
* password
|
2016-04-06 18:28:32 +02:00
|
|
|
*/
|
2016-09-14 14:27:54 +02:00
|
|
|
public SClient4WPSSession(String user, String password) {
|
2016-05-19 19:35:48 +02:00
|
|
|
super();
|
2017-05-12 12:32:43 +02:00
|
|
|
logger.debug("Create SClient4WPSSession: [user=" + user + ", password=" + password + "]");
|
2016-04-06 18:28:32 +02:00
|
|
|
this.user = user;
|
|
|
|
this.password = password;
|
|
|
|
options = new XmlOptions();
|
|
|
|
options.setLoadStripWhitespace();
|
|
|
|
options.setLoadTrimTextBuffer();
|
|
|
|
loggedServices = new HashMap<String, CapabilitiesDocument>();
|
|
|
|
processDescriptions = new HashMap<String, ProcessDescriptionsDocument>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connects to a WPS and retrieves Capabilities plus puts all available
|
|
|
|
* Descriptions into cache.
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
* the entry point for the service. This is used as id for
|
|
|
|
* further identification of the service.
|
|
|
|
* @return true, if connect succeeded, false else.
|
|
|
|
* @throws WPSClientException
|
2017-05-12 12:32:43 +02:00
|
|
|
* wps client exception
|
2016-04-06 18:28:32 +02:00
|
|
|
*/
|
2016-05-19 19:35:48 +02:00
|
|
|
|
2016-04-06 18:28:32 +02:00
|
|
|
public boolean connect(String url) throws WPSClientException {
|
2016-05-19 19:35:48 +02:00
|
|
|
logger.info("CONNECT: " + url);
|
2017-03-14 12:33:53 +01:00
|
|
|
logger.debug("LoggedSevices: " + loggedServices.keySet());
|
2016-04-06 18:28:32 +02:00
|
|
|
if (loggedServices.containsKey(url)) {
|
2017-03-14 12:33:53 +01:00
|
|
|
logger.debug("Service already registered: " + url);
|
2016-04-06 18:28:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-19 19:35:48 +02:00
|
|
|
logger.debug("Service not registered");
|
2016-04-06 18:28:32 +02:00
|
|
|
CapabilitiesDocument capsDoc = retrieveCapsViaGET(url);
|
|
|
|
if (capsDoc != null) {
|
2016-05-19 19:35:48 +02:00
|
|
|
logger.debug("Adding caps to logged services " + url);
|
2016-04-06 18:28:32 +02:00
|
|
|
loggedServices.put(url, capsDoc);
|
2016-05-19 19:35:48 +02:00
|
|
|
logger.debug("Logged Services key: " + loggedServices.keySet());
|
|
|
|
} else {
|
|
|
|
logger.error("CapsDoc is null!");
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
2016-05-19 19:35:48 +02:00
|
|
|
|
2016-04-06 18:28:32 +02:00
|
|
|
ProcessDescriptionsDocument processDescs = describeAllProcesses(url);
|
|
|
|
if (processDescs != null && capsDoc != null) {
|
2017-05-12 12:32:43 +02:00
|
|
|
logger.debug("Adding processes descriptions to logged services " + url);
|
2016-04-06 18:28:32 +02:00
|
|
|
processDescriptions.put(url, processDescs);
|
2017-05-12 12:32:43 +02:00
|
|
|
logger.debug("ProcessDescriptions key: " + processDescriptions.keySet());
|
2016-04-06 18:28:32 +02:00
|
|
|
return true;
|
2016-05-19 19:35:48 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
logger.error("ProcessDescs is null!");
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.warn("retrieving caps failed, caps are null");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connects to a WPS and retrieves Capabilities plus puts all available
|
|
|
|
* Descriptions into cache.
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
* the entry point for the service. This is used as id for
|
|
|
|
* further identification of the service.
|
|
|
|
* @return true, if connect succeeded, false else.
|
|
|
|
* @throws WPSClientException
|
2017-05-12 12:32:43 +02:00
|
|
|
* wps client exception
|
2016-04-06 18:28:32 +02:00
|
|
|
*/
|
|
|
|
public boolean connectForMonitoring(String url) throws WPSClientException {
|
2017-03-14 12:33:53 +01:00
|
|
|
logger.debug("CONNECT");
|
2016-04-06 18:28:32 +02:00
|
|
|
if (loggedServices.containsKey(url)) {
|
2017-03-14 12:33:53 +01:00
|
|
|
logger.debug("Service already registered: " + url);
|
2016-04-06 18:28:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.warn("retrieving caps failed, caps are null");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* removes a service from the session
|
|
|
|
*
|
|
|
|
* @param url
|
2017-05-12 12:32:43 +02:00
|
|
|
* url
|
2016-04-06 18:28:32 +02:00
|
|
|
*/
|
|
|
|
public void disconnect(String url) {
|
2016-05-19 19:35:48 +02:00
|
|
|
/*
|
|
|
|
* if (loggedServices.containsKey(url)) { loggedServices.remove(url);
|
|
|
|
* processDescriptions.remove(url);
|
|
|
|
* logger.info("service removed successfully: " + url); }
|
|
|
|
*/
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the serverIDs of all loggedServices
|
|
|
|
*
|
2017-05-12 12:32:43 +02:00
|
|
|
* @return list of service
|
2016-04-06 18:28:32 +02:00
|
|
|
*/
|
|
|
|
public List<String> getLoggedServices() {
|
2016-05-19 19:35:48 +02:00
|
|
|
if (loggedServices != null && loggedServices.keySet() != null) {
|
|
|
|
return new ArrayList<String>(loggedServices.keySet());
|
|
|
|
} else {
|
|
|
|
return new ArrayList<String>();
|
|
|
|
}
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* informs you if the descriptions for the specified service is already in
|
|
|
|
* the session. in normal case it should return true :)
|
|
|
|
*
|
|
|
|
* @param serverID
|
2017-05-12 12:32:43 +02:00
|
|
|
* server id
|
|
|
|
* @return success if contain descriptions in cache
|
2016-04-06 18:28:32 +02:00
|
|
|
*/
|
|
|
|
public boolean descriptionsAvailableInCache(String serverID) {
|
|
|
|
return processDescriptions.containsKey(serverID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the cached processdescriptions of a service.
|
|
|
|
*
|
2017-05-12 12:32:43 +02:00
|
|
|
*
|
|
|
|
* @param wpsUrl
|
|
|
|
* wps url
|
|
|
|
* @return process description document
|
2016-04-06 18:28:32 +02:00
|
|
|
* @throws IOException
|
2017-05-12 12:32:43 +02:00
|
|
|
* IO exception
|
2016-04-06 18:28:32 +02:00
|
|
|
*/
|
2017-05-12 12:32:43 +02:00
|
|
|
private ProcessDescriptionsDocument getProcessDescriptionsFromCache(String wpsUrl) throws IOException {
|
2016-04-06 18:28:32 +02:00
|
|
|
if (!descriptionsAvailableInCache(wpsUrl)) {
|
|
|
|
try {
|
|
|
|
connect(wpsUrl);
|
|
|
|
} catch (WPSClientException e) {
|
|
|
|
throw new IOException("Could not initialize WPS " + wpsUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return processDescriptions.get(wpsUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return the processDescription for a specific process from Cache.
|
|
|
|
*
|
|
|
|
* @param serverID
|
2017-05-12 12:32:43 +02:00
|
|
|
* server id
|
2016-04-06 18:28:32 +02:00
|
|
|
* @param processID
|
2017-05-12 12:32:43 +02:00
|
|
|
* process id
|
2016-04-06 18:28:32 +02:00
|
|
|
* @return a ProcessDescription for a specific process from Cache.
|
|
|
|
* @throws IOException
|
2017-05-12 12:32:43 +02:00
|
|
|
* IO exception
|
2016-04-06 18:28:32 +02:00
|
|
|
*/
|
2017-05-12 12:32:43 +02:00
|
|
|
public ProcessDescriptionType getProcessDescription(String serverID, String processID) throws IOException {
|
|
|
|
ProcessDescriptionType[] processes = getProcessDescriptionsFromCache(serverID).getProcessDescriptions()
|
|
|
|
.getProcessDescriptionArray();
|
2016-04-06 18:28:32 +02:00
|
|
|
for (ProcessDescriptionType process : processes) {
|
|
|
|
if (process.getIdentifier().getStringValue().equals(processID)) {
|
|
|
|
return process;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delivers all ProcessDescriptions from a WPS
|
|
|
|
*
|
|
|
|
* @param wpsUrl
|
|
|
|
* the URL of the WPS
|
|
|
|
* @return An Array of ProcessDescriptions
|
|
|
|
* @throws IOException
|
2017-05-12 12:32:43 +02:00
|
|
|
* IO exception
|
2016-04-06 18:28:32 +02:00
|
|
|
*/
|
2017-05-12 12:32:43 +02:00
|
|
|
public ProcessDescriptionType[] getAllProcessDescriptions(String wpsUrl) throws IOException {
|
|
|
|
return getProcessDescriptionsFromCache(wpsUrl).getProcessDescriptions().getProcessDescriptionArray();
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* looks up, if the service exists already in session.
|
2017-05-12 12:32:43 +02:00
|
|
|
*
|
|
|
|
* @param serverID
|
|
|
|
* service id
|
|
|
|
* @return true if service is registered
|
2016-04-06 18:28:32 +02:00
|
|
|
*/
|
|
|
|
public boolean serviceAlreadyRegistered(String serverID) {
|
|
|
|
return loggedServices.containsKey(serverID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* provides you the cached capabilities for a specified service.
|
|
|
|
*
|
|
|
|
* @param url
|
2017-05-12 12:32:43 +02:00
|
|
|
* service url
|
|
|
|
* @return capabilities document
|
2016-04-06 18:28:32 +02:00
|
|
|
*/
|
|
|
|
public CapabilitiesDocument getWPSCaps(String url) {
|
|
|
|
return loggedServices.get(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* retrieves all current available ProcessDescriptions of a WPS. Mention: to
|
|
|
|
* get the current list of all processes, which will be requested, the
|
|
|
|
* cached capabilities will be used. Please keep that in mind. the retrieved
|
|
|
|
* descriptions will not be cached, so only transient information!
|
|
|
|
*
|
|
|
|
* @param url
|
2017-05-12 12:32:43 +02:00
|
|
|
* service url
|
|
|
|
* @return process descriptions document
|
2016-04-06 18:28:32 +02:00
|
|
|
* @throws WPSClientException
|
2017-05-12 12:32:43 +02:00
|
|
|
* WPS Client expcetion
|
2016-04-06 18:28:32 +02:00
|
|
|
*/
|
2017-05-12 12:32:43 +02:00
|
|
|
public ProcessDescriptionsDocument describeAllProcesses(String url) throws WPSClientException {
|
2016-04-06 18:28:32 +02:00
|
|
|
CapabilitiesDocument doc = loggedServices.get(url);
|
|
|
|
if (doc == null) {
|
|
|
|
logger.warn("serviceCaps are null, perhaps server does not exist");
|
|
|
|
return null;
|
|
|
|
}
|
2017-05-12 12:32:43 +02:00
|
|
|
ProcessBriefType[] processes = doc.getCapabilities().getProcessOfferings().getProcessArray();
|
2016-04-06 18:28:32 +02:00
|
|
|
String[] processIDs = new String[processes.length];
|
|
|
|
for (int i = 0; i < processIDs.length; i++) {
|
|
|
|
processIDs[i] = processes[i].getIdentifier().getStringValue();
|
|
|
|
}
|
|
|
|
return describeProcess(processIDs, url);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* retrieves the desired description for a service. the retrieved
|
|
|
|
* information will not be held in cache!
|
|
|
|
*
|
2017-05-12 12:32:43 +02:00
|
|
|
*
|
2016-04-06 18:28:32 +02:00
|
|
|
* @param processIDs
|
2017-05-12 12:32:43 +02:00
|
|
|
* process ids
|
2016-04-06 18:28:32 +02:00
|
|
|
* @param serverID
|
2017-05-12 12:32:43 +02:00
|
|
|
* server id
|
|
|
|
* @return process description document
|
2016-04-06 18:28:32 +02:00
|
|
|
* @throws WPSClientException
|
2017-05-12 12:32:43 +02:00
|
|
|
* WPS Client expcetion
|
2016-04-06 18:28:32 +02:00
|
|
|
*/
|
2017-05-12 12:32:43 +02:00
|
|
|
public ProcessDescriptionsDocument describeProcess(String[] processIDs, String serverID) throws WPSClientException {
|
2016-04-06 18:28:32 +02:00
|
|
|
|
|
|
|
CapabilitiesDocument caps = this.loggedServices.get(serverID);
|
2017-05-12 12:32:43 +02:00
|
|
|
Operation[] operations = caps.getCapabilities().getOperationsMetadata().getOperationArray();
|
2016-04-06 18:28:32 +02:00
|
|
|
String url = null;
|
|
|
|
for (Operation operation : operations) {
|
|
|
|
if (operation.getName().equals("DescribeProcess")) {
|
2017-05-12 12:32:43 +02:00
|
|
|
url = operation.getDCPArray()[0].getHTTP().getGetArray()[0].getHref();
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (url == null) {
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException("Missing DescribeOperation in Capabilities");
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
|
|
|
return retrieveDescriptionViaGET(processIDs, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes a process at a WPS
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
* url of server not the entry additionally defined in the caps.
|
|
|
|
* @param execute
|
|
|
|
* Execute document
|
|
|
|
* @return either an ExecuteResponseDocument or an InputStream if asked for
|
|
|
|
* RawData or an Exception Report
|
|
|
|
*/
|
2017-05-12 12:32:43 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param serverID
|
|
|
|
* server id
|
|
|
|
* @param execute
|
|
|
|
* execute document
|
|
|
|
* @param rawData
|
|
|
|
* true if raw data
|
|
|
|
* @return Object
|
|
|
|
* @throws WPSClientException
|
|
|
|
* WPS Client exception
|
|
|
|
*/
|
|
|
|
private Object execute(String serverID, ExecuteDocument execute, boolean rawData) throws WPSClientException {
|
2016-04-06 18:28:32 +02:00
|
|
|
CapabilitiesDocument caps = loggedServices.get(serverID);
|
2017-05-12 12:32:43 +02:00
|
|
|
Operation[] operations = caps.getCapabilities().getOperationsMetadata().getOperationArray();
|
2016-04-06 18:28:32 +02:00
|
|
|
String url = null;
|
|
|
|
for (Operation operation : operations) {
|
|
|
|
if (operation.getName().equals("Execute")) {
|
2017-05-12 12:32:43 +02:00
|
|
|
url = operation.getDCPArray()[0].getHTTP().getPostArray()[0].getHref();
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (url == null) {
|
|
|
|
throw new WPSClientException(
|
|
|
|
"Caps does not contain any information about the entry point for process execution");
|
|
|
|
}
|
|
|
|
execute.getExecute().setVersion(SUPPORTED_VERSION);
|
|
|
|
return retrieveExecuteResponseViaPOST(url, execute, rawData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes a process at a WPS
|
|
|
|
*
|
2017-05-12 12:32:43 +02:00
|
|
|
* @param serverId
|
|
|
|
* server id url of server not the entry additionally defined in
|
|
|
|
* the caps.
|
2016-04-06 18:28:32 +02:00
|
|
|
* @param execute
|
|
|
|
* Execute document
|
|
|
|
* @return either an ExecuteResponseDocument or an InputStream if asked for
|
|
|
|
* RawData or an Exception Report
|
2017-05-12 12:32:43 +02:00
|
|
|
* @throws WPSClientException
|
|
|
|
* WPS Client Exception
|
2016-04-06 18:28:32 +02:00
|
|
|
*/
|
2017-05-12 12:32:43 +02:00
|
|
|
public Object execute(String serverId, ExecuteDocument execute) throws WPSClientException {
|
|
|
|
if (execute.getExecute().isSetResponseForm() == true && execute.getExecute().isSetResponseForm() == true
|
2016-04-06 18:28:32 +02:00
|
|
|
&& execute.getExecute().getResponseForm().isSetRawDataOutput() == true) {
|
2017-05-12 11:58:15 +02:00
|
|
|
return execute(serverId, execute, true);
|
2016-04-06 18:28:32 +02:00
|
|
|
} else {
|
2017-05-12 11:58:15 +02:00
|
|
|
return execute(serverId, execute, false);
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:32:43 +02:00
|
|
|
private CapabilitiesDocument retrieveCapsViaGET(String url) throws WPSClientException {
|
2016-05-19 19:35:48 +02:00
|
|
|
logger.debug("retrieveCapsViaGET: " + url);
|
2016-04-06 18:28:32 +02:00
|
|
|
ClientCapabiltiesRequest req = new ClientCapabiltiesRequest();
|
|
|
|
url = req.getRequest(url);
|
2016-05-19 19:35:48 +02:00
|
|
|
|
2016-04-06 18:28:32 +02:00
|
|
|
try {
|
|
|
|
String authString = user + ":" + password;
|
2016-05-09 18:57:18 +02:00
|
|
|
logger.debug("auth string: " + authString);
|
2016-04-06 18:28:32 +02:00
|
|
|
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
|
|
|
|
String encoded = new String(authEncBytes);
|
2016-05-09 18:57:18 +02:00
|
|
|
logger.debug("Base64 encoded auth string: " + encoded);
|
2016-04-06 18:28:32 +02:00
|
|
|
|
|
|
|
URL urlObj = new URL(url);
|
2017-05-12 12:32:43 +02:00
|
|
|
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
|
2016-04-06 18:28:32 +02:00
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
connection.setRequestProperty("Authorization", "Basic " + encoded);
|
|
|
|
InputStream is = connection.getInputStream();
|
|
|
|
Document doc = checkInputStream(is);
|
2017-05-12 12:32:43 +02:00
|
|
|
CapabilitiesDocument capabilitiesDocument = CapabilitiesDocument.Factory.parse(doc, options);
|
2016-05-19 19:35:48 +02:00
|
|
|
return capabilitiesDocument;
|
2016-04-06 18:28:32 +02:00
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
e.printStackTrace();
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException("Capabilities URL seems to be unvalid: " + url, e);
|
2016-04-06 18:28:32 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException("Error occured while retrieving capabilities from url: " + url, e);
|
2016-04-06 18:28:32 +02:00
|
|
|
} catch (XmlException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new WPSClientException("Error occured while parsing XML", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:32:43 +02:00
|
|
|
private ProcessDescriptionsDocument retrieveDescriptionViaGET(String[] processIDs, String url)
|
|
|
|
throws WPSClientException {
|
2016-04-06 18:28:32 +02:00
|
|
|
try {
|
2017-05-12 12:32:43 +02:00
|
|
|
logger.debug("RetrieveDescription GET: " + processIDs + " url:" + url);
|
2016-04-06 18:28:32 +02:00
|
|
|
|
2017-05-12 12:32:43 +02:00
|
|
|
Path tempFile = Files.createTempFile("WPSProcessDescriptions", "txt");
|
2016-06-08 13:02:54 +02:00
|
|
|
|
|
|
|
List<String> lines = new ArrayList<>();
|
|
|
|
lines.add("<wps:ProcessDescriptions xmlns:wps=\"http://www.opengis.net/wps/1.0.0\" "
|
|
|
|
+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
|
|
|
|
+ "xmlns:ows=\"http://www.opengis.net/ows/1.1\" "
|
|
|
|
+ "xsi:schemaLocation=\"http://www.opengis.net/wps/1.0.0 "
|
2017-05-12 12:32:43 +02:00
|
|
|
+ "http://schemas.opengis.net/wps/1.0.0/wpsDescribeProcess_response.xsd\" " + "xml:lang=\"en-US\" "
|
2016-06-08 13:02:54 +02:00
|
|
|
+ "service=\"WPS\" version=\"1.0.0\">");
|
2017-05-12 12:32:43 +02:00
|
|
|
Files.write(tempFile, lines, Charset.defaultCharset(), StandardOpenOption.APPEND);
|
2016-06-08 13:02:54 +02:00
|
|
|
|
|
|
|
for (String processId : processIDs) {
|
|
|
|
String[] process = { processId };
|
2016-09-14 14:27:54 +02:00
|
|
|
DClientDescribeProcessRequest req = new DClientDescribeProcessRequest();
|
2016-06-08 13:02:54 +02:00
|
|
|
req.setIdentifier(process);
|
|
|
|
String requestURL = req.getRequest(url);
|
|
|
|
|
|
|
|
String authString = user + ":" + password;
|
|
|
|
logger.debug("auth string: " + authString);
|
2017-05-12 12:32:43 +02:00
|
|
|
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
|
2016-06-08 13:02:54 +02:00
|
|
|
String encoded = new String(authEncBytes);
|
|
|
|
logger.debug("Base64 encoded auth string: " + encoded);
|
|
|
|
|
|
|
|
URL urlObj = new URL(requestURL);
|
2017-05-12 12:32:43 +02:00
|
|
|
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
|
2016-06-08 13:02:54 +02:00
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
connection.setDoOutput(true);
|
2017-05-12 12:32:43 +02:00
|
|
|
connection.setRequestProperty("Authorization", "Basic " + encoded);
|
2016-06-08 13:02:54 +02:00
|
|
|
InputStream is = connection.getInputStream();
|
2017-05-12 12:32:43 +02:00
|
|
|
lines = retrievesSingleDescription(is);
|
|
|
|
Files.write(tempFile, lines, Charset.defaultCharset(), StandardOpenOption.APPEND);
|
2016-06-08 13:02:54 +02:00
|
|
|
}
|
|
|
|
lines = new ArrayList<>();
|
|
|
|
lines.add("</wps:ProcessDescriptions>");
|
2017-05-12 12:32:43 +02:00
|
|
|
Files.write(tempFile, lines, Charset.defaultCharset(), StandardOpenOption.APPEND);
|
2016-06-08 13:02:54 +02:00
|
|
|
|
|
|
|
logger.debug(tempFile.toString());
|
2017-05-12 12:32:43 +02:00
|
|
|
Document doc = null;
|
|
|
|
try (InputStream inputStream = Files.newInputStream(tempFile, StandardOpenOption.READ)) {
|
2016-06-08 13:02:54 +02:00
|
|
|
doc = checkInputStream(inputStream);
|
2017-05-12 12:32:43 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
ProcessDescriptionsDocument processDescriptionsDocument = ProcessDescriptionsDocument.Factory.parse(doc,
|
|
|
|
options);
|
|
|
|
|
2016-06-08 13:02:54 +02:00
|
|
|
Files.delete(tempFile);
|
|
|
|
return processDescriptionsDocument;
|
2017-05-12 12:32:43 +02:00
|
|
|
|
2016-04-06 18:28:32 +02:00
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
logger.error("URL seems not to be valid");
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new WPSClientException("URL seems not to be valid", e);
|
|
|
|
} catch (IOException e) {
|
|
|
|
logger.error("Error occured while receiving data");
|
|
|
|
e.printStackTrace();
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException("Error occured while receiving data", e);
|
2016-04-06 18:28:32 +02:00
|
|
|
} catch (XmlException e) {
|
|
|
|
logger.error("Error occured while parsing ProcessDescription document");
|
|
|
|
e.printStackTrace();
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException("Error occured while parsing ProcessDescription document", e);
|
2016-04-06 18:28:32 +02:00
|
|
|
} catch (Throwable e) {
|
|
|
|
logger.error(e.getLocalizedMessage());
|
|
|
|
e.printStackTrace();
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException(e.getLocalizedMessage(), new Exception(e));
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:32:43 +02:00
|
|
|
protected List<String> retrievesSingleDescription(InputStream is) throws WPSClientException {
|
2016-06-08 13:02:54 +02:00
|
|
|
try {
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
|
|
|
|
|
|
|
List<String> lines = new ArrayList<>();
|
|
|
|
String line = null;
|
|
|
|
boolean elementProcessDescriptionsFound = false;
|
|
|
|
boolean elementProcessDescriptionsClosureFound = false;
|
|
|
|
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
if (elementProcessDescriptionsFound) {
|
|
|
|
if (elementProcessDescriptionsClosureFound) {
|
|
|
|
if (line.contains("</wps:ProcessDescriptions>")) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
lines.add(line);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int closeIndex = line.indexOf(">");
|
|
|
|
if (closeIndex != -1) {
|
|
|
|
elementProcessDescriptionsClosureFound = true;
|
|
|
|
if (closeIndex == line.length() - 1) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (line.contains("<wps:ProcessDescriptions")) {
|
|
|
|
elementProcessDescriptionsFound = true;
|
|
|
|
int closeIndex = line.indexOf(">");
|
|
|
|
if (closeIndex != -1) {
|
|
|
|
elementProcessDescriptionsClosureFound = true;
|
|
|
|
if (closeIndex == line.length() - 1) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lines;
|
|
|
|
} catch (Throwable e) {
|
|
|
|
logger.error(e.getLocalizedMessage());
|
|
|
|
e.printStackTrace();
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException(e.getLocalizedMessage(), new Exception(e));
|
2016-06-08 13:02:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:32:43 +02:00
|
|
|
private InputStream retrieveDataViaPOST(XmlObject obj, String urlString) throws WPSClientException {
|
2016-04-06 18:28:32 +02:00
|
|
|
try {
|
|
|
|
logger.debug("RetrieveDataViaPost(): " + urlString);
|
|
|
|
String authString = user + ":" + password;
|
2016-05-09 18:57:18 +02:00
|
|
|
logger.debug("auth string: " + authString);
|
2016-04-06 18:28:32 +02:00
|
|
|
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
|
|
|
|
String encoded = new String(authEncBytes);
|
2016-05-09 18:57:18 +02:00
|
|
|
logger.debug("Base64 encoded auth string: " + encoded);
|
2016-04-06 18:28:32 +02:00
|
|
|
|
|
|
|
URL url = new URL(urlString);
|
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
2017-11-14 19:42:47 +01:00
|
|
|
|
2016-04-06 18:28:32 +02:00
|
|
|
conn.setRequestMethod("POST");
|
|
|
|
conn.setRequestProperty("Authorization", "Basic " + encoded);
|
|
|
|
conn.setRequestProperty("Accept-Encoding", "gzip");
|
2017-11-14 19:42:47 +01:00
|
|
|
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
|
2016-04-06 18:28:32 +02:00
|
|
|
conn.setDoOutput(true);
|
|
|
|
obj.save(conn.getOutputStream());
|
|
|
|
InputStream input = null;
|
|
|
|
String encoding = conn.getContentEncoding();
|
|
|
|
if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
|
2017-11-14 19:42:47 +01:00
|
|
|
logger.debug("Read encoding GZIP");
|
2016-04-06 18:28:32 +02:00
|
|
|
input = new GZIPInputStream(conn.getInputStream());
|
|
|
|
} else {
|
2017-11-14 19:42:47 +01:00
|
|
|
logger.debug("Read encoding: "+encoding);
|
2016-04-06 18:28:32 +02:00
|
|
|
input = conn.getInputStream();
|
|
|
|
}
|
|
|
|
return input;
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new WPSClientException("URL seems to be unvalid", e);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new WPSClientException("Error while transmission", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Document checkInputStream(InputStream is) throws WPSClientException {
|
|
|
|
try {
|
2016-06-08 13:02:54 +02:00
|
|
|
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
|
|
|
|
fac.setNamespaceAware(true);
|
2016-04-06 18:28:32 +02:00
|
|
|
Document doc = fac.newDocumentBuilder().parse(is);
|
2016-06-08 13:02:54 +02:00
|
|
|
logger.debug("Document: " + doc);
|
|
|
|
if (doc == null) {
|
|
|
|
logger.error("Document is null");
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException("Error in check input stream: Document is null");
|
2016-06-08 13:02:54 +02:00
|
|
|
}
|
|
|
|
|
2017-05-12 12:32:43 +02:00
|
|
|
if (getFirstElementNode(doc.getFirstChild()).getLocalName().equals("ExceptionReport")
|
|
|
|
&& getFirstElementNode(doc.getFirstChild()).getNamespaceURI().equals(OGC_OWS_URI)) {
|
2016-04-06 18:28:32 +02:00
|
|
|
try {
|
2017-05-12 12:32:43 +02:00
|
|
|
ExceptionReportDocument exceptionDoc = ExceptionReportDocument.Factory.parse(doc);
|
2016-04-06 18:28:32 +02:00
|
|
|
logger.debug(exceptionDoc.xmlText(options));
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException("Error occured while executing query", exceptionDoc);
|
2016-04-06 18:28:32 +02:00
|
|
|
} catch (XmlException e) {
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException("Error while parsing ExceptionReport retrieved from server", e);
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
2016-06-08 13:02:54 +02:00
|
|
|
} else {
|
|
|
|
logger.debug("No Exception Report");
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
|
|
|
return doc;
|
|
|
|
} catch (SAXException e) {
|
2017-05-12 12:32:43 +02:00
|
|
|
logger.error("Error while parsing input: " + e.getLocalizedMessage());
|
2016-04-06 18:28:32 +02:00
|
|
|
e.printStackTrace();
|
2016-06-08 13:02:54 +02:00
|
|
|
throw new WPSClientException("Error while parsing input", e);
|
2016-04-06 18:28:32 +02:00
|
|
|
} catch (IOException e) {
|
2017-05-12 12:32:43 +02:00
|
|
|
logger.error("Error occured while transfer: " + e.getLocalizedMessage());
|
2016-04-06 18:28:32 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
throw new WPSClientException("Error occured while transfer", e);
|
|
|
|
} catch (ParserConfigurationException e) {
|
2017-05-12 12:32:43 +02:00
|
|
|
logger.error("Error occured, parser is not correctly configured: " + e.getLocalizedMessage());
|
2016-04-06 18:28:32 +02:00
|
|
|
e.printStackTrace();
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException("Error occured, parser is not correctly configured", e);
|
2016-06-08 13:02:54 +02:00
|
|
|
} catch (WPSClientException e) {
|
|
|
|
throw e;
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Node getFirstElementNode(Node node) {
|
|
|
|
if (node == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
|
|
|
return node;
|
|
|
|
} else {
|
|
|
|
return getFirstElementNode(node.getNextSibling());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:32:43 +02:00
|
|
|
private Object retrieveExecuteResponseViaPOST(String url, ExecuteDocument doc, boolean rawData)
|
|
|
|
throws WPSClientException {
|
2016-04-06 18:28:32 +02:00
|
|
|
InputStream is = retrieveDataViaPOST(doc, url);
|
|
|
|
if (rawData) {
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
Document documentObj = checkInputStream(is);
|
|
|
|
ExceptionReportDocument erDoc = null;
|
|
|
|
try {
|
|
|
|
|
|
|
|
return ExecuteResponseDocument.Factory.parse(documentObj);
|
|
|
|
} catch (XmlException e) {
|
|
|
|
try {
|
|
|
|
erDoc = ExceptionReportDocument.Factory.parse(documentObj);
|
|
|
|
} catch (XmlException e1) {
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException("Error occured while parsing executeResponse", e);
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
|
|
|
return erDoc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String[] getProcessNames(String url) throws IOException {
|
2017-05-12 12:32:43 +02:00
|
|
|
ProcessDescriptionType[] processes = getProcessDescriptionsFromCache(url).getProcessDescriptions()
|
|
|
|
.getProcessDescriptionArray();
|
2016-04-06 18:28:32 +02:00
|
|
|
String[] processNames = new String[processes.length];
|
|
|
|
for (int i = 0; i < processNames.length; i++) {
|
|
|
|
processNames[i] = processes[i].getIdentifier().getStringValue();
|
|
|
|
}
|
|
|
|
return processNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes a process at a WPS
|
|
|
|
*
|
2017-05-12 11:58:15 +02:00
|
|
|
* @param urlString
|
2016-04-06 18:28:32 +02:00
|
|
|
* url of server not the entry additionally defined in the caps.
|
|
|
|
* @param executeAsGETString
|
|
|
|
* KVP Execute request
|
|
|
|
* @return either an ExecuteResponseDocument or an InputStream if asked for
|
|
|
|
* RawData or an Exception Report
|
2017-05-12 12:32:43 +02:00
|
|
|
* @throws WPSClientException
|
|
|
|
* WPS Client exception
|
2016-04-06 18:28:32 +02:00
|
|
|
*/
|
2017-05-12 12:32:43 +02:00
|
|
|
public Object executeViaGET(String urlString, String executeAsGETString) throws WPSClientException {
|
2016-04-06 18:28:32 +02:00
|
|
|
urlString = urlString + executeAsGETString;
|
|
|
|
logger.debug("ExecuteViaGet() Url: " + urlString);
|
|
|
|
try {
|
|
|
|
// TODO
|
|
|
|
String authString = user + ":" + password;
|
|
|
|
logger.debug("auth string: " + authString);
|
|
|
|
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
|
|
|
|
String encoded = new String(authEncBytes);
|
|
|
|
logger.debug("Base64 encoded auth string: " + encoded);
|
|
|
|
|
|
|
|
URL url = new URL(urlString);
|
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
|
conn.setRequestMethod("GET");
|
|
|
|
conn.setRequestProperty("Authorization", "Basic " + encoded);
|
|
|
|
conn.setDoOutput(true);
|
|
|
|
InputStream is = conn.getInputStream();
|
|
|
|
|
|
|
|
if (executeAsGETString.toUpperCase().contains("RAWDATA")) {
|
|
|
|
logger.debug("ExecuteAsGETString as RAWDATA");
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
Document doc = checkInputStream(is);
|
|
|
|
ExceptionReportDocument erDoc = null;
|
|
|
|
logger.debug("ExecuteAsGETString as Document");
|
|
|
|
|
|
|
|
try {
|
|
|
|
return ExecuteResponseDocument.Factory.parse(doc);
|
|
|
|
} catch (XmlException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
try {
|
|
|
|
erDoc = ExceptionReportDocument.Factory.parse(doc);
|
|
|
|
} catch (XmlException e1) {
|
|
|
|
e1.printStackTrace();
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException("Error occured while parsing executeResponse", e);
|
2016-04-06 18:28:32 +02:00
|
|
|
|
|
|
|
}
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException("Error occured while parsing executeResponse", erDoc);
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
e.printStackTrace();
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException("Capabilities URL seems to be unvalid: " + urlString, e);
|
2016-04-06 18:28:32 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException("Error occured while retrieving capabilities from url: " + urlString, e);
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-05-19 19:35:48 +02:00
|
|
|
|
2017-05-12 12:32:43 +02:00
|
|
|
public String cancelComputation(String url, String computationId) throws WPSClientException {
|
2016-05-19 19:35:48 +02:00
|
|
|
|
2016-04-06 18:28:32 +02:00
|
|
|
try {
|
|
|
|
String authString = user + ":" + password;
|
2016-05-09 18:57:18 +02:00
|
|
|
logger.debug("auth string: " + authString);
|
2016-04-06 18:28:32 +02:00
|
|
|
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
|
|
|
|
String encoded = new String(authEncBytes);
|
2016-05-09 18:57:18 +02:00
|
|
|
logger.debug("Base64 encoded auth string: " + encoded);
|
2016-04-06 18:28:32 +02:00
|
|
|
|
2016-05-19 19:35:48 +02:00
|
|
|
url += "?id=" + computationId;
|
2016-04-06 18:28:32 +02:00
|
|
|
URL urlObj = new URL(url);
|
2017-05-12 12:32:43 +02:00
|
|
|
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
|
2016-04-06 18:28:32 +02:00
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
connection.setRequestProperty("Authorization", "Basic " + encoded);
|
2016-05-19 19:35:48 +02:00
|
|
|
String responseMessage = connection.getResponseMessage();
|
2016-04-06 18:28:32 +02:00
|
|
|
return responseMessage;
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
e.printStackTrace();
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException("Capabilities URL seems to be unvalid: " + url, e);
|
2016-04-06 18:28:32 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2017-05-12 12:32:43 +02:00
|
|
|
throw new WPSClientException("Error occured while retrieving capabilities from url: " + url, e);
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|