001    /*
002     * #%L
003     * License Maven Plugin
004     * %%
005     * Copyright (C) 2010 - 2011 CodeLutin, Codehaus, Tony Chemit
006     * %%
007     * This program is free software: you can redistribute it and/or modify
008     * it under the terms of the GNU Lesser General Public License as 
009     * published by the Free Software Foundation, either version 3 of the 
010     * License, or (at your option) any later version.
011     * 
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Lesser Public License for more details.
016     * 
017     * You should have received a copy of the GNU General Lesser Public 
018     * License along with this program.  If not, see
019     * <http://www.gnu.org/licenses/lgpl-3.0.html>.
020     * #L%
021     */
022    package org.codehaus.mojo.license;
023    
024    import java.io.*;
025    import java.net.URL;
026    import java.net.URLConnection;
027    
028    /**
029     * Utilities for downloading remote license files.
030     *
031     * @author pgier
032     * @since 1.0
033     */
034    public class LicenseDownloader
035    {
036    
037        /**
038         * Defines the connection timeout in milliseconds when attempting to download license files.
039         */
040        public static final int DEFAULT_CONNECTION_TIMEOUT = 5000;
041    
042        public static void downloadLicense( String licenseUrlString, File outputFile )
043            throws IOException
044        {
045            InputStream licenseInputStream = null;
046            FileOutputStream fos = null;
047    
048            try
049            {
050                URL licenseUrl = new URL( licenseUrlString );
051                URLConnection connection = licenseUrl.openConnection();
052                connection.setConnectTimeout( DEFAULT_CONNECTION_TIMEOUT );
053                connection.setReadTimeout( DEFAULT_CONNECTION_TIMEOUT );
054                licenseInputStream = connection.getInputStream();
055                fos = new FileOutputStream( outputFile );
056                copyStream( licenseInputStream, fos );
057                licenseInputStream.close();
058                fos.close();
059            }
060            finally
061            {
062                FileUtil.tryClose( licenseInputStream );
063                FileUtil.tryClose( fos );
064            }
065    
066        }
067    
068        /**
069         * Copy data from one stream to another.
070         *
071         * @param inStream
072         * @param outStream
073         * @throws IOException
074         */
075        private static void copyStream( InputStream inStream, OutputStream outStream )
076            throws IOException
077        {
078            byte[] buf = new byte[1024];
079            int len;
080            while ( ( len = inStream.read( buf ) ) > 0 )
081            {
082                outStream.write( buf, 0, len );
083            }
084        }
085    
086    }