diff options
Diffstat (limited to 'rulemgt/src')
5 files changed, 54 insertions, 256 deletions
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java index 21e29b3..ac381d1 100644 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java @@ -15,37 +15,17 @@ */
package org.openo.holmes.rulemgt.bolt.enginebolt;
-import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;
-
import com.fasterxml.jackson.databind.ObjectMapper;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.io.UnsupportedEncodingException;
-import java.nio.charset.Charset;
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.http.ConnectionClosedException;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.methods.HttpDelete;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.client.methods.HttpPut;
-import org.apache.http.client.methods.HttpRequestBase;
-import org.apache.http.entity.BufferedHttpEntity;
-import org.apache.http.entity.ByteArrayEntity;
-import org.apache.http.entity.ContentType;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
-import org.apache.http.protocol.HTTP;
-import org.apache.http.util.EntityUtils;
+import org.glassfish.jersey.client.ClientConfig;
import org.jvnet.hk2.annotations.Service;
-import org.openo.holmes.common.config.MicroServiceConfig;
-import org.openo.holmes.common.exception.CorrelationException;
-import org.openo.holmes.common.utils.I18nProxy;
import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
import org.openo.holmes.rulemgt.constant.RuleMgtConstant;
@@ -56,152 +36,31 @@ public class EngineService { String url = "http://10.250.0.3:9102";
- protected HttpResponse delete(String packageName) throws IOException {
- return deleteRequest(url + RuleMgtConstant.ENGINE_PATH + "/" + packageName);
+ protected Response delete(String packageName) throws IOException {
+ Client client = createClient();
+ WebTarget webTarget = client.target(url + RuleMgtConstant.ENGINE_PATH + "/" + packageName);
+ return webTarget.request(MediaType.APPLICATION_JSON).delete();
+ }
+
+ private Client createClient() {
+ ClientConfig clientConfig = new ClientConfig();
+ return ClientBuilder.newClient(clientConfig);
}
- protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine)
+ protected Response check(CorrelationCheckRule4Engine correlationCheckRule4Engine)
throws IOException {
+ Client client = createClient();
ObjectMapper mapper = new ObjectMapper();
String content = mapper.writeValueAsString(correlationCheckRule4Engine);
- String queryUrl = MicroServiceConfig.getMsbServerAddr()
- + "/openoapi/microservices/v1/services/holmes-engine/version/v1";
- HttpGet httpGet = new HttpGet(queryUrl);
- CloseableHttpClient httpClient = HttpClients.createDefault();
- try {
- HttpResponse httpResponse = httpClient.execute(httpGet);
- log.info("response entity:" + EntityUtils.toString(httpResponse.getEntity()));
- } finally {
- httpClient.close();
- }
- return postRequest(url + RuleMgtConstant.ENGINE_PATH, content);
+ WebTarget webTarget = client.target(url + RuleMgtConstant.ENGINE_PATH);
+ return webTarget.request(MediaType.APPLICATION_JSON).post(Entity.entity(content, MediaType.APPLICATION_JSON));
}
- protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws IOException {
+ protected Response deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws IOException {
+ Client client = createClient();
ObjectMapper mapper = new ObjectMapper();
String content = mapper.writeValueAsString(correlationDeployRule4Engine);
- return putRequest(url + RuleMgtConstant.ENGINE_PATH, content);
- }
-
- private HttpResponse postRequest(String url, String content) throws IOException {
- CloseableHttpClient httpClient = HttpClients.createDefault();
- try {
- HttpPost httpPost = new HttpPost(url);
- log.info("url:" + url + "," + "post:" + httpPost);
- setHeader(httpPost);
- if (StringUtils.isNotEmpty(content)) {
- httpPost.setEntity(new ByteArrayEntity(content.getBytes()));
- }
- return httpClient.execute(httpPost);
- } finally {
- httpClient.close();
- }
- }
-
- private HttpResponse putRequest(String url, String content) throws IOException {
- CloseableHttpClient httpClient = HttpClients.createDefault();
- try {
- HttpPut httpPut = new HttpPut(url);
- setHeader(httpPut);
- if (StringUtils.isNotEmpty(content)) {
- httpPut.setEntity(new ByteArrayEntity(content.getBytes()));
- }
- HttpResponse response = httpClient.execute(httpPut);
- log.info("Return value for put request is " + EntityUtils.toString(response.getEntity()) + ".");
- return response;
- } finally {
- httpClient.close();
- }
- }
-
- private HttpResponse deleteRequest(String url) throws IOException {
- CloseableHttpClient httpClient = HttpClients.createDefault();
- try {
- HttpDelete httpDelete = new HttpDelete(url);
- setHeader(httpDelete);
- return httpClient.execute(httpDelete);
- } finally {
- httpClient.close();
- }
- }
-
- private void setHeader(HttpRequestBase httpRequestBase) {
- httpRequestBase.setHeader("Accept", "application/json");
- httpRequestBase.setHeader("Content-Type", "application/json");
- }
-
- public String getResponseContent(HttpResponse response) {
- HttpEntity entity = response.getEntity();
- InputStream is = null;
- if (entity != null) {
- try {
- is = entity.getContent();
- final ContentType contentType = ContentType.getOrDefault(entity);
- Charset charset = contentType.getCharset();
- if (charset == null) {
- charset = HTTP.DEF_CONTENT_CHARSET;
- }
- final StringBuilder b = new StringBuilder();
- final char[] tmp = new char[1024];
- final Reader reader = new InputStreamReader(is, charset);
- try {
- int l;
- while ((l = reader.read(tmp)) != -1) {
- b.append(tmp, 0, l);
- }
- } catch (ConnectionClosedException ignore) {
-
- } catch (IOException e) {
- log.info("Failed to read the contents of the input stream of the http entity.", e);
- }
- return b.toString();
- } catch (IOException e) {
- log.info("Failed to read the contents of the http entity.", e);
- } finally {
- try {
- if (is != null) {
- is.close();
- }
- } catch (IOException e) {
- log.info("Failed to close the input stream of the http entity.", e);
- }
- }
- }
- return "{}";
- }
-
- public byte[] getData(HttpEntity httpEntity) throws IOException {
- log.info("Rule deployed. Package name: " + httpEntity.getContent().toString()
- + ". Content length: " + httpEntity.getContentLength());
- BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- bufferedHttpEntity.writeTo(byteArrayOutputStream);
- byte[] responseBytes = byteArrayOutputStream.toByteArray();
- return responseBytes;
- }
-
-// public String getResponseContent(HttpResponse httpResponse) throws CorrelationException {
-// byte[] dataByte;
-// String result = null;
-// try {
-// HttpEntity httpEntity = httpResponse.getEntity();
-// if (httpEntity != null) {
-// byte[] responseBytes = getData(httpEntity);
-// dataByte = responseBytes;
-// result = bytesToString(dataByte);
-// }
-// return result;
-// } catch (Exception e) {
-// throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR, e);
-// }
-// }
-
- private String bytesToString(byte[] bytes) throws UnsupportedEncodingException {
- if (bytes != null) {
- String returnStr = new String(bytes, "utf-8");
- returnStr = StringUtils.trim(returnStr);
- return returnStr;
- }
- return null;
+ WebTarget webTarget = client.target(url + RuleMgtConstant.ENGINE_PATH);
+ return webTarget.request(MediaType.APPLICATION_JSON).put(Entity.entity(content, MediaType.APPLICATION_JSON));
}
}
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java index a65d88b..c568723 100644 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java @@ -15,12 +15,10 @@ */
package org.openo.holmes.rulemgt.bolt.enginebolt;
-import java.io.IOException;
import javax.inject.Inject;
+import javax.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
-import org.apache.http.HttpResponse;
-import org.apache.http.util.EntityUtils;
import org.jvnet.hk2.annotations.Service;
import org.openo.holmes.common.exception.CorrelationException;
import org.openo.holmes.common.utils.I18nProxy;
@@ -36,18 +34,16 @@ public class EngineWrapper { private EngineService engineService;
public String deployEngine(CorrelationDeployRule4Engine correlationRule) throws CorrelationException {
- HttpResponse httpResponse;
+ Response response;
try {
- httpResponse = engineService.deploy(correlationRule);
+ response = engineService.deploy(correlationRule);
} catch (Exception e) {
throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED, e);
}
- if (httpResponse.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) {
+ if (response.getStatus() == RuleMgtConstant.RESPONSE_STATUS_OK) {
log.info("Call deploy rule rest interface in engine successfully.");
- String content = engineService.getResponseContent(httpResponse);
try {
- log.info("Deploy result from the engine is: " + content + ".");
- JSONObject json = JSONObject.fromObject(content);
+ JSONObject json = JSONObject.fromObject(response.readEntity(String.class));
return json.get(RuleMgtConstant.PACKAGE).toString();
} catch (Exception e) {
throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR, e);
@@ -58,13 +54,13 @@ public class EngineWrapper { }
public boolean deleteRuleFromEngine(String packageName) throws CorrelationException {
- HttpResponse httpResponse;
+ Response response;
try {
- httpResponse = engineService.delete(packageName);
+ response = engineService.delete(packageName);
} catch (Exception e) {
throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CALL_DELETE_RULE_REST_FAILED, e);
}
- if (httpResponse.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) {
+ if (response.getStatus() == RuleMgtConstant.RESPONSE_STATUS_OK) {
log.info("Call delete rule rest interface in engine successfully.");
return true;
} else {
@@ -75,25 +71,17 @@ public class EngineWrapper { public boolean checkRuleFromEngine(CorrelationCheckRule4Engine correlationCheckRule4Engine)
throws CorrelationException {
log.info("content:" + correlationCheckRule4Engine.getContent());
- HttpResponse httpResponse;
+ Response response;
try {
- httpResponse = engineService.check(correlationCheckRule4Engine);
+ response = engineService.check(correlationCheckRule4Engine);
} catch (Exception e) {
throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CALL_CHECK_RULE_REST_FAILED, e);
}
- if (httpResponse.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) {
+ if (response.getStatus() == RuleMgtConstant.RESPONSE_STATUS_OK) {
log.info("Call check rule rest interface in engine successfully.");
return true;
} else {
- try {
- log.info(httpResponse.getStatusLine().getStatusCode() + "," + EntityUtils
- .toString(httpResponse.getEntity()));
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CHECK_NO_PASS);
- } catch (IOException e) {
- log.info(httpResponse.getStatusLine().getStatusCode() + "," + httpResponse.getStatusLine()
- .getReasonPhrase());
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CHECK_NO_PASS, e);
- }
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CHECK_NO_PASS);
}
}
}
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleDao.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleDao.java index f800f0c..3a7f300 100644 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleDao.java +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleDao.java @@ -31,7 +31,7 @@ import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper; public abstract class CorrelationRuleDao {
@GetGeneratedKeys
- @SqlUpdate("INSERT INTO APLUS_RULE (NAME,DESCRIPTION,ENABLE,TEMPLATEID,ENGINETYPE,CREATOR,UPDATOR,PARAMS,DOMAIN ,CONTENT ,VENDOR,CREATETIME,UPDATETIME,ENGINEID,ISMANUAL,PACKAGE,RID) VALUES (:name,:description,:enabled,:templateID,:engineType,:creator,:modifier,:params,:domain,:content,:vendor,:createTime,:updateTime,:engineId,:isManual,:packageName,:rid)")
+ @SqlUpdate("INSERT INTO APLUS_RULE (NAME,DESCRIPTION,ENABLE,TEMPLATEID,ENGINETYPE,CREATOR,UPDATOR,PARAMS,CONTENT ,VENDOR,CREATETIME,UPDATETIME,ENGINEID,PACKAGE,RID) VALUES (:name,:description,:enabled,:templateID,:engineType,:creator,:modifier,:params,:content,:vendor,:createTime,:updateTime,:engineID,:packageName,:rid)")
protected abstract int addRule(@BindBean CorrelationRule correlationRule);
@SqlUpdate("UPDATE APLUS_RULE SET DESCRIPTION=:description,ENABLE=:enabled,CONTENT=:content,UPDATOR=:modifier,UPDATETIME=:updateTime WHERE RID=:rid")
diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java index 3b1c9bd..f4275eb 100644 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java @@ -20,15 +20,11 @@ package org.openo.holmes.rulemgt.bolt.enginebolt; import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
-import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
-import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Rule;
-import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
@@ -57,39 +53,4 @@ public class EngineServiceTest { correlationDeployRule4Engine.setContent("{\"package\":\"test\"}");
correlationDeployRule4Engine.setEngineId("engine_id");
}
-
- @Test
- public void getResponseContent_http_entity_is_null() throws Exception {
- EasyMock.expect(httpResponseMock.getEntity()).andReturn(null);
- PowerMock.replayAll();
-
- engineService.getResponseContent(httpResponseMock);
-
- PowerMock.verifyAll();
- }
-
- @Test
- public void delete_exception() throws Exception {
- thrown.expect(Exception.class);
-
- engineService.delete("test");
-
- }
-
- @Test
- public void deploy_exception() throws Exception {
-
- thrown.expect(Exception.class);
-
- engineService.deploy(correlationDeployRule4Engine);
-
- }
-
- @Test
- public void check_normal() throws Exception {
- thrown.expect(Exception.class);
-
- engineService.check(new CorrelationCheckRule4Engine());
-
- }
}
\ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java index 7746394..fe7696b 100644 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java @@ -20,7 +20,7 @@ package org.openo.holmes.rulemgt.bolt.enginebolt; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
-import org.apache.http.HttpResponse;
+import javax.ws.rs.core.Response;
import org.apache.http.StatusLine;
import org.easymock.EasyMock;
import org.junit.Before;
@@ -40,13 +40,13 @@ public class EngineWrapperTest { public ExpectedException thrown = ExpectedException.none();
private EngineWrapper engineWrapper = new EngineWrapper();
private EngineService engineServiceMock;
- private HttpResponse httpResponseMock;
+ private Response response;
private StatusLine statusLineMock;
@Before
public void setUp() throws Exception {
engineServiceMock = PowerMock.createMock(EngineService.class);
- httpResponseMock = PowerMock.createMock(HttpResponse.class);
+ response = PowerMock.createMock(Response.class);
statusLineMock = PowerMock.createMock(StatusLine.class);
Whitebox.setInternalState(engineWrapper, "engineService", engineServiceMock);
}
@@ -71,10 +71,8 @@ public class EngineWrapperTest { thrown.expectMessage(I18nProxy.ENGINE_DEPLOY_RULE_FAILED);
EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
- .andReturn(httpResponseMock);
- EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
- EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);
-
+ .andReturn(response);
+ EasyMock.expect(response.getStatus()).andReturn(400);
PowerMock.replayAll();
engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
@@ -89,12 +87,9 @@ public class EngineWrapperTest { thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR);
EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
- .andReturn(httpResponseMock);
- EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
- EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
- EasyMock.expect(engineServiceMock.getResponseContent(EasyMock.anyObject(HttpResponse.class)))
- .andReturn(content);
-
+ .andReturn(response);
+ EasyMock.expect(response.getStatus()).andReturn(200);
+ EasyMock.expect(response.readEntity(String.class)).andReturn(content);
PowerMock.replayAll();
engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
@@ -106,11 +101,9 @@ public class EngineWrapperTest { public void deployEngine_success() throws Exception {
String content = "{\"package\":\"test\"}";
EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
- .andReturn(httpResponseMock);
- EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
- EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
- EasyMock.expect(engineServiceMock.getResponseContent(EasyMock.anyObject(HttpResponse.class)))
- .andReturn(content);
+ .andReturn(response);
+ EasyMock.expect(response.getStatus()).andReturn(200);
+ EasyMock.expect(response.readEntity(String.class)).andReturn(content);
PowerMock.replayAll();
String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
@@ -139,9 +132,8 @@ public class EngineWrapperTest { thrown.expectMessage(I18nProxy.ENGINE_DELETE_RULE_FAILED);
EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))
- .andReturn(httpResponseMock);
- EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
- EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);
+ .andReturn(response);
+ EasyMock.expect(response.getStatus()).andReturn(400);
PowerMock.replayAll();
@@ -153,9 +145,8 @@ public class EngineWrapperTest { @Test
public void deleteRuleFromEngine_success() throws Exception {
EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))
- .andReturn(httpResponseMock);
- EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
- EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
+ .andReturn(response);
+ EasyMock.expect(response.getStatus()).andReturn(200);
PowerMock.replayAll();
@@ -181,9 +172,8 @@ public class EngineWrapperTest { @Test
public void checkRuleFromEngine_success() throws Exception {
EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
- .andReturn(httpResponseMock);
- EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
- EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
+ .andReturn(response);
+ EasyMock.expect(response.getStatus()).andReturn(200);
PowerMock.replayAll();
|