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