fix https bug
[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.entity.StringEntity;\r
24 import org.apache.http.impl.client.CloseableHttpClient;\r
25 import org.jvnet.hk2.annotations.Service;\r
26 import org.onap.holmes.common.utils.GsonUtil;\r
27 import org.onap.holmes.common.utils.HttpsUtils;\r
28 import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
29 import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
30 import org.onap.holmes.rulemgt.constant.RuleMgtConstant;\r
31 import org.onap.holmes.common.config.MicroServiceConfig;\r
32 \r
33 @Slf4j\r
34 @Service\r
35 public class EngineService {\r
36 \r
37     private static final String PREFIX = "https://";\r
38     private static final String PORT = ":9102";\r
39 \r
40     protected HttpResponse delete(String packageName, String ip) throws Exception {\r
41         HashMap headers = createHeaders();\r
42         String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH + "/" + packageName;\r
43         CloseableHttpClient httpClient = null;\r
44         try {\r
45             httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);\r
46             return HttpsUtils.delete(url, headers, httpClient);\r
47         } finally {\r
48             closeHttpClient(httpClient);\r
49         }\r
50     }\r
51 \r
52     protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip)\r
53             throws Exception {\r
54         String content = GsonUtil.beanToJson(correlationCheckRule4Engine);\r
55         HashMap headers = createHeaders();\r
56         String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH;\r
57         CloseableHttpClient httpClient = null;\r
58         try {\r
59             httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);\r
60             return HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content), httpClient);\r
61         } finally {\r
62             closeHttpClient(httpClient);\r
63         }\r
64     }\r
65 \r
66     protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine, String ip) throws Exception {\r
67         String content = GsonUtil.beanToJson(correlationDeployRule4Engine);\r
68         HashMap headers = createHeaders();\r
69         String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH;\r
70         CloseableHttpClient httpClient = null;\r
71         try {\r
72             httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);\r
73             return HttpsUtils.put(url, headers, new HashMap<>(), new StringEntity(content),httpClient);\r
74         } finally {\r
75             closeHttpClient(httpClient);\r
76         }\r
77     }\r
78 \r
79     private void closeHttpClient(CloseableHttpClient httpClient) {\r
80         if (httpClient != null) {\r
81             try {\r
82                 httpClient.close();\r
83             } catch (IOException e) {\r
84                 log.warn("Failed to close http client!");\r
85             }\r
86         }\r
87     }\r
88 \r
89     private HashMap<String, String> createHeaders() {\r
90         HashMap<String, String> headers = new HashMap<>();\r
91         headers.put("Content-Type", MediaType.APPLICATION_JSON);\r
92         headers.put("Accept", MediaType.APPLICATION_JSON);\r
93         return headers;\r
94     }\r
95 }\r