X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=rulemgt%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fholmes%2Frulemgt%2Fbolt%2Fenginebolt%2FEngineService.java;h=15af38d6803909ece27c4a63faeec2da6dad1119;hb=57d0392cd069569f8797fded20a6406f5e6f156f;hp=5dc97b4f2cb19ca12c0fbf580c6273c7817e2973;hpb=3893b89844f0e76d1a80c578216c4320fac65ed9;p=holmes%2Frule-management.git diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java index 5dc97b4..15af38d 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java @@ -15,17 +15,19 @@ */ package org.onap.holmes.rulemgt.bolt.enginebolt; -import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.WebTarget; +import java.util.HashMap; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import lombok.extern.slf4j.Slf4j; -import org.glassfish.jersey.client.ClientConfig; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.common.utils.GsonUtil; +import org.onap.holmes.common.utils.HttpsUtils; import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; import org.onap.holmes.rulemgt.constant.RuleMgtConstant; @@ -35,32 +37,67 @@ import org.onap.holmes.common.config.MicroServiceConfig; @Service public class EngineService { - protected Response delete(String packageName) throws IOException { - Client client = createClient(); - WebTarget webTarget = client - .target(MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH + "/" + packageName); - return webTarget.request(MediaType.APPLICATION_JSON).delete(); + private static final String PREFIX = "https://"; + private static final String PORT = ":9102"; + + protected HttpResponse delete(String packageName, String ip) throws Exception { + HashMap headers = createHeaders(); + String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH + "/" + packageName; + CloseableHttpClient httpClient = null; + HttpDelete httpDelete = new HttpDelete(url); + try { + httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); + return HttpsUtils.delete(httpDelete, headers, httpClient); + } finally { + httpDelete.releaseConnection(); + closeHttpClient(httpClient); + } + } + + protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip) + throws Exception { + String content = GsonUtil.beanToJson(correlationCheckRule4Engine); + HashMap headers = createHeaders(); + String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH; + CloseableHttpClient httpClient = null; + HttpPost httpPost = new HttpPost(url); + try { + httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); + return HttpsUtils.post(httpPost, headers, new HashMap<>(), new StringEntity(content), httpClient); + } finally { + httpPost.releaseConnection(); + closeHttpClient(httpClient); + } } - private Client createClient() { - ClientConfig clientConfig = new ClientConfig(); - return ClientBuilder.newClient(clientConfig); + protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine, String ip) throws Exception { + String content = GsonUtil.beanToJson(correlationDeployRule4Engine); + HashMap headers = createHeaders(); + String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH; + CloseableHttpClient httpClient = null; + HttpPut httpPut = new HttpPut(url); + try { + httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); + return HttpsUtils.put(httpPut, headers, new HashMap<>(), new StringEntity(content),httpClient); + } finally { + closeHttpClient(httpClient); + } } - protected Response check(CorrelationCheckRule4Engine correlationCheckRule4Engine) - throws IOException { - Client client = createClient(); - ObjectMapper mapper = new ObjectMapper(); - String content = mapper.writeValueAsString(correlationCheckRule4Engine); - WebTarget webTarget = client.target(MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH); - return webTarget.request(MediaType.APPLICATION_JSON).post(Entity.entity(content, MediaType.APPLICATION_JSON)); + private void closeHttpClient(CloseableHttpClient httpClient) { + if (httpClient != null) { + try { + httpClient.close(); + } catch (IOException e) { + log.warn("Failed to close http client!"); + } + } } - protected Response deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws IOException { - Client client = createClient(); - ObjectMapper mapper = new ObjectMapper(); - String content = mapper.writeValueAsString(correlationDeployRule4Engine); - WebTarget webTarget = client.target(MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH); - return webTarget.request(MediaType.APPLICATION_JSON).put(Entity.entity(content, MediaType.APPLICATION_JSON)); + private HashMap createHeaders() { + HashMap headers = new HashMap<>(); + headers.put("Content-Type", MediaType.APPLICATION_JSON); + headers.put("Accept", MediaType.APPLICATION_JSON); + return headers; } }