001    /**
002     * Copyright (c) 2000-2013 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    
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    }