Search in sources :

Example 1 with TemplateRepository

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);
}
Also used : Path(java.nio.file.Path) TemplateRepository(com.thinkbiganalytics.repository.api.TemplateRepository) TemplateMetadataWrapper(com.thinkbiganalytics.repository.api.TemplateMetadataWrapper) ExportTemplate(com.thinkbiganalytics.metadata.api.template.export.ExportTemplate) TemplateMetadata(com.thinkbiganalytics.repository.api.TemplateMetadata)

Example 2 with TemplateRepository

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()));
}
Also used : Path(java.nio.file.Path) TemplateRepository(com.thinkbiganalytics.repository.api.TemplateRepository) FileInputStream(java.io.FileInputStream)

Example 3 with TemplateRepository

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<>();
}
Also used : UploadProgressService(com.thinkbiganalytics.feedmgr.service.UploadProgressService) RegisteredTemplateService(com.thinkbiganalytics.feedmgr.service.template.RegisteredTemplateService) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) TemplateImporter(com.thinkbiganalytics.feedmgr.service.template.importing.TemplateImporter) StringUtils(org.apache.commons.lang3.StringUtils) TemplateChangeEvent(com.thinkbiganalytics.metadata.api.event.template.TemplateChangeEvent) ObjectMapperSerializer(com.thinkbiganalytics.json.ObjectMapperSerializer) ByteArrayInputStream(java.io.ByteArrayInputStream) ImportUtil(com.thinkbiganalytics.feedmgr.util.ImportUtil) TemplateSearchFilter(com.thinkbiganalytics.repository.api.TemplateSearchFilter) TypeReference(com.fasterxml.jackson.core.type.TypeReference) MetadataAccess(com.thinkbiganalytics.metadata.api.MetadataAccess) Path(java.nio.file.Path) RepositoryService(com.thinkbiganalytics.repository.api.RepositoryService) ResourceLoader(org.springframework.core.io.ResourceLoader) ImportTemplate(com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate) MetadataEventListener(com.thinkbiganalytics.metadata.api.event.MetadataEventListener) FILESYSTEM(com.thinkbiganalytics.repository.api.TemplateRepository.RepositoryType.FILESYSTEM) Set(java.util.Set) MetadataEventService(com.thinkbiganalytics.metadata.api.event.MetadataEventService) Collectors(java.util.stream.Collectors) ExportTemplate(com.thinkbiganalytics.metadata.api.template.export.ExportTemplate) List(java.util.List) Principal(java.security.Principal) Stream(java.util.stream.Stream) PostConstruct(javax.annotation.PostConstruct) ImportTemplateOptions(com.thinkbiganalytics.feedmgr.rest.model.ImportTemplateOptions) Optional(java.util.Optional) TemplateChange(com.thinkbiganalytics.metadata.api.event.template.TemplateChange) FilenameUtils(org.apache.commons.io.FilenameUtils) SearchResultImpl(com.thinkbiganalytics.rest.model.search.SearchResultImpl) TemplateComparator.valueOf(com.thinkbiganalytics.repository.api.TemplateSearchFilter.TemplateComparator.valueOf) DigestUtils(org.springframework.util.DigestUtils) PropertySource(org.springframework.context.annotation.PropertySource) Scheduled(org.springframework.scheduling.annotation.Scheduled) TemplateMetadataWrapper(com.thinkbiganalytics.repository.api.TemplateMetadataWrapper) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) HashSet(java.util.HashSet) Inject(javax.inject.Inject) TemplateRepository(com.thinkbiganalytics.repository.api.TemplateRepository) TemplateModelTransform(com.thinkbiganalytics.feedmgr.service.template.TemplateModelTransform) Service(org.springframework.stereotype.Service) DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) MetadataChange(com.thinkbiganalytics.metadata.api.event.MetadataChange) RegisteredTemplate(com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate) TemplateImporterFactory(com.thinkbiganalytics.feedmgr.service.template.importing.TemplateImporterFactory) NAME(com.thinkbiganalytics.repository.api.TemplateSearchFilter.TemplateComparator.NAME) Logger(org.slf4j.Logger) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TemplateExporter(com.thinkbiganalytics.metadata.api.template.export.TemplateExporter) FileInputStream(java.io.FileInputStream) SearchResult(com.thinkbiganalytics.rest.model.search.SearchResult) TemplateMetadata(com.thinkbiganalytics.repository.api.TemplateMetadata) Paths(java.nio.file.Paths) Cache(com.google.common.cache.Cache) Comparator(java.util.Comparator) Collections(java.util.Collections) ImportComponentOption(com.thinkbiganalytics.feedmgr.rest.model.ImportComponentOption) InputStream(java.io.InputStream) TemplateRepository(com.thinkbiganalytics.repository.api.TemplateRepository) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) TypeReference(com.fasterxml.jackson.core.type.TypeReference) HashSet(java.util.HashSet)

Example 4 with TemplateRepository

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;
}
Also used : UploadProgressService(com.thinkbiganalytics.feedmgr.service.UploadProgressService) RegisteredTemplateService(com.thinkbiganalytics.feedmgr.service.template.RegisteredTemplateService) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) TemplateImporter(com.thinkbiganalytics.feedmgr.service.template.importing.TemplateImporter) StringUtils(org.apache.commons.lang3.StringUtils) TemplateChangeEvent(com.thinkbiganalytics.metadata.api.event.template.TemplateChangeEvent) ObjectMapperSerializer(com.thinkbiganalytics.json.ObjectMapperSerializer) ByteArrayInputStream(java.io.ByteArrayInputStream) ImportUtil(com.thinkbiganalytics.feedmgr.util.ImportUtil) TemplateSearchFilter(com.thinkbiganalytics.repository.api.TemplateSearchFilter) TypeReference(com.fasterxml.jackson.core.type.TypeReference) MetadataAccess(com.thinkbiganalytics.metadata.api.MetadataAccess) Path(java.nio.file.Path) RepositoryService(com.thinkbiganalytics.repository.api.RepositoryService) ResourceLoader(org.springframework.core.io.ResourceLoader) ImportTemplate(com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate) MetadataEventListener(com.thinkbiganalytics.metadata.api.event.MetadataEventListener) FILESYSTEM(com.thinkbiganalytics.repository.api.TemplateRepository.RepositoryType.FILESYSTEM) Set(java.util.Set) MetadataEventService(com.thinkbiganalytics.metadata.api.event.MetadataEventService) Collectors(java.util.stream.Collectors) ExportTemplate(com.thinkbiganalytics.metadata.api.template.export.ExportTemplate) List(java.util.List) Principal(java.security.Principal) Stream(java.util.stream.Stream) PostConstruct(javax.annotation.PostConstruct) ImportTemplateOptions(com.thinkbiganalytics.feedmgr.rest.model.ImportTemplateOptions) Optional(java.util.Optional) TemplateChange(com.thinkbiganalytics.metadata.api.event.template.TemplateChange) FilenameUtils(org.apache.commons.io.FilenameUtils) SearchResultImpl(com.thinkbiganalytics.rest.model.search.SearchResultImpl) TemplateComparator.valueOf(com.thinkbiganalytics.repository.api.TemplateSearchFilter.TemplateComparator.valueOf) DigestUtils(org.springframework.util.DigestUtils) PropertySource(org.springframework.context.annotation.PropertySource) Scheduled(org.springframework.scheduling.annotation.Scheduled) TemplateMetadataWrapper(com.thinkbiganalytics.repository.api.TemplateMetadataWrapper) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) HashSet(java.util.HashSet) Inject(javax.inject.Inject) TemplateRepository(com.thinkbiganalytics.repository.api.TemplateRepository) TemplateModelTransform(com.thinkbiganalytics.feedmgr.service.template.TemplateModelTransform) Service(org.springframework.stereotype.Service) DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) MetadataChange(com.thinkbiganalytics.metadata.api.event.MetadataChange) RegisteredTemplate(com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate) TemplateImporterFactory(com.thinkbiganalytics.feedmgr.service.template.importing.TemplateImporterFactory) NAME(com.thinkbiganalytics.repository.api.TemplateSearchFilter.TemplateComparator.NAME) Logger(org.slf4j.Logger) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TemplateExporter(com.thinkbiganalytics.metadata.api.template.export.TemplateExporter) FileInputStream(java.io.FileInputStream) SearchResult(com.thinkbiganalytics.rest.model.search.SearchResult) TemplateMetadata(com.thinkbiganalytics.repository.api.TemplateMetadata) Paths(java.nio.file.Paths) Cache(com.google.common.cache.Cache) Comparator(java.util.Comparator) Collections(java.util.Collections) ImportComponentOption(com.thinkbiganalytics.feedmgr.rest.model.ImportComponentOption) InputStream(java.io.InputStream) Path(java.nio.file.Path) DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) TemplateRepository(com.thinkbiganalytics.repository.api.TemplateRepository) ImportTemplateOptions(com.thinkbiganalytics.feedmgr.rest.model.ImportTemplateOptions) ImportComponentOption(com.thinkbiganalytics.feedmgr.rest.model.ImportComponentOption) ImportTemplate(com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate) FileInputStream(java.io.FileInputStream) TemplateImporter(com.thinkbiganalytics.feedmgr.service.template.importing.TemplateImporter) TypeReference(com.fasterxml.jackson.core.type.TypeReference) TemplateMetadata(com.thinkbiganalytics.repository.api.TemplateMetadata)

Aggregations

TemplateRepository (com.thinkbiganalytics.repository.api.TemplateRepository)4 Path (java.nio.file.Path)4 ExportTemplate (com.thinkbiganalytics.metadata.api.template.export.ExportTemplate)3 TemplateMetadata (com.thinkbiganalytics.repository.api.TemplateMetadata)3 TemplateMetadataWrapper (com.thinkbiganalytics.repository.api.TemplateMetadataWrapper)3 FileInputStream (java.io.FileInputStream)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Cache (com.google.common.cache.Cache)2 ImportComponentOption (com.thinkbiganalytics.feedmgr.rest.model.ImportComponentOption)2 ImportTemplateOptions (com.thinkbiganalytics.feedmgr.rest.model.ImportTemplateOptions)2 RegisteredTemplate (com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate)2 UploadProgressService (com.thinkbiganalytics.feedmgr.service.UploadProgressService)2 RegisteredTemplateService (com.thinkbiganalytics.feedmgr.service.template.RegisteredTemplateService)2 TemplateModelTransform (com.thinkbiganalytics.feedmgr.service.template.TemplateModelTransform)2 TemplateImporter (com.thinkbiganalytics.feedmgr.service.template.importing.TemplateImporter)2 TemplateImporterFactory (com.thinkbiganalytics.feedmgr.service.template.importing.TemplateImporterFactory)2 ImportTemplate (com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate)2 ImportUtil (com.thinkbiganalytics.feedmgr.util.ImportUtil)2