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);
}
}
}
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);
}
}
}
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;
}
Aggregations