2 * Copyright 2017 ZTE Corporation.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
16 package org.openo.holmes.rulemgt.bolt.enginebolt;
\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
44 public class EngineService {
\r
46 protected HttpResponse delete(String packageName) throws IOException {
\r
47 return deleteRequest(MicroServiceConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH + "/" + packageName);
\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
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
63 private HttpResponse postRequest(String url, String content) throws IOException {
\r
64 CloseableHttpClient httpClient = HttpClients.createDefault();
\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
72 return httpClient.execute(httpPost);
\r
78 private HttpResponse putRequest(String url, String content) throws IOException {
\r
79 CloseableHttpClient httpClient = HttpClients.createDefault();
\r
81 HttpPut httpPut = new HttpPut(url);
\r
83 if (StringUtils.isNotEmpty(content)) {
\r
84 httpPut.setEntity(new ByteArrayEntity(content.getBytes()));
\r
86 return httpClient.execute(httpPut);
\r
92 private HttpResponse deleteRequest(String url) throws IOException {
\r
93 CloseableHttpClient httpClient = HttpClients.createDefault();
\r
95 HttpDelete httpDelete = new HttpDelete(url);
\r
96 setHeader(httpDelete);
\r
97 return httpClient.execute(httpDelete);
\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
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
117 public String getResponseContent(HttpResponse httpResponse) throws CorrelationException {
\r
119 String result = null;
\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
128 } catch (Exception e) {
\r
129 throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR, e);
\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