001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.api.utils;
021    
022    import org.apache.commons.io.IOUtils;
023    import org.slf4j.LoggerFactory;
024    import org.sonar.api.BatchComponent;
025    import org.sonar.api.ServerComponent;
026    import org.sonar.api.platform.Server;
027    
028    import java.io.File;
029    import java.io.FileOutputStream;
030    import java.io.IOException;
031    import java.io.InputStream;
032    import java.net.HttpURLConnection;
033    import java.net.URI;
034    
035    /**
036     * Simple class to download a file from a HTTP repository.
037     *
038     * @since 2.2
039     */
040    public class HttpDownloader implements BatchComponent, ServerComponent {
041    
042      public static final int TIMEOUT_MILLISECONDS = 20 * 1000;
043    
044      private Server server = null;
045    
046      public HttpDownloader(Server server) {
047        this.server = server;
048      }
049    
050      public HttpDownloader() {
051      }
052    
053      public void download(URI uri, File toFile) {
054        InputStream input = null;
055        FileOutputStream output = null;
056        try {
057          HttpURLConnection connection = newHttpConnection(uri);
058          output = new FileOutputStream(toFile, false);
059          input = connection.getInputStream();
060          IOUtils.copy(input, output);
061    
062        } catch (Exception e) {
063          throw new SonarException("Fail to download the file: " + uri, e);
064          
065        } finally {
066          IOUtils.closeQuietly(input);
067          IOUtils.closeQuietly(output);
068        }
069      }
070    
071      public byte[] download(URI uri) {
072        InputStream input = null;
073        try {
074          HttpURLConnection connection = newHttpConnection(uri);
075          input = connection.getInputStream();
076          return IOUtils.toByteArray(input);
077    
078        } catch (Exception e) {
079          throw new SonarException("Fail to download the file: " + uri, e);
080    
081        } finally {
082          IOUtils.closeQuietly(input);
083        }
084      }
085    
086      public InputStream openStream(URI uri) {
087        try {
088          HttpURLConnection connection = newHttpConnection(uri);
089          return connection.getInputStream();
090    
091        } catch (Exception e) {
092          throw new SonarException("Fail to download the file: " + uri, e);
093        }
094      }
095    
096      private HttpURLConnection newHttpConnection(URI uri) throws IOException {
097        LoggerFactory.getLogger(getClass()).info("Download: " + uri);
098        HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
099        connection.setConnectTimeout(TIMEOUT_MILLISECONDS);
100        connection.setReadTimeout(TIMEOUT_MILLISECONDS);
101        connection.setUseCaches(true);
102        connection.setRequestProperty("User-Agent", getUserAgent());
103        connection.setInstanceFollowRedirects(true);
104        return connection;
105      }
106    
107      private String getUserAgent() {
108        return (server != null ? "Sonar " + server.getVersion() : "Sonar");
109      }
110    }