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.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
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.dataanalysis.ecoengine.configuration.AlgorithmConfiguration;
|
||||
import org.w3c.dom.Document;
|
||||
|
@ -28,10 +40,36 @@ import org.xml.sax.SAXException;
|
|||
|
||||
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 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 {
|
||||
String crlf = "\r\n";
|
||||
String twoHyphens = "--";
|
||||
|
@ -39,7 +77,7 @@ public class DivaHTTPClient {
|
|||
|
||||
HttpURLConnection httpUrlConnection = null;
|
||||
try {
|
||||
|
||||
AnalysisLogger.getLogger().debug("Sending file ...");
|
||||
URL url = new URL(WEB_HTTP + "/upload");
|
||||
httpUrlConnection = (HttpURLConnection) url.openConnection();
|
||||
httpUrlConnection.setUseCaches(false);
|
||||
|
@ -49,15 +87,12 @@ public class DivaHTTPClient {
|
|||
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
|
||||
httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
|
||||
|
||||
httpUrlConnection.setRequestProperty(
|
||||
"Content-Type", "multipart/form-data;boundary=" + boundary);
|
||||
httpUrlConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
|
||||
|
||||
DataOutputStream request = new DataOutputStream(
|
||||
httpUrlConnection.getOutputStream());
|
||||
DataOutputStream request = new DataOutputStream(httpUrlConnection.getOutputStream());
|
||||
|
||||
request.writeBytes("--" + boundary + crlf);
|
||||
request.writeBytes("Content-Disposition: form-data; name=\"data" + "\";filename=\"" +
|
||||
file.getName() + "\"" + crlf);
|
||||
request.writeBytes("Content-Disposition: form-data; name=\"data" + "\";filename=\"" + file.getName() + "\"" + crlf);
|
||||
request.writeBytes(crlf);
|
||||
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
|
@ -67,18 +102,14 @@ public class DivaHTTPClient {
|
|||
lineString = reader.readLine();
|
||||
}
|
||||
|
||||
|
||||
reader.close();
|
||||
request.writeBytes(crlf);
|
||||
request.writeBytes(twoHyphens + boundary +
|
||||
twoHyphens + crlf);
|
||||
request.writeBytes(twoHyphens + boundary + twoHyphens + crlf);
|
||||
request.flush();
|
||||
request.close();
|
||||
InputStream responseStream = new
|
||||
BufferedInputStream(httpUrlConnection.getInputStream());
|
||||
InputStream responseStream = new BufferedInputStream(httpUrlConnection.getInputStream());
|
||||
|
||||
BufferedReader responseStreamReader =
|
||||
new BufferedReader(new InputStreamReader(responseStream));
|
||||
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
|
||||
|
||||
String line = "";
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
@ -102,21 +133,18 @@ public class DivaHTTPClient {
|
|||
httpUrlConnection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* sessionid
|
||||
id from previous upload
|
||||
len
|
||||
correlation length (arc degrees)
|
||||
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
|
||||
* sessionid id from previous upload len correlation length (arc degrees)
|
||||
* 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 {
|
||||
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;
|
||||
|
||||
try {
|
||||
|
@ -131,7 +159,10 @@ public class DivaHTTPClient {
|
|||
get_url += "y1=" + y1 + "&";
|
||||
get_url += "dy=" + dy + "&";
|
||||
get_url += "level=" + level + "";
|
||||
if (useCurrents)
|
||||
get_url += "&adv=1";
|
||||
// System.out.println("GET URL="+get_url);
|
||||
AnalysisLogger.getLogger().debug("Get URL to sent: \n" + get_url);
|
||||
URL url = new URL(get_url);
|
||||
|
||||
httpUrlConnection = (HttpURLConnection) url.openConnection();
|
||||
|
@ -141,11 +172,9 @@ public class DivaHTTPClient {
|
|||
httpUrlConnection.setRequestMethod("GET");
|
||||
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
|
||||
httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
|
||||
InputStream responseStream = new
|
||||
BufferedInputStream(httpUrlConnection.getInputStream());
|
||||
InputStream responseStream = new BufferedInputStream(httpUrlConnection.getInputStream());
|
||||
|
||||
BufferedReader responseStreamReader =
|
||||
new BufferedReader(new InputStreamReader(responseStream));
|
||||
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
|
||||
|
||||
String line = "";
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
@ -156,7 +185,6 @@ public class DivaHTTPClient {
|
|||
|
||||
responseStreamReader.close();
|
||||
|
||||
|
||||
String response = stringBuilder.toString();
|
||||
|
||||
// System.out.println("RESPONSE STRING: "+response);
|
||||
|
@ -182,12 +210,63 @@ public class DivaHTTPClient {
|
|||
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;
|
||||
try {
|
||||
result_xml = postFile(file);
|
||||
|
@ -199,15 +278,14 @@ public class DivaHTTPClient {
|
|||
throw new SAXException("Error in HTTP response no response element found!");
|
||||
}
|
||||
|
||||
|
||||
// Create Document object by XMLResponse
|
||||
Document doc = loadXMLFromString(result_xml);
|
||||
// Print XML Response
|
||||
AnalysisLogger.getLogger().debug("DIVA XML Response: \n" + result_xml);
|
||||
|
||||
|
||||
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;
|
||||
try {
|
||||
obs_count = Integer.parseInt(root.getChildNodes().item(1).getTextContent());
|
||||
|
@ -250,7 +328,6 @@ public class DivaHTTPClient {
|
|||
out.close();
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
AnalysisLogger.setLogger("cfg/" + AlgorithmConfiguration.defaultLoggerFile);
|
||||
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");
|
||||
File f = new File(System.getProperty("user.dir") + File.separator + "temp.nc");
|
||||
|
||||
if(!f.exists()) f.createNewFile();
|
||||
downloadFileDiva(WEB_HTTP+DOWN_FILE_NC+
|
||||
respAnalysis.getIdentifier(),System.getProperty("user.dir")+File.separator+"temp.nc");
|
||||
if (!f.exists())
|
||||
f.createNewFile();
|
||||
downloadFileDiva(WEB_HTTP + DOWN_FILE_NC + respAnalysis.getIdentifier(), System.getProperty("user.dir") + File.separator + "temp.nc");
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 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();
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
InputSource is = new InputSource(new StringReader(xml));
|
||||
|
|
Loading…
Reference in New Issue