|
34 | 34 | import java.util.concurrent.ConcurrentHashMap;
|
35 | 35 | import java.util.stream.Collectors;
|
36 | 36 |
|
37 |
| -import org.apache.http.HttpResponse; |
38 |
| -import org.apache.http.client.ClientProtocolException; |
39 |
| -import org.apache.http.client.ResponseHandler; |
40 |
| -import org.apache.http.client.config.CookieSpecs; |
41 |
| -import org.apache.http.client.config.RequestConfig; |
42 |
| -import org.apache.http.client.methods.HttpGet; |
43 |
| -import org.apache.http.impl.client.CloseableHttpClient; |
44 |
| -import org.apache.http.impl.client.DefaultServiceUnavailableRetryStrategy; |
45 |
| -import org.apache.http.impl.client.HttpClients; |
46 |
| -import org.apache.http.util.EntityUtils; |
| 37 | +import org.apache.hc.client5.http.classic.methods.HttpGet; |
| 38 | +import org.apache.hc.client5.http.config.RequestConfig; |
| 39 | +import org.apache.hc.client5.http.cookie.StandardCookieSpec; |
| 40 | +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
| 41 | +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; |
| 42 | +import org.apache.hc.client5.http.impl.classic.HttpClients; |
| 43 | +import org.apache.hc.core5.http.io.entity.EntityUtils; |
| 44 | +import org.apache.hc.core5.util.Timeout; |
47 | 45 | import org.apache.logging.log4j.LogManager;
|
48 | 46 | import org.apache.logging.log4j.Logger;
|
49 | 47 |
|
@@ -148,53 +146,46 @@ public boolean isCid(Integer cid) {
|
148 | 146 | }
|
149 | 147 |
|
150 | 148 | public String getJSONFromUrl(String url) {
|
151 |
| - // Making HTTP request |
152 |
| - CloseableHttpClient httpclient = HttpClients.custom() |
153 |
| - .setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD) |
154 |
| - .setConnectTimeout(5 * 1000).setSocketTimeout(60 * 1000).build()) |
155 |
| - .setServiceUnavailableRetryStrategy(new DefaultServiceUnavailableRetryStrategy(5, 5000)).build(); |
156 |
| - try { |
157 |
| - HttpGet httpget = new HttpGet(url); |
158 |
| - // Create a custom response handler |
159 |
| - |
160 |
| - // a response handler that throws an exception if status is not 200 |
161 |
| - ResponseHandler<String> responseHandler = new ResponseHandler<String>() { |
162 |
| - @Override |
163 |
| - public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { |
164 |
| - int status = response.getStatusLine().getStatusCode(); |
165 |
| - |
166 |
| - if (status == 404) { |
167 |
| - logger.warn("PUGREST.NotFound"); |
| 149 | + try (CloseableHttpClient httpClient = HttpClients.custom() |
| 150 | + .setDefaultRequestConfig(RequestConfig.custom() |
| 151 | + .setConnectTimeout(Timeout.ofSeconds(5)) |
| 152 | + .setResponseTimeout(Timeout.ofSeconds(60)) |
| 153 | + .setCookieSpec(StandardCookieSpec.STRICT) |
| 154 | + .build()) |
| 155 | + .build()) { |
| 156 | + HttpGet httpGet = new HttpGet(url); |
| 157 | + try (CloseableHttpResponse response = httpClient.execute(httpGet)) { |
| 158 | + int status = response.getCode(); |
| 159 | + if (status == 404) { |
| 160 | + logger.warn("PUGREST.NotFound"); |
| 161 | + try { |
168 | 162 | return EntityUtils.toString(response.getEntity());
|
| 163 | + } catch (org.apache.hc.core5.http.ParseException e) { |
| 164 | + logger.error("Error parsing HTTP response entity", e); |
| 165 | + return ""; |
169 | 166 | }
|
170 |
| - |
171 |
| - if (status != 200) { |
172 |
| - logger.error("HTTP respomse:" + status); |
| 167 | + } |
| 168 | + if (status != 200) { |
| 169 | + logger.error("HTTP response: " + status); |
| 170 | + try { |
173 | 171 | logger.error(EntityUtils.toString(response.getEntity()));
|
174 |
| - throw new ClientProtocolException("Pubchem no success."); |
| 172 | + } catch (org.apache.hc.core5.http.ParseException e) { |
| 173 | + logger.error("Error parsing HTTP response entity", e); |
| 174 | + return ""; |
175 | 175 | }
|
| 176 | + throw new IOException("Pubchem no success."); |
| 177 | + } |
| 178 | + try { |
176 | 179 | return EntityUtils.toString(response.getEntity());
|
| 180 | + } catch (org.apache.hc.core5.http.ParseException e) { |
| 181 | + logger.error("Error parsing HTTP response entity", e); |
| 182 | + return ""; |
177 | 183 | }
|
178 |
| - |
179 |
| - }; |
180 |
| - |
181 |
| - String responseBody = httpclient.execute(httpget, responseHandler); |
182 |
| - return responseBody; |
183 |
| - } catch (ClientProtocolException e) { |
184 |
| - // TODO Auto-generated catch block |
185 |
| - e.printStackTrace(); |
186 |
| - } catch (IOException e) { |
187 |
| - // TODO Auto-generated catch block |
188 |
| - e.printStackTrace(); |
189 |
| - } finally { |
190 |
| - try { |
191 |
| - httpclient.close(); |
192 |
| - } catch (IOException e) { |
193 |
| - // TODO Auto-generated catch block |
194 |
| - e.printStackTrace(); |
195 | 184 | }
|
| 185 | + } catch (IOException e) { |
| 186 | + logger.error("Error fetching JSON from URL: " + url, e); |
| 187 | + return ""; |
196 | 188 | }
|
197 |
| - return ""; |
198 | 189 | }
|
199 | 190 |
|
200 | 191 | // Respons classes for json structure
|
|
0 commit comments