<dependency>\r
<groupId>org.onap.msb.java-sdk</groupId>\r
<artifactId>msb-java-sdk</artifactId>\r
- <version>1.1.0-SNAPSHOT</version>\r
+ <version>1.1.1-SNAPSHOT</version>\r
</dependency>\r
<dependency>\r
<groupId>org.reflections</groupId>\r
*/\r
package org.onap.holmes.rulemgt.bolt.enginebolt;\r
\r
+import java.io.IOException;\r
import java.util.HashMap;\r
import javax.ws.rs.core.MediaType;\r
import lombok.extern.slf4j.Slf4j;\r
import org.apache.http.HttpResponse;\r
import org.apache.http.entity.StringEntity;\r
+import org.apache.http.impl.client.CloseableHttpClient;\r
import org.jvnet.hk2.annotations.Service;\r
import org.onap.holmes.common.utils.GsonUtil;\r
import org.onap.holmes.common.utils.HttpsUtils;\r
protected HttpResponse delete(String packageName, String ip) throws Exception {\r
HashMap headers = createHeaders();\r
String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH + "/" + packageName;\r
- return HttpsUtils.delete(url, headers);\r
+ CloseableHttpClient httpClient = null;\r
+ try {\r
+ httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);\r
+ return HttpsUtils.delete(url, headers, httpClient);\r
+ } finally {\r
+ closeHttpClient(httpClient);\r
+ }\r
}\r
\r
protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip)\r
String content = GsonUtil.beanToJson(correlationCheckRule4Engine);\r
HashMap headers = createHeaders();\r
String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH;\r
- return HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content));\r
+ CloseableHttpClient httpClient = null;\r
+ try {\r
+ httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);\r
+ return HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content), httpClient);\r
+ } finally {\r
+ closeHttpClient(httpClient);\r
+ }\r
}\r
\r
protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine, String ip) throws Exception {\r
String content = GsonUtil.beanToJson(correlationDeployRule4Engine);\r
HashMap headers = createHeaders();\r
String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH;\r
- return HttpsUtils.put(url, headers, new HashMap<>(), new StringEntity(content));\r
+ CloseableHttpClient httpClient = null;\r
+ try {\r
+ httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);\r
+ return HttpsUtils.put(url, headers, new HashMap<>(), new StringEntity(content),httpClient);\r
+ } finally {\r
+ closeHttpClient(httpClient);\r
+ }\r
+ }\r
+\r
+ private void closeHttpClient(CloseableHttpClient httpClient) {\r
+ if (httpClient != null) {\r
+ try {\r
+ httpClient.close();\r
+ } catch (IOException e) {\r
+ log.warn("Failed to close http client!");\r
+ }\r
+ }\r
}\r
\r
private HashMap<String, String> createHeaders() {\r
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
import org.onap.holmes.common.dcae.DcaeConfigurationQuery;
import org.onap.holmes.common.dcae.entity.DcaeConfigurations;
import org.onap.holmes.common.dcae.entity.Rule;
log.error("Failed to get right response!" + e.getMessage(), e);
} catch (IOException e) {
log.error("Failed to extract response entity. " + e.getMessage(), e);
+ } catch (Exception e) {
+ log.error("Failed to build http client. " + e.getMessage(), e);
}
}
if (ruleQueryListResponse != null) {
public RuleQueryListResponse getAllCorrelationRules() throws CorrelationException, IOException {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", MediaType.APPLICATION_JSON);
- HttpResponse httpResponse = HttpsUtils.get(url, headers);
- String response = HttpsUtils.extractResponseEntity(httpResponse);
- return JSON.parseObject(response,RuleQueryListResponse.class);
+ CloseableHttpClient httpClient = null;
+ try {
+ httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpResponse httpResponse = HttpsUtils.get(url, headers, httpClient);
+ String response = HttpsUtils.extractResponseEntity(httpResponse);
+ return JSON.parseObject(response,RuleQueryListResponse.class);
+ } finally {
+ closeHttpClient(httpClient);
+ }
}
private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) throws CorrelationException {
headers.put("Content-Type", MediaType.APPLICATION_JSON);
headers.put("Accept", MediaType.APPLICATION_JSON);
HttpResponse httpResponse;
+ CloseableHttpClient httpClient = null;
try {
+ httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
httpResponse = HttpsUtils
- .put(url, headers, new HashMap<>(), new StringEntity(content));
+ .put(url, headers, new HashMap<>(), new StringEntity(content), httpClient);
} catch (UnsupportedEncodingException e) {
throw new CorrelationException("Failed to create https entity.", e);
} catch (Exception e) {
throw new CorrelationException(e.getMessage());
+ } finally {
+ closeHttpClient(httpClient);
}
if (httpResponse != null) {
suc = httpResponse.getStatusLine().getStatusCode() == 200;
ruleResult4APIs.forEach(correlationRule ->{
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", MediaType.APPLICATION_JSON);
+ CloseableHttpClient httpClient = null;
try {
- HttpsUtils.delete(url + "/" + correlationRule.getRuleId(), headers);
+ httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpsUtils.delete(url + "/" + correlationRule.getRuleId(), headers, httpClient);
} catch (Exception e) {
log.warn("Failed to delete rule, the rule id is : " + correlationRule.getRuleId()
+ " exception messge is : " + e.getMessage(), e);
+ } finally {
+ closeHttpClient(httpClient);
}
});
}
ruleCreateRequest.setEnabled(1);
return ruleCreateRequest;
}
+
+ private void closeHttpClient(CloseableHttpClient httpClient) {
+ if (httpClient != null) {
+ try {
+ httpClient.close();
+ } catch (IOException e) {
+ log.warn("Failed to close http client!");
+ }
+ }
+ }
}
package org.onap.holmes.rulemgt.msb;
+import java.io.IOException;
+import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
+import org.apache.http.impl.client.CloseableHttpClient;
import org.jvnet.hk2.annotations.Service;
import org.onap.holmes.common.api.entity.ServiceEntity;
import org.onap.holmes.common.api.entity.ServiceNode4Query;
import java.util.List;
@Service
+@Slf4j
public class EngineIpList {
private String[] msbAddrInfo;
public List<String> getServiceCount()throws Exception{
String response;
+ CloseableHttpClient httpClient = null;
try {
+ httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
HttpResponse httpResponse = HttpsUtils
- .get(url, new HashMap<>());
+ .get(url, new HashMap<>(), httpClient);
response = HttpsUtils.extractResponseEntity(httpResponse);
} catch (Exception e) {
throw e;
+ } finally {
+ if (httpClient != null) {
+ try {
+ httpClient.close();
+ } catch (IOException e) {
+ log.warn("Failed to close http client!");
+ }
+ }
}
ServiceEntity service = GsonUtil.jsonToBean(response, ServiceEntity.class);
List<ServiceNode4Query> nodesList = service.getNodes();
package org.onap.holmes.rulemgt.bolt.enginebolt;\r
\r
\r
+import static org.hamcrest.MatcherAssert.assertThat;\r
+import static org.hamcrest.Matchers.any;\r
+import static org.hamcrest.Matchers.equalTo;\r
+\r
+import java.util.HashMap;\r
import org.apache.http.HttpResponse;\r
import org.apache.http.client.methods.CloseableHttpResponse;\r
import org.apache.http.impl.client.CloseableHttpClient;\r
import org.apache.http.impl.client.HttpClients;\r
+import org.easymock.EasyMock;\r
+import org.hamcrest.Matchers;\r
import org.junit.Before;\r
import org.junit.Rule;\r
+import org.junit.Test;\r
import org.junit.rules.ExpectedException;\r
+import org.onap.holmes.common.utils.HttpsUtils;\r
import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
import org.powermock.api.easymock.PowerMock;\r
+import org.powermock.core.classloader.annotations.PowerMockIgnore;\r
import org.powermock.core.classloader.annotations.PrepareForTest;\r
import org.powermock.modules.junit4.rule.PowerMockRule;\r
+import org.powermock.reflect.Whitebox;\r
\r
-@PrepareForTest({HttpClients.class, CloseableHttpClient.class})\r
+@PrepareForTest({HttpClients.class, CloseableHttpClient.class, HttpsUtils.class})\r
+@PowerMockIgnore("javax.net.ssl.*")\r
public class EngineServiceTest {\r
\r
@Rule\r
correlationDeployRule4Engine.setContent("{\"package\":\"test\"}");\r
correlationDeployRule4Engine.setEngineId("engine_id");\r
}\r
+\r
+ @Test\r
+ public void testEngineService_createHeaders_ok() throws Exception {\r
+ PowerMock.resetAll();\r
+ HashMap<String, String> headers = Whitebox.invokeMethod(engineService, "createHeaders");\r
+ assertThat(headers.get("Content-Type"), equalTo("application/json"));\r
+ assertThat(headers.get("Accept"), equalTo("application/json"));\r
+ }\r
+\r
+ @Test\r
+ public void testEngineService_closeHttpClient_ok() throws Exception {\r
+ PowerMock.resetAll();\r
+ CloseableHttpClient closeableHttpClient = HttpsUtils\r
+ .getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);\r
+ Whitebox.invokeMethod(engineService, "closeHttpClient", closeableHttpClient);\r
+ }\r
+\r
}
\ No newline at end of file