...
|
...
|
@@ -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));
|
...
|
...
|
|