Search in sources :

Example 6 with Resource

use of com.dtstack.taier.dao.dto.Resource in project Taier by DTStack.

the class ConsoleComponentService method parseUploadFileToMap.

private Map<String, Map<String, Object>> parseUploadFileToMap(List<Resource> resources) {
    if (CollectionUtils.isEmpty(resources)) {
        throw new RdosDefineException("The uploaded file cannot be empty");
    }
    Resource resource = resources.get(0);
    if (!resource.getFileName().endsWith(ZIP_SUFFIX)) {
        throw new RdosDefineException("The compressed package format only supports ZIP format");
    }
    String upzipLocation = USER_DIR_UNZIP + File.separator + resource.getFileName();
    try {
        Map<String, Map<String, Object>> confMap = new HashMap<>();
        // 解压缩获得配置文件
        String xmlZipLocation = resource.getUploadedFileName();
        List<File> xmlFiles = XmlFileUtil.getFilesFromZip(xmlZipLocation, upzipLocation, null);
        if (CollectionUtils.isEmpty(xmlFiles)) {
            throw new RdosDefineException("The configuration file cannot be empty");
        }
        for (File file : xmlFiles) {
            Map<String, Object> fileMap = null;
            if (file.getName().startsWith(".")) {
                // .开头过滤
                continue;
            }
            if (file.getName().endsWith("xml")) {
                // xml文件
                fileMap = Xml2JsonUtil.xml2map(file);
            } else if (file.getName().endsWith("json")) {
                // json文件
                String jsonStr = Xml2JsonUtil.readFile(file);
                if (StringUtils.isBlank(jsonStr)) {
                    continue;
                }
                fileMap = (Map<String, Object>) JSONObject.parseObject(jsonStr, Map.class);
            }
            if (null != fileMap) {
                confMap.put(file.getName(), fileMap);
            }
        }
        return confMap;
    } catch (Exception e) {
        LOGGER.error("parseAndUploadXmlFile file error ", e);
        throw new RdosDefineException(ExceptionUtil.getErrorMessage(e));
    } finally {
        if (StringUtils.isNotBlank(upzipLocation)) {
            ZipUtil.deletefile(upzipLocation);
        }
    }
}
Also used : RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) Resource(com.dtstack.taier.dao.dto.Resource) JSONObject(com.alibaba.fastjson.JSONObject) File(java.io.File) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) IOException(java.io.IOException)

Example 7 with Resource

use of com.dtstack.taier.dao.dto.Resource in project Taier by DTStack.

the class ConsoleComponentService method parseKerberos.

/**
 * 解析对应的kerberos的zip中principle
 *
 * @param resourcesFromFiles
 * @return
 */
public List<String> parseKerberos(List<Resource> resourcesFromFiles) {
    if (CollectionUtils.isEmpty(resourcesFromFiles)) {
        return null;
    }
    Resource resource = resourcesFromFiles.get(0);
    String unzipLocation = USER_DIR_UNZIP + File.separator + resource.getFileName();
    try {
        // 解压到本地
        List<File> files = ZipUtil.upzipFile(resource.getUploadedFileName(), unzipLocation);
        if (CollectionUtils.isEmpty(files)) {
            throw new RdosDefineException("Hadoop-Kerberos file decompression error");
        }
        File fileKeyTab = files.stream().filter(f -> f.getName().endsWith(KEYTAB_SUFFIX)).findFirst().orElse(null);
        if (fileKeyTab == null) {
            throw new RdosDefineException("There must be a keytab file in the zip file of the uploaded Hadoop-Kerberos file, please add the keytab file");
        }
        // 获取principal
        List<PrincipalName> principal = this.getPrincipal(fileKeyTab);
        return principal.stream().map(PrincipalName::getName).collect(Collectors.toList());
    } finally {
        try {
            FileUtils.deleteDirectory(new File(unzipLocation));
        } catch (IOException e) {
            LOGGER.error("delete update file {} error", unzipLocation);
        }
    }
}
Also used : RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) Resource(com.dtstack.taier.dao.dto.Resource) PrincipalName(org.apache.kerby.kerberos.kerb.type.base.PrincipalName) IOException(java.io.IOException) File(java.io.File)

Example 8 with Resource

use of com.dtstack.taier.dao.dto.Resource in project Taier by DTStack.

the class ConsoleComponentService method parseJsonFile.

private List<Object> parseJsonFile(List<Resource> resources) {
    List<Object> data = new ArrayList<>();
    // 当作json来解析
    for (Resource resource : resources) {
        try {
            String fileInfo = FileUtils.readFileToString(new File(resource.getUploadedFileName()));
            data.add(PublicUtil.strToMap(fileInfo));
        } catch (Exception e) {
            LOGGER.error("parse json config resource error {} ", resource.getUploadedFileName());
            throw new RdosDefineException("JSON file format error");
        }
    }
    return data;
}
Also used : RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) Resource(com.dtstack.taier.dao.dto.Resource) JSONObject(com.alibaba.fastjson.JSONObject) File(java.io.File) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) IOException(java.io.IOException)

Aggregations

RdosDefineException (com.dtstack.taier.common.exception.RdosDefineException)8 Resource (com.dtstack.taier.dao.dto.Resource)8 File (java.io.File)6 IOException (java.io.IOException)5 JSONObject (com.alibaba.fastjson.JSONObject)3 SftpFileManage (com.dtstack.taier.pluginapi.sftp.SftpFileManage)3 EComponentType (com.dtstack.taier.common.enums.EComponentType)2 SftpConfig (com.dtstack.taier.pluginapi.sftp.SftpConfig)2 ComponentVO (com.dtstack.taier.scheduler.vo.ComponentVO)2 Transactional (org.springframework.transaction.annotation.Transactional)2 JSONArray (com.alibaba.fastjson.JSONArray)1 ZIP_SUFFIX (com.dtstack.taier.common.constant.CommonConstant.ZIP_SUFFIX)1 DictType (com.dtstack.taier.common.enums.DictType)1 DownloadType (com.dtstack.taier.common.enums.DownloadType)1 EnvironmentContext (com.dtstack.taier.common.env.EnvironmentContext)1 ErrorCode (com.dtstack.taier.common.exception.ErrorCode)1 ComponentVersionUtil (com.dtstack.taier.common.util.ComponentVersionUtil)1 MathUtil (com.dtstack.taier.common.util.MathUtil)1 Xml2JsonUtil (com.dtstack.taier.common.util.Xml2JsonUtil)1 ZipUtil (com.dtstack.taier.common.util.ZipUtil)1