diff --git a/src/org/gcube/dataanalysis/seadatanet/DivaAnalysisGetResponse.java b/src/org/gcube/dataanalysis/seadatanet/DivaAnalysisGetResponse.java new file mode 100644 index 0000000..3edecd4 --- /dev/null +++ b/src/org/gcube/dataanalysis/seadatanet/DivaAnalysisGetResponse.java @@ -0,0 +1,70 @@ +package org.gcube.dataanalysis.seadatanet; + +public class DivaAnalysisGetResponse { + + public DivaAnalysisGetResponse(String id,double len,double stn, + double x0,double x1, double dx,double y0,double y1,double dy,int level) { + super(); + this.x0 = x0; + this.x1 = x1; + this.dx = dx; + this.y0 = y0; + this.y1 = y1; + this.dy = dy; + this.len = len; + this.stn = stn; + this.sessionid = id; + } + + @Override + public String toString() { + return "DivaFilePostResponse [obs_x0=" + obs_x0 + ", obs_x1=" + + obs_x1 + ", obs_y0=" + obs_y0 + ", obs_y1=" + obs_y1 + + ", obs_v0=" + obs_v0 + ", obs_v1=" + obs_v1 + + ", obs_count=" + obs_count + ", sessionid=" + sessionid + + "]"; + } + + + + + public Double getVmax() { + return vmax; + } + + public void setVmax(Double vmax) { + this.vmax = vmax; + } + + public Double getStat_obs_count_used() { + return stat_obs_count_used; + } + + public void setStat_obs_count_used(Double stat_obs_count_used) { + this.stat_obs_count_used = stat_obs_count_used; + } + + public Double getStat_posteriori_stn() { + return stat_posteriori_stn; + } + + public void setStat_posteriori_stn(Double stat_posteriori_stn) { + this.stat_posteriori_stn = stat_posteriori_stn; + } + + public String getSessionid() { + return sessionid; + } + + public void setSessionid(String sessionid) { + this.sessionid = sessionid; + } + + + + + Double vmax; + Double stat_obs_count_used; + Double stat_posteriori_stn; + String sessionid; +} diff --git a/src/org/gcube/dataanalysis/seadatanet/DivaHTTPClient.java b/src/org/gcube/dataanalysis/seadatanet/DivaHTTPClient.java index 848d074..e9fb720 100644 --- a/src/org/gcube/dataanalysis/seadatanet/DivaHTTPClient.java +++ b/src/org/gcube/dataanalysis/seadatanet/DivaHTTPClient.java @@ -17,77 +17,79 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger; +import org.jfree.util.Log; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.InputSource; 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 postFile(File file) throws Exception { String crlf = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; - + HttpURLConnection httpUrlConnection = null; try { - - 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()); + 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()); request.writeBytes("--" + boundary + crlf); request.writeBytes("Content-Disposition: form-data; name=\"data" + "\";filename=\"" + - file.getName() + "\"" + crlf); + file.getName() + "\"" + crlf); request.writeBytes(crlf); - + BufferedReader reader = new BufferedReader(new FileReader(file)); String lineString = reader.readLine(); while (lineString!=null) { request.writeBytes(lineString); lineString = reader.readLine(); } - - + + reader.close(); request.writeBytes(crlf); request.writeBytes(twoHyphens + boundary + - twoHyphens + crlf); + twoHyphens + crlf); request.flush(); request.close(); InputStream responseStream = new - BufferedInputStream(httpUrlConnection.getInputStream()); + BufferedInputStream(httpUrlConnection.getInputStream()); - BufferedReader responseStreamReader = - new BufferedReader(new InputStreamReader(responseStream)); + BufferedReader responseStreamReader = + new BufferedReader(new InputStreamReader(responseStream)); - String line = ""; - StringBuilder stringBuilder = new StringBuilder(); + String line = ""; + StringBuilder stringBuilder = new StringBuilder(); - while ((line = responseStreamReader.readLine()) != null) { - stringBuilder.append(line).append("\n"); - } - responseStreamReader.close(); + 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; - String response = stringBuilder.toString(); - responseStream.close(); - httpUrlConnection.disconnect(); - AnalysisLogger.getLogger().debug("Response "+response); - return response; - } catch (IOException e) { throw new Exception(e); } finally { @@ -95,8 +97,81 @@ 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 + */ + public static String getAnalysis (String id,double len,double stn,double x0,double x1, double dx,double y0,double y1,double dy,double level) throws Exception { + HttpURLConnection httpUrlConnection = null; + 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+""; + System.out.println("GET 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(); + + Document doc = loadXMLFromString(response); + System.out.println("DENTRO: "+ doc.getFirstChild()); + + + + responseStream.close(); + httpUrlConnection.disconnect(); + AnalysisLogger.getLogger().debug("Response "+response); + return response; + + } catch (IOException e) { + throw new Exception(e); + } finally { + if (httpUrlConnection != null) + httpUrlConnection.disconnect(); + } + } + + public static DivaFilePostResponse uploadFile(File file) throws Exception, IOException, ParserConfigurationException, SAXException { String result_xml = null; @@ -105,12 +180,12 @@ public class DivaHTTPClient { } catch (Exception e) { e.printStackTrace(); } - + if (result_xml == null) { throw new SAXException("Error in HTTP response no response element found!"); } - - + + //Create Document object by XMLResponse Document doc = loadXMLFromString(result_xml); //Print XML Response @@ -133,17 +208,18 @@ public class DivaHTTPClient { return new DivaFilePostResponse(obs_x0, obs_x1, obs_y0, obs_y1, obs_v0, obs_v1, obs_count, sessionid); } - + public static void main(String[] args) throws Exception { - DivaFilePostResponse response=uploadFile(new File("temperature_argo10.txt")); + DivaFilePostResponse response=uploadFile(new File("C:"+File.separator+"Users"+File.separator+"marbas"+File.separator+"Desktop"+File.separator+"temperature_argo.txt")); + System.out.println(getAnalysis(response.getSessionid(),5,4,76,78,1,35,78,1,2)); System.out.println(response); } - - + + //PARSER XML private static Document loadXMLFromString(String xml) throws ParserConfigurationException, SAXException, IOException {