Removed Dependency: httpclient
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / bolt / enginebolt / EngineService.java
index 8782d7a..794ea93 100644 (file)
@@ -1,5 +1,5 @@
 /**\r
- * Copyright 2017-2020 ZTE Corporation.\r
+ * Copyright 2017-2021 ZTE Corporation.\r
  * <p>\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  */\r
 package org.onap.holmes.rulemgt.bolt.enginebolt;\r
 \r
-import lombok.extern.slf4j.Slf4j;\r
-import org.apache.http.HttpResponse;\r
-import org.apache.http.client.methods.HttpDelete;\r
-import org.apache.http.client.methods.HttpPost;\r
-import org.apache.http.client.methods.HttpPut;\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
+import org.onap.holmes.common.utils.CommonUtils;\r
+import org.onap.holmes.common.utils.JerseyClient;\r
 import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
 import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
-import org.onap.holmes.rulemgt.constant.RuleMgtConstant;\r
 \r
+import javax.ws.rs.client.Entity;\r
 import javax.ws.rs.core.MediaType;\r
-import java.io.IOException;\r
-import java.util.HashMap;\r
 \r
-@Slf4j\r
+import static org.onap.holmes.rulemgt.constant.RuleMgtConstant.ENGINE_PATH;\r
+\r
 @Service\r
 public class EngineService {\r
 \r
-    private static final String HTTPS = "https://";\r
-    private static final String HTTP = "http://";\r
-    private static final String PORT = ":9102";\r
+    private static final String PORT = "9102";\r
+    private static final String SEP = "//";\r
+    private static final String COLON = ":";\r
 \r
-    protected HttpResponse delete(String packageName, String ip) throws Exception {\r
-        HashMap headers = createHeaders();\r
-        String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH + "/" + packageName;\r
-        CloseableHttpClient httpClient = null;\r
-        HttpDelete httpDelete = new HttpDelete(url);\r
-        try {\r
-            httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);\r
-            return HttpsUtils.delete(httpDelete, headers, httpClient);\r
-        } finally {\r
-            httpDelete.releaseConnection();\r
-            closeHttpClient(httpClient);\r
-        }\r
+    protected boolean delete(String packageName, String ip) {\r
+        return new JerseyClient()\r
+                .path(packageName)\r
+                .delete(getUrl(ip)) != null;\r
     }\r
 \r
-    protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip)\r
-            throws Exception {\r
-        String content = GsonUtil.beanToJson(correlationCheckRule4Engine);\r
-        HashMap headers = createHeaders();\r
-        String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH;\r
-        CloseableHttpClient httpClient = null;\r
-        HttpPost httpPost = new HttpPost(url);\r
-        try {\r
-            httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);\r
-            return HttpsUtils.post(httpPost, headers, new HashMap<>(), new StringEntity(content), httpClient);\r
-        } finally {\r
-            httpPost.releaseConnection();\r
-            closeHttpClient(httpClient);\r
-        }\r
+    protected boolean check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip) {\r
+        return new JerseyClient()\r
+                .header("Accept", MediaType.APPLICATION_JSON)\r
+                .post(getUrl(ip), Entity.json(correlationCheckRule4Engine)) != null;\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 = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH;\r
-        CloseableHttpClient httpClient = null;\r
-        HttpPut httpPut = new HttpPut(url);\r
-        try {\r
-            httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);\r
-            return HttpsUtils.put(httpPut, headers, new HashMap<>(), new StringEntity(content), httpClient);\r
-        } finally {\r
-            closeHttpClient(httpClient);\r
-        }\r
+    protected String deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine, String ip) {\r
+        return new JerseyClient()\r
+                .header("Accept", MediaType.APPLICATION_JSON)\r
+                .put(getUrl(ip), Entity.json(correlationDeployRule4Engine));\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
-        HashMap<String, String> headers = new HashMap<>();\r
-        headers.put("Content-Type", MediaType.APPLICATION_JSON);\r
-        headers.put("Accept", MediaType.APPLICATION_JSON);\r
-        return headers;\r
+    private String getRequestPref() {\r
+        return CommonUtils.isHttpsEnabled() ? JerseyClient.PROTOCOL_HTTPS : JerseyClient.PROTOCOL_HTTP;\r
     }\r
 \r
-    private String getRequestPref() {\r
-        return HttpsUtils.isHttpsEnabled() ? HTTPS : HTTP;\r
+    private String getUrl(String ip) {\r
+        return new StringBuilder(getRequestPref())\r
+                .append(SEP)\r
+                .append(ip)\r
+                .append(COLON)\r
+                .append(PORT)\r
+                .append(ENGINE_PATH)\r
+                .toString();\r
     }\r
 }\r