Search in sources :

Example 1 with AntivirusJobEvent

use of com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent in project ArachneCentralAPI by OHDSI.

the class BaseAnalysisServiceImpl method saveFile.

@Override
public AnalysisFile saveFile(MultipartFile multipartFile, IUser user, A analysis, String label, Boolean isExecutable, DataReference dataReference) throws IOException {
    String originalFilename = multipartFile.getOriginalFilename();
    String fileNameLowerCase = UUID.randomUUID().toString();
    try {
        Path analysisPath = getAnalysisPath(analysis);
        Path targetPath = Paths.get(analysisPath.toString(), fileNameLowerCase);
        Files.copy(multipartFile.getInputStream(), targetPath, REPLACE_EXISTING);
        AnalysisFile analysisFile = new AnalysisFile();
        analysisFile.setDataReference(dataReference);
        analysisFile.setUuid(fileNameLowerCase);
        analysisFile.setAnalysis(analysis);
        analysisFile.setContentType(CommonFileUtils.getContentType(originalFilename, targetPath.toString()));
        analysisFile.setLabel(label);
        analysisFile.setAuthor(user);
        analysisFile.setUpdatedBy(user);
        analysisFile.setExecutable(false);
        analysisFile.setRealName(originalFilename);
        Date created = new Date();
        analysisFile.setCreated(created);
        analysisFile.setUpdated(created);
        analysisFile.setVersion(1);
        beforeSaveAnalysisFile(analysisFile);
        AnalysisFile saved = analysisFileRepository.save(analysisFile);
        analysis.getFiles().add(saved);
        afterSaveAnalysisFile(saved);
        if (Boolean.TRUE.equals(isExecutable)) {
            setIsExecutable(saved.getUuid());
        }
        preprocessorService.preprocessFile(analysis, analysisFile);
        eventPublisher.publishEvent(new AntivirusJobEvent(this, new AntivirusJob(saved.getId(), saved.getRealName(), new FileInputStream(targetPath.toString()), AntivirusJobFileType.ANALYSIS_FILE)));
        return saved;
    } catch (IOException | RuntimeException ex) {
        String message = "error save file to disk, filename=" + fileNameLowerCase + " ex=" + ex.toString();
        LOGGER.error(message, ex);
        throw new IOException(message);
    }
}
Also used : Path(java.nio.file.Path) AntivirusJob(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) AnalysisFile(com.odysseusinc.arachne.portal.model.AnalysisFile) AntivirusJobEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent) IOException(java.io.IOException) Date(java.util.Date) FileInputStream(java.io.FileInputStream)

Example 2 with AntivirusJobEvent

use of com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent in project ArachneCentralAPI by OHDSI.

the class BasePaperServiceImpl method saveFile.

@PreAuthorize("hasPermission(#paperId, 'Paper', " + "T(com.odysseusinc.arachne.portal.security.ArachnePermission).LIMITED_EDIT_PAPER)")
@Transactional(rollbackFor = Exception.class)
@Override
public String saveFile(Long paperId, MultipartFile file, PaperFileType fileType, String label, IUser user) throws IOException {
    if (file == null) {
        throw new IllegalArgumentException("File must not be null");
    }
    final Paper paper = get(paperId);
    final String realName = file.getOriginalFilename();
    final String contentType = CommonFileUtils.getContentType(realName, file);
    AbstractPaperFile paperFile = savePaperFile(fileType, label, user, paper, contentType, realName, null);
    fileService.saveFile(file, paperFile);
    AntivirusJobFileType antivirusJobFileType;
    switch(fileType) {
        case PAPER:
            antivirusJobFileType = AntivirusJobFileType.PAPER_PAPER_FILE;
            break;
        case PROTOCOL:
            antivirusJobFileType = AntivirusJobFileType.PAPER_PROTOCOL_FILE;
            break;
        default:
            throw new IllegalArgumentException();
    }
    eventPublisher.publishEvent(new AntivirusJobEvent(this, new AntivirusJob(paperFile.getId(), paperFile.getRealName(), fileService.getFileInputStream(paperFile), antivirusJobFileType)));
    return paperFile.getUuid();
}
Also used : AntivirusJob(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob) AntivirusJobFileType(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobFileType) Paper(com.odysseusinc.arachne.portal.model.Paper) AntivirusJobEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent) AbstractPaperFile(com.odysseusinc.arachne.portal.model.AbstractPaperFile) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with AntivirusJobEvent

use of com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent in project ArachneCentralAPI by OHDSI.

the class BaseStudyServiceImpl method saveFile.

@Override
@PreAuthorize("hasPermission(#studyId, 'Study', " + "T(com.odysseusinc.arachne.portal.security.ArachnePermission).UPLOAD_FILES)")
public String saveFile(MultipartFile multipartFile, Long studyId, String label, IUser user) throws IOException {
    Study study = studyRepository.findOne(studyId);
    String fileNameLowerCase = UUID.randomUUID().toString();
    try {
        StudyFile studyFile = new StudyFile();
        studyFile.setUuid(fileNameLowerCase);
        studyFile.setStudy(study);
        studyFile.setLabel(label);
        studyFile.setRealName(multipartFile.getOriginalFilename());
        Date created = new Date();
        studyFile.setCreated(created);
        studyFile.setUpdated(created);
        studyFile.setAuthor(user);
        // Save study after uuid and Study were set
        fileService.saveFile(multipartFile, studyFile);
        // Detect file content type (requires file to exist)
        String contentType = CommonFileUtils.getContentType(multipartFile.getOriginalFilename(), fileService.getStudyFilePath(studyFile).toAbsolutePath().toString());
        studyFile.setContentType(contentType);
        // Save entity
        studyFileRepository.save(studyFile);
        eventPublisher.publishEvent(new AntivirusJobEvent(this, new AntivirusJob(studyFile.getId(), studyFile.getRealName(), fileService.getFileInputStream(studyFile), AntivirusJobFileType.STUDY_FILE)));
        return fileNameLowerCase;
    } catch (IOException | RuntimeException ex) {
        String message = "error save file to disk, filename=" + fileNameLowerCase + " ex=" + ex.toString();
        LOGGER.debug(message, ex);
        throw new IOException(message);
    }
}
Also used : UserStudy(com.odysseusinc.arachne.portal.model.UserStudy) Study(com.odysseusinc.arachne.portal.model.Study) FavouriteStudy(com.odysseusinc.arachne.portal.model.FavouriteStudy) AntivirusJob(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) StudyFile(com.odysseusinc.arachne.portal.model.StudyFile) AntivirusJobEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent) IOException(java.io.IOException) Date(java.util.Date) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 4 with AntivirusJobEvent

use of com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent in project ArachneCentralAPI by OHDSI.

the class BaseAnalysisServiceImpl method writeToFile.

@Override
@PreAuthorize("hasPermission(#analysisFile, " + "T(com.odysseusinc.arachne.portal.security.ArachnePermission).DELETE_ANALYSIS_FILES)")
public void writeToFile(AnalysisFile analysisFile, FileDTO fileContentDTO, IUser updatedBy) throws IOException {
    Analysis analysis = analysisFile.getAnalysis();
    throwAccessDeniedExceptionIfLocked(analysis);
    Study study = analysis.getStudy();
    try {
        Path analysisFolder = analysisHelper.getAnalysisFolder(analysis);
        if (Files.notExists(analysisFolder)) {
            Files.createDirectories(analysisFolder);
        }
        Path targetPath = analysisFolder.resolve(analysisFile.getUuid());
        byte[] content = fileContentDTO.getContent().getBytes(StandardCharsets.UTF_8);
        try (final InputStream stream = new ByteArrayInputStream(content)) {
            Files.copy(stream, targetPath, REPLACE_EXISTING);
        }
        analysisFile.setUpdated(new Date());
        analysisFile.setEntryPoint(analysisFile.getEntryPoint());
        analysisFile.setUpdatedBy(updatedBy);
        analysisFile.setContentType(CommonFileUtils.getContentType(analysisFile.getRealName(), targetPath.toString()));
        analysisFile.incrementVersion();
        analysisFile.setAntivirusStatus(AntivirusStatus.SCANNING);
        analysisFile.setAntivirusDescription(null);
        final AnalysisFile saved = analysisFileRepository.save(analysisFile);
        eventPublisher.publishEvent(new AntivirusJobEvent(this, new AntivirusJob(saved.getId(), saved.getRealName(), new FileInputStream(targetPath.toString()), AntivirusJobFileType.ANALYSIS_FILE)));
    } catch (IOException | RuntimeException ex) {
        String message = "error save file to disk, filename=" + analysisFile.getUuid() + " ex=" + ex.toString();
        LOGGER.error(message, ex);
        throw new IOException(message);
    }
}
Also used : Path(java.nio.file.Path) Study(com.odysseusinc.arachne.portal.model.Study) AntivirusJob(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) AnalysisFile(com.odysseusinc.arachne.portal.model.AnalysisFile) IOException(java.io.IOException) Date(java.util.Date) FileInputStream(java.io.FileInputStream) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) ByteArrayInputStream(java.io.ByteArrayInputStream) Analysis(com.odysseusinc.arachne.portal.model.Analysis) AntivirusJobEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 5 with AntivirusJobEvent

use of com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent in project ArachneCentralAPI by OHDSI.

the class AntivirusServiceImpl method processRequest.

@EventListener
@Async(value = "antivirusScanExecutor")
public void processRequest(AntivirusJobEvent event) {
    final AntivirusJob antivirusJob = event.getAntivirusJob();
    final AntivirusJobFileType fileType = antivirusJob.getAntivirusJobFileType();
    final Long fileId = antivirusJob.getFileId();
    logger.debug(PROCESSING_SCAN_REQUEST, fileId, fileType);
    String description = null;
    AntivirusStatus status;
    try {
        final ScanResult scan = retryTemplate.execute((RetryCallback<ScanResult, Exception>) retryContext -> {
            logger.debug(PROCESSING_SCAN_ATTEMPT, fileId, fileType);
            return scan(antivirusJob.getContent());
        });
        if (scan instanceof ScanResult.OK) {
            status = AntivirusStatus.OK;
        } else {
            status = AntivirusStatus.INFECTED;
            description = scan.toString();
        }
    } catch (Exception e) {
        logger.error("Error scanning file: {}", e.getMessage());
        if (e instanceof ClamavException) {
            final Throwable cause = e.getCause();
            description = cause.getMessage();
        } else {
            description = e.getMessage();
        }
        status = AntivirusStatus.NOT_SCANNED;
    }
    logger.debug(PROCESSING_SCAN_RESULT, fileId, fileType, status);
    publishResponse(fileType, fileId, status, description);
}
Also used : Async(org.springframework.scheduling.annotation.Async) LoggerFactory(org.slf4j.LoggerFactory) AntivirusJobResponse(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobResponse) Autowired(org.springframework.beans.factory.annotation.Autowired) ScanResult(xyz.capybara.clamav.commands.scan.result.ScanResult) AntivirusStatus(com.odysseusinc.arachne.portal.model.AntivirusStatus) AntivirusJobStudyFileResponseEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobStudyFileResponseEvent) Value(org.springframework.beans.factory.annotation.Value) AntivirusJobEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent) Service(org.springframework.stereotype.Service) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ClamavException(xyz.capybara.clamav.ClamavException) AntivirusJobPaperProtocolFileResponseEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobPaperProtocolFileResponseEvent) Logger(org.slf4j.Logger) AntivirusJobFileType(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobFileType) AntivirusJobAnalysisFileResponseEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobAnalysisFileResponseEvent) EventListener(org.springframework.context.event.EventListener) IOException(java.io.IOException) AntivirusJobPaperPaperFileResponseEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobPaperPaperFileResponseEvent) AntivirusJobResponseEventBase(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobResponseEventBase) ClamavClient(xyz.capybara.clamav.ClamavClient) AntivirusJob(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob) RetryCallback(org.springframework.retry.RetryCallback) RetryTemplate(org.springframework.retry.support.RetryTemplate) InputStream(java.io.InputStream) AntivirusJob(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob) ScanResult(xyz.capybara.clamav.commands.scan.result.ScanResult) AntivirusJobFileType(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobFileType) AntivirusStatus(com.odysseusinc.arachne.portal.model.AntivirusStatus) ClamavException(xyz.capybara.clamav.ClamavException) IOException(java.io.IOException) ClamavException(xyz.capybara.clamav.ClamavException) Async(org.springframework.scheduling.annotation.Async) EventListener(org.springframework.context.event.EventListener)

Aggregations

AntivirusJob (com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob)5 AntivirusJobEvent (com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent)5 IOException (java.io.IOException)4 IORuntimeException (com.odysseusinc.arachne.portal.exception.IORuntimeException)3 Date (java.util.Date)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 AnalysisFile (com.odysseusinc.arachne.portal.model.AnalysisFile)2 Study (com.odysseusinc.arachne.portal.model.Study)2 AntivirusJobFileType (com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobFileType)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 Path (java.nio.file.Path)2 AbstractPaperFile (com.odysseusinc.arachne.portal.model.AbstractPaperFile)1 Analysis (com.odysseusinc.arachne.portal.model.Analysis)1 AntivirusStatus (com.odysseusinc.arachne.portal.model.AntivirusStatus)1 FavouriteStudy (com.odysseusinc.arachne.portal.model.FavouriteStudy)1 Paper (com.odysseusinc.arachne.portal.model.Paper)1 StudyFile (com.odysseusinc.arachne.portal.model.StudyFile)1 UserStudy (com.odysseusinc.arachne.portal.model.UserStudy)1 AntivirusJobAnalysisFileResponseEvent (com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobAnalysisFileResponseEvent)1