1a1aa838ebfb9e226f8422cce090534d2e35faf2
[holmes/rule-management.git] / rulemgt / src / main / java / org / openo / 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.openo.holmes.rulemgt.bolt.enginebolt;\r
17 \r
18 import com.fasterxml.jackson.databind.ObjectMapper;\r
19 import java.io.ByteArrayOutputStream;\r
20 import java.io.IOException;\r
21 import java.io.UnsupportedEncodingException;\r
22 import org.apache.commons.lang3.StringUtils;\r
23 import org.apache.http.HttpEntity;\r
24 import org.apache.http.HttpResponse;\r
25 import org.apache.http.client.methods.HttpDelete;\r
26 import org.apache.http.client.methods.HttpPost;\r
27 import org.apache.http.client.methods.HttpPut;\r
28 import org.apache.http.client.methods.HttpRequestBase;\r
29 import org.apache.http.entity.BufferedHttpEntity;\r
30 import org.apache.http.entity.ByteArrayEntity;\r
31 import org.apache.http.impl.client.CloseableHttpClient;\r
32 import org.apache.http.impl.client.HttpClients;\r
33 import org.jvnet.hk2.annotations.Service;\r
34 import org.openo.holmes.common.config.MicroServiceConfig;\r
35 import org.openo.holmes.common.exception.CorrelationException;\r
36 import org.openo.holmes.common.utils.I18nProxy;\r
37 import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
38 import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
39 import org.openo.holmes.rulemgt.constant.RuleMgtConstant;\r
40 \r
41 @Service\r
42 public class EngineService {\r
43 \r
44     protected HttpResponse delete(String packageName) throws IOException {\r
45         return deleteRequest(MicroServiceConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH + "/" + packageName);\r
46     }\r
47 \r
48     protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine)\r
49             throws IOException {\r
50         ObjectMapper mapper = new ObjectMapper();\r
51         String content = mapper.writeValueAsString(correlationCheckRule4Engine);\r
52         return postRequest(MicroServiceConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH, content);\r
53     }\r
54 \r
55     protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws IOException {\r
56         ObjectMapper mapper = new ObjectMapper();\r
57         String content = mapper.writeValueAsString(correlationDeployRule4Engine);\r
58         return putRequest(MicroServiceConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH, content);\r
59     }\r
60 \r
61     private HttpResponse postRequest(String url, String content) throws IOException {\r
62         CloseableHttpClient httpClient = HttpClients.createDefault();\r
63         try {\r
64             HttpPost httpPost = new HttpPost(url);\r
65             setHeader(httpPost);\r
66             if (StringUtils.isNotEmpty(content)) {\r
67                 httpPost.setEntity(new ByteArrayEntity(content.getBytes()));\r
68             }\r
69             return httpClient.execute(httpPost);\r
70         } finally {\r
71             httpClient.close();\r
72         }\r
73     }\r
74 \r
75     private HttpResponse putRequest(String url, String content) throws IOException {\r
76         CloseableHttpClient httpClient = HttpClients.createDefault();\r
77         try {\r
78             HttpPut httpPut = new HttpPut(url);\r
79             setHeader(httpPut);\r
80             if (StringUtils.isNotEmpty(content)) {\r
81                 httpPut.setEntity(new ByteArrayEntity(content.getBytes()));\r
82             }\r
83             return httpClient.execute(httpPut);\r
84         } finally {\r
85             httpClient.close();\r
86         }\r
87     }\r
88 \r
89     private HttpResponse deleteRequest(String url) throws IOException {\r
90         CloseableHttpClient httpClient = HttpClients.createDefault();\r
91         try {\r
92             HttpDelete httpDelete = new HttpDelete(url);\r
93             setHeader(httpDelete);\r
94             return httpClient.execute(httpDelete);\r
95         } finally {\r
96             httpClient.close();\r
97         }\r
98     }\r
99 \r
100     private void setHeader(HttpRequestBase httpRequestBase) {\r
101         httpRequestBase.setHeader("Content-Type", "text/html;charset=UTF-8");\r
102         httpRequestBase.setHeader("Accept", "application/json");\r
103         httpRequestBase.setHeader("Content-Type", "application/json");\r
104     }\r
105 \r
106     public byte[] getData(HttpEntity httpEntity) throws IOException {\r
107         BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);\r
108         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();\r
109         bufferedHttpEntity.writeTo(byteArrayOutputStream);\r
110         byte[] responseBytes = byteArrayOutputStream.toByteArray();\r
111         return responseBytes;\r
112     }\r
113 \r
114     public String getResponseContent(HttpResponse httpResponse) throws CorrelationException {\r
115         byte[] dataByte;\r
116         String result = null;\r
117         try {\r
118             HttpEntity httpEntity = httpResponse.getEntity();\r
119             if (httpEntity != null) {\r
120                 byte[] responseBytes = getData(httpEntity);\r
121                 dataByte = responseBytes;\r
122                 result = bytesToString(dataByte);\r
123             }\r
124             return result;\r
125         } catch (Exception e) {\r
126             throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR, e);\r
127         }\r
128     }\r
129 \r
130     private String bytesToString(byte[] bytes) throws UnsupportedEncodingException {\r
131         if (bytes != null) {\r
132             String returnStr = new String(bytes, "utf-8");\r
133             returnStr = StringUtils.trim(returnStr);\r
134             return returnStr;\r
135         }\r
136         return null;\r
137     }\r
138 }\r