HTTP/S Modifications
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / bolt / enginebolt / EngineService.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 package org.onap.holmes.rulemgt.bolt.enginebolt;\r
17 \r
18 import java.io.IOException;\r
19 import java.util.HashMap;\r
20 import javax.ws.rs.core.MediaType;\r
21 import lombok.extern.slf4j.Slf4j;\r
22 import org.apache.http.HttpResponse;\r
23 import org.apache.http.client.methods.HttpDelete;\r
24 import org.apache.http.client.methods.HttpPost;\r
25 import org.apache.http.client.methods.HttpPut;\r
26 import org.apache.http.entity.StringEntity;\r
27 import org.apache.http.impl.client.CloseableHttpClient;\r
28 import org.jvnet.hk2.annotations.Service;\r
29 import org.onap.holmes.common.utils.GsonUtil;\r
30 import org.onap.holmes.common.utils.HttpsUtils;\r
31 import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
32 import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
33 import org.onap.holmes.rulemgt.constant.RuleMgtConstant;\r
34 \r
35 @Slf4j\r
36 @Service\r
37 public class EngineService {\r
38 \r
39     private static final String HTTPS = "https://";\r
40     private static final String HTTP = "http://";\r
41     private static final String PORT = ":9102";\r
42 \r
43     protected HttpResponse delete(String packageName, String ip) throws Exception {\r
44         HashMap headers = createHeaders();\r
45         String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH + "/" + packageName;\r
46         CloseableHttpClient httpClient = null;\r
47         HttpDelete httpDelete = new HttpDelete(url);\r
48         try {\r
49             httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);\r
50             return HttpsUtils.delete(httpDelete, headers, httpClient);\r
51         } finally {\r
52             httpDelete.releaseConnection();\r
53             closeHttpClient(httpClient);\r
54         }\r
55     }\r
56 \r
57     protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip)\r
58             throws Exception {\r
59         String content = GsonUtil.beanToJson(correlationCheckRule4Engine);\r
60         HashMap headers = createHeaders();\r
61         String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH;\r
62         CloseableHttpClient httpClient = null;\r
63         HttpPost httpPost = new HttpPost(url);\r
64         try {\r
65             httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);\r
66             return HttpsUtils.post(httpPost, headers, new HashMap<>(), new StringEntity(content), httpClient);\r
67         } finally {\r
68             httpPost.releaseConnection();\r
69             closeHttpClient(httpClient);\r
70         }\r
71     }\r
72 \r
73     protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine, String ip) throws Exception {\r
74         String content = GsonUtil.beanToJson(correlationDeployRule4Engine);\r
75         HashMap headers = createHeaders();\r
76         String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH;\r
77         CloseableHttpClient httpClient = null;\r
78         HttpPut httpPut = new HttpPut(url);\r
79         try {\r
80             httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);\r
81             return HttpsUtils.put(httpPut, headers, new HashMap<>(), new StringEntity(content),httpClient);\r
82         } finally {\r
83             closeHttpClient(httpClient);\r
84         }\r
85     }\r
86 \r
87     private void closeHttpClient(CloseableHttpClient httpClient) {\r
88         if (httpClient != null) {\r
89             try {\r
90                 httpClient.close();\r
91             } catch (IOException e) {\r
92                 log.warn("Failed to close http client!");\r
93             }\r
94         }\r
95     }\r
96 \r
97     private HashMap<String, String> createHeaders() {\r
98         HashMap<String, String> headers = new HashMap<>();\r
99         headers.put("Content-Type", MediaType.APPLICATION_JSON);\r
100         headers.put("Accept", MediaType.APPLICATION_JSON);\r
101         return headers;\r
102     }\r
103 \r
104     private String getRequestPref(){\r
105         return HttpsUtils.isHttpsEnabled() ? HTTPS : HTTP;\r
106     }\r
107 }\r