001    /**
002     * Copyright (c) 2000-present 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            @Override
030            public boolean isActive() {
031                    return _ACTIVE;
032            }
033    
034            @Override
035            public void scan(byte[] bytes) throws AntivirusScannerException {
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            @Override
056            public void scan(InputStream inputStream) throws AntivirusScannerException {
057                    File file = null;
058    
059                    try {
060                            file = FileUtil.createTempFile(_ANTIVIRUS_EXTENSION);
061    
062                            FileUtil.write(file, inputStream);
063    
064                            scan(file);
065                    }
066                    catch (IOException ioe) {
067                            throw new SystemException("Unable to write temporary file", ioe);
068                    }
069                    finally {
070                            if (file != null) {
071                                    file.delete();
072                            }
073                    }
074            }
075    
076            private static final boolean _ACTIVE = true;
077    
078            private static final String _ANTIVIRUS_EXTENSION = "avs";
079    
080    }