001
014
015 package com.liferay.portlet.documentlibrary.antivirus;
016
017 import com.liferay.document.library.kernel.antivirus.AntivirusScannerException;
018 import com.liferay.document.library.kernel.antivirus.BaseFileAntivirusScanner;
019
020 import java.io.File;
021 import java.io.IOException;
022
023
026 public class ClamAntivirusScannerImpl extends BaseFileAntivirusScanner {
027
028 @Override
029 public void scan(File file) throws AntivirusScannerException {
030 Process process = null;
031
032 try {
033 ProcessBuilder processBuilder = new ProcessBuilder(
034 "clamscan", "--stdout", "--no-summary", file.getAbsolutePath());
035
036 processBuilder.redirectErrorStream(true);
037
038 process = processBuilder.start();
039
040 process.waitFor();
041
042 int exitValue = process.exitValue();
043
044 if (exitValue == 1) {
045 throw new AntivirusScannerException(
046 "Virus detected in " + file.getAbsolutePath(),
047 AntivirusScannerException.VIRUS_DETECTED);
048 }
049 else if (exitValue >= 2) {
050 throw new AntivirusScannerException(
051 AntivirusScannerException.PROCESS_FAILURE);
052 }
053 }
054 catch (InterruptedException | IOException e) {
055 throw new AntivirusScannerException(
056 AntivirusScannerException.PROCESS_FAILURE);
057 }
058 finally {
059 if (process != null) {
060 process.destroy();
061 }
062 }
063 }
064
065 }