git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/SeaDataNetConnector@152668 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
bf7c9ffaf0
commit
8d02d0ebe2
|
@ -10,15 +10,27 @@ import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.io.PrintWriter;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
|
||||||
|
import org.apache.commons.httpclient.HttpClient;
|
||||||
|
import org.apache.commons.httpclient.methods.PostMethod;
|
||||||
|
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
|
||||||
|
import org.apache.commons.httpclient.methods.multipart.FilePart;
|
||||||
|
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
|
||||||
|
import org.apache.commons.httpclient.methods.multipart.Part;
|
||||||
|
import org.apache.commons.httpclient.methods.multipart.StringPart;
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
|
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
|
||||||
import org.gcube.dataanalysis.ecoengine.configuration.AlgorithmConfiguration;
|
import org.gcube.dataanalysis.ecoengine.configuration.AlgorithmConfiguration;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
|
@ -28,10 +40,36 @@ import org.xml.sax.SAXException;
|
||||||
|
|
||||||
public class DivaHTTPClient {
|
public class DivaHTTPClient {
|
||||||
|
|
||||||
//public static String WEB_HTTP="http://gher-diva.phys.ulg.ac.be/web-vis/Python/web";
|
|
||||||
public static String WEB_HTTP = "http://ec.oceanbrowser.net/emodnet/Python/web";
|
public static String WEB_HTTP = "http://ec.oceanbrowser.net/emodnet/Python/web";
|
||||||
public static String DOWN_FILE_NC = "/download?fieldname=";
|
public static String DOWN_FILE_NC = "/download?fieldname=";
|
||||||
|
|
||||||
|
// -F "sessionid=your_session_id" -F uname=u -F vname=v -F
|
||||||
|
// "data_vel=@/home/abarth/workspace/divaonweb-test-data/velocity/example123.nc"
|
||||||
|
// -F "sessionid=your_session_id" -F uname=u -F vname=v -F
|
||||||
|
// "data_vel=@/home/abarth/workspace/divaonweb-test-data/velocity/example123.nc"
|
||||||
|
|
||||||
|
public static int postVelocityFiles(String id, String uname, String vname, File binaryFile) throws Exception {
|
||||||
|
|
||||||
|
String url = WEB_HTTP + "/uploadvel";
|
||||||
|
PostMethod post = new PostMethod(url);
|
||||||
|
|
||||||
|
Part[] parts = new Part[] {
|
||||||
|
new FilePart("data_vel",binaryFile),
|
||||||
|
new StringPart("sessionid",id),
|
||||||
|
new StringPart("uname",uname),
|
||||||
|
new StringPart("vname",vname)
|
||||||
|
};
|
||||||
|
|
||||||
|
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
|
||||||
|
HttpClient client = new HttpClient();
|
||||||
|
int response = client.executeMethod(post);
|
||||||
|
|
||||||
|
AnalysisLogger.getLogger().debug("VELOCITY FILE POST Response:"+response);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String postFile(File file) throws Exception {
|
public static String postFile(File file) throws Exception {
|
||||||
String crlf = "\r\n";
|
String crlf = "\r\n";
|
||||||
String twoHyphens = "--";
|
String twoHyphens = "--";
|
||||||
|
@ -39,7 +77,7 @@ public class DivaHTTPClient {
|
||||||
|
|
||||||
HttpURLConnection httpUrlConnection = null;
|
HttpURLConnection httpUrlConnection = null;
|
||||||
try {
|
try {
|
||||||
|
AnalysisLogger.getLogger().debug("Sending file ...");
|
||||||
URL url = new URL(WEB_HTTP + "/upload");
|
URL url = new URL(WEB_HTTP + "/upload");
|
||||||
httpUrlConnection = (HttpURLConnection) url.openConnection();
|
httpUrlConnection = (HttpURLConnection) url.openConnection();
|
||||||
httpUrlConnection.setUseCaches(false);
|
httpUrlConnection.setUseCaches(false);
|
||||||
|
@ -49,15 +87,12 @@ public class DivaHTTPClient {
|
||||||
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
|
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
|
||||||
httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
|
httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
|
||||||
|
|
||||||
httpUrlConnection.setRequestProperty(
|
httpUrlConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
|
||||||
"Content-Type", "multipart/form-data;boundary=" + boundary);
|
|
||||||
|
|
||||||
DataOutputStream request = new DataOutputStream(
|
DataOutputStream request = new DataOutputStream(httpUrlConnection.getOutputStream());
|
||||||
httpUrlConnection.getOutputStream());
|
|
||||||
|
|
||||||
request.writeBytes("--" + boundary + crlf);
|
request.writeBytes("--" + boundary + crlf);
|
||||||
request.writeBytes("Content-Disposition: form-data; name=\"data" + "\";filename=\"" +
|
request.writeBytes("Content-Disposition: form-data; name=\"data" + "\";filename=\"" + file.getName() + "\"" + crlf);
|
||||||
file.getName() + "\"" + crlf);
|
|
||||||
request.writeBytes(crlf);
|
request.writeBytes(crlf);
|
||||||
|
|
||||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||||
|
@ -67,18 +102,14 @@ public class DivaHTTPClient {
|
||||||
lineString = reader.readLine();
|
lineString = reader.readLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
reader.close();
|
reader.close();
|
||||||
request.writeBytes(crlf);
|
request.writeBytes(crlf);
|
||||||
request.writeBytes(twoHyphens + boundary +
|
request.writeBytes(twoHyphens + boundary + twoHyphens + crlf);
|
||||||
twoHyphens + crlf);
|
|
||||||
request.flush();
|
request.flush();
|
||||||
request.close();
|
request.close();
|
||||||
InputStream responseStream = new
|
InputStream responseStream = new BufferedInputStream(httpUrlConnection.getInputStream());
|
||||||
BufferedInputStream(httpUrlConnection.getInputStream());
|
|
||||||
|
|
||||||
BufferedReader responseStreamReader =
|
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
|
||||||
new BufferedReader(new InputStreamReader(responseStream));
|
|
||||||
|
|
||||||
String line = "";
|
String line = "";
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
@ -102,21 +133,18 @@ public class DivaHTTPClient {
|
||||||
httpUrlConnection.disconnect();
|
httpUrlConnection.disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sessionid
|
* sessionid id from previous upload len correlation length (arc degrees)
|
||||||
id from previous upload
|
* stn signal to noise ratio (non-dimensional) x0,x1,dx first and last
|
||||||
len
|
* longitude values and resolution of the grid y0,y1,dy idem for latitude
|
||||||
correlation length (arc degrees)
|
* level depth level
|
||||||
stn
|
|
||||||
signal to noise ratio (non-dimensional)
|
|
||||||
x0,x1,dx
|
|
||||||
first and last longitude values and resolution of the grid
|
|
||||||
y0,y1,dy
|
|
||||||
idem for latitude
|
|
||||||
level
|
|
||||||
depth level
|
|
||||||
*/
|
*/
|
||||||
public static DivaAnalysisGetResponse getAnalysis(String id, double len, double stn, double x0, double x1, double dx, double y0, double y1, double dy, double level) throws Exception {
|
public static DivaAnalysisGetResponse getAnalysis(String id, double len, double stn, double x0, double x1, double dx, double y0, double y1, double dy, double level) throws Exception {
|
||||||
|
return getAnalysis(id, len, stn, x0, x1, dx, y0, y1, dy, level, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DivaAnalysisGetResponse getAnalysis(String id, double len, double stn, double x0, double x1, double dx, double y0, double y1, double dy, double level, Boolean useCurrents) throws Exception {
|
||||||
HttpURLConnection httpUrlConnection = null;
|
HttpURLConnection httpUrlConnection = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -131,7 +159,10 @@ public class DivaHTTPClient {
|
||||||
get_url += "y1=" + y1 + "&";
|
get_url += "y1=" + y1 + "&";
|
||||||
get_url += "dy=" + dy + "&";
|
get_url += "dy=" + dy + "&";
|
||||||
get_url += "level=" + level + "";
|
get_url += "level=" + level + "";
|
||||||
|
if (useCurrents)
|
||||||
|
get_url += "&adv=1";
|
||||||
// System.out.println("GET URL="+get_url);
|
// System.out.println("GET URL="+get_url);
|
||||||
|
AnalysisLogger.getLogger().debug("Get URL to sent: \n" + get_url);
|
||||||
URL url = new URL(get_url);
|
URL url = new URL(get_url);
|
||||||
|
|
||||||
httpUrlConnection = (HttpURLConnection) url.openConnection();
|
httpUrlConnection = (HttpURLConnection) url.openConnection();
|
||||||
|
@ -141,11 +172,9 @@ public class DivaHTTPClient {
|
||||||
httpUrlConnection.setRequestMethod("GET");
|
httpUrlConnection.setRequestMethod("GET");
|
||||||
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
|
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
|
||||||
httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
|
httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
|
||||||
InputStream responseStream = new
|
InputStream responseStream = new BufferedInputStream(httpUrlConnection.getInputStream());
|
||||||
BufferedInputStream(httpUrlConnection.getInputStream());
|
|
||||||
|
|
||||||
BufferedReader responseStreamReader =
|
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
|
||||||
new BufferedReader(new InputStreamReader(responseStream));
|
|
||||||
|
|
||||||
String line = "";
|
String line = "";
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
@ -156,7 +185,6 @@ public class DivaHTTPClient {
|
||||||
|
|
||||||
responseStreamReader.close();
|
responseStreamReader.close();
|
||||||
|
|
||||||
|
|
||||||
String response = stringBuilder.toString();
|
String response = stringBuilder.toString();
|
||||||
|
|
||||||
// System.out.println("RESPONSE STRING: "+response);
|
// System.out.println("RESPONSE STRING: "+response);
|
||||||
|
@ -182,12 +210,63 @@ public class DivaHTTPClient {
|
||||||
httpUrlConnection.disconnect();
|
httpUrlConnection.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DivaFitResponse getFit(String id) throws Exception {
|
||||||
|
HttpURLConnection httpUrlConnection = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
String get_url = WEB_HTTP + "/fit?";
|
||||||
|
get_url += "sessionid=" + id;
|
||||||
|
System.out.println("GET FIT URL=" + get_url);
|
||||||
|
URL url = new URL(get_url);
|
||||||
|
|
||||||
|
httpUrlConnection = (HttpURLConnection) url.openConnection();
|
||||||
|
httpUrlConnection.setUseCaches(false);
|
||||||
|
httpUrlConnection.setDoOutput(true);
|
||||||
|
|
||||||
|
httpUrlConnection.setRequestMethod("GET");
|
||||||
|
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
|
||||||
|
httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
|
||||||
|
InputStream responseStream = new BufferedInputStream(httpUrlConnection.getInputStream());
|
||||||
|
|
||||||
|
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
|
||||||
|
|
||||||
|
String line = "";
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
while ((line = responseStreamReader.readLine()) != null) {
|
||||||
|
stringBuilder.append(line).append("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
responseStreamReader.close();
|
||||||
|
String response = stringBuilder.toString();
|
||||||
|
|
||||||
|
// System.out.println("RESPONSE STRING: "+response);
|
||||||
|
AnalysisLogger.getLogger().debug("Response from DIVA Fit: \n" + response);
|
||||||
|
|
||||||
|
Document doc = loadXMLFromString(response);
|
||||||
|
Double length_scale = Double.parseDouble(doc.getElementsByTagName("stat").item(0).getTextContent());
|
||||||
|
Double signal_to_noise = Double.parseDouble(doc.getElementsByTagName("stat").item(1).getTextContent());
|
||||||
|
Double variance_of_background = Double.parseDouble(doc.getElementsByTagName("stat").item(2).getTextContent());
|
||||||
|
Double fit_quality = Double.parseDouble(doc.getElementsByTagName("stat").item(3).getTextContent());
|
||||||
|
String plot = (doc.getElementsByTagName("plot").item(0).getTextContent());
|
||||||
|
|
||||||
|
responseStream.close();
|
||||||
|
httpUrlConnection.disconnect();
|
||||||
|
|
||||||
|
return new DivaFitResponse(id, length_scale, signal_to_noise, variance_of_background, fit_quality, plot);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new Exception(e);
|
||||||
|
} finally {
|
||||||
|
if (httpUrlConnection != null)
|
||||||
|
httpUrlConnection.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static DivaFilePostResponse uploadFile(File file) throws Exception, IOException, ParserConfigurationException, SAXException {
|
||||||
public static DivaFilePostResponse uploadFile(File file) throws Exception, IOException, ParserConfigurationException, SAXException
|
|
||||||
{
|
|
||||||
String result_xml = null;
|
String result_xml = null;
|
||||||
try {
|
try {
|
||||||
result_xml = postFile(file);
|
result_xml = postFile(file);
|
||||||
|
@ -199,15 +278,14 @@ public class DivaHTTPClient {
|
||||||
throw new SAXException("Error in HTTP response no response element found!");
|
throw new SAXException("Error in HTTP response no response element found!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Create Document object by XMLResponse
|
// Create Document object by XMLResponse
|
||||||
Document doc = loadXMLFromString(result_xml);
|
Document doc = loadXMLFromString(result_xml);
|
||||||
// Print XML Response
|
// Print XML Response
|
||||||
AnalysisLogger.getLogger().debug("DIVA XML Response: \n" + result_xml);
|
AnalysisLogger.getLogger().debug("DIVA XML Response: \n" + result_xml);
|
||||||
|
|
||||||
|
|
||||||
Node root = doc.getDocumentElement();
|
Node root = doc.getDocumentElement();
|
||||||
if(root== null) throw new SAXException("Error in HTTP response no root element found!");
|
if (root == null)
|
||||||
|
throw new SAXException("Error in HTTP response no root element found!");
|
||||||
Integer obs_count = null;
|
Integer obs_count = null;
|
||||||
try {
|
try {
|
||||||
obs_count = Integer.parseInt(root.getChildNodes().item(1).getTextContent());
|
obs_count = Integer.parseInt(root.getChildNodes().item(1).getTextContent());
|
||||||
|
@ -250,7 +328,6 @@ public class DivaHTTPClient {
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
AnalysisLogger.setLogger("cfg/" + AlgorithmConfiguration.defaultLoggerFile);
|
AnalysisLogger.setLogger("cfg/" + AlgorithmConfiguration.defaultLoggerFile);
|
||||||
DivaFilePostResponse response = uploadFile(new File("resources" + File.separator + "files" + File.separator + "temperature_argo.txt"));
|
DivaFilePostResponse response = uploadFile(new File("resources" + File.separator + "files" + File.separator + "temperature_argo.txt"));
|
||||||
|
@ -264,16 +341,14 @@ public class DivaHTTPClient {
|
||||||
// System.out.println(System.getProperty("user.dir")+File.separator+"temp.nc");
|
// System.out.println(System.getProperty("user.dir")+File.separator+"temp.nc");
|
||||||
File f = new File(System.getProperty("user.dir") + File.separator + "temp.nc");
|
File f = new File(System.getProperty("user.dir") + File.separator + "temp.nc");
|
||||||
|
|
||||||
if(!f.exists()) f.createNewFile();
|
if (!f.exists())
|
||||||
downloadFileDiva(WEB_HTTP+DOWN_FILE_NC+
|
f.createNewFile();
|
||||||
respAnalysis.getIdentifier(),System.getProperty("user.dir")+File.separator+"temp.nc");
|
downloadFileDiva(WEB_HTTP + DOWN_FILE_NC + respAnalysis.getIdentifier(), System.getProperty("user.dir") + File.separator + "temp.nc");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// PARSER XML
|
// PARSER XML
|
||||||
private static Document loadXMLFromString(String xml) throws ParserConfigurationException, SAXException, IOException
|
private static Document loadXMLFromString(String xml) throws ParserConfigurationException, SAXException, IOException {
|
||||||
{
|
|
||||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||||
InputSource is = new InputSource(new StringReader(xml));
|
InputSource is = new InputSource(new StringReader(xml));
|
||||||
|
|
Loading…
Reference in New Issue