Search in sources :

Example 6 with AntivirusJob

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

the class BaseAnalysisServiceImpl method writeContentAndCheckForViruses.

private void writeContentAndCheckForViruses(final AnalysisFile analysisFile, final IUser updatedBy, final String content) throws IOException {
    try {
        final Analysis analysis = analysisFile.getAnalysis();
        Path analysisFolder = analysisHelper.getAnalysisFolder(analysis);
        if (Files.notExists(analysisFolder)) {
            Files.createDirectories(analysisFolder);
        }
        Path targetPath = analysisFolder.resolve(analysisFile.getUuid());
        byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
        try (final InputStream stream = new ByteArrayInputStream(bytes)) {
            Files.copy(stream, targetPath, REPLACE_EXISTING);
        }
        analysisFile.setUpdated(new Date());
        analysisFile.setEntryPoint(analysisFile.getEntryPoint());
        analysisFile.setUpdatedBy(updatedBy);
        analysisFile.setContentType(CommonFileUtils.getContentType(analysisFile.getName(), targetPath.toString()));
        analysisFile.incrementVersion();
        analysisFile.setAntivirusStatus(AntivirusStatus.SCANNING);
        analysisFile.setAntivirusDescription(null);
        eventPublisher.publishEvent(new AntivirusJobEvent(this, new AntivirusJob(analysisFile.getId(), analysisFile.getName(), 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) AntivirusJob(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Date(java.util.Date) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Analysis(com.odysseusinc.arachne.portal.model.Analysis) AntivirusJobEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent)

Example 7 with AntivirusJob

use of com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob 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 (InputStream content = antivirusJob.getContent()) {
        clamavClientAvailabilityCheck();
        final ScanResult scan = retryTemplate.execute((RetryCallback<ScanResult, Exception>) retryContext -> {
            logger.debug(PROCESSING_SCAN_ATTEMPT, fileId, fileType);
            return scan(content);
        });
        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 : CommunicationException(xyz.capybara.clamav.CommunicationException) 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) 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) InputStream(java.io.InputStream) AntivirusStatus(com.odysseusinc.arachne.portal.model.AntivirusStatus) CommunicationException(xyz.capybara.clamav.CommunicationException) ClamavException(xyz.capybara.clamav.ClamavException) 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)7 AntivirusJobEvent (com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent)7 IOException (java.io.IOException)5 IORuntimeException (com.odysseusinc.arachne.portal.exception.IORuntimeException)4 FileInputStream (java.io.FileInputStream)4 Path (java.nio.file.Path)4 Date (java.util.Date)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 AnalysisFile (com.odysseusinc.arachne.portal.model.AnalysisFile)3 InputStream (java.io.InputStream)3 Analysis (com.odysseusinc.arachne.portal.model.Analysis)2 Study (com.odysseusinc.arachne.portal.model.Study)2 AntivirusJobFileType (com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobFileType)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArachneSystemRuntimeException (com.odysseusinc.arachne.portal.exception.ArachneSystemRuntimeException)1 ValidationRuntimeException (com.odysseusinc.arachne.portal.exception.ValidationRuntimeException)1 AbstractPaperFile (com.odysseusinc.arachne.portal.model.AbstractPaperFile)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