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.FileUtil;
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 abstract class BaseFileAntivirusScanner implements AntivirusScanner {
028    
029            public boolean isActive() {
030                    return _ACTIVE;
031            }
032    
033            public void scan(byte[] bytes)
034                    throws AntivirusScannerException, SystemException {
035    
036                    File file = null;
037    
038                    try {
039                            file = FileUtil.createTempFile(_ANTIVIRUS_EXTENSION);
040    
041                            FileUtil.write(file, bytes);
042    
043                            scan(file);
044                    }
045                    catch (IOException ioe) {
046                            throw new SystemException("Unable to write temporary file", ioe);
047                    }
048                    finally {
049                            if (file != null) {
050                                    file.delete();
051                            }
052                    }
053            }
054    
055            public void scan(InputStream inputStream)
056                    throws AntivirusScannerException, SystemException {
057    
058                    File file = null;
059    
060                    try {
061                            file = FileUtil.createTempFile(_ANTIVIRUS_EXTENSION);
062    
063                            FileUtil.write(file, inputStream);
064    
065                            scan(file);
066                    }
067                    catch (IOException ioe) {
068                            throw new SystemException("Unable to write temporary file", ioe);
069                    }
070                    finally {
071                            if (file != null) {
072                                    file.delete();
073                            }
074                    }
075            }
076    
077            private static final boolean _ACTIVE = true;
078    
079            private static final String _ANTIVIRUS_EXTENSION = "avs";
080    
081    }