use of com.thinkbiganalytics.repository.api.TemplateRepository in project kylo by Teradata.
the class FilesystemRepositoryService method publishTemplate.
@Override
public TemplateMetadataWrapper publishTemplate(String repositoryName, String repositoryType, String templateId, boolean overwrite) throws Exception {
// export template
ExportTemplate zipFile = templateExporter.exportTemplate(templateId);
// get repository
TemplateRepository repository = getRepositoryByNameAndType(repositoryName, repositoryType);
// Check if template already exists
Optional<TemplateMetadataWrapper> foundMetadataOpt = Optional.empty();
for (TemplateMetadataWrapper item : listTemplatesByRepository(repository)) {
if (item.getTemplateName().equals(zipFile.getTemplateName())) {
foundMetadataOpt = Optional.of(item);
break;
}
}
if (foundMetadataOpt.isPresent()) {
if (!overwrite) {
throw new UnsupportedOperationException("Template with same name already exists.");
}
log.info("Overwriting template with same name.");
}
String digest = DigestUtils.md5DigestAsHex(zipFile.getFile());
Path templatePath = Paths.get(repository.getLocation() + "/" + zipFile.getFileName());
TemplateMetadata metadata = new TemplateMetadata(zipFile.getTemplateName(), zipFile.getDescription(), zipFile.getFileName().toString(), digest, zipFile.isStream(), false, templatePath.toFile().lastModified());
// create repositoryItem
TemplateMetadataWrapper templateMetadata = foundMetadataOpt.isPresent() ? foundMetadataOpt.get() : new TemplateMetadataWrapper(metadata);
String baseName = FilenameUtils.getBaseName(templateMetadata.getFileName());
// write file in first of templateLocations
Files.write(templatePath, zipFile.getFile());
log.info("Finished publishing template {} to repository {}.", templateMetadata.getTemplateName(), repository.getName());
return new TemplateMetadataWrapper(metadata);
}
use of com.thinkbiganalytics.repository.api.TemplateRepository in project kylo by Teradata.
the class FilesystemRepositoryService method downloadTemplate.
@Override
public byte[] downloadTemplate(String repositoryName, String repositoryType, String fileName) throws Exception {
// get repository
TemplateRepository repository = getRepositoryByNameAndType(repositoryName, repositoryType);
Path path = Paths.get(repository.getLocation() + "/" + fileName);
if (!Files.exists(Paths.get(repository.getLocation() + "/" + fileName))) {
throw new RuntimeException("File not found for download: " + fileName);
}
log.info("Begin template download {}", fileName);
return ImportUtil.streamToByteArray(new FileInputStream(path.toFile()));
}
use of com.thinkbiganalytics.repository.api.TemplateRepository in project kylo by Teradata.
the class FilesystemRepositoryService method listRepositories.
@Override
public List<TemplateRepository> listRepositories() {
TypeReference<List<TemplateRepository>> typeReference = new TypeReference<List<TemplateRepository>>() {
};
try {
InputStream is = resourceLoader.getResource("classpath:repositories.json").getInputStream();
List<TemplateRepository> repositories = new ArrayList<>();
repositories.add(new TemplateRepository("Kylo Repository", defaultKyloRepository, "", FILESYSTEM, true));
repositories.addAll(mapper.readValue(is, typeReference));
Set<String> set = new HashSet<>(repositories.size());
return repositories.stream().filter(r -> // location must be provided
StringUtils.isNotBlank(r.getLocation()) && // location must be valid
Files.exists(Paths.get(r.getLocation().trim())) && // unique name per repository type
set.add(r.getName().trim().toLowerCase() + "_" + r.getType().getKey().trim().toLowerCase())).collect(Collectors.toList());
} catch (Exception e) {
log.error("Error reading template repositories", e);
}
return new ArrayList<>();
}
use of com.thinkbiganalytics.repository.api.TemplateRepository in project kylo by Teradata.
the class FilesystemRepositoryService method importTemplate.
@Override
public ImportTemplate importTemplate(String repositoryName, String repositoryType, String fileName, String uploadKey, String importComponents) throws Exception {
Optional<TemplateRepository> repository = listRepositories().stream().filter(r -> StringUtils.equals(r.getName(), repositoryName) && StringUtils.equals(r.getType().getKey(), repositoryType)).findFirst();
Path filePath = Paths.get(repository.get().getLocation() + "/" + fileName);
if (!filePath.toFile().exists()) {
throw new RuntimeException("Unable to find template file to import: " + fileName);
}
log.info("Begin template import {}", fileName);
ImportTemplateOptions options = new ImportTemplateOptions();
options.setUploadKey(uploadKey);
byte[] content = ImportUtil.streamToByteArray(new FileInputStream(filePath.toFile()));
String checksum = DigestUtils.md5DigestAsHex(content);
uploadProgressService.newUpload(uploadKey);
ImportTemplate importTemplate = null;
TemplateImporter templateImporter = null;
if (importComponents == null) {
templateImporter = templateImporterFactory.apply(fileName, content, options);
importTemplate = templateImporter.validate();
importTemplate.setSuccess(false);
} else {
options.setImportComponentOptions(ObjectMapperSerializer.deserialize(importComponents, new TypeReference<Set<ImportComponentOption>>() {
}));
templateImporter = templateImporterFactory.apply(fileName, content, options);
importTemplate = templateImporter.validateAndImport();
}
log.info("End template import {} - {}", fileName, importTemplate.isSuccess());
// update template metadata
String baseName = FilenameUtils.getBaseName(fileName);
Path metadataPath = Paths.get(repository.get().getLocation() + "/" + baseName + ".json");
TemplateMetadata templateMetadata = mapper.readValue(metadataPath.toFile(), TemplateMetadata.class);
templateMetadata.setChecksum(checksum);
long updateTime = importTemplate.getTemplateToImport().getUpdateDate().getTime();
templateMetadata.setLastModified(updateTime);
templateMetadata.setUpdateAvailable(false);
mapper.writer(new DefaultPrettyPrinter()).writeValue(metadataPath.toFile(), templateMetadata);
log.info("Generated checksum for {} - {}", templateMetadata.getTemplateName(), checksum);
templateUpdateInfoCache.put(templateMetadata.getTemplateName(), false);
return importTemplate;
}
Aggregations