From d971f6c6a732827f35810d34ca8ba5214e522f6a Mon Sep 17 00:00:00 2001 From: Raod <1130305001@qq.com> Date: Mon, 19 Jul 2021 11:50:15 +0800 Subject: [PATCH] http --- .../business/constant/BusinessConstant.java | 5 +++ .../service/impl/DataSourceServiceImpl.java | 18 +++++++-- .../impl/DataSourceServiceImplTest.java | 37 +++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 report-core/src/test/java/com/anjiplus/template/gaea/business/modules/dataSource/service/impl/DataSourceServiceImplTest.java diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/constant/BusinessConstant.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/constant/BusinessConstant.java index 406ad20e..08357c95 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/constant/BusinessConstant.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/constant/BusinessConstant.java @@ -7,6 +7,11 @@ package com.anjiplus.template.gaea.business.constant; */ public interface BusinessConstant { + String LEFT_BIG_BOAST = "{"; + String RIGTH_BIG_BOAST = "}"; + String LEFT_MIDDLE_BOAST = "["; + String RIGHT_MIDDLE_BOAST = "]"; + /** * 字典项重复 */ diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/dataSource/service/impl/DataSourceServiceImpl.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/dataSource/service/impl/DataSourceServiceImpl.java index 2ea982c6..810877a3 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/dataSource/service/impl/DataSourceServiceImpl.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/dataSource/service/impl/DataSourceServiceImpl.java @@ -10,6 +10,7 @@ import com.anji.plus.gaea.exception.BusinessException; import com.anji.plus.gaea.exception.BusinessExceptionBuilder; import com.anji.plus.gaea.utils.GaeaAssert; import com.anjiplus.template.gaea.business.code.ResponseCode; +import com.anjiplus.template.gaea.business.constant.BusinessConstant; import com.anjiplus.template.gaea.business.modules.dataSet.controller.dto.DataSetDto; import com.anjiplus.template.gaea.business.modules.dataSetParam.service.DataSetParamService; import com.anjiplus.template.gaea.business.modules.dataSource.controller.dto.DataSourceDto; @@ -267,9 +268,9 @@ public class DataSourceServiceImpl implements DataSourceService { HttpHeaders headers = new HttpHeaders(); headers.setAll(JSONObject.parseObject(dto.getHeader(), Map.class)); HttpEntity entity = new HttpEntity<>(dto.getDynSentence(), headers); - ResponseEntity exchange; + ResponseEntity exchange; try { - exchange = restTemplate.exchange(dto.getApiUrl(), HttpMethod.valueOf(dto.getMethod()), entity, JSONObject.class); + exchange = restTemplate.exchange(dto.getApiUrl(), HttpMethod.valueOf(dto.getMethod()), entity, Object.class); } catch (Exception e) { log.error("error",e); throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, e.getMessage()); @@ -277,9 +278,18 @@ public class DataSourceServiceImpl implements DataSourceService { if (exchange.getStatusCode().isError()) { throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, exchange.getBody()); } - JSONObject body = exchange.getBody(); + Object body = exchange.getBody(); + String jsonStr = JSONObject.toJSONString(body); List result = new ArrayList<>(); - result.add(body); + if (jsonStr.trim().startsWith(BusinessConstant.LEFT_BIG_BOAST) && jsonStr.trim().endsWith(BusinessConstant.RIGTH_BIG_BOAST)) { + //JSONObject + result.add(JSONObject.parseObject(jsonStr)); + } else if (jsonStr.trim().startsWith(BusinessConstant.LEFT_MIDDLE_BOAST) && jsonStr.trim().endsWith(BusinessConstant.RIGHT_MIDDLE_BOAST)) { + //List + result = JSONArray.parseArray(jsonStr, JSONObject.class); + } else { + result.add(new JSONObject()); + } return result; } diff --git a/report-core/src/test/java/com/anjiplus/template/gaea/business/modules/dataSource/service/impl/DataSourceServiceImplTest.java b/report-core/src/test/java/com/anjiplus/template/gaea/business/modules/dataSource/service/impl/DataSourceServiceImplTest.java new file mode 100644 index 00000000..315bd0bc --- /dev/null +++ b/report-core/src/test/java/com/anjiplus/template/gaea/business/modules/dataSource/service/impl/DataSourceServiceImplTest.java @@ -0,0 +1,37 @@ +package com.anjiplus.template.gaea.business.modules.dataSource.service.impl; + +import com.alibaba.fastjson.JSONObject; +import com.anjiplus.template.gaea.business.ReportApplication; +import com.anjiplus.template.gaea.business.modules.dataSource.controller.dto.DataSourceDto; +import com.anjiplus.template.gaea.business.modules.dataSource.service.DataSourceService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.List; + + +/** + * Created by raodeming on 2021/7/19. + */ +@SpringBootTest(classes = ReportApplication.class) +@RunWith(SpringRunner.class) +public class DataSourceServiceImplTest { + + @Autowired + private DataSourceService dataSourceService; + + @Test + public void testHttp(){ + DataSourceDto dto = new DataSourceDto(); + dto.setSourceType("http"); + dto.setHeader("{\"Content-Type\":\"application/json\"}"); + dto.setSourceConfig("{\"apiUrl\":\"http://10.108.26.163:9200/_xpack/sql?format=json\",\"method\":\"POST\",\"header\":\"{\\\"Content-Type\\\":\\\"application/json\\\"}\",\"body\":\"{\\\"query\\\":\\\"select 1\\\"}\"}"); + dto.setDynSentence("{\"query\": \"select HISTOGRAM(logTime,INTERVAL 1 MONTH) as h ,count(flag),flag from \\\"analysis-wifilogin\\\" where logTime>='2021-02-22 00:28:10.000' and logTime<'2021-03-22 00:28:10.000' GROUP BY h,flag\"}"); + List execute = dataSourceService.execute(dto); + System.out.println(execute); + } + +}