Commit d03dc8a73dc0df7ef432c397051c6110f7829686
1 parent
041cc394
Exists in
master
Make VOTableConnection more generic (just a class to send any kind of queries) a…
…nd move it in utils package.
Showing
6 changed files
with
86 additions
and
48 deletions
Show diff stats
src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java
@@ -17,7 +17,11 @@ | @@ -17,7 +17,11 @@ | ||
17 | package eu.omp.irap.vespa.epntapclient.votable.controller; | 17 | package eu.omp.irap.vespa.epntapclient.votable.controller; |
18 | 18 | ||
19 | import java.io.IOException; | 19 | import java.io.IOException; |
20 | +import java.text.SimpleDateFormat; | ||
21 | +import java.util.Date; | ||
22 | +import java.util.HashMap; | ||
20 | import java.util.List; | 23 | import java.util.List; |
24 | +import java.util.Map; | ||
21 | import java.util.logging.Logger; | 25 | import java.util.logging.Logger; |
22 | 26 | ||
23 | import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantDisplayVOTableException; | 27 | import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantDisplayVOTableException; |
@@ -28,6 +32,8 @@ import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantDi | @@ -28,6 +32,8 @@ import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantDi | ||
28 | import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException; | 32 | import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException; |
29 | import eu.omp.irap.vespa.epntapclient.votable.model.Table; | 33 | import eu.omp.irap.vespa.epntapclient.votable.model.Table; |
30 | import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE; | 34 | import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE; |
35 | +import eu.omp.irap.vespa.epntapclient.votable.utils.Consts; | ||
36 | +import eu.omp.irap.vespa.epntapclient.votable.utils.Network; | ||
31 | import eu.omp.irap.vespa.epntapclient.votable.view.VOTableViewListener; | 37 | import eu.omp.irap.vespa.epntapclient.votable.view.VOTableViewListener; |
32 | 38 | ||
33 | /** | 39 | /** |
@@ -90,7 +96,7 @@ public class VOTableController implements VOTableViewListener { | @@ -90,7 +96,7 @@ public class VOTableController implements VOTableViewListener { | ||
90 | public void readTable() throws CantDisplayVOTableException, CantSendQueryException { | 96 | public void readTable() throws CantDisplayVOTableException, CantSendQueryException { |
91 | 97 | ||
92 | if (voTablePath == null) { | 98 | if (voTablePath == null) { |
93 | - voTablePath = VOTableConnection.sendQuery(targetURL, queryLanguage, query); | 99 | + voTablePath = VOTableController.getVOTable(targetURL, query); |
94 | } | 100 | } |
95 | voTable = VOTableParser.parseVOTable(voTablePath); | 101 | voTable = VOTableParser.parseVOTable(voTablePath); |
96 | 102 | ||
@@ -119,6 +125,21 @@ public class VOTableController implements VOTableViewListener { | @@ -119,6 +125,21 @@ public class VOTableController implements VOTableViewListener { | ||
119 | } | 125 | } |
120 | } | 126 | } |
121 | 127 | ||
128 | + public static String getVOTable(String targetURL, String query) throws CantSendQueryException { | ||
129 | + String dateStr = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss_SSS").format(new Date()); | ||
130 | + String voTablePath = Consts.TMP_DIR + "/votable" + dateStr + ".xml"; | ||
131 | + | ||
132 | + String url = targetURL + "/sync"; | ||
133 | + | ||
134 | + Map<String, String> parameters = new HashMap<>(); | ||
135 | + parameters.put("REQUEST", Consts.QUERY_REQUEST); | ||
136 | + parameters.put("LANG", Consts.QUERY_LANG); | ||
137 | + parameters.put("FORMAT", query); | ||
138 | + | ||
139 | + Network.sendQuery(url, parameters, voTablePath); | ||
140 | + return voTablePath; | ||
141 | + } | ||
142 | + | ||
122 | public String[] getColumns() { | 143 | public String[] getColumns() { |
123 | return voTableColumns; | 144 | return voTableColumns; |
124 | } | 145 | } |
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; | @@ -33,7 +33,7 @@ import eu.omp.irap.vespa.epntapclient.votable.model.Field; | ||
33 | import eu.omp.irap.vespa.epntapclient.votable.model.Stream; | 33 | import eu.omp.irap.vespa.epntapclient.votable.model.Stream; |
34 | import eu.omp.irap.vespa.epntapclient.votable.model.Table; | 34 | import eu.omp.irap.vespa.epntapclient.votable.model.Table; |
35 | import eu.omp.irap.vespa.epntapclient.votable.model.TableData; | 35 | import eu.omp.irap.vespa.epntapclient.votable.model.TableData; |
36 | -import eu.omp.irap.vespa.epntapclient.votable.utils.Utils; | 36 | +import eu.omp.irap.vespa.epntapclient.votable.utils.Debug; |
37 | 37 | ||
38 | /** | 38 | /** |
39 | * @author N. Jourdane | 39 | * @author N. Jourdane |
@@ -94,7 +94,7 @@ public class VOTableDataParser { | @@ -94,7 +94,7 @@ public class VOTableDataParser { | ||
94 | VOTableDataParser.parseFITSStream(table.getDATA().getFITS().getSTREAM(), fields); | 94 | VOTableDataParser.parseFITSStream(table.getDATA().getFITS().getSTREAM(), fields); |
95 | } | 95 | } |
96 | 96 | ||
97 | - Utils.printObject("voTableData", data); | 97 | + Debug.printObject("voTableData", data); |
98 | } | 98 | } |
99 | 99 | ||
100 | /** | 100 | /** |
src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java
@@ -85,6 +85,23 @@ public abstract class VOTableException extends Exception { | @@ -85,6 +85,23 @@ public abstract class VOTableException extends Exception { | ||
85 | } | 85 | } |
86 | } | 86 | } |
87 | 87 | ||
88 | + /** The URL is not correctly formated. */ | ||
89 | + public static class UnsupportedParameterEncodingException extends CantSendQueryException { | ||
90 | + | ||
91 | + /** */ | ||
92 | + private static final long serialVersionUID = 1L; | ||
93 | + | ||
94 | + | ||
95 | + /** | ||
96 | + * @param uri The URI sent to the server. | ||
97 | + * @param e The exception thrown. | ||
98 | + */ | ||
99 | + public UnsupportedParameterEncodingException(String key, String value, Exception e) { | ||
100 | + super("The URL parameter " + key + " with value '" + value | ||
101 | + + "' is not correctly encoded.", e); | ||
102 | + } | ||
103 | + } | ||
104 | + | ||
88 | /** Can not print the result in the file. */ | 105 | /** Can not print the result in the file. */ |
89 | public static class CantPrintRequestResultException extends CantSendQueryException { | 106 | public static class CantPrintRequestResultException extends CantSendQueryException { |
90 | 107 |
src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Consts.java
@@ -36,6 +36,10 @@ public class Consts { | @@ -36,6 +36,10 @@ public class Consts { | ||
36 | // http://gavo.aip.de/tap | 36 | // http://gavo.aip.de/tap |
37 | // http://voparis-cdpp.obspm.fr/tap | 37 | // http://voparis-cdpp.obspm.fr/tap |
38 | 38 | ||
39 | + public static final String QUERY_LANG = "ADQL"; | ||
40 | + | ||
41 | + public static final String QUERY_REQUEST = "doQuery"; | ||
42 | + | ||
39 | 43 | ||
40 | /** Constructor to hide the implicit public one. */ | 44 | /** Constructor to hide the implicit public one. */ |
41 | private Consts() { | 45 | private Consts() { |
src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java renamed to src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Network.java
@@ -14,7 +14,7 @@ | @@ -14,7 +14,7 @@ | ||
14 | * <http://www.gnu.org/licenses/>. | 14 | * <http://www.gnu.org/licenses/>. |
15 | */ | 15 | */ |
16 | 16 | ||
17 | -package eu.omp.irap.vespa.epntapclient.votable.controller; | 17 | +package eu.omp.irap.vespa.epntapclient.votable.utils; |
18 | 18 | ||
19 | import java.io.BufferedReader; | 19 | import java.io.BufferedReader; |
20 | import java.io.FileNotFoundException; | 20 | import java.io.FileNotFoundException; |
@@ -25,8 +25,7 @@ import java.io.UnsupportedEncodingException; | @@ -25,8 +25,7 @@ import java.io.UnsupportedEncodingException; | ||
25 | import java.net.HttpURLConnection; | 25 | import java.net.HttpURLConnection; |
26 | import java.net.URL; | 26 | import java.net.URL; |
27 | import java.net.URLEncoder; | 27 | import java.net.URLEncoder; |
28 | -import java.text.SimpleDateFormat; | ||
29 | -import java.util.Date; | 28 | +import java.util.Map; |
30 | import java.util.logging.Level; | 29 | import java.util.logging.Level; |
31 | import java.util.logging.Logger; | 30 | import java.util.logging.Logger; |
32 | 31 | ||
@@ -36,24 +35,23 @@ import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSe | @@ -36,24 +35,23 @@ import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSe | ||
36 | import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantGetResponseCode; | 35 | import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantGetResponseCode; |
37 | import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantOpenConnectionException; | 36 | import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantOpenConnectionException; |
38 | import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantPrintRequestResultException; | 37 | import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantPrintRequestResultException; |
39 | -import eu.omp.irap.vespa.epntapclient.votable.utils.Const; | 38 | +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.UnsupportedParameterEncodingException; |
39 | +import eu.omp.irap.vespa.epntapclient.votable.utils.Strings.StringJoiner; | ||
40 | 40 | ||
41 | /** | 41 | /** |
42 | - * This class provide HTTP connection support to send requests through POST / GET protocols. | ||
43 | - * | ||
44 | * @author N. Jourdane | 42 | * @author N. Jourdane |
45 | */ | 43 | */ |
46 | -public final class VOTableConnection { | 44 | +public class Network { |
47 | 45 | ||
48 | - /** The logger for the class VOTableConnection. */ | ||
49 | - private static final Logger logger = Logger.getLogger(VOTableConnection.class.getName()); | 46 | + /** The logger for the class Network. */ |
47 | + private static final Logger logger = Logger.getLogger(Network.class.getName()); | ||
50 | 48 | ||
51 | /** The user agent used for the requests. */ | 49 | /** The user agent used for the requests. */ |
52 | private static final String USER_AGENT = "Mozilla/5.0"; | 50 | private static final String USER_AGENT = "Mozilla/5.0"; |
53 | 51 | ||
54 | 52 | ||
55 | - /** Constructor to hide the implicit public one. */ | ||
56 | - private VOTableConnection() { | 53 | + /** Private constructor to hide the implicit public one. */ |
54 | + private Network() { | ||
57 | } | 55 | } |
58 | 56 | ||
59 | /** | 57 | /** |
@@ -63,31 +61,30 @@ public final class VOTableConnection { | @@ -63,31 +61,30 @@ public final class VOTableConnection { | ||
63 | * @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. |
64 | * @throws CantSendQueryException If the query can not be sent. | 62 | * @throws CantSendQueryException If the query can not be sent. |
65 | */ | 63 | */ |
66 | - public static String sendQuery(String targetURL, String queryLanguage, | ||
67 | - String query) throws CantSendQueryException { | 64 | + public static void sendQuery(String url, Map<String, String> parameters, String outputPath) |
65 | + throws CantSendQueryException { | ||
68 | 66 | ||
69 | - SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd_hhmmss_SSS"); | ||
70 | - String voTablePath = Const.TMP_DIR + "/votable" + ft.format(new Date()) + ".xml"; | 67 | + StringJoiner paramJoiner = new StringJoiner("&"); |
68 | + for (Map.Entry<String, String> param : parameters.entrySet()) { | ||
69 | + try { | ||
70 | + String value = URLEncoder.encode(param.getValue(), Consts.ENCODING) | ||
71 | + .replace("+", "%20") | ||
72 | + .replace(".", "%2E").replace("-", "%2D").replace("*", "%2A") | ||
73 | + .replace("_", "%5F"); | ||
74 | + paramJoiner.add(param.getKey() + "=" + value); | ||
75 | + | ||
76 | + } catch (UnsupportedEncodingException e) { | ||
77 | + throw new UnsupportedParameterEncodingException(param.getKey(), param.getValue(), | ||
78 | + e); | ||
79 | + } | ||
71 | 80 | ||
72 | - String uri = targetURL + "/sync"; | ||
73 | - String parameters = "REQUEST=doQuery&LANG=" + queryLanguage + "&FORMAT=votable&QUERY="; | ||
74 | - try { | ||
75 | - parameters += URLEncoder.encode(query, Const.ENCODING).replace("+", "%20") | ||
76 | - .replace(".", "%2E").replace("-", "%2D").replace("*", "%2A") | ||
77 | - .replace("_", "%5F"); | ||
78 | - } catch (UnsupportedEncodingException e) { | ||
79 | - throw new CantSendQueryException.MalformedURLException(uri, e); | ||
80 | } | 81 | } |
81 | 82 | ||
82 | - VOTableConnection.logger.info("request: " + uri + "?" + parameters); | ||
83 | try { | 83 | try { |
84 | - VOTableConnection.printRequestResult(VOTableConnection.sendGet(uri, parameters), | ||
85 | - voTablePath); | 84 | + Network.printRequestResult(Network.sendGet(url + paramJoiner), outputPath); |
86 | } catch (IOException e) { | 85 | } catch (IOException e) { |
87 | - throw new CantPrintRequestResultException(voTablePath, e); | 86 | + throw new CantPrintRequestResultException(outputPath, e); |
88 | } | 87 | } |
89 | - | ||
90 | - return voTablePath; | ||
91 | } | 88 | } |
92 | 89 | ||
93 | /** | 90 | /** |
@@ -96,17 +93,16 @@ public final class VOTableConnection { | @@ -96,17 +93,16 @@ public final class VOTableConnection { | ||
96 | * @return A string builder containing the result of the query. | 93 | * @return A string builder containing the result of the query. |
97 | * @throws CantSendQueryException If the query can not be sent. | 94 | * @throws CantSendQueryException If the query can not be sent. |
98 | */ | 95 | */ |
99 | - public static StringBuilder sendGet(String url, String urlParameters) | 96 | + private static StringBuilder sendGet(String url) |
100 | throws CantSendQueryException { | 97 | throws CantSendQueryException { |
101 | String inputLine; | 98 | String inputLine; |
102 | StringBuilder response = new StringBuilder(); | 99 | StringBuilder response = new StringBuilder(); |
103 | URL obj; | 100 | URL obj; |
104 | - String uri = url + "?" + urlParameters; | ||
105 | 101 | ||
106 | try { | 102 | try { |
107 | - obj = new URL(uri); | 103 | + obj = new URL(url); |
108 | } catch (java.net.MalformedURLException e) { | 104 | } catch (java.net.MalformedURLException e) { |
109 | - throw new CantSendQueryException.MalformedURLException(uri, e); | 105 | + throw new CantSendQueryException.MalformedURLException(url, e); |
110 | } | 106 | } |
111 | 107 | ||
112 | HttpURLConnection con; | 108 | HttpURLConnection con; |
@@ -115,20 +111,20 @@ public final class VOTableConnection { | @@ -115,20 +111,20 @@ public final class VOTableConnection { | ||
115 | con = (HttpURLConnection) obj.openConnection(); | 111 | con = (HttpURLConnection) obj.openConnection(); |
116 | con.setRequestMethod("GET"); | 112 | con.setRequestMethod("GET"); |
117 | } catch (IOException e) { | 113 | } catch (IOException e) { |
118 | - throw new CantOpenConnectionException(uri, e); | 114 | + throw new CantOpenConnectionException(url, e); |
119 | } | 115 | } |
120 | 116 | ||
121 | - con.setRequestProperty("User-Agent", VOTableConnection.USER_AGENT); | 117 | + con.setRequestProperty("User-Agent", Network.USER_AGENT); |
122 | int code = -1; | 118 | int code = -1; |
123 | 119 | ||
124 | try { | 120 | try { |
125 | code = con.getResponseCode(); | 121 | code = con.getResponseCode(); |
126 | } catch (IOException e) { | 122 | } catch (IOException e) { |
127 | - throw new CantGetResponseCode(uri, e); | 123 | + throw new CantGetResponseCode(url, e); |
128 | } | 124 | } |
129 | 125 | ||
130 | if (code != HttpURLConnection.HTTP_OK && code != HttpURLConnection.HTTP_BAD_REQUEST) { | 126 | if (code != HttpURLConnection.HTTP_OK && code != HttpURLConnection.HTTP_BAD_REQUEST) { |
131 | - throw new BadResponseCodeException(uri, code); | 127 | + throw new BadResponseCodeException(url, code); |
132 | } | 128 | } |
133 | 129 | ||
134 | try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) { | 130 | try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) { |
@@ -136,14 +132,14 @@ public final class VOTableConnection { | @@ -136,14 +132,14 @@ public final class VOTableConnection { | ||
136 | response.append(inputLine); | 132 | response.append(inputLine); |
137 | } | 133 | } |
138 | } catch (IOException e1) { | 134 | } catch (IOException e1) { |
139 | - VOTableConnection.logger.log(Level.WARNING, "Can not get input stream", e1); | 135 | + Network.logger.log(Level.WARNING, "Can not get input stream", e1); |
140 | try (BufferedReader in = new BufferedReader( | 136 | try (BufferedReader in = new BufferedReader( |
141 | new InputStreamReader(con.getErrorStream()))) { | 137 | new InputStreamReader(con.getErrorStream()))) { |
142 | while ((inputLine = in.readLine()) != null) { | 138 | while ((inputLine = in.readLine()) != null) { |
143 | response.append(inputLine); | 139 | response.append(inputLine); |
144 | } | 140 | } |
145 | } catch (IOException e2) { | 141 | } catch (IOException e2) { |
146 | - throw new CantGetErrorStream(uri, e2); | 142 | + throw new CantGetErrorStream(url, e2); |
147 | } | 143 | } |
148 | } | 144 | } |
149 | 145 | ||
@@ -160,12 +156,12 @@ public final class VOTableConnection { | @@ -160,12 +156,12 @@ public final class VOTableConnection { | ||
160 | private static void printRequestResult(StringBuilder response, String resultFileName) | 156 | private static void printRequestResult(StringBuilder response, String resultFileName) |
161 | throws IOException { | 157 | throws IOException { |
162 | 158 | ||
163 | - try (PrintWriter writer = new PrintWriter(resultFileName, Const.ENCODING)) { | 159 | + try (PrintWriter writer = new PrintWriter(resultFileName, Consts.ENCODING)) { |
164 | writer.println(response); | 160 | writer.println(response); |
165 | } catch (FileNotFoundException e1) { | 161 | } catch (FileNotFoundException e1) { |
166 | throw new IOException("The file " + resultFileName + " is not found.", e1); | 162 | throw new IOException("The file " + resultFileName + " is not found.", e1); |
167 | } catch (UnsupportedEncodingException e2) { | 163 | } catch (UnsupportedEncodingException e2) { |
168 | - throw new IOException("Bad encoding with " + Const.ENCODING, e2); | 164 | + throw new IOException("Bad encoding with " + Consts.ENCODING, e2); |
169 | } | 165 | } |
170 | } | 166 | } |
171 | -} | ||
172 | \ No newline at end of file | 167 | \ No newline at end of file |
168 | +} |
src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableView.java
@@ -29,7 +29,7 @@ import javax.swing.table.DefaultTableModel; | @@ -29,7 +29,7 @@ import javax.swing.table.DefaultTableModel; | ||
29 | import javax.swing.table.TableModel; | 29 | import javax.swing.table.TableModel; |
30 | import javax.swing.table.TableRowSorter; | 30 | import javax.swing.table.TableRowSorter; |
31 | 31 | ||
32 | -import eu.omp.irap.vespa.epntapclient.votable.utils.Utils; | 32 | +import eu.omp.irap.vespa.epntapclient.votable.utils.Debug; |
33 | 33 | ||
34 | /** | 34 | /** |
35 | * The main class of the View of the application. | 35 | * The main class of the View of the application. |
@@ -87,8 +87,8 @@ public class VOTableView extends JPanel implements TableModelListener { | @@ -87,8 +87,8 @@ public class VOTableView extends JPanel implements TableModelListener { | ||
87 | */ | 87 | */ |
88 | public void fillTable(String[] columns, List<Object[]> data) { | 88 | public void fillTable(String[] columns, List<Object[]> data) { |
89 | Object[][] values = data.toArray(new Object[data.size()][]); | 89 | Object[][] values = data.toArray(new Object[data.size()][]); |
90 | - Utils.printObject("tableColumns", columns); | ||
91 | - Utils.printObject("tableData", values); | 90 | + Debug.printObject("tableColumns", columns); |
91 | + Debug.printObject("tableData", values); | ||
92 | tableData.setDataVector(values, columns); | 92 | tableData.setDataVector(values, columns); |
93 | if (values.length != 0) { | 93 | if (values.length != 0) { |
94 | table.setRowSelectionInterval(0, 0); | 94 | table.setRowSelectionInterval(0, 0); |