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;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
|
|
|
|
public class DivaHTTPClient {
|
|
|
|
|
|
|
|
public static String WEB_HTTP="http://gher-diva.phys.ulg.ac.be/web-vis/Python/web";
|
|
|
|
|
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-15 15:48:59 +02:00
|
|
|
|
2015-10-19 10:38:54 +02:00
|
|
|
HttpURLConnection httpUrlConnection = null;
|
|
|
|
try {
|
2015-10-15 15:48:59 +02:00
|
|
|
|
2015-10-19 10:38:54 +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");
|
2015-10-15 15:48:59 +02:00
|
|
|
|
2015-10-19 10:38:54 +02:00
|
|
|
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);
|
|
|
|
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);
|
|
|
|
request.flush();
|
|
|
|
request.close();
|
|
|
|
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();
|
|
|
|
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;
|
|
|
|
try {
|
|
|
|
result_xml = postFile(file);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2015-10-15 15:48:59 +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 10:38:54 +02:00
|
|
|
|
2015-10-15 15:48:59 +02:00
|
|
|
//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();
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
2015-10-19 10:38:54 +02:00
|
|
|
DivaFilePostResponse response=uploadFile(new File("temperature_argo10.txt"));
|
2015-10-15 15:48:59 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|