Commit 17e4ebd913e11b9cf40f0b37ace18dfc7ed4896e
1 parent
240ca5ba
Exists in
master
HTTP Connection improvements, in order to send queries with the VOTable package:
- Move Connection class from controller/HTTPConnection.java to votable/controller/VOTableConnection.java; - Delete the unused sendPost() function; - Move sendQuery() from controller/EpnTapController.java to votable/controller/VOTableConnection.java
Showing
1 changed file
with
49 additions
and
63 deletions
Show diff stats
src/main/java/eu/omp/irap/vespa/epntapclient/controller/HTTPConnection.java renamed to src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java
... | ... | @@ -14,10 +14,9 @@ |
14 | 14 | * <http://www.gnu.org/licenses/>. |
15 | 15 | */ |
16 | 16 | |
17 | -package eu.omp.irap.vespa.epntapclient.controller; | |
17 | +package eu.omp.irap.vespa.epntapclient.votable.controller; | |
18 | 18 | |
19 | 19 | import java.io.BufferedReader; |
20 | -import java.io.DataOutputStream; | |
21 | 20 | import java.io.FileNotFoundException; |
22 | 21 | import java.io.IOException; |
23 | 22 | import java.io.InputStreamReader; |
... | ... | @@ -25,12 +24,11 @@ import java.io.PrintWriter; |
25 | 24 | import java.io.UnsupportedEncodingException; |
26 | 25 | import java.net.HttpURLConnection; |
27 | 26 | import java.net.URL; |
28 | -import java.util.logging.Level; | |
29 | - | |
30 | -import javax.net.ssl.HttpsURLConnection; | |
27 | +import java.net.URLEncoder; | |
28 | +import java.text.SimpleDateFormat; | |
29 | +import java.util.Date; | |
31 | 30 | |
32 | 31 | import eu.omp.irap.vespa.epntapclient.utils.Const; |
33 | -import eu.omp.irap.vespa.epntapclient.utils.Err; | |
34 | 32 | import eu.omp.irap.vespa.epntapclient.utils.Log; |
35 | 33 | |
36 | 34 | /** |
... | ... | @@ -38,43 +36,40 @@ import eu.omp.irap.vespa.epntapclient.utils.Log; |
38 | 36 | * |
39 | 37 | * @author N. Jourdane |
40 | 38 | */ |
41 | -public class HTTPConnection { | |
39 | +public class VOTableConnection { | |
42 | 40 | /** The user agent used for the requests. */ |
43 | - private final static String USER_AGENT = "Mozilla/5.0"; //$NON-NLS-1$ | |
41 | + private final static String USER_AGENT = "Mozilla/5.0"; | |
44 | 42 | |
45 | 43 | /** |
46 | - * @param url The target URL of the request. | |
47 | - * @param urlParameters The parameters of the request. | |
48 | - * @param resultFileName The name of the file where the request response content is writed. | |
49 | - * @return The response code of the request (200 if successful). | |
44 | + * @param targetURL The url of the registry to communicate (ie. "http://reg.g-vo.org"). | |
45 | + * @param serviceType The type of service (ie. "tap"). | |
46 | + * @param queryLanguage The language used for the queries (ie. "ADQL"). | |
47 | + * @param query The query to ask to the registry. | |
48 | + * @throws Exception If the query can not be sent or got a bad response code. | |
49 | + * @return The path of the temporary XML file containing the http response. | |
50 | 50 | */ |
51 | - public static int sendGet(String url, String urlParameters, String resultFileName) { | |
52 | - String inputLine; | |
53 | - StringBuffer response = new StringBuffer(); | |
54 | - URL obj; | |
51 | + public static String sendQuery(String targetURL, String serviceType, String queryLanguage, | |
52 | + String query) throws Exception { | |
53 | + | |
54 | + SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd_hhmmss_S"); | |
55 | + String voTablePath = Const.TMP_DIR + "/votable" + ft.format(new Date()) + ".xml"; | |
56 | + | |
55 | 57 | int responseCode = -1; |
56 | - BufferedReader in; | |
58 | + String uri = targetURL + "/" + serviceType + "/sync"; | |
59 | + String parameters = "REQUEST=doQuery&LANG=" + queryLanguage + "&QUERY="; | |
60 | + parameters += URLEncoder.encode(query, Const.CHARACTER_SET); | |
57 | 61 | |
62 | + Log.LOGGER.info("request: " + uri + "?" + parameters); | |
58 | 63 | try { |
59 | - obj = new URL(url + "?" + urlParameters); //$NON-NLS-1$ | |
60 | - HttpURLConnection con = (HttpURLConnection) obj.openConnection(); | |
61 | - con.setRequestMethod("GET"); //$NON-NLS-1$ | |
62 | - | |
63 | - // add request header | |
64 | - con.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$ | |
65 | - responseCode = con.getResponseCode(); | |
66 | - in = new BufferedReader(new InputStreamReader(con.getInputStream())); | |
67 | - while ((inputLine = in.readLine()) != null) { | |
68 | - response.append(inputLine); | |
69 | - } | |
70 | - in.close(); | |
71 | - } catch (IOException e1) { | |
72 | - Log.LOGGER.log(Level.SEVERE, Err.BAD_REQUEST, e1); | |
64 | + responseCode = VOTableConnection.sendGet(uri, parameters, voTablePath); | |
65 | + } catch (Exception e1) { | |
66 | + throw new Exception("Can not send the specified query: " + uri + "?" + query, e1); | |
67 | + } | |
68 | + if (responseCode != 200) { | |
69 | + throw new Exception("Bad response code for the query: " + query); | |
73 | 70 | } |
74 | 71 | |
75 | - printRequestResult(response, resultFileName); | |
76 | - | |
77 | - return responseCode; | |
72 | + return voTablePath; | |
78 | 73 | } |
79 | 74 | |
80 | 75 | /** |
... | ... | @@ -82,39 +77,30 @@ public class HTTPConnection { |
82 | 77 | * @param urlParameters The parameters of the request. |
83 | 78 | * @param resultFileName The name of the file where the request response content is writed. |
84 | 79 | * @return The response code of the request (200 if successful). |
80 | + * @throws Exception Can not send the specified request. | |
85 | 81 | */ |
86 | - public static int sendPost(String url, String urlParameters, String resultFileName) { | |
87 | - | |
88 | - URL obj; | |
82 | + private static int sendGet(String url, String urlParameters, String resultFileName) | |
83 | + throws Exception { | |
84 | + String inputLine; | |
89 | 85 | StringBuffer response = new StringBuffer(); |
86 | + URL obj = new URL(url + "?" + urlParameters); | |
90 | 87 | int responseCode = -1; |
91 | - DataOutputStream wr; | |
92 | - BufferedReader in; | |
93 | 88 | |
94 | 89 | try { |
95 | - obj = new URL(url); | |
96 | - HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); | |
97 | - | |
98 | - // Add request header | |
99 | - con.setRequestMethod("POST"); //$NON-NLS-1$ | |
100 | - con.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$ | |
101 | - con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); //$NON-NLS-1$ //$NON-NLS-2$ | |
102 | - | |
103 | - con.setDoOutput(true); | |
104 | - wr = new DataOutputStream(con.getOutputStream()); | |
105 | - wr.writeBytes(urlParameters); | |
106 | - wr.flush(); | |
107 | - wr.close(); | |
90 | + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); | |
91 | + con.setRequestMethod("GET"); | |
92 | + | |
93 | + // add request header | |
94 | + con.setRequestProperty("User-Agent", USER_AGENT); | |
108 | 95 | responseCode = con.getResponseCode(); |
109 | - in = new BufferedReader(new InputStreamReader(con.getInputStream())); | |
110 | - String inputLine; | |
96 | + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); | |
111 | 97 | |
112 | 98 | while ((inputLine = in.readLine()) != null) { |
113 | 99 | response.append(inputLine); |
114 | 100 | } |
115 | - in.close(); | |
101 | + new BufferedReader(new InputStreamReader(con.getInputStream())).close(); | |
116 | 102 | } catch (IOException e1) { |
117 | - Log.LOGGER.log(Level.SEVERE, Err.BAD_REQUEST, e1); | |
103 | + throw new Exception("Can not send the specified request.", e1); | |
118 | 104 | } |
119 | 105 | |
120 | 106 | printRequestResult(response, resultFileName); |
... | ... | @@ -123,22 +109,22 @@ public class HTTPConnection { |
123 | 109 | } |
124 | 110 | |
125 | 111 | /** |
126 | - * Try to print result in resultfileName. | |
112 | + * Print result of the query in a file. | |
127 | 113 | * |
128 | 114 | * @param response The request response content. |
129 | 115 | * @param resultFileName The name of the file where the request response content is writed. |
116 | + * @throws Exception File not found or bad encoding. | |
130 | 117 | */ |
131 | - private static void printRequestResult(StringBuffer response, String resultFileName) { | |
132 | - PrintWriter writer; | |
118 | + private static void printRequestResult(StringBuffer response, String resultFileName) | |
119 | + throws Exception { | |
133 | 120 | |
134 | - try { | |
135 | - writer = new PrintWriter(resultFileName, Const.CHARACTER_SET); | |
121 | + try (PrintWriter writer = new PrintWriter(resultFileName, Const.CHARACTER_SET)) { | |
136 | 122 | writer.println(response); |
137 | 123 | writer.close(); |
138 | 124 | } catch (FileNotFoundException e1) { |
139 | - Log.LOGGER.log(Level.SEVERE, Err.NOT_FOUND + resultFileName, e1); | |
125 | + throw new Exception("The file " + resultFileName + " is not found.", e1); | |
140 | 126 | } catch (UnsupportedEncodingException e2) { |
141 | - Log.LOGGER.log(Level.SEVERE, Err.WRONG_ENCODING + Const.CHARACTER_SET, e2); | |
127 | + throw new Exception("Bad encoding with " + Const.CHARACTER_SET, e2); | |
142 | 128 | } |
143 | 129 | } |
144 | 130 | } |
145 | 131 | \ No newline at end of file | ... | ... |