001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
025     * @author Michael C. Han
026     */
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    }