2015-10-15 15:48:59 +02:00
|
|
|
package org.gcube.dataanalysis.seadatanet;
|
|
|
|
|
2015-10-19 10:38:54 +02:00
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.BufferedReader;
|
2015-10-22 11:48:29 +02:00
|
|
|
import java.io.ByteArrayInputStream;
|
2015-10-19 10:38:54 +02:00
|
|
|
import java.io.DataOutputStream;
|
2015-10-15 15:48:59 +02:00
|
|
|
import java.io.File;
|
2015-10-19 10:38:54 +02:00
|
|
|
import java.io.FileReader;
|
2015-10-15 15:48:59 +02:00
|
|
|
import java.io.IOException;
|
2015-10-19 10:38:54 +02:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.StringReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
2015-10-15 15:48:59 +02:00
|
|
|
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
|
2015-10-19 10:38:54 +02:00
|
|
|
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
|
2015-10-19 18:15:47 +02:00
|
|
|
import org.jfree.util.Log;
|
2015-10-19 10:38:54 +02:00
|
|
|
import org.w3c.dom.Document;
|
|
|
|
import org.w3c.dom.Node;
|
2015-10-15 15:48:59 +02:00
|
|
|
import org.xml.sax.InputSource;
|
|
|
|
import org.xml.sax.SAXException;
|
2015-10-19 18:15:47 +02:00
|
|
|
|
2015-10-26 11:32:32 +01:00
|
|
|
import com.ibm.icu.util.RangeValueIterator.Element;
|
|
|
|
|
2015-10-15 15:48:59 +02:00
|
|
|
public class DivaHTTPClient {
|
|
|
|
|
|
|
|
public static String WEB_HTTP="http://gher-diva.phys.ulg.ac.be/web-vis/Python/web";
|
2015-10-19 18:15:47 +02:00
|
|
|
|
2015-10-19 10:38:54 +02:00
|
|
|
public static String postFile(File file) throws Exception {
|
|
|
|
String crlf = "\r\n";
|
|
|
|
String twoHyphens = "--";
|
|
|
|
String boundary = "*****";
|
2015-10-19 18:15:47 +02:00
|
|
|
|
2015-10-19 10:38:54 +02:00
|
|
|
HttpURLConnection httpUrlConnection = null;
|
|
|
|
try {
|
2015-10-19 18:15:47 +02:00
|
|
|
|
|
|
|
URL url = new URL(WEB_HTTP+"/upload");
|
|
|
|
httpUrlConnection = (HttpURLConnection) url.openConnection();
|
|
|
|
httpUrlConnection.setUseCaches(false);
|
|
|
|
httpUrlConnection.setDoOutput(true);
|
|
|
|
|
|
|
|
httpUrlConnection.setRequestMethod("POST");
|
|
|
|
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
|
|
|
|
httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
|
|
|
|
|
|
|
|
httpUrlConnection.setRequestProperty(
|
|
|
|
"Content-Type", "multipart/form-data;boundary=" + boundary);
|
|
|
|
|
|
|
|
DataOutputStream request = new DataOutputStream(
|
|
|
|
httpUrlConnection.getOutputStream());
|
2015-10-19 10:38:54 +02:00
|
|
|
|
|
|
|
request.writeBytes("--" + boundary + crlf);
|
|
|
|
request.writeBytes("Content-Disposition: form-data; name=\"data" + "\";filename=\"" +
|
2015-10-19 18:15:47 +02:00
|
|
|
file.getName() + "\"" + crlf);
|
2015-10-19 10:38:54 +02:00
|
|
|
request.writeBytes(crlf);
|
2015-10-19 18:15:47 +02:00
|
|
|
|
2015-10-19 10:38:54 +02:00
|
|
|
BufferedReader reader = new BufferedReader(new FileReader(file));
|
|
|
|
String lineString = reader.readLine();
|
|
|
|
while (lineString!=null) {
|
|
|
|
request.writeBytes(lineString);
|
|
|
|
lineString = reader.readLine();
|
|
|
|
}
|
2015-10-19 18:15:47 +02:00
|
|
|
|
|
|
|
|
2015-10-19 10:38:54 +02:00
|
|
|
reader.close();
|
|
|
|
request.writeBytes(crlf);
|
|
|
|
request.writeBytes(twoHyphens + boundary +
|
2015-10-19 18:15:47 +02:00
|
|
|
twoHyphens + crlf);
|
2015-10-19 10:38:54 +02:00
|
|
|
request.flush();
|
|
|
|
request.close();
|
|
|
|
InputStream responseStream = new
|
2015-10-19 18:15:47 +02:00
|
|
|
BufferedInputStream(httpUrlConnection.getInputStream());
|
2015-10-19 10:38:54 +02:00
|
|
|
|
2015-10-19 18:15:47 +02:00
|
|
|
BufferedReader responseStreamReader =
|
|
|
|
new BufferedReader(new InputStreamReader(responseStream));
|
2015-10-19 10:38:54 +02:00
|
|
|
|
2015-10-19 18:15:47 +02:00
|
|
|
String line = "";
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
2015-10-19 10:38:54 +02:00
|
|
|
|
2015-10-19 18:15:47 +02:00
|
|
|
while ((line = responseStreamReader.readLine()) != null) {
|
|
|
|
stringBuilder.append(line).append("\n");
|
|
|
|
}
|
|
|
|
responseStreamReader.close();
|
|
|
|
|
|
|
|
String response = stringBuilder.toString();
|
|
|
|
|
|
|
|
responseStream.close();
|
|
|
|
httpUrlConnection.disconnect();
|
|
|
|
AnalysisLogger.getLogger().debug("Response "+response);
|
|
|
|
return response;
|
2015-10-19 10:38:54 +02:00
|
|
|
|
2015-10-19 18:15:47 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new Exception(e);
|
|
|
|
} finally {
|
|
|
|
if (httpUrlConnection != null)
|
2015-10-19 10:38:54 +02:00
|
|
|
httpUrlConnection.disconnect();
|
2015-10-19 18:15:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2015-10-22 12:06:29 +02:00
|
|
|
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 {
|
2015-10-19 18:15:47 +02:00
|
|
|
HttpURLConnection httpUrlConnection = null;
|
2015-10-22 11:48:29 +02:00
|
|
|
|
2015-10-19 18:15:47 +02:00
|
|
|
try {
|
|
|
|
String get_url=WEB_HTTP+"/make_analysis?";
|
|
|
|
get_url+="sessionid="+id+"&";
|
|
|
|
get_url+="len="+len+"&";
|
|
|
|
get_url+="stn="+stn+"&";
|
|
|
|
get_url+="x0="+x0+"&";
|
|
|
|
get_url+="x1="+x1+"&";
|
|
|
|
get_url+="dx="+dx+"&";
|
|
|
|
get_url+="y0="+y0+"&";
|
|
|
|
get_url+="y1="+y1+"&";
|
|
|
|
get_url+="dy="+dy+"&";
|
|
|
|
get_url+="level="+level+"";
|
2015-10-26 11:32:32 +01:00
|
|
|
//System.out.println("GET URL="+get_url);
|
2015-10-19 18:15:47 +02:00
|
|
|
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();
|
2015-10-22 15:40:03 +02:00
|
|
|
|
2015-10-19 18:15:47 +02:00
|
|
|
while ((line = responseStreamReader.readLine()) != null) {
|
|
|
|
stringBuilder.append(line).append("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
responseStreamReader.close();
|
|
|
|
|
|
|
|
|
|
|
|
String response = stringBuilder.toString();
|
|
|
|
|
2015-10-22 15:40:03 +02:00
|
|
|
//System.out.println("RESPONSE STRING: "+response);
|
2015-10-22 11:48:29 +02:00
|
|
|
|
2015-10-19 18:15:47 +02:00
|
|
|
Document doc = loadXMLFromString(response);
|
2015-10-26 11:32:32 +01:00
|
|
|
Node idNode = doc.getElementsByTagName("result").item(0);
|
|
|
|
String identifier = idNode.getAttributes().item(0).getNodeValue();
|
2015-10-22 11:48:29 +02:00
|
|
|
Double vmin = Double.parseDouble(doc.getElementsByTagName("stat").item(0).getTextContent());
|
|
|
|
Double vmax = Double.parseDouble(doc.getElementsByTagName("stat").item(1).getTextContent());
|
|
|
|
Double stat_obs_count_used = Double.parseDouble(doc.getElementsByTagName("stat").item(2).getTextContent());
|
|
|
|
Double stat_posteriori_stn = Double.parseDouble(doc.getElementsByTagName("stat").item(3).getTextContent());
|
2015-10-19 18:15:47 +02:00
|
|
|
|
|
|
|
responseStream.close();
|
|
|
|
httpUrlConnection.disconnect();
|
|
|
|
AnalysisLogger.getLogger().debug("Response "+response);
|
2015-10-22 12:06:29 +02:00
|
|
|
//return response;
|
|
|
|
|
2015-10-26 11:32:32 +01:00
|
|
|
return new DivaAnalysisGetResponse(identifier, vmin, vmax, stat_obs_count_used, stat_posteriori_stn);
|
2015-10-19 18:15:47 +02:00
|
|
|
|
2015-10-19 10:38:54 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new Exception(e);
|
|
|
|
} finally {
|
|
|
|
if (httpUrlConnection != null)
|
|
|
|
httpUrlConnection.disconnect();
|
|
|
|
}
|
2015-10-22 12:06:29 +02:00
|
|
|
|
2015-10-22 11:48:29 +02:00
|
|
|
|
2015-10-19 10:38:54 +02:00
|
|
|
}
|
2015-10-19 18:15:47 +02:00
|
|
|
|
|
|
|
|
2015-10-19 10:38:54 +02:00
|
|
|
public static DivaFilePostResponse uploadFile(File file) throws Exception, IOException, ParserConfigurationException, SAXException
|
|
|
|
{
|
|
|
|
String result_xml = null;
|
|
|
|
try {
|
|
|
|
result_xml = postFile(file);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2015-10-15 15:48:59 +02:00
|
|
|
}
|
2015-10-19 18:15:47 +02:00
|
|
|
|
2015-10-19 10:38:54 +02:00
|
|
|
if (result_xml == null) {
|
|
|
|
throw new SAXException("Error in HTTP response no response element found!");
|
2015-10-15 15:48:59 +02:00
|
|
|
}
|
2015-10-19 18:15:47 +02:00
|
|
|
|
|
|
|
|
2015-10-15 15:48:59 +02:00
|
|
|
//Create Document object by XMLResponse
|
|
|
|
Document doc = loadXMLFromString(result_xml);
|
|
|
|
//Print XML Response
|
2015-10-26 11:32:32 +01:00
|
|
|
AnalysisLogger.getLogger().debug(result_xml);
|
|
|
|
|
2015-10-15 15:48:59 +02:00
|
|
|
|
|
|
|
Node root = doc.getDocumentElement();
|
|
|
|
if(root== null) throw new SAXException("Error in HTTP response no root element found!");
|
|
|
|
|
|
|
|
Integer obs_count=Integer.parseInt(root.getChildNodes().item(1).getTextContent());
|
|
|
|
|
|
|
|
Double obs_x0=Double.parseDouble(root.getChildNodes().item(3).getTextContent());
|
|
|
|
Double obs_x1=Double.parseDouble(root.getChildNodes().item(5).getTextContent());
|
|
|
|
Double obs_y0=Double.parseDouble(root.getChildNodes().item(7).getTextContent());
|
|
|
|
Double obs_y1=Double.parseDouble(root.getChildNodes().item(9).getTextContent());
|
|
|
|
Double obs_v0=Double.parseDouble(root.getChildNodes().item(11).getTextContent());
|
|
|
|
Double obs_v1=Double.parseDouble(root.getChildNodes().item(13).getTextContent());
|
|
|
|
|
|
|
|
String sessionid=root.getChildNodes().item(15).getTextContent();
|
|
|
|
|
|
|
|
return new DivaFilePostResponse(obs_x0, obs_x1, obs_y0, obs_y1, obs_v0, obs_v1, obs_count, sessionid);
|
|
|
|
|
|
|
|
}
|
2015-10-19 18:15:47 +02:00
|
|
|
|
2015-10-15 15:48:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
2015-10-26 11:32:32 +01:00
|
|
|
DivaFilePostResponse response=uploadFile(new File("resources"+File.separator+"files"+File.separator+"temperature_argo.txt"));
|
2015-10-15 15:48:59 +02:00
|
|
|
|
2015-10-22 15:40:03 +02:00
|
|
|
//System.out.println(getAnalysis(response.getSessionid(),5,4,76,78,1,35,78,1,2));
|
|
|
|
//System.out.println(response);
|
|
|
|
DivaAnalysisGetResponse respAnalysis = getAnalysis(response.getSessionid(),5, 4, 76, 78, 1, 35, 78, 1, 2);
|
2015-10-26 11:32:32 +01:00
|
|
|
System.out.println("Response: "+respAnalysis.toString());
|
|
|
|
|
|
|
|
|
|
|
|
Downloader downFileNC = new Downloader();
|
|
|
|
//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();
|
|
|
|
downFileNC.downloadFile(WEB_HTTP+"/download?fieldname="+
|
|
|
|
respAnalysis.getIdentifier(),System.getProperty("user.dir")+File.separator+"temp.nc");
|
|
|
|
|
2015-10-15 15:48:59 +02:00
|
|
|
}
|
2015-10-19 18:15:47 +02:00
|
|
|
|
|
|
|
|
2015-10-15 15:48:59 +02:00
|
|
|
//PARSER XML
|
|
|
|
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));
|
|
|
|
return builder.parse(is);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|