use of org.apache.inlong.manager.common.exceptions.BusinessException in project incubator-inlong by apache.
the class CommonDBServerController method upload.
/**
* Batch import - upload import config
*/
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ApiOperation(value = "Batch Import - upload config")
public String upload(@RequestParam("file") MultipartFile file) throws Exception {
Preconditions.checkFalse(file.isEmpty(), "File cannot be empty");
String fileName = file.getOriginalFilename();
LOGGER.info("The name of the uploaded file is: " + fileName);
String filePath = ClassUtils.getDefaultClassLoader().getResource("").getPath();
File fileDir = new File(filePath, "common_server");
if (!fileDir.exists()) {
fileDir.mkdirs();
}
String username = LoginUserUtils.getLoginUserDetail().getUserName();
String time = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
fileName = username + "_" + time + "_" + fileName;
Path path = Paths.get(fileDir.getAbsolutePath(), fileName);
if (Files.exists(path)) {
throw new BusinessException(ErrorCodeEnum.COMMON_FILE_UPLOAD_FAIL, "The file [" + fileName + "] already exists, please try again later");
}
int count = 0;
try {
// Save files
file.transferTo(path.toFile());
try (BufferedReader br = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
ColumnPositionMappingStrategy strategy = new ColumnPositionMappingStrategy();
strategy.setType(CommonDbServerInfo.class);
String[] fields = { "connectionName", "dbType", "dbServerIp", "port", "dbName", "username", "password", "hasSelect", "hasInsert", "hasUpdate", "hasDelete", "inCharges", "isRegionId", "dbDescription", "backupDbServerIp", "backupDbPort", "visiblePerson", "visibleGroup" };
strategy.setColumnMapping(fields);
CsvToBean csvToBean = new CsvToBeanBuilder(br).withType(CommonDbServerInfo.class).withMappingStrategy(strategy).withIgnoreLeadingWhiteSpace(true).withSeparator(',').build();
List<CommonDbServerInfo> dbServerInfos = csvToBean.parse();
if (dbServerInfos.size() > 10000) {
return "Failed, the number of data exceeds the upper limit [10000]";
}
StringBuilder sb = new StringBuilder();
sb.append("csv format, The fields are connectionName, dbType, dbServerIp, port, dbName, " + "username, password, hasSelect, hasInsert, hasUpdate, hasDelete, inCharges, " + "isRegionId, dbDescription, backupDbServerIp, backupDbPort, visiblePerson, visibleGroup\n");
sb.append("Incorrect data check\n");
// check
int i = 1;
boolean passed = true;
for (CommonDbServerInfo entry : dbServerInfos) {
if (!SmallTools.ipCheck(entry.getDbServerIp())) {
sb.append(i + " column, dbServerIp=[" + entry.getDbServerIp() + "]Incorrect check\n");
passed = false;
}
if (!SmallTools.portCheck(entry.getPort())) {
sb.append(i + " column, port=[" + entry.getPort() + "]Incorrect check\n");
passed = false;
}
i++;
}
if (!passed) {
return sb.toString();
}
for (CommonDbServerInfo entry : dbServerInfos) {
commonDBServerService.save(entry);
// System.out.println("entry = "+entry);
count++;
}
}
return "Overall success, a total of 2 " + count + "are written.";
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
return "Overall failure, a total of 2 " + count + "are written.";
}
use of org.apache.inlong.manager.common.exceptions.BusinessException in project incubator-inlong by apache.
the class CommonFileServerController method upload.
/**
* Batch import - upload import config
*/
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ApiOperation(value = "Batch Import - upload config")
public String upload(@RequestParam("file") MultipartFile file) throws Exception {
Preconditions.checkFalse(file.isEmpty(), "File cannot be empty");
String fileName = file.getOriginalFilename();
LOGGER.info("The name of the upload file is: " + fileName);
String filePath = ClassUtils.getDefaultClassLoader().getResource("").getPath();
File fileDir = new File(filePath, "common_server");
if (!fileDir.exists()) {
fileDir.mkdirs();
}
String username = LoginUserUtils.getLoginUserDetail().getUserName();
String time = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
fileName = username + "_" + time + "_" + fileName;
Path path = Paths.get(fileDir.getAbsolutePath(), fileName);
if (Files.exists(path)) {
throw new BusinessException(ErrorCodeEnum.COMMON_FILE_UPLOAD_FAIL, "The file [" + fileName + "] already exists, please try again later");
}
int count = 0;
try {
// Save files
file.transferTo(path.toFile());
try (BufferedReader br = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
ColumnPositionMappingStrategy<CommonFileServerInfo> strategy = new ColumnPositionMappingStrategy<>();
strategy.setType(CommonFileServerInfo.class);
String[] fields = { "ip", "port", "type", "isInnerIp", "issueType", "username", "password", "visiblePerson", "visibleGroup" };
strategy.setColumnMapping(fields);
CsvToBean<CommonFileServerInfo> csvToBean = new CsvToBeanBuilder<CommonFileServerInfo>(br).withType(CommonFileServerInfo.class).withMappingStrategy(strategy).withIgnoreLeadingWhiteSpace(true).withSeparator(',').build();
List<CommonFileServerInfo> fileServerInfos = csvToBean.parse();
if (fileServerInfos.size() > 10000) {
return "Failed, the number of data exceeds the upper limit [10000]";
}
StringBuilder sb = new StringBuilder();
sb.append("csv format, The fields are ip, port, type, isInnerIp, issueType, username, password, " + "visiblePerson, visibleGroup\n");
sb.append("Incorrect data check\n");
// check
int i = 1;
boolean passed = true;
for (CommonFileServerInfo entry : fileServerInfos) {
if (!SmallTools.ipCheck(entry.getIp())) {
sb.append(i).append(" column, ip=[").append(entry.getIp()).append("]Incorrect check\n");
passed = false;
}
if (!SmallTools.portCheck(entry.getPort())) {
sb.append(i).append(" column, port=[").append(entry.getPort()).append("]Incorrect check\n");
passed = false;
}
i++;
}
if (!passed) {
return sb.toString();
}
for (CommonFileServerInfo entry : fileServerInfos) {
commonFileServerService.create(entry);
count++;
}
}
return "success, a total of 2 " + count + "are written.";
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
return "failure, a total of 2 " + count + "are written.";
}
use of org.apache.inlong.manager.common.exceptions.BusinessException in project incubator-inlong by apache.
the class CommonOperateService method getPulsarClusterInfo.
/**
* Get Pulsar cluster by the given type.
*
* @return Pulsar cluster info.
*/
public PulsarClusterInfo getPulsarClusterInfo(String type) {
ThirdPartyClusterEntity clusterEntity = getMQCluster(type);
if (clusterEntity == null || StringUtils.isBlank(clusterEntity.getExtParams())) {
throw new BusinessException("pulsar cluster or pulsar ext params is empty");
}
Map<String, String> configParams = JsonUtils.parse(clusterEntity.getExtParams(), Map.class);
PulsarClusterInfo pulsarClusterInfo = PulsarClusterInfo.builder().brokerServiceUrl(clusterEntity.getUrl()).token(clusterEntity.getToken()).build();
String adminUrl = configParams.get(Constant.PULSAR_ADMINURL);
Preconditions.checkNotNull(adminUrl, "adminUrl is empty, check third party cluster table");
pulsarClusterInfo.setAdminUrl(adminUrl);
pulsarClusterInfo.setType(clusterEntity.getType());
return pulsarClusterInfo;
}
use of org.apache.inlong.manager.common.exceptions.BusinessException in project incubator-inlong by apache.
the class IcebergStreamSinkOperation method saveOpt.
@Override
public Integer saveOpt(SinkRequest request, String operator) {
String sinkType = request.getSinkType();
Preconditions.checkTrue(Constant.SINK_ICEBERG.equals(sinkType), ErrorCodeEnum.SINK_TYPE_NOT_SUPPORT.getMessage() + ": " + sinkType);
IcebergSinkRequest icebergSinkRequest = (IcebergSinkRequest) request;
StreamSinkEntity entity = CommonBeanUtils.copyProperties(icebergSinkRequest, StreamSinkEntity::new);
entity.setStatus(EntityStatus.SINK_NEW.getCode());
entity.setIsDeleted(EntityStatus.UN_DELETED.getCode());
entity.setCreator(operator);
entity.setModifier(operator);
Date now = new Date();
entity.setCreateTime(now);
entity.setModifyTime(now);
// get the ext params
IcebergSinkDTO dto = IcebergSinkDTO.getFromRequest(icebergSinkRequest);
try {
entity.setExtParams(objectMapper.writeValueAsString(dto));
} catch (Exception e) {
throw new BusinessException(ErrorCodeEnum.SINK_SAVE_FAILED);
}
sinkMapper.insert(entity);
Integer sinkId = entity.getId();
request.setId(sinkId);
this.saveFieldOpt(request);
return sinkId;
}
use of org.apache.inlong.manager.common.exceptions.BusinessException in project incubator-inlong by apache.
the class KafkaStreamSinkOperation method updateOpt.
@Override
public void updateOpt(SinkRequest request, String operator) {
String sinkType = request.getSinkType();
Preconditions.checkTrue(Constant.SINK_KAFKA.equals(sinkType), String.format(Constant.SINK_TYPE_NOT_SAME, Constant.SINK_KAFKA, sinkType));
StreamSinkEntity entity = sinkMapper.selectByPrimaryKey(request.getId());
Preconditions.checkNotNull(entity, ErrorCodeEnum.SINK_INFO_NOT_FOUND.getMessage());
KafkaSinkRequest kafkaSinkRequest = (KafkaSinkRequest) request;
CommonBeanUtils.copyProperties(kafkaSinkRequest, entity, true);
try {
KafkaSinkDTO dto = KafkaSinkDTO.getFromRequest(kafkaSinkRequest);
entity.setExtParams(objectMapper.writeValueAsString(dto));
} catch (Exception e) {
throw new BusinessException(ErrorCodeEnum.SINK_INFO_INCORRECT.getMessage());
}
entity.setPreviousStatus(entity.getStatus());
entity.setStatus(EntityStatus.GROUP_CONFIG_ING.getCode());
entity.setModifier(operator);
entity.setModifyTime(new Date());
sinkMapper.updateByPrimaryKeySelective(entity);
boolean onlyAdd = EntityStatus.SINK_CONFIG_SUCCESSFUL.getCode().equals(entity.getPreviousStatus());
this.updateFieldOpt(onlyAdd, kafkaSinkRequest);
LOGGER.info("success to update sink of type={}", sinkType);
}
Aggregations