|
|
@@ -4,9 +4,9 @@ package com.izouma.meta.websocket;
|
|
|
import com.izouma.meta.config.GeneralProperties;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.HttpStatus;
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
@@ -20,6 +20,8 @@ import java.util.Set;
|
|
|
@Slf4j
|
|
|
@AllArgsConstructor
|
|
|
public class WebsocketCommon {
|
|
|
+
|
|
|
+ private final HttpClient client = HttpClients.createDefault();
|
|
|
private GeneralProperties generalProperties;
|
|
|
|
|
|
/**
|
|
|
@@ -64,25 +66,21 @@ public class WebsocketCommon {
|
|
|
|
|
|
public String getRequest(String url) {
|
|
|
String requestUrl = generalProperties.getUrlPrefix().concat(url);
|
|
|
- HttpClient client = HttpClients.createDefault();
|
|
|
HttpGet get = new HttpGet(requestUrl);
|
|
|
- String obj;
|
|
|
- try {
|
|
|
- log.info(String.format("execute get request url : %S ", requestUrl));
|
|
|
- HttpResponse res = client.execute(get);
|
|
|
+ try (CloseableHttpResponse res = (CloseableHttpResponse) client.execute(get)) {
|
|
|
+ log.info(String.format("execute get request url : %s ", requestUrl));
|
|
|
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
|
|
- obj = EntityUtils.toString(res.getEntity());
|
|
|
+ return EntityUtils.toString(res.getEntity());
|
|
|
} else {
|
|
|
- String errMsg = String.format(" request [%S] returned error ", requestUrl);
|
|
|
- log.info(errMsg);
|
|
|
+ String errMsg = String.format("request [%s] returned error", requestUrl);
|
|
|
+ log.error(errMsg);
|
|
|
throw new RuntimeException(errMsg);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- String errMsg = String.format(" client failed to execute request for url: %S ", requestUrl);
|
|
|
- log.info(errMsg);
|
|
|
- throw new RuntimeException(errMsg);
|
|
|
+ String errMsg = String.format("client failed to execute request for url: %s", requestUrl);
|
|
|
+ log.error(errMsg, e);
|
|
|
+ throw new RuntimeException(errMsg, e);
|
|
|
}
|
|
|
- return obj;
|
|
|
}
|
|
|
|
|
|
}
|