Authored by 鲁尚清

Merge branch 'master' of http://192.168.1.136:82/monitor_v3/anji-plus-report int…

…o master-ajreport-lushangqing
... ... @@ -5,6 +5,7 @@ import org.springframework.util.PropertyPlaceholderHelper;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
... ... @@ -18,7 +19,7 @@ public class ParamsResolverHelper {
new PropertyPlaceholderHelper(placeholderPrefix, placeholderSuffix);
public static String resolveParams(final Map<String, Object> param, String con) {
con = helper.replacePlaceholders(con, (key -> param.get(key) + ""));
con = helper.replacePlaceholders(con, (key -> Objects.equals(param.get(key) ,null) ? "" : param.get(key) + ""));
return con;
}
... ...
... ... @@ -29,15 +29,14 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.sql.*;
import java.util.ArrayList;
import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author Raod
... ... @@ -104,7 +103,9 @@ public class DataSourceServiceImpl implements DataSourceService {
testRelationalDb(dto);
break;
case JdbcConstants.HTTP:
testHttp(dto);
// Start Wang 2022/1/21 10:51 http请求不做验证
// testHttp(dto);
// End Wang 2022/1/21 10:51
break;
default:
throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_TYPE_DOES_NOT_MATCH_TEMPORARILY);
... ... @@ -295,22 +296,40 @@ public class DataSourceServiceImpl implements DataSourceService {
*/
public List<JSONObject> executeHttp(DataSourceDto dto) {
analysisHttpConfig(dto);
HttpMethod httpMethod = HttpMethod.valueOf(dto.getMethod());
if(Objects.equals(HttpMethod.GET, httpMethod)){
Map<String, Object> contextData = dto.getContextData();
if(!contextData.isEmpty()){
JSONObject o = (JSONObject)JSONObject.toJSON(contextData);
dto.setDynSentence(o.toJSONString());
}
}
HttpHeaders headers = new HttpHeaders();
headers.setAll(JSONObject.parseObject(dto.getHeader(), Map.class));
HttpEntity<String> entity = new HttpEntity<>(dto.getDynSentence(), headers);
ResponseEntity<Object> exchange;
List<JSONObject> result = new ArrayList<>();
try {
exchange = restTemplate.exchange(dto.getApiUrl(), HttpMethod.valueOf(dto.getMethod()), entity, Object.class);
exchange = restTemplate.exchange(dto.getApiUrl(),httpMethod , entity, Object.class);
} catch (HttpClientErrorException error){
int rawStatusCode = error.getRawStatusCode();
// 401 接口请求无权限
JSONObject jsonObject = new JSONObject();
jsonObject.put("code",rawStatusCode);
jsonObject.put("data",error.getMessage());
result.add(jsonObject);
return result;
} catch (Exception e) {
log.error("error",e);
throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, e.getMessage());
}
if (exchange.getStatusCode().isError()) {
if (exchange != null && exchange.getStatusCode().isError()) {
throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, exchange.getBody());
}
Object body = exchange.getBody();
String jsonStr = JSONObject.toJSONString(body);
List<JSONObject> result = new ArrayList<>();
if (jsonStr.trim().startsWith(BusinessConstant.LEFT_BIG_BOAST) && jsonStr.trim().endsWith(BusinessConstant.RIGTH_BIG_BOAST)) {
//JSONObject
result.add(JSONObject.parseObject(jsonStr));
... ...