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