001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.documentlibrary.antivirus;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    
019    import java.io.File;
020    import java.io.IOException;
021    
022    /**
023     * @author Michael C. Han
024     */
025    public class ClamAntivirusScannerImpl extends BaseFileAntivirusScanner {
026    
027            @Override
028            public void scan(File file)
029                    throws AntivirusScannerException, SystemException {
030    
031                    ProcessBuilder processBuilder = new ProcessBuilder(
032                            "clamscan", "--stdout", "--no-summary", file.getAbsolutePath());
033    
034                    processBuilder.redirectErrorStream(true);
035    
036                    Process process = null;
037    
038                    try {
039                            process = processBuilder.start();
040    
041                            process.waitFor();
042    
043                            int exitValue = process.exitValue();
044    
045                            if (exitValue == 1) {
046                                    throw new AntivirusScannerException(
047                                            "Virus detected in " + file.getAbsolutePath());
048                            }
049                            else if (exitValue >= 2) {
050                                    throw new AntivirusScannerException(
051                                            "Unable to scan file due to inability to execute " +
052                                                    "antivirus process");
053                            }
054                    }
055                    catch (IOException ioe) {
056                            throw new SystemException("Unable to scan file", ioe);
057                    }
058                    catch (InterruptedException ie) {
059                            throw new SystemException("Unable to scan file", ie);
060                    }
061                    finally {
062                            if (process != null) {
063                                    process.destroy();
064                            }
065                    }
066            }
067    
068    }