diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/gui/ParamField.java b/src/main/java/eu/omp/irap/vespa/epntapclient/gui/ParamField.java index 1d95e53..381c045 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/gui/ParamField.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/gui/ParamField.java @@ -24,8 +24,10 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -42,8 +44,8 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableConnection; import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException; +import eu.omp.irap.vespa.epntapclient.votable.utils.Network; /** * A field used to set a service parameter to build the query (in the parameter panel). ParamField @@ -428,9 +430,11 @@ public abstract class ParamField extends JPanel { * @throws CantSendQueryException If the resolver do not work. */ static String[] getItems(String begining) throws CantSendQueryException { - StringBuilder resolverResult; - resolverResult = VOTableConnection.sendGet(ParamField.RESOLVER_URL, - "q=\"" + begining + "\""); + Map<String, String> params = new HashMap<>(); + params.put("q", "\"" + begining + "\""); + + StringBuilder resolverResult = Network.sendQuery(ParamField.RESOLVER_URL, params); + JsonObject root = new JsonParser().parse(resolverResult.toString()).getAsJsonObject(); int count = Integer.parseInt(root.get("count").toString()); String[] targetNames = new String[count]; diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java index 0d0d6fe..b6773e3 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java @@ -67,7 +67,7 @@ public class VOTableApp { try { ctrl.readTable(); } catch (VOTableException e) { - System.console().writer().println("Error: " + e.getMessage()); + System.out.println("Error: " + e.getMessage()); VOTableApp.logger.log(Level.WARNING, e.getMessage(), e); return; } 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 4f0ea2e..3f59601 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 @@ -19,9 +19,9 @@ 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.SortedMap; +import java.util.TreeMap; import java.util.logging.Logger; import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantDisplayVOTableException; @@ -131,7 +131,7 @@ public class VOTableController implements VOTableViewListener { String url = targetURL + "/sync"; - Map<String, String> parameters = new HashMap<>(); + SortedMap<String, String> parameters = new TreeMap<>(); parameters.put("REQUEST", Consts.QUERY_REQUEST); parameters.put("LANG", Consts.QUERY_LANG); parameters.put("FORMAT", query); 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 9af4a60..c4a4f53 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 @@ -77,26 +77,42 @@ public abstract class VOTableException extends Exception { /** - * @param uri The URI sent to the server. + * @param url The url sent to the server. * @param e The exception thrown. */ - public MalformedURLException(String uri, Exception e) { - super("The URL " + uri + " is not correctly formated.", e); + public MalformedURLException(String url, Exception e) { + super("The URL " + url + " is not correctly formated.", e); } } /** The URL is not correctly formated. */ - public static class UnsupportedParameterEncodingException extends CantSendQueryException { + public static class CantWriteQueryResultException extends CantSendQueryException { /** */ private static final long serialVersionUID = 1L; /** - * @param uri The URI sent to the server. + * @param url The url sent to the server. * @param e The exception thrown. */ - public UnsupportedParameterEncodingException(String key, String value, Exception e) { + public CantWriteQueryResultException(String path, Exception e) { + super("Can not write the query result in " + path + ".", e); + } + } + + /** The URL is not correctly formated. */ + public static class UnsupportedParamEncodingException extends CantSendQueryException { + + /** */ + private static final long serialVersionUID = 1L; + + + /** + * @param url The url sent to the server. + * @param e The exception thrown. + */ + public UnsupportedParamEncodingException(String key, String value, Exception e) { super("The URL parameter " + key + " with value '" + value + "' is not correctly encoded.", e); } @@ -126,11 +142,11 @@ public abstract class VOTableException extends Exception { /** - * @param uri The URI sent to the server. + * @param url The url sent to the server. * @param e The exception thrown. */ - public CantOpenConnectionException(String uri, IOException e) { - super("Can not open an HTTP connection to " + uri, e); + public CantOpenConnectionException(String url, IOException e) { + super("Can not open an HTTP connection to " + url, e); } } @@ -142,11 +158,11 @@ public abstract class VOTableException extends Exception { /** - * @param uri The URI sent to the server. + * @param url The url sent to the server. * @param e The exception thrown. */ - public CantGetResponseCode(String uri, IOException e) { - super("Can not get the server response code for the request " + uri, e); + public CantGetResponseCode(String url, IOException e) { + super("Can not get the server response code for the request " + url, e); } } @@ -158,12 +174,12 @@ public abstract class VOTableException extends Exception { /** - * @param uri The URI sent to the server. + * @param url The url sent to the server. * @param responseCode The HTTP GET response code, which is bad. */ - public BadResponseCodeException(String uri, int responseCode) { + public BadResponseCodeException(String url, int responseCode) { super("The server returned the bad response code `" + responseCode - + "` for the request " + uri); + + "` for the request " + url); } } @@ -175,12 +191,12 @@ public abstract class VOTableException extends Exception { /** - * @param uri The URI sent to the server. + * @param url The url sent to the server. * @param e The exception thrown. */ - public CantGetErrorStream(String uri, IOException e) { + public CantGetErrorStream(String url, IOException e) { super("Can not get the input stream of the result, neither the error stream " - + "for the request " + uri, e); + + "for the request " + url, e); } } 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 index 5952a22..a79d01b 100644 --- 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 @@ -34,8 +34,8 @@ import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSe 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.controller.VOTableException.CantSendQueryException.CantWriteQueryResultException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.UnsupportedParamEncodingException; import eu.omp.irap.vespa.epntapclient.votable.utils.Strings.StringJoiner; /** @@ -61,7 +61,7 @@ public class Network { * @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<String, String> parameters, String outputPath) + public static StringBuilder sendQuery(String url, Map<String, String> parameters) throws CantSendQueryException { StringJoiner paramJoiner = new StringJoiner("&"); @@ -74,17 +74,23 @@ public class Network { paramJoiner.add(param.getKey() + "=" + value); } catch (UnsupportedEncodingException e) { - throw new UnsupportedParameterEncodingException(param.getKey(), param.getValue(), - e); + throw new UnsupportedParamEncodingException(param.getKey(), param.getValue(), e); } - } - try { - Network.printRequestResult(Network.sendGet(url + paramJoiner), outputPath); - } catch (IOException e) { - throw new CantPrintRequestResultException(outputPath, e); + return Network.sendGet(url + "?" + paramJoiner); + } + + public static void sendQuery(String url, Map<String, String> parameters, String outputPath) + throws CantSendQueryException { + StringBuilder result = Network.sendQuery(url, parameters); + + try (PrintWriter writer = new PrintWriter(outputPath, Consts.ENCODING)) { + writer.println(result); + } catch (FileNotFoundException | UnsupportedEncodingException e) { + throw new CantWriteQueryResultException(outputPath, e); } + } /** @@ -146,22 +152,4 @@ public class Network { 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); - } - } } -- libgit2 0.21.2