111 lines
3.7 KiB
Java
111 lines
3.7 KiB
Java
|
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);
|
||
|
}
|
||
|
|
||
|
}
|