Search in sources :

Example 26 with BusinessException

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.";
}
Also used : Path(java.nio.file.Path) ColumnPositionMappingStrategy(com.opencsv.bean.ColumnPositionMappingStrategy) IOException(java.io.IOException) Date(java.util.Date) BusinessException(org.apache.inlong.manager.common.exceptions.BusinessException) CommonDbServerInfo(org.apache.inlong.manager.common.pojo.commonserver.CommonDbServerInfo) CsvToBean(com.opencsv.bean.CsvToBean) CsvToBeanBuilder(com.opencsv.bean.CsvToBeanBuilder) BufferedReader(java.io.BufferedReader) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) SimpleDateFormat(java.text.SimpleDateFormat) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 27 with BusinessException

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.";
}
Also used : Path(java.nio.file.Path) CommonFileServerInfo(org.apache.inlong.manager.common.pojo.commonserver.CommonFileServerInfo) ColumnPositionMappingStrategy(com.opencsv.bean.ColumnPositionMappingStrategy) IOException(java.io.IOException) Date(java.util.Date) BusinessException(org.apache.inlong.manager.common.exceptions.BusinessException) BufferedReader(java.io.BufferedReader) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) SimpleDateFormat(java.text.SimpleDateFormat) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 28 with BusinessException

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;
}
Also used : BusinessException(org.apache.inlong.manager.common.exceptions.BusinessException) PulsarClusterInfo(org.apache.inlong.common.pojo.dataproxy.PulsarClusterInfo) ThirdPartyClusterEntity(org.apache.inlong.manager.dao.entity.ThirdPartyClusterEntity)

Example 29 with BusinessException

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;
}
Also used : BusinessException(org.apache.inlong.manager.common.exceptions.BusinessException) IcebergSinkRequest(org.apache.inlong.manager.common.pojo.sink.iceberg.IcebergSinkRequest) IcebergSinkDTO(org.apache.inlong.manager.common.pojo.sink.iceberg.IcebergSinkDTO) StreamSinkEntity(org.apache.inlong.manager.dao.entity.StreamSinkEntity) Date(java.util.Date) BusinessException(org.apache.inlong.manager.common.exceptions.BusinessException)

Example 30 with BusinessException

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);
}
Also used : KafkaSinkDTO(org.apache.inlong.manager.common.pojo.sink.kafka.KafkaSinkDTO) BusinessException(org.apache.inlong.manager.common.exceptions.BusinessException) KafkaSinkRequest(org.apache.inlong.manager.common.pojo.sink.kafka.KafkaSinkRequest) StreamSinkEntity(org.apache.inlong.manager.dao.entity.StreamSinkEntity) BusinessException(org.apache.inlong.manager.common.exceptions.BusinessException) Date(java.util.Date)

Aggregations

BusinessException (org.apache.inlong.manager.common.exceptions.BusinessException)58 Date (java.util.Date)20 Transactional (org.springframework.transaction.annotation.Transactional)18 InlongGroupEntity (org.apache.inlong.manager.dao.entity.InlongGroupEntity)14 StreamSinkEntity (org.apache.inlong.manager.dao.entity.StreamSinkEntity)8 InlongStreamEntity (org.apache.inlong.manager.dao.entity.InlongStreamEntity)7 InlongGroupInfo (org.apache.inlong.manager.common.pojo.group.InlongGroupInfo)6 SourceFileDetailEntity (org.apache.inlong.manager.dao.entity.SourceFileDetailEntity)5 ThirdPartyClusterEntity (org.apache.inlong.manager.dao.entity.ThirdPartyClusterEntity)5 GroupState (org.apache.inlong.manager.common.enums.GroupState)4 InlongGroupPulsarEntity (org.apache.inlong.manager.dao.entity.InlongGroupPulsarEntity)4 SourceDbDetailEntity (org.apache.inlong.manager.dao.entity.SourceDbDetailEntity)4 PulsarClusterInfo (org.apache.inlong.common.pojo.dataproxy.PulsarClusterInfo)3 InlongGroupPulsarInfo (org.apache.inlong.manager.common.pojo.group.InlongGroupPulsarInfo)3 GroupResourceProcessForm (org.apache.inlong.manager.common.pojo.workflow.form.GroupResourceProcessForm)3 SourceDbBasicEntity (org.apache.inlong.manager.dao.entity.SourceDbBasicEntity)3 ColumnPositionMappingStrategy (com.opencsv.bean.ColumnPositionMappingStrategy)2 ApiOperation (io.swagger.annotations.ApiOperation)2 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2