package org.gcube.dataanalysis.seadatanet; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; 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.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger; 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()); 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(); } 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 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 { DivaFilePostResponse response=uploadFile(new File("temperature_argo10.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); } }