2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2009 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.updatecenter.server;
22 import org.apache.commons.io.FileUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.http.HttpEntity;
25 import org.apache.http.HttpResponse;
26 import org.apache.http.auth.AuthScope;
27 import org.apache.http.auth.UsernamePasswordCredentials;
28 import org.apache.http.client.ResponseHandler;
29 import org.apache.http.client.methods.HttpGet;
30 import org.apache.http.impl.client.DefaultHttpClient;
31 import org.apache.http.util.EntityUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 import java.io.IOException;
38 import java.net.URISyntaxException;
40 public class HttpDownloader {
41 private static Logger LOG = LoggerFactory.getLogger(HttpDownloader.class);
43 private File outputDir;
45 public HttpDownloader(File outputDir) {
46 this.outputDir = outputDir;
49 public File download(String url, boolean force) throws IOException, URISyntaxException {
50 return download(url, force, null, null);
52 public File download(String url, boolean force, String login, String password) throws IOException, URISyntaxException {
53 FileUtils.forceMkdir(outputDir);
55 String filename = StringUtils.substringAfterLast(url, "/");
56 File output = new File(outputDir, filename);
57 if (force || !output.exists() || output.length() <= 0) {
58 downloadFile(new URI(url), output, login, password);
60 LOG.info("Already downloaded: " + url);
65 File downloadFile(URI fileURI, File toFile, String login, String password) {
66 LOG.info("Download " + fileURI + " in " + toFile);
67 DefaultHttpClient client = new DefaultHttpClient();
69 if (StringUtils.isNotBlank(login)) {
70 client.getCredentialsProvider().setCredentials(
71 new AuthScope(fileURI.getHost(), fileURI.getPort()),
72 new UsernamePasswordCredentials(login, password));
74 HttpGet httpget = new HttpGet(fileURI);
75 byte[] data = client.execute(httpget, new ByteResponseHandler());
77 FileUtils.writeByteArrayToFile(toFile, data);
80 } catch (Exception e) {
81 LOG.error("Fail to download " + fileURI + " to " + toFile, e);
82 FileUtils.deleteQuietly(toFile);
85 client.getConnectionManager().shutdown();
90 static class ByteResponseHandler implements ResponseHandler<byte[]> {
91 public byte[] handleResponse(HttpResponse response) throws IOException {
92 HttpEntity entity = response.getEntity();
93 if (response.getStatusLine().getStatusCode()!=200) {
94 throw new RuntimeException("Unvalid HTTP response: " + response.getStatusLine());
97 return EntityUtils.toByteArray(entity);