001
014
015 package com.liferay.portlet.documentlibrary.antivirus;
016
017 import com.liferay.portal.kernel.exception.SystemException;
018 import com.liferay.portal.kernel.util.StringUtil;
019
020 import java.io.File;
021 import java.io.IOException;
022 import java.io.InputStream;
023
024
027 public class ClamAntivirusScannerImpl extends BaseFileAntivirusScanner {
028
029 public void scan(File file)
030 throws AntivirusScannerException, SystemException {
031
032 Runtime runtime = Runtime.getRuntime();
033
034 String filePath = file.getAbsolutePath();
035
036 String[] parameters = new String[] {
037 "clamscan", "--stdout", "--no-summary", filePath };
038
039 Process process = null;
040
041 try {
042 process = runtime.exec(parameters);
043
044 InputStream inputStream = process.getInputStream();
045
046 String scanResult = StringUtil.read(inputStream);
047
048 process.waitFor();
049
050 int exitValue = process.exitValue();
051
052 if (exitValue != 0) {
053 throw new AntivirusScannerException(
054 "Unable to scan file due to inability to execute " +
055 "antivirus process");
056 }
057
058 if (scanResult.contains("FOUND")) {
059 throw new AntivirusScannerException(
060 "Virus detected in " + filePath);
061 }
062 }
063 catch (IOException ioe) {
064 throw new SystemException("Unable to scan file", ioe);
065 }
066 catch (InterruptedException ie) {
067 throw new SystemException("Unable to scan file", ie);
068 }
069 finally {
070 if (process != null) {
071 process.destroy();
072 }
073 }
074 }
075
076 }