diff --git a/src/org/gcube/dataanalysis/seadatanet/DivaFilePostResponse.java b/src/org/gcube/dataanalysis/seadatanet/DivaFilePostResponse.java new file mode 100644 index 0000000..e36ecdf --- /dev/null +++ b/src/org/gcube/dataanalysis/seadatanet/DivaFilePostResponse.java @@ -0,0 +1,104 @@ +package org.gcube.dataanalysis.seadatanet; + +class DivaFilePostResponse{ + Double obs_x0; + Double obs_x1; + Double obs_y0; + Double obs_y1; + Double obs_v0; + Double obs_v1; + + Integer obs_count; + + String sessionid; + + public Double getObs_x0() { + return obs_x0; + } + + public void setObs_x0(Double obs_x0) { + this.obs_x0 = obs_x0; + } + + public Double getObs_x1() { + return obs_x1; + } + + public void setObs_x1(Double obs_x1) { + this.obs_x1 = obs_x1; + } + + public Double getObs_y0() { + return obs_y0; + } + + public void setObs_y0(Double obs_y0) { + this.obs_y0 = obs_y0; + } + + public Double getObs_y1() { + return obs_y1; + } + + public void setObs_y1(Double obs_y1) { + this.obs_y1 = obs_y1; + } + + public Double getObs_v0() { + return obs_v0; + } + + public void setObs_v0(Double obs_v0) { + this.obs_v0 = obs_v0; + } + + public Double getObs_v1() { + return obs_v1; + } + + public void setObs_v1(Double obs_v1) { + this.obs_v1 = obs_v1; + } + + public Integer getObs_count() { + return obs_count; + } + + public void setObs_count(Integer obs_count) { + this.obs_count = obs_count; + } + + public String getSessionid() { + return sessionid; + } + + public void setSessionid(String sessionid) { + this.sessionid = sessionid; + } + + public DivaFilePostResponse(Double obs_x0, Double obs_x1, Double obs_y0, + Double obs_y1, Double obs_v0, Double obs_v1, Integer obs_count, + String sessionid) { + super(); + this.obs_x0 = obs_x0; + this.obs_x1 = obs_x1; + this.obs_y0 = obs_y0; + this.obs_y1 = obs_y1; + this.obs_v0 = obs_v0; + this.obs_v1 = obs_v1; + this.obs_count = obs_count; + this.sessionid = sessionid; + } + + @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 + + "]"; + } + + + + } \ No newline at end of file diff --git a/src/org/gcube/dataanalysis/seadatanet/DivaHTTPClient.java b/src/org/gcube/dataanalysis/seadatanet/DivaHTTPClient.java new file mode 100644 index 0000000..58a45a8 --- /dev/null +++ b/src/org/gcube/dataanalysis/seadatanet/DivaHTTPClient.java @@ -0,0 +1,110 @@ +package org.gcube.dataanalysis.seadatanet; + +import java.io.File; +import java.io.IOException; +import java.util.Scanner; +import java.util.logging.Logger; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.http.HttpEntity; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.entity.mime.content.FileBody; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.w3c.dom.*; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + + +import java.io.*; + +public class DivaHTTPClient { + + public static String WEB_HTTP="http://gher-diva.phys.ulg.ac.be/web-vis/Python/web"; + + + public static DivaFilePostResponse uploadFile(File file) throws ClientProtocolException, IOException, ParserConfigurationException, SAXException + { + + //connection + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpPost httppost = new HttpPost(WEB_HTTP+"/upload"); + + //Creation of MultipartEntity + FileBody bin = new FileBody(file); + HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("data", bin).build(); + httppost.setEntity(reqEntity); + + //response Server + CloseableHttpResponse response = httpclient.execute(httppost); + Logger.getGlobal().info(response.getStatusLine()+""); + + HttpEntity resEntity = response.getEntity(); + if (resEntity == null) { + throw new SAXException("Error in HTTP response no response element found!"); + } + + //saving Response into String + String result_xml=""; + Scanner scanner=new Scanner(resEntity.getContent()); + while(scanner.hasNext()) + { + result_xml + +=scanner.nextLine(); + } + + //Create Document object by XMLResponse + Document doc = loadXMLFromString(result_xml); + //Print XML Response + System.out.println( result_xml); + + 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(); + + EntityUtils.consume(resEntity); + response.close(); + + httpclient.close(); + + 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("C:"+File.separator+"Users"+File.separator+"marbas"+File.separator+"Desktop"+File.separator+"temperature_argo.txt")); + + System.out.println(response); + } + + + //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); + } + +} diff --git a/src/org/gcube/dataanalysis/seadatanet/SeaDataNetConnector.java b/src/org/gcube/dataanalysis/seadatanet/SeaDataNetConnector.java index 4c925c6..3bf4759 100644 --- a/src/org/gcube/dataanalysis/seadatanet/SeaDataNetConnector.java +++ b/src/org/gcube/dataanalysis/seadatanet/SeaDataNetConnector.java @@ -104,6 +104,12 @@ public class SeaDataNetConnector extends StandardLocalExternalAlgorithm { fileWriterDiva.write(" "+oarray[0]+" "+oarray[1]+" "+oarray[2]+"\n"); } fileWriterDiva.close(); + + DivaHTTPClient neo= new DivaHTTPClient(); + DivaFilePostResponse response=neo.uploadFile(new File("C:"+File.separator+"Users"+File.separator+"marbas"+File.separator+"Desktop"+File.separator+"temperature_argo.txt")); + AnalysisLogger.getLogger().debug("Server Response: "+response.getSessionid()); + + }