diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/controller/HTTPConnection.java b/src/main/java/eu/omp/irap/vespa/epntapclient/controller/HTTPConnection.java deleted file mode 100644 index 6fd52c2..0000000 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/controller/HTTPConnection.java +++ /dev/null @@ -1,144 +0,0 @@ -/** - * This file is a part of EpnTAPClient. - * This program aims to provide EPN-TAP support for software clients, like CASSIS spectrum analyzer. - * See draft specifications: https://voparis-confluence.obspm.fr/pages/viewpage.action?pageId=559861 - * Copyright (C) 2016 Institut de Recherche en Astrophysique et Planétologie. - * - * This program is free software: you can - * redistribute it and/or modify it under the terms of the GNU General Public License as published - * by the Free Software Foundation, either version 3 of the License, or (at your option) any later - * version. This program is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. You should have received a copy of - * the GNU General Public License along with this program. If not, see - * . - */ - -package eu.omp.irap.vespa.epntapclient.controller; - -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.logging.Level; - -import javax.net.ssl.HttpsURLConnection; - -import eu.omp.irap.vespa.epntapclient.utils.Const; -import eu.omp.irap.vespa.epntapclient.utils.Err; -import eu.omp.irap.vespa.epntapclient.utils.Log; - -/** - * This class provide HTTP connection support to send requests through POST / GET protocols. - * - * @author N. Jourdane - */ -public class HTTPConnection { - /** The user agent used for the requests. */ - private final static String USER_AGENT = "Mozilla/5.0"; //$NON-NLS-1$ - - /** - * @param url The target URL of the request. - * @param urlParameters The parameters of the request. - * @param resultFileName The name of the file where the request response content is writed. - * @return The response code of the request (200 if successful). - */ - public static int sendGet(String url, String urlParameters, String resultFileName) { - String inputLine; - StringBuffer response = new StringBuffer(); - URL obj; - int responseCode = -1; - BufferedReader in; - - try { - obj = new URL(url + "?" + urlParameters); //$NON-NLS-1$ - HttpURLConnection con = (HttpURLConnection) obj.openConnection(); - con.setRequestMethod("GET"); //$NON-NLS-1$ - - // add request header - con.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$ - responseCode = con.getResponseCode(); - in = new BufferedReader(new InputStreamReader(con.getInputStream())); - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } - in.close(); - } catch (IOException e1) { - Log.LOGGER.log(Level.SEVERE, Err.BAD_REQUEST, e1); - } - - printRequestResult(response, resultFileName); - - return responseCode; - } - - /** - * @param url The target URL of the request. - * @param urlParameters The parameters of the request. - * @param resultFileName The name of the file where the request response content is writed. - * @return The response code of the request (200 if successful). - */ - public static int sendPost(String url, String urlParameters, String resultFileName) { - - URL obj; - StringBuffer response = new StringBuffer(); - int responseCode = -1; - DataOutputStream wr; - BufferedReader in; - - try { - obj = new URL(url); - HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); - - // Add request header - con.setRequestMethod("POST"); //$NON-NLS-1$ - con.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$ - con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); //$NON-NLS-1$ //$NON-NLS-2$ - - con.setDoOutput(true); - wr = new DataOutputStream(con.getOutputStream()); - wr.writeBytes(urlParameters); - wr.flush(); - wr.close(); - responseCode = con.getResponseCode(); - in = new BufferedReader(new InputStreamReader(con.getInputStream())); - String inputLine; - - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } - in.close(); - } catch (IOException e1) { - Log.LOGGER.log(Level.SEVERE, Err.BAD_REQUEST, e1); - } - - printRequestResult(response, resultFileName); - - return responseCode; - } - - /** - * Try to print result in resultfileName. - * - * @param response The request response content. - * @param resultFileName The name of the file where the request response content is writed. - */ - private static void printRequestResult(StringBuffer response, String resultFileName) { - PrintWriter writer; - - try { - writer = new PrintWriter(resultFileName, Const.CHARACTER_SET); - writer.println(response); - writer.close(); - } catch (FileNotFoundException e1) { - Log.LOGGER.log(Level.SEVERE, Err.NOT_FOUND + resultFileName, e1); - } catch (UnsupportedEncodingException e2) { - Log.LOGGER.log(Level.SEVERE, Err.WRONG_ENCODING + Const.CHARACTER_SET, e2); - } - } -} \ No newline at end of file diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java new file mode 100644 index 0000000..bd4479f --- /dev/null +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java @@ -0,0 +1,130 @@ +/** + * This file is a part of EpnTAPClient. + * This program aims to provide EPN-TAP support for software clients, like CASSIS spectrum analyzer. + * See draft specifications: https://voparis-confluence.obspm.fr/pages/viewpage.action?pageId=559861 + * Copyright (C) 2016 Institut de Recherche en Astrophysique et Planétologie. + * + * This program is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, or (at your option) any later + * version. This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. You should have received a copy of + * the GNU General Public License along with this program. If not, see + * . + */ + +package eu.omp.irap.vespa.epntapclient.votable.controller; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.text.SimpleDateFormat; +import java.util.Date; + +import eu.omp.irap.vespa.epntapclient.utils.Const; +import eu.omp.irap.vespa.epntapclient.utils.Log; + +/** + * This class provide HTTP connection support to send requests through POST / GET protocols. + * + * @author N. Jourdane + */ +public class VOTableConnection { + /** The user agent used for the requests. */ + private final static String USER_AGENT = "Mozilla/5.0"; + + /** + * @param targetURL The url of the registry to communicate (ie. "http://reg.g-vo.org"). + * @param serviceType The type of service (ie. "tap"). + * @param queryLanguage The language used for the queries (ie. "ADQL"). + * @param query The query to ask to the registry. + * @throws Exception If the query can not be sent or got a bad response code. + * @return The path of the temporary XML file containing the http response. + */ + public static String sendQuery(String targetURL, String serviceType, String queryLanguage, + String query) throws Exception { + + SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd_hhmmss_S"); + String voTablePath = Const.TMP_DIR + "/votable" + ft.format(new Date()) + ".xml"; + + int responseCode = -1; + String uri = targetURL + "/" + serviceType + "/sync"; + String parameters = "REQUEST=doQuery&LANG=" + queryLanguage + "&QUERY="; + parameters += URLEncoder.encode(query, Const.CHARACTER_SET); + + Log.LOGGER.info("request: " + uri + "?" + parameters); + try { + responseCode = VOTableConnection.sendGet(uri, parameters, voTablePath); + } catch (Exception e1) { + throw new Exception("Can not send the specified query: " + uri + "?" + query, e1); + } + if (responseCode != 200) { + throw new Exception("Bad response code for the query: " + query); + } + + return voTablePath; + } + + /** + * @param url The target URL of the request. + * @param urlParameters The parameters of the request. + * @param resultFileName The name of the file where the request response content is writed. + * @return The response code of the request (200 if successful). + * @throws Exception Can not send the specified request. + */ + private static int sendGet(String url, String urlParameters, String resultFileName) + throws Exception { + String inputLine; + StringBuffer response = new StringBuffer(); + URL obj = new URL(url + "?" + urlParameters); + int responseCode = -1; + + try { + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); + con.setRequestMethod("GET"); + + // add request header + con.setRequestProperty("User-Agent", USER_AGENT); + responseCode = con.getResponseCode(); + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + new BufferedReader(new InputStreamReader(con.getInputStream())).close(); + } catch (IOException e1) { + throw new Exception("Can not send the specified request.", e1); + } + + printRequestResult(response, resultFileName); + + return responseCode; + } + + /** + * Print result of the query in a file. + * + * @param response The request response content. + * @param resultFileName The name of the file where the request response content is writed. + * @throws Exception File not found or bad encoding. + */ + private static void printRequestResult(StringBuffer response, String resultFileName) + throws Exception { + + try (PrintWriter writer = new PrintWriter(resultFileName, Const.CHARACTER_SET)) { + writer.println(response); + writer.close(); + } catch (FileNotFoundException e1) { + throw new Exception("The file " + resultFileName + " is not found.", e1); + } catch (UnsupportedEncodingException e2) { + throw new Exception("Bad encoding with " + Const.CHARACTER_SET, e2); + } + } +} \ No newline at end of file -- libgit2 0.21.2