summaryrefslogtreecommitdiffstats
path: root/holmes-actions/src/main
diff options
context:
space:
mode:
authorShiwei Tian <tian.shiwei@zte.com.cn>2017-09-27 10:19:15 +0800
committerShiwei Tian <tian.shiwei@zte.com.cn>2017-09-27 15:30:49 +0800
commitc9ed775685b01f5622618216748eeac3000285c4 (patch)
treeae11132542e4266daee864cc6fe5c318ab58288d /holmes-actions/src/main
parent4eaf0290dd2572f40526da9cfd09a1ccee4da76d (diff)
modify unit test and aai query
Issue-ID: HOLMES-44 Change-Id: I9d67fc26681dfb540ae4d011cde4d0cdc5f3d2a3 Signed-off-by: Shiwei Tian <tian.shiwei@zte.com.cn>
Diffstat (limited to 'holmes-actions/src/main')
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java63
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiResponseUtil.java97
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/aai/CorrelationUtil.java23
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/aai/config/AaiConfig.java4
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java1
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java1
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java29
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java128
8 files changed, 197 insertions, 149 deletions
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java
index 36479d7..aa840ba 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java
@@ -15,6 +15,10 @@ package org.onap.holmes.common.aai;
import java.util.HashMap;
import java.util.Map;
+import java.util.stream.Stream;
+import javax.inject.Inject;
+import lombok.extern.slf4j.Slf4j;
+import org.jvnet.hk2.annotations.Service;
import org.onap.holmes.common.aai.config.AaiConfig;
import org.onap.holmes.common.aai.entity.VmEntity;
import org.onap.holmes.common.aai.entity.VnfEntity;
@@ -22,8 +26,11 @@ import org.onap.holmes.common.config.MicroServiceConfig;
import org.onap.holmes.common.exception.CorrelationException;
import org.onap.holmes.common.utils.HttpsUtils;
+@Service
+@Slf4j
public class AaiQuery {
+ @Inject
private AaiResponseUtil aaiResponseUtil;
public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
@@ -36,7 +43,7 @@ public class AaiQuery {
}
public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
- String url = MicroServiceConfig.getMsbServerAddr() + getVmResourceLinks(vserverId, vserverName);
+ String url = getVmUrl(vserverId, vserverName);
String response = getResponse(url);
try {
return aaiResponseUtil.convertJsonToVmEntity(response);
@@ -45,6 +52,18 @@ public class AaiQuery {
}
}
+ private String getVmUrl(String vserverId, String vserverName) throws CorrelationException {
+ String url = "";
+ String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
+ String baseUrl = getBaseUrl("");
+ if (baseUrl.startsWith("http")) {
+ url = baseUrl + getMsbSuffixAddr(resourceLinkUrl);
+ } else {
+ url = baseUrl + resourceLinkUrl;
+ }
+ return url;
+ }
+
private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException {
String response = getResourceLinksResponse(vserverId, vserverName);
try {
@@ -55,29 +74,55 @@ public class AaiQuery {
}
private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
- String url =
- MicroServiceConfig.getMsbServerAddr() + AaiConfig.VM_ADDR + "vserver-id:EQUALS:"
- + vserverId;
+ String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR) + "vserver-id:EQUALS:" + vserverId);
String response = getResponse(url);
if (response.equals("")) {
- url = MicroServiceConfig.getMsbServerAddr() + AaiConfig.VM_ADDR
- + "vserver-name:EQUALS:" + vserverName;
+ url = getBaseUrl(AaiConfig.AAI_VM_ADDR + "vserver-name:EQUALS:" + vserverName);
response = getResponse(url);
}
return response;
}
private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException {
- String url = MicroServiceConfig.getMsbServerAddr() + AaiConfig.VNF_ADDR + "vnf-id=" + vnfId;
+ String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR)+ "vnf-id=" + vnfId);
String response = getResponse(url);
if (response.equals("")) {
- url = MicroServiceConfig.getMsbServerAddr() + AaiConfig.VNF_ADDR + "vnf-name="
- + vnfName;
+ url = getBaseUrl(AaiConfig.AAI_VNF_ADDR + "vnf-name=" + vnfName);
response = getResponse(url);
}
return response;
}
+ private String getBaseUrl(String suffixUrl) {
+ String url = "";
+ try {
+ url = MicroServiceConfig.getMsbServerAddr() + suffixUrl;
+ } catch (Exception e) {
+ log.info("Failed to get msb address");
+ }
+ if (url.equals("")) {
+ try {
+ url = "https:\\\\" + MicroServiceConfig.getServiceAddrInfoFromCBS("aai_config")
+ + suffixUrl;
+ } catch (Exception e) {
+ log.info("Failed to get aai address");
+ }
+ }
+ return url;
+ }
+
+ private String getMsbSuffixAddr(String suffixUrl) {
+ String[] addrSplits = suffixUrl.substring(1).split("/");
+ String ret = addrSplits[1];
+ addrSplits[1] = addrSplits[2];
+ addrSplits[2] = ret;
+ StringBuffer stringBuffer = new StringBuffer();
+ for (String split : addrSplits) {
+ stringBuffer.append("/" + split);
+ }
+ return stringBuffer.toString();
+ }
+
private String getResponse(String url) throws CorrelationException {
String response = "";
try {
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiResponseUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiResponseUtil.java
index 3869522..ccfb4fd 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiResponseUtil.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiResponseUtil.java
@@ -30,6 +30,7 @@ import org.onap.holmes.common.aai.entity.VmEntity;
import org.onap.holmes.common.aai.entity.VmResourceLink;
import org.onap.holmes.common.aai.entity.VnfEntity;
+@Service
public class AaiResponseUtil {
public List<VmResourceLink> convertJsonToVmResourceLink(String responseJson) throws IOException {
@@ -106,73 +107,65 @@ public class AaiResponseUtil {
private List<VmResourceLink> convertResultDataList(JsonNode resultData) {
List<VmResourceLink> vmResourceLinkList = new ArrayList<>();
- if (resultData.isArray()) {
- resultData.forEach(node ->{
- if (node.has("resource-link") && node.has("resource-type")) {
- VmResourceLink vmResourceLink = new VmResourceLink();
- vmResourceLink.setResourceLink(getTextElementByNode(node, "resource-link"));
- vmResourceLink.setResourceType(getTextElementByNode(node, "resource-type"));
- vmResourceLinkList.add(vmResourceLink);
- }
- });
- }
+ resultData.forEach(node ->{
+ if (node.has("resource-link") && node.has("resource-type")) {
+ VmResourceLink vmResourceLink = new VmResourceLink();
+ vmResourceLink.setResourceLink(getTextElementByNode(node, "resource-link"));
+ vmResourceLink.setResourceType(getTextElementByNode(node, "resource-type"));
+ vmResourceLinkList.add(vmResourceLink);
+ }
+ });
return vmResourceLinkList;
}
private List<Relationship> convertRelationships(JsonNode relationshipNode) {
List<Relationship> relationshipList = new ArrayList<>();
- if (relationshipNode.isArray()) {
- relationshipNode.forEach(node ->{
- Relationship relationship = new Relationship();
- relationship.setRelatedLink(getTextElementByNode(node, "related-link"));
- relationship.setRelatedTo(getTextElementByNode(node, "related-to"));
- if (node.has("related-to-property")) {
- JsonNode relatedToPropertyNode = node.get("related-to-property");
- relationship.setRelatedToPropertyList(
- convertRelatedToProperty(relatedToPropertyNode));
- } else {
- relationship.setRelatedToPropertyList(Collections.emptyList());
- }
- if (node.has("relationship-data")) {
- JsonNode relationshipDataNode = node.get("relationship-data");
- relationship
- .setRelationshipDataList(convertRelationshipDate(relationshipDataNode));
- } else {
- relationship.setRelationshipDataList(Collections.emptyList());
- }
- relationshipList.add(relationship);
- });
- }
+ relationshipNode.forEach(node ->{
+ Relationship relationship = new Relationship();
+ relationship.setRelatedLink(getTextElementByNode(node, "related-link"));
+ relationship.setRelatedTo(getTextElementByNode(node, "related-to"));
+ if (node.has("related-to-property")) {
+ JsonNode relatedToPropertyNode = node.get("related-to-property");
+ relationship.setRelatedToPropertyList(
+ convertRelatedToProperty(relatedToPropertyNode));
+ } else {
+ relationship.setRelatedToPropertyList(Collections.emptyList());
+ }
+ if (node.has("relationship-data")) {
+ JsonNode relationshipDataNode = node.get("relationship-data");
+ relationship
+ .setRelationshipDataList(convertRelationshipDate(relationshipDataNode));
+ } else {
+ relationship.setRelationshipDataList(Collections.emptyList());
+ }
+ relationshipList.add(relationship);
+ });
return relationshipList;
}
private List<RelationshipData> convertRelationshipDate(JsonNode relationshipDataNode) {
List<RelationshipData> relationshipDataList = new ArrayList<>();
- if (relationshipDataNode.isArray()) {
- relationshipDataNode.forEach(node ->{
- RelationshipData relationshipData = new RelationshipData();
- relationshipData.setRelationshipKey(
- getTextElementByNode(node,"relationship-key"));
- relationshipData.setRelationshipValue(
- getTextElementByNode(node,"relationship-value"));
- relationshipDataList.add(relationshipData);
- });
- }
+ relationshipDataNode.forEach(node ->{
+ RelationshipData relationshipData = new RelationshipData();
+ relationshipData.setRelationshipKey(
+ getTextElementByNode(node,"relationship-key"));
+ relationshipData.setRelationshipValue(
+ getTextElementByNode(node,"relationship-value"));
+ relationshipDataList.add(relationshipData);
+ });
return relationshipDataList;
}
private List<RelatedToProperty> convertRelatedToProperty(JsonNode relatedToPropertyNode) {
List<RelatedToProperty> relatedToPropertyList = new ArrayList<>();
- if (relatedToPropertyNode.isArray()) {
- relatedToPropertyNode.forEach(node ->{
- RelatedToProperty relatedToProperty = new RelatedToProperty();
- relatedToProperty
- .setPropertyKey(getTextElementByNode(node, "property-key"));
- relatedToProperty.setPropertyValue(
- getTextElementByNode(node, "property-value"));
- relatedToPropertyList.add(relatedToProperty);
- });
- }
+ relatedToPropertyNode.forEach(node ->{
+ RelatedToProperty relatedToProperty = new RelatedToProperty();
+ relatedToProperty
+ .setPropertyKey(getTextElementByNode(node, "property-key"));
+ relatedToProperty.setPropertyValue(
+ getTextElementByNode(node, "property-value"));
+ relatedToPropertyList.add(relatedToProperty);
+ });
return relatedToPropertyList;
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/CorrelationUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/CorrelationUtil.java
index 894b6f1..9295f53 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/CorrelationUtil.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/CorrelationUtil.java
@@ -18,12 +18,13 @@ import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.onap.holmes.common.aai.entity.RelationshipList.Relationship;
import org.onap.holmes.common.aai.entity.VmEntity;
+import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
import org.onap.holmes.common.exception.CorrelationException;
@Slf4j
public class CorrelationUtil {
- private AaiQuery aaiQuery;
+ private static AaiQuery aaiQuery;
private static class LazyHolder {
private static final CorrelationUtil INSTANCE = new CorrelationUtil();
@@ -31,31 +32,33 @@ public class CorrelationUtil {
private CorrelationUtil (){}
public static final CorrelationUtil getInstance() {
+ if (aaiQuery == null) {
+ aaiQuery = ServiceLocatorHolder.getLocator().getService(AaiQuery.class);
+ }
return LazyHolder.INSTANCE;
}
- public boolean isTopologicallyRelated(String eventId, String sourceId, String sourceName) {
-
- return Optional.ofNullable(getVmEntity(sourceId, sourceName)).map(vmEntity ->
- getIsRelated(eventId, vmEntity)).orElse(false);
+ public boolean isTopologicallyRelated(String sourceId, String rootSourceId, String rootSourceName) {
+ return Optional.ofNullable(getVmEntity(rootSourceId, rootSourceName)).map(vmEntity ->
+ getIsRelated(sourceId, vmEntity)).orElse(false);
}
- private boolean getIsRelated(String eventId, VmEntity vmEntity) {
+ private boolean getIsRelated(String sourceId, VmEntity vmEntity) {
List<Relationship> relationships = vmEntity.getRelationshipList().getRelationships();
for (Relationship relationship : relationships) {
boolean isRelated = relationship.getRelationshipDataList().stream().anyMatch(
- relationshipData -> relationshipData.getRelationshipValue().equals(eventId));
+ relationshipData -> relationshipData.getRelationshipValue().equals(sourceId));
if (isRelated) {
return true;
}
}
- return false;
+ return false;
}
- private VmEntity getVmEntity(String sourceId, String sourceName) {
+ private VmEntity getVmEntity(String rootSourceId, String rootSourceName) {
VmEntity vmEntity = null;
try {
- vmEntity = aaiQuery.getAaiVmData(sourceId, sourceName);
+ vmEntity = aaiQuery.getAaiVmData(rootSourceId, rootSourceName);
} catch (CorrelationException e) {
log.error("Failed to get vm data", e.getMessage());
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/config/AaiConfig.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/config/AaiConfig.java
index e3bacc1..0161eb0 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/config/AaiConfig.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/config/AaiConfig.java
@@ -15,9 +15,9 @@ package org.onap.holmes.common.aai.config;
public class AaiConfig {
- public static String VNF_ADDR = "/aai/v11/network/generic-vnfs/generic-vnf?";
+ public static String AAI_VNF_ADDR = "/aai/v11/network/generic-vnfs/generic-vnf?";
- public static String VM_ADDR = "/aai/v11/search/nodes-query?search-node-type=vserver&filter=";
+ public static String AAI_VM_ADDR = "/aai/v11/search/nodes-query?search-node-type=vserver&filter=";
public static String X_TRANSACTION_ID = "9999";
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java b/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java
index 127903f..918ed6c 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java
@@ -51,6 +51,7 @@ public class VesAlarm implements Cloneable, Serializable{
private Long faultFieldsVersion;
private String specificProblem;
private String vfStatus;
+ private String parentId;
@Override
public int hashCode() {
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java b/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java
index bb78288..01a5f2e 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java
@@ -22,7 +22,6 @@ import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.glassfish.jersey.client.ClientConfig;
-import org.onap.holmes.common.api.stat.Alarm;
import org.onap.holmes.common.constant.AlarmConst;
@Slf4j
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java
index 9c3d19b..79c861b 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java
@@ -15,10 +15,13 @@
*/
package org.onap.holmes.common.dmaap;
+import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.List;
import java.util.Optional;
+import javax.inject.Inject;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
+import org.jvnet.hk2.annotations.Service;
import org.onap.holmes.common.aai.AaiQuery;
import org.onap.holmes.common.aai.entity.RelationshipList.RelationshipData;
import org.onap.holmes.common.aai.entity.VmEntity;
@@ -27,29 +30,35 @@ import org.onap.holmes.common.api.stat.VesAlarm;
import org.onap.holmes.common.dmaap.entity.PolicyMsg;
import org.onap.holmes.common.dmaap.entity.PolicyMsg.EVENT_STATUS;
import org.onap.holmes.common.exception.CorrelationException;
+import org.onap.holmes.common.utils.JacksonUtil;
@Slf4j
-@AllArgsConstructor
+@Service
public class DmaapService {
- private static AaiQuery aaiQuery;
- private static Publisher publisher;
+ @Inject
+ private AaiQuery aaiQuery;
+ @Inject
+ private Publisher publisher;
- public static void publishPolicyMsg(PolicyMsg policyMsg) {
+ public void publishPolicyMsg(PolicyMsg policyMsg) {
try {
publisher.publish(policyMsg);
+ log.info("send policyMsg: " + JacksonUtil.beanToJson(policyMsg));
} catch (CorrelationException e) {
log.error("Failed to publish policyMsg to dmaap", e.getMessage());
+ } catch (JsonProcessingException e) {
+ log.info("Failed to convert policyMsg to json");
}
}
- public static PolicyMsg getPolicyMsg(VesAlarm vesAlarm) {
+ public PolicyMsg getPolicyMsg(VesAlarm vesAlarm) {
return Optional.ofNullable(getVmEntity(vesAlarm.getSourceId(), vesAlarm.getSourceName()))
.map(vmEntity -> getEnrichedPolicyMsg(vmEntity, vesAlarm))
.orElse(getDefaultPolicyMsg(vesAlarm.getSourceName()));
}
- private static String getVserverInstanceId(VnfEntity vnfEntity) {
+ private String getVserverInstanceId(VnfEntity vnfEntity) {
String vserverInstanceId = "";
if (vnfEntity != null) {
List<RelationshipData> relationshipDataList = vnfEntity.getRelationshipList()
@@ -65,7 +74,7 @@ public class DmaapService {
return vserverInstanceId;
}
- private static VnfEntity getVnfEntity(String vnfId, String vnfName) {
+ private VnfEntity getVnfEntity(String vnfId, String vnfName) {
VnfEntity vnfEntity = null;
try {
vnfEntity = aaiQuery.getAaiVnfData(vnfId, vnfName);
@@ -75,7 +84,7 @@ public class DmaapService {
return vnfEntity;
}
- private static VmEntity getVmEntity(String sourceId, String sourceName) {
+ private VmEntity getVmEntity(String sourceId, String sourceName) {
VmEntity vmEntity = null;
try {
vmEntity = aaiQuery.getAaiVmData(sourceId, sourceName);
@@ -85,7 +94,7 @@ public class DmaapService {
return vmEntity;
}
- private static PolicyMsg getEnrichedPolicyMsg(VmEntity vmEntity, VesAlarm vesAlarm) {
+ private PolicyMsg getEnrichedPolicyMsg(VmEntity vmEntity, VesAlarm vesAlarm) {
VnfEntity vnfEntity = getVnfEntity(vesAlarm.getEventId(), vesAlarm.getEventName());
String vserverInstatnceId = getVserverInstanceId(vnfEntity);
PolicyMsg policyMsg = new PolicyMsg();
@@ -118,7 +127,7 @@ public class DmaapService {
return policyMsg;
}
- private static PolicyMsg getDefaultPolicyMsg(String sourceName) {
+ private PolicyMsg getDefaultPolicyMsg(String sourceName) {
PolicyMsg policyMsg = new PolicyMsg();
policyMsg.setTarget("vserver.vserver-name");
policyMsg.setTargetType("VM");
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java
index 3ef1201..441261f 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java
@@ -32,6 +32,7 @@ import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
@@ -79,73 +80,88 @@ public class HttpsUtils {
public static String post(String url, Map<String, String> header, Map<String, String> param,
HttpEntity entity) throws Exception {
- String result = "";
- CloseableHttpClient httpClient = null;
+ HttpResponse httpResponse = null;
try {
- httpClient = getHttpClient();
- HttpPost httpPost = new HttpPost(url);
- if (!header.isEmpty()) {
- for (Map.Entry<String, String> entry : header.entrySet()) {
- httpPost.addHeader(entry.getKey(), entry.getValue());
- }
+ CloseableHttpClient httpClient = getHttpClient();
+ HttpPost httpPost = getHttpPost(url, header, param, entity);
+ httpResponse = getHttpResponse(httpClient, httpPost);
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to use post method query data from server");
+ }
+
+ return getResponseEntity(httpResponse);
+ }
+
+ public static String get(String url, Map<String, String> header) throws Exception {
+ HttpResponse httpResponse = null;
+ try {
+ CloseableHttpClient httpClient = getHttpClient();
+ HttpGet httpGet = getHttpGet(url, header);
+ httpResponse = getHttpResponse(httpClient, httpGet);
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to use get method query data from server");
+ }
+ return getResponseEntity(httpResponse);
+ }
+
+ private static HttpPost getHttpPost(String url, Map<String, String> header,
+ Map<String, String> param, HttpEntity entity) {
+ HttpPost httpPost = new HttpPost(url);
+ if (!header.isEmpty()) {
+ for (Map.Entry<String, String> entry : header.entrySet()) {
+ httpPost.addHeader(entry.getKey(), entry.getValue());
}
- if (!param.isEmpty()) {
- List<NameValuePair> formparams = new ArrayList<>();
- for (Map.Entry<String, String> entry : param.entrySet()) {
- formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
- }
- UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams,
- Consts.UTF_8);
- httpPost.setEntity(urlEncodedFormEntity);
+ }
+ if (!param.isEmpty()) {
+ List<NameValuePair> formparams = new ArrayList<>();
+ for (Map.Entry<String, String> entry : param.entrySet()) {
+ formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
- if (entity != null) {
- httpPost.setEntity(entity);
+ UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams,
+ Consts.UTF_8);
+ httpPost.setEntity(urlEncodedFormEntity);
+ }
+ if (entity != null) {
+ httpPost.setEntity(entity);
+ }
+ return httpPost;
+ }
+
+ private static HttpGet getHttpGet(String url, Map<String, String> header) {
+ HttpGet httpGet = new HttpGet(url);
+ if (!header.isEmpty()) {
+ for (Map.Entry<String, String> entry : header.entrySet()) {
+ httpGet.addHeader(entry.getKey(), entry.getValue());
}
- HttpResponse httpResponse = httpClient.execute(httpPost);
+ }
+ return httpGet;
+ }
+
+ private static String getResponseEntity(HttpResponse httpResponse) throws IOException {
+ String result = "";
+ if (httpResponse != null) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity resEntity = httpResponse.getEntity();
result = EntityUtils.toString(resEntity);
- } else {
- readHttpResponse(httpResponse);
- }
- } catch (Exception e) {
- throw new CorrelationException("Failed to use post method to get data from server");
- } finally {
- if (httpClient != null) {
- httpClient.close();
}
}
return result;
}
- public static String get(String url, Map<String, String> header) throws Exception {
- String result = "";
- CloseableHttpClient httpClient = null;
+ private static HttpResponse getHttpResponse(CloseableHttpClient httpClient, HttpRequestBase httpRequest)
+ throws Exception {
+ HttpResponse httpResponse = null;
try {
- httpClient = getHttpClient();
- HttpGet httpGet = new HttpGet(url);
- if (!header.isEmpty()) {
- for (Map.Entry<String, String> entry : header.entrySet()) {
- httpGet.addHeader(entry.getKey(), entry.getValue());
- }
- }
- HttpResponse httpResponse = httpClient.execute(httpGet);
- int statusCode = httpResponse.getStatusLine().getStatusCode();
- if (statusCode == HttpStatus.SC_OK) {
- HttpEntity resEntity = httpResponse.getEntity();
- result = EntityUtils.toString(resEntity);
- } else {
- readHttpResponse(httpResponse);
- }
+ httpResponse = httpClient.execute(httpRequest);
} catch (Exception e) {
- throw new CorrelationException("Failed to use get method get data from server");
+ throw new CorrelationException("Failed to get data from server");
} finally {
if (httpClient != null) {
httpClient.close();
}
}
- return result;
+ return httpResponse;
}
private static CloseableHttpClient getHttpClient() throws Exception {
@@ -156,22 +172,4 @@ public class HttpsUtils {
.build();
return httpClient;
}
-
- private static String readHttpResponse(HttpResponse httpResponse)
- throws ParseException, IOException {
- StringBuilder builder = new StringBuilder();
- HttpEntity entity = httpResponse.getEntity();
- builder.append("status:" + httpResponse.getStatusLine());
- builder.append("headers:");
- HeaderIterator iterator = httpResponse.headerIterator();
- while (iterator.hasNext()) {
- builder.append("\t" + iterator.next());
- }
- if (entity != null) {
- String responseString = EntityUtils.toString(entity);
- builder.append("response length:" + responseString.length());
- builder.append("response content:" + responseString.replace("\r\n", ""));
- }
- return builder.toString();
- }
}