git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/SeaDataNetConnector@119852 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
cde4b2476c
commit
121dc8fdb9
|
@ -1,64 +1,115 @@
|
||||||
package org.gcube.dataanalysis.seadatanet;
|
package org.gcube.dataanalysis.seadatanet;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Scanner;
|
import java.io.InputStream;
|
||||||
import java.util.logging.Logger;
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
|
||||||
import org.apache.http.HttpEntity;
|
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
|
||||||
import org.apache.http.client.ClientProtocolException;
|
import org.w3c.dom.Document;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.w3c.dom.Node;
|
||||||
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.InputSource;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
|
|
||||||
public class DivaHTTPClient {
|
public class DivaHTTPClient {
|
||||||
|
|
||||||
public static String WEB_HTTP="http://gher-diva.phys.ulg.ac.be/web-vis/Python/web";
|
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 = "*****";
|
||||||
|
|
||||||
public static DivaFilePostResponse uploadFile(File file) throws ClientProtocolException, IOException, ParserConfigurationException, SAXException
|
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());
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
//connection
|
if (result_xml == null) {
|
||||||
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!");
|
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
|
//Create Document object by XMLResponse
|
||||||
Document doc = loadXMLFromString(result_xml);
|
Document doc = loadXMLFromString(result_xml);
|
||||||
|
@ -79,11 +130,6 @@ public class DivaHTTPClient {
|
||||||
|
|
||||||
String sessionid=root.getChildNodes().item(15).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);
|
return new DivaFilePostResponse(obs_x0, obs_x1, obs_y0, obs_y1, obs_v0, obs_v1, obs_count, sessionid);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -92,7 +138,7 @@ public class DivaHTTPClient {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
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"));
|
DivaFilePostResponse response=uploadFile(new File("temperature_argo10.txt"));
|
||||||
|
|
||||||
System.out.println(response);
|
System.out.println(response);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue