Commit 468b75237ccdd4b113f5763919b1e02102e34f52

Authored by Nathanael Jourdane
1 parent ba7e38e8
Exists in master

Improve Network class use.

src/main/java/eu/omp/irap/vespa/epntapclient/gui/ParamField.java
@@ -24,8 +24,10 @@ import java.text.DateFormat; @@ -24,8 +24,10 @@ import java.text.DateFormat;
24 import java.text.ParseException; 24 import java.text.ParseException;
25 import java.text.SimpleDateFormat; 25 import java.text.SimpleDateFormat;
26 import java.util.ArrayList; 26 import java.util.ArrayList;
  27 +import java.util.HashMap;
27 import java.util.List; 28 import java.util.List;
28 import java.util.Locale; 29 import java.util.Locale;
  30 +import java.util.Map;
29 import java.util.logging.Level; 31 import java.util.logging.Level;
30 import java.util.logging.Logger; 32 import java.util.logging.Logger;
31 33
@@ -42,8 +44,8 @@ import com.google.gson.JsonArray; @@ -42,8 +44,8 @@ import com.google.gson.JsonArray;
42 import com.google.gson.JsonObject; 44 import com.google.gson.JsonObject;
43 import com.google.gson.JsonParser; 45 import com.google.gson.JsonParser;
44 46
45 -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableConnection;  
46 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException; 47 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException;
  48 +import eu.omp.irap.vespa.epntapclient.votable.utils.Network;
47 49
48 /** 50 /**
49 * A field used to set a service parameter to build the query (in the parameter panel). ParamField 51 * 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 { @@ -428,9 +430,11 @@ public abstract class ParamField extends JPanel {
428 * @throws CantSendQueryException If the resolver do not work. 430 * @throws CantSendQueryException If the resolver do not work.
429 */ 431 */
430 static String[] getItems(String begining) throws CantSendQueryException { 432 static String[] getItems(String begining) throws CantSendQueryException {
431 - StringBuilder resolverResult;  
432 - resolverResult = VOTableConnection.sendGet(ParamField.RESOLVER_URL,  
433 - "q=\"" + begining + "\""); 433 + Map<String, String> params = new HashMap<>();
  434 + params.put("q", "\"" + begining + "\"");
  435 +
  436 + StringBuilder resolverResult = Network.sendQuery(ParamField.RESOLVER_URL, params);
  437 +
434 JsonObject root = new JsonParser().parse(resolverResult.toString()).getAsJsonObject(); 438 JsonObject root = new JsonParser().parse(resolverResult.toString()).getAsJsonObject();
435 int count = Integer.parseInt(root.get("count").toString()); 439 int count = Integer.parseInt(root.get("count").toString());
436 String[] targetNames = new String[count]; 440 String[] targetNames = new String[count];
src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java
@@ -67,7 +67,7 @@ public class VOTableApp { @@ -67,7 +67,7 @@ public class VOTableApp {
67 try { 67 try {
68 ctrl.readTable(); 68 ctrl.readTable();
69 } catch (VOTableException e) { 69 } catch (VOTableException e) {
70 - System.console().writer().println("Error: " + e.getMessage()); 70 + System.out.println("Error: " + e.getMessage());
71 VOTableApp.logger.log(Level.WARNING, e.getMessage(), e); 71 VOTableApp.logger.log(Level.WARNING, e.getMessage(), e);
72 return; 72 return;
73 } 73 }
src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java
@@ -19,9 +19,9 @@ package eu.omp.irap.vespa.epntapclient.votable.controller; @@ -19,9 +19,9 @@ package eu.omp.irap.vespa.epntapclient.votable.controller;
19 import java.io.IOException; 19 import java.io.IOException;
20 import java.text.SimpleDateFormat; 20 import java.text.SimpleDateFormat;
21 import java.util.Date; 21 import java.util.Date;
22 -import java.util.HashMap;  
23 import java.util.List; 22 import java.util.List;
24 -import java.util.Map; 23 +import java.util.SortedMap;
  24 +import java.util.TreeMap;
25 import java.util.logging.Logger; 25 import java.util.logging.Logger;
26 26
27 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantDisplayVOTableException; 27 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantDisplayVOTableException;
@@ -131,7 +131,7 @@ public class VOTableController implements VOTableViewListener { @@ -131,7 +131,7 @@ public class VOTableController implements VOTableViewListener {
131 131
132 String url = targetURL + "/sync"; 132 String url = targetURL + "/sync";
133 133
134 - Map<String, String> parameters = new HashMap<>(); 134 + SortedMap<String, String> parameters = new TreeMap<>();
135 parameters.put("REQUEST", Consts.QUERY_REQUEST); 135 parameters.put("REQUEST", Consts.QUERY_REQUEST);
136 parameters.put("LANG", Consts.QUERY_LANG); 136 parameters.put("LANG", Consts.QUERY_LANG);
137 parameters.put("FORMAT", query); 137 parameters.put("FORMAT", query);
src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java
@@ -77,26 +77,42 @@ public abstract class VOTableException extends Exception { @@ -77,26 +77,42 @@ public abstract class VOTableException extends Exception {
77 77
78 78
79 /** 79 /**
80 - * @param uri The URI sent to the server. 80 + * @param url The url sent to the server.
81 * @param e The exception thrown. 81 * @param e The exception thrown.
82 */ 82 */
83 - public MalformedURLException(String uri, Exception e) {  
84 - super("The URL " + uri + " is not correctly formated.", e); 83 + public MalformedURLException(String url, Exception e) {
  84 + super("The URL " + url + " is not correctly formated.", e);
85 } 85 }
86 } 86 }
87 87
88 /** The URL is not correctly formated. */ 88 /** The URL is not correctly formated. */
89 - public static class UnsupportedParameterEncodingException extends CantSendQueryException { 89 + public static class CantWriteQueryResultException extends CantSendQueryException {
90 90
91 /** */ 91 /** */
92 private static final long serialVersionUID = 1L; 92 private static final long serialVersionUID = 1L;
93 93
94 94
95 /** 95 /**
96 - * @param uri The URI sent to the server. 96 + * @param url The url sent to the server.
97 * @param e The exception thrown. 97 * @param e The exception thrown.
98 */ 98 */
99 - public UnsupportedParameterEncodingException(String key, String value, Exception e) { 99 + public CantWriteQueryResultException(String path, Exception e) {
  100 + super("Can not write the query result in " + path + ".", e);
  101 + }
  102 + }
  103 +
  104 + /** The URL is not correctly formated. */
  105 + public static class UnsupportedParamEncodingException extends CantSendQueryException {
  106 +
  107 + /** */
  108 + private static final long serialVersionUID = 1L;
  109 +
  110 +
  111 + /**
  112 + * @param url The url sent to the server.
  113 + * @param e The exception thrown.
  114 + */
  115 + public UnsupportedParamEncodingException(String key, String value, Exception e) {
100 super("The URL parameter " + key + " with value '" + value 116 super("The URL parameter " + key + " with value '" + value
101 + "' is not correctly encoded.", e); 117 + "' is not correctly encoded.", e);
102 } 118 }
@@ -126,11 +142,11 @@ public abstract class VOTableException extends Exception { @@ -126,11 +142,11 @@ public abstract class VOTableException extends Exception {
126 142
127 143
128 /** 144 /**
129 - * @param uri The URI sent to the server. 145 + * @param url The url sent to the server.
130 * @param e The exception thrown. 146 * @param e The exception thrown.
131 */ 147 */
132 - public CantOpenConnectionException(String uri, IOException e) {  
133 - super("Can not open an HTTP connection to " + uri, e); 148 + public CantOpenConnectionException(String url, IOException e) {
  149 + super("Can not open an HTTP connection to " + url, e);
134 } 150 }
135 } 151 }
136 152
@@ -142,11 +158,11 @@ public abstract class VOTableException extends Exception { @@ -142,11 +158,11 @@ public abstract class VOTableException extends Exception {
142 158
143 159
144 /** 160 /**
145 - * @param uri The URI sent to the server. 161 + * @param url The url sent to the server.
146 * @param e The exception thrown. 162 * @param e The exception thrown.
147 */ 163 */
148 - public CantGetResponseCode(String uri, IOException e) {  
149 - super("Can not get the server response code for the request " + uri, e); 164 + public CantGetResponseCode(String url, IOException e) {
  165 + super("Can not get the server response code for the request " + url, e);
150 } 166 }
151 } 167 }
152 168
@@ -158,12 +174,12 @@ public abstract class VOTableException extends Exception { @@ -158,12 +174,12 @@ public abstract class VOTableException extends Exception {
158 174
159 175
160 /** 176 /**
161 - * @param uri The URI sent to the server. 177 + * @param url The url sent to the server.
162 * @param responseCode The HTTP GET response code, which is bad. 178 * @param responseCode The HTTP GET response code, which is bad.
163 */ 179 */
164 - public BadResponseCodeException(String uri, int responseCode) { 180 + public BadResponseCodeException(String url, int responseCode) {
165 super("The server returned the bad response code `" + responseCode 181 super("The server returned the bad response code `" + responseCode
166 - + "` for the request " + uri); 182 + + "` for the request " + url);
167 } 183 }
168 } 184 }
169 185
@@ -175,12 +191,12 @@ public abstract class VOTableException extends Exception { @@ -175,12 +191,12 @@ public abstract class VOTableException extends Exception {
175 191
176 192
177 /** 193 /**
178 - * @param uri The URI sent to the server. 194 + * @param url The url sent to the server.
179 * @param e The exception thrown. 195 * @param e The exception thrown.
180 */ 196 */
181 - public CantGetErrorStream(String uri, IOException e) { 197 + public CantGetErrorStream(String url, IOException e) {
182 super("Can not get the input stream of the result, neither the error stream " 198 super("Can not get the input stream of the result, neither the error stream "
183 - + "for the request " + uri, e); 199 + + "for the request " + url, e);
184 } 200 }
185 } 201 }
186 202
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 @@ -34,8 +34,8 @@ import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSe
34 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantGetErrorStream; 34 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantGetErrorStream;
35 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantGetResponseCode; 35 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantGetResponseCode;
36 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantOpenConnectionException; 36 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantOpenConnectionException;
37 -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantPrintRequestResultException;  
38 -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.UnsupportedParameterEncodingException; 37 +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantWriteQueryResultException;
  38 +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.UnsupportedParamEncodingException;
39 import eu.omp.irap.vespa.epntapclient.votable.utils.Strings.StringJoiner; 39 import eu.omp.irap.vespa.epntapclient.votable.utils.Strings.StringJoiner;
40 40
41 /** 41 /**
@@ -61,7 +61,7 @@ public class Network { @@ -61,7 +61,7 @@ public class Network {
61 * @return The path of the temporary XML file containing the http response. 61 * @return The path of the temporary XML file containing the http response.
62 * @throws CantSendQueryException If the query can not be sent. 62 * @throws CantSendQueryException If the query can not be sent.
63 */ 63 */
64 - public static void sendQuery(String url, Map<String, String> parameters, String outputPath) 64 + public static StringBuilder sendQuery(String url, Map<String, String> parameters)
65 throws CantSendQueryException { 65 throws CantSendQueryException {
66 66
67 StringJoiner paramJoiner = new StringJoiner("&"); 67 StringJoiner paramJoiner = new StringJoiner("&");
@@ -74,17 +74,23 @@ public class Network { @@ -74,17 +74,23 @@ public class Network {
74 paramJoiner.add(param.getKey() + "=" + value); 74 paramJoiner.add(param.getKey() + "=" + value);
75 75
76 } catch (UnsupportedEncodingException e) { 76 } catch (UnsupportedEncodingException e) {
77 - throw new UnsupportedParameterEncodingException(param.getKey(), param.getValue(),  
78 - e); 77 + throw new UnsupportedParamEncodingException(param.getKey(), param.getValue(), e);
79 } 78 }
80 -  
81 } 79 }
82 80
83 - try {  
84 - Network.printRequestResult(Network.sendGet(url + paramJoiner), outputPath);  
85 - } catch (IOException e) {  
86 - throw new CantPrintRequestResultException(outputPath, e); 81 + return Network.sendGet(url + "?" + paramJoiner);
  82 + }
  83 +
  84 + public static void sendQuery(String url, Map<String, String> parameters, String outputPath)
  85 + throws CantSendQueryException {
  86 + StringBuilder result = Network.sendQuery(url, parameters);
  87 +
  88 + try (PrintWriter writer = new PrintWriter(outputPath, Consts.ENCODING)) {
  89 + writer.println(result);
  90 + } catch (FileNotFoundException | UnsupportedEncodingException e) {
  91 + throw new CantWriteQueryResultException(outputPath, e);
87 } 92 }
  93 +
88 } 94 }
89 95
90 /** 96 /**
@@ -146,22 +152,4 @@ public class Network { @@ -146,22 +152,4 @@ public class Network {
146 return response; 152 return response;
147 } 153 }
148 154
149 - /**  
150 - * Print result of the query in a file.  
151 - *  
152 - * @param response The request response content.  
153 - * @param resultFileName The name of the file where the request response content is writed.  
154 - * @throws IOException File not found or bad encoding.  
155 - */  
156 - private static void printRequestResult(StringBuilder response, String resultFileName)  
157 - throws IOException {  
158 -  
159 - try (PrintWriter writer = new PrintWriter(resultFileName, Consts.ENCODING)) {  
160 - writer.println(response);  
161 - } catch (FileNotFoundException e1) {  
162 - throw new IOException("The file " + resultFileName + " is not found.", e1);  
163 - } catch (UnsupportedEncodingException e2) {  
164 - throw new IOException("Bad encoding with " + Consts.ENCODING, e2);  
165 - }  
166 - }  
167 } 155 }