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 deleted file mode 100644 index d5840a6..0000000 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java +++ /dev/null @@ -1,171 +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.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 java.util.logging.Level; -import java.util.logging.Logger; - -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.BadResponseCodeException; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantGetErrorStream; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantGetResponseCode; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantOpenConnectionException; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantPrintRequestResultException; -import eu.omp.irap.vespa.epntapclient.votable.utils.Const; - -/** - * This class provide HTTP connection support to send requests through POST / GET protocols. - * - * @author N. Jourdane - */ -public final class VOTableConnection { - - /** The logger for the class VOTableConnection. */ - private static final Logger logger = Logger.getLogger(VOTableConnection.class.getName()); - - /** The user agent used for the requests. */ - private static final String USER_AGENT = "Mozilla/5.0"; - - - /** Constructor to hide the implicit public one. */ - private VOTableConnection() { - } - - /** - * @param targetURL The URL of the registry to communicate (ie. "http://reg.g-vo.org"). - * @param queryLanguage The language used for the queries (ie. "ADQL"). - * @param query The query to ask to the registry. - * @return The path of the temporary XML file containing the http response. - * @throws CantSendQueryException If the query can not be sent. - */ - public static String sendQuery(String targetURL, String queryLanguage, - String query) throws CantSendQueryException { - - SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd_hhmmss_SSS"); - String voTablePath = Const.TMP_DIR + "/votable" + ft.format(new Date()) + ".xml"; - - String uri = targetURL + "/sync"; - String parameters = "REQUEST=doQuery&LANG=" + queryLanguage + "&FORMAT=votable&QUERY="; - try { - parameters += URLEncoder.encode(query, Const.ENCODING).replace("+", "%20") - .replace(".", "%2E").replace("-", "%2D").replace("*", "%2A") - .replace("_", "%5F"); - } catch (UnsupportedEncodingException e) { - throw new CantSendQueryException.MalformedURLException(uri, e); - } - - VOTableConnection.logger.info("request: " + uri + "?" + parameters); - try { - VOTableConnection.printRequestResult(VOTableConnection.sendGet(uri, parameters), - voTablePath); - } catch (IOException e) { - throw new CantPrintRequestResultException(voTablePath, e); - } - - return voTablePath; - } - - /** - * @param url The target URL of the request. - * @param urlParameters The parameters of the request. - * @return A string builder containing the result of the query. - * @throws CantSendQueryException If the query can not be sent. - */ - public static StringBuilder sendGet(String url, String urlParameters) - throws CantSendQueryException { - String inputLine; - StringBuilder response = new StringBuilder(); - URL obj; - String uri = url + "?" + urlParameters; - - try { - obj = new URL(uri); - } catch (java.net.MalformedURLException e) { - throw new CantSendQueryException.MalformedURLException(uri, e); - } - - HttpURLConnection con; - - try { - con = (HttpURLConnection) obj.openConnection(); - con.setRequestMethod("GET"); - } catch (IOException e) { - throw new CantOpenConnectionException(uri, e); - } - - con.setRequestProperty("User-Agent", VOTableConnection.USER_AGENT); - int code = -1; - - try { - code = con.getResponseCode(); - } catch (IOException e) { - throw new CantGetResponseCode(uri, e); - } - - if (code != HttpURLConnection.HTTP_OK && code != HttpURLConnection.HTTP_BAD_REQUEST) { - throw new BadResponseCodeException(uri, code); - } - - try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) { - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } - } catch (IOException e1) { - VOTableConnection.logger.log(Level.WARNING, "Can not get input stream", e1); - try (BufferedReader in = new BufferedReader( - new InputStreamReader(con.getErrorStream()))) { - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } - } catch (IOException e2) { - throw new CantGetErrorStream(uri, e2); - } - } - - return response; - } - - /** - * 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 IOException File not found or bad encoding. - */ - private static void printRequestResult(StringBuilder response, String resultFileName) - throws IOException { - - try (PrintWriter writer = new PrintWriter(resultFileName, Const.ENCODING)) { - writer.println(response); - } catch (FileNotFoundException e1) { - throw new IOException("The file " + resultFileName + " is not found.", e1); - } catch (UnsupportedEncodingException e2) { - throw new IOException("Bad encoding with " + Const.ENCODING, e2); - } - } -} \ No newline at end of file diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java index b598ae4..4f0ea2e 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java @@ -17,7 +17,11 @@ package eu.omp.irap.vespa.epntapclient.votable.controller; import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.logging.Logger; import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantDisplayVOTableException; @@ -28,6 +32,8 @@ import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantDi import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException; import eu.omp.irap.vespa.epntapclient.votable.model.Table; import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE; +import eu.omp.irap.vespa.epntapclient.votable.utils.Consts; +import eu.omp.irap.vespa.epntapclient.votable.utils.Network; import eu.omp.irap.vespa.epntapclient.votable.view.VOTableViewListener; /** @@ -90,7 +96,7 @@ public class VOTableController implements VOTableViewListener { public void readTable() throws CantDisplayVOTableException, CantSendQueryException { if (voTablePath == null) { - voTablePath = VOTableConnection.sendQuery(targetURL, queryLanguage, query); + voTablePath = VOTableController.getVOTable(targetURL, query); } voTable = VOTableParser.parseVOTable(voTablePath); @@ -119,6 +125,21 @@ public class VOTableController implements VOTableViewListener { } } + public static String getVOTable(String targetURL, String query) throws CantSendQueryException { + String dateStr = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss_SSS").format(new Date()); + String voTablePath = Consts.TMP_DIR + "/votable" + dateStr + ".xml"; + + String url = targetURL + "/sync"; + + Map parameters = new HashMap<>(); + parameters.put("REQUEST", Consts.QUERY_REQUEST); + parameters.put("LANG", Consts.QUERY_LANG); + parameters.put("FORMAT", query); + + Network.sendQuery(url, parameters, voTablePath); + return voTablePath; + } + public String[] getColumns() { return voTableColumns; } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableDataParser.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableDataParser.java index 4c758d2..a47d892 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableDataParser.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableDataParser.java @@ -33,7 +33,7 @@ import eu.omp.irap.vespa.epntapclient.votable.model.Field; import eu.omp.irap.vespa.epntapclient.votable.model.Stream; import eu.omp.irap.vespa.epntapclient.votable.model.Table; import eu.omp.irap.vespa.epntapclient.votable.model.TableData; -import eu.omp.irap.vespa.epntapclient.votable.utils.Utils; +import eu.omp.irap.vespa.epntapclient.votable.utils.Debug; /** * @author N. Jourdane @@ -94,7 +94,7 @@ public class VOTableDataParser { VOTableDataParser.parseFITSStream(table.getDATA().getFITS().getSTREAM(), fields); } - Utils.printObject("voTableData", data); + Debug.printObject("voTableData", data); } /** diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java index 50847b6..9af4a60 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java @@ -85,6 +85,23 @@ public abstract class VOTableException extends Exception { } } + /** The URL is not correctly formated. */ + public static class UnsupportedParameterEncodingException extends CantSendQueryException { + + /** */ + private static final long serialVersionUID = 1L; + + + /** + * @param uri The URI sent to the server. + * @param e The exception thrown. + */ + public UnsupportedParameterEncodingException(String key, String value, Exception e) { + super("The URL parameter " + key + " with value '" + value + + "' is not correctly encoded.", e); + } + } + /** Can not print the result in the file. */ public static class CantPrintRequestResultException extends CantSendQueryException { diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Consts.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Consts.java index e122004..721ef91 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Consts.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Consts.java @@ -36,6 +36,10 @@ public class Consts { // http://gavo.aip.de/tap // http://voparis-cdpp.obspm.fr/tap + public static final String QUERY_LANG = "ADQL"; + + public static final String QUERY_REQUEST = "doQuery"; + /** Constructor to hide the implicit public one. */ private Consts() { diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Network.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Network.java new file mode 100644 index 0000000..5952a22 --- /dev/null +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Network.java @@ -0,0 +1,167 @@ +/* + * 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.utils; + +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.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.BadResponseCodeException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantGetErrorStream; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantGetResponseCode; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantOpenConnectionException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantPrintRequestResultException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.UnsupportedParameterEncodingException; +import eu.omp.irap.vespa.epntapclient.votable.utils.Strings.StringJoiner; + +/** + * @author N. Jourdane + */ +public class Network { + + /** The logger for the class Network. */ + private static final Logger logger = Logger.getLogger(Network.class.getName()); + + /** The user agent used for the requests. */ + private static final String USER_AGENT = "Mozilla/5.0"; + + + /** Private constructor to hide the implicit public one. */ + private Network() { + } + + /** + * @param targetURL The URL of the registry to communicate (ie. "http://reg.g-vo.org"). + * @param queryLanguage The language used for the queries (ie. "ADQL"). + * @param query The query to ask to the registry. + * @return The path of the temporary XML file containing the http response. + * @throws CantSendQueryException If the query can not be sent. + */ + public static void sendQuery(String url, Map parameters, String outputPath) + throws CantSendQueryException { + + StringJoiner paramJoiner = new StringJoiner("&"); + for (Map.Entry param : parameters.entrySet()) { + try { + String value = URLEncoder.encode(param.getValue(), Consts.ENCODING) + .replace("+", "%20") + .replace(".", "%2E").replace("-", "%2D").replace("*", "%2A") + .replace("_", "%5F"); + paramJoiner.add(param.getKey() + "=" + value); + + } catch (UnsupportedEncodingException e) { + throw new UnsupportedParameterEncodingException(param.getKey(), param.getValue(), + e); + } + + } + + try { + Network.printRequestResult(Network.sendGet(url + paramJoiner), outputPath); + } catch (IOException e) { + throw new CantPrintRequestResultException(outputPath, e); + } + } + + /** + * @param url The target URL of the request. + * @param urlParameters The parameters of the request. + * @return A string builder containing the result of the query. + * @throws CantSendQueryException If the query can not be sent. + */ + private static StringBuilder sendGet(String url) + throws CantSendQueryException { + String inputLine; + StringBuilder response = new StringBuilder(); + URL obj; + + try { + obj = new URL(url); + } catch (java.net.MalformedURLException e) { + throw new CantSendQueryException.MalformedURLException(url, e); + } + + HttpURLConnection con; + + try { + con = (HttpURLConnection) obj.openConnection(); + con.setRequestMethod("GET"); + } catch (IOException e) { + throw new CantOpenConnectionException(url, e); + } + + con.setRequestProperty("User-Agent", Network.USER_AGENT); + int code = -1; + + try { + code = con.getResponseCode(); + } catch (IOException e) { + throw new CantGetResponseCode(url, e); + } + + if (code != HttpURLConnection.HTTP_OK && code != HttpURLConnection.HTTP_BAD_REQUEST) { + throw new BadResponseCodeException(url, code); + } + + try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) { + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + } catch (IOException e1) { + Network.logger.log(Level.WARNING, "Can not get input stream", e1); + try (BufferedReader in = new BufferedReader( + new InputStreamReader(con.getErrorStream()))) { + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + } catch (IOException e2) { + throw new CantGetErrorStream(url, e2); + } + } + + return response; + } + + /** + * 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 IOException File not found or bad encoding. + */ + private static void printRequestResult(StringBuilder response, String resultFileName) + throws IOException { + + try (PrintWriter writer = new PrintWriter(resultFileName, Consts.ENCODING)) { + writer.println(response); + } catch (FileNotFoundException e1) { + throw new IOException("The file " + resultFileName + " is not found.", e1); + } catch (UnsupportedEncodingException e2) { + throw new IOException("Bad encoding with " + Consts.ENCODING, e2); + } + } +} diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableView.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableView.java index db1b5da..180e9c6 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableView.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableView.java @@ -29,7 +29,7 @@ import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import javax.swing.table.TableRowSorter; -import eu.omp.irap.vespa.epntapclient.votable.utils.Utils; +import eu.omp.irap.vespa.epntapclient.votable.utils.Debug; /** * The main class of the View of the application. @@ -87,8 +87,8 @@ public class VOTableView extends JPanel implements TableModelListener { */ public void fillTable(String[] columns, List data) { Object[][] values = data.toArray(new Object[data.size()][]); - Utils.printObject("tableColumns", columns); - Utils.printObject("tableData", values); + Debug.printObject("tableColumns", columns); + Debug.printObject("tableData", values); tableData.setDataVector(values, columns); if (values.length != 0) { table.setRowSelectionInterval(0, 0); -- libgit2 0.21.2