diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/downloader/ListDownloader.java b/LauncherAPI/src/main/java/ru/gravit/launcher/downloader/ListDownloader.java new file mode 100644 index 00000000..b1a54412 --- /dev/null +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/downloader/ListDownloader.java @@ -0,0 +1,52 @@ +package ru.gravit.launcher.downloader; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Path; +import java.util.List; + +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.client.LaxRedirectStrategy; + +import ru.gravit.utils.helper.IOHelper; + +public class ListDownloader { + public void download(String base, List applies, Path dstDirFile) throws IOException, URISyntaxException { + try(CloseableHttpClient httpclient = HttpClients.custom() + .setRedirectStrategy(new LaxRedirectStrategy()) + .build()) { + + HttpGet get = null; + for (String apply : applies) { + URI u = new URL(base.concat(apply)).toURI(); + if (get == null) get = new HttpGet(u); + else { + get.reset(); + get.setURI(u); + } + httpclient.execute(get, new FileDownloadResponseHandler(dstDirFile.resolve(apply))); + } + } + } + + static class FileDownloadResponseHandler implements ResponseHandler { + private final Path target; + public FileDownloadResponseHandler(Path target) { + this.target = target; + } + @Override + public Path handleResponse(HttpResponse response) throws ClientProtocolException, IOException { + InputStream source = response.getEntity().getContent(); + IOHelper.transfer(source, this.target); + return this.target; + } + } +} \ No newline at end of file