use of com.tvd12.ezyhttp.client.concurrent.DownloadCancellationToken in project ezyhttp by youngmonkeys.
the class HttpClientProxyTest method downloadToFileButCancelTest.
@Test
public void downloadToFileButCancelTest() {
// given
String fileUrl = "https://resources.tvd12.com/ezy-settings-1.0.0.xsd";
HttpClientProxy sut = HttpClientProxy.builder().requestQueueCapacity(1).threadPoolSize(1).build();
// when
DownloadCancellationToken cancellationToken = new DownloadCancellationToken();
cancellationToken.cancel();
Throwable e = Asserts.assertThrows(() -> sut.download(fileUrl, new File("test-output/no-commit"), cancellationToken));
// then
Asserts.assertEqualsType(e, DownloadCancelledException.class);
sut.close();
sut.stop();
}
use of com.tvd12.ezyhttp.client.concurrent.DownloadCancellationToken in project ezyhttp by youngmonkeys.
the class HttpClientProxyTest method downloadToFileByRequestWithCancellationTokenTest.
@Test
public void downloadToFileByRequestWithCancellationTokenTest() throws Exception {
// given
String fileUrl = "https://resources.tvd12.com/ezy-settings-1.0.0.xsd";
DownloadRequest request = new DownloadRequest().setFileURL(fileUrl).setConnectTimeout(5000).setReadTimeout(5000).setHeaders(MultiValueMap.builder().setValue("hello", "world").build());
HttpClientProxy sut = HttpClientProxy.builder().requestQueueCapacity(1).threadPoolSize(1).build();
// when
String fileName = sut.download(request, new File("test-output/no-commit"), new DownloadCancellationToken());
// then
Asserts.assertEquals(fileName, "ezy-settings-1.0.0.xsd");
Asserts.assertTrue(new File("test-output/no-commit/ezy-settings-1.0.0.xsd").exists());
sut.close();
sut.stop();
}
use of com.tvd12.ezyhttp.client.concurrent.DownloadCancellationToken in project ezyhttp by youngmonkeys.
the class HttpClientProxyTest method downloadToOutputStreamWithCancellationTokenTest.
@Test
public void downloadToOutputStreamWithCancellationTokenTest() throws Exception {
// given
String fileUrl = "https://resources.tvd12.com/ezy-settings-1.0.0.xsd";
HttpClientProxy sut = HttpClientProxy.builder().requestQueueCapacity(1).threadPoolSize(1).build();
File outFile = new File("test-output/no-commit/download-test.xml");
EzyFileUtil.createFileIfNotExists(outFile);
OutputStream outputStream = new FileOutputStream(outFile);
// when
sut.download(fileUrl, outputStream, new DownloadCancellationToken());
// then
Asserts.assertTrue(new File("test-output/no-commit/download-test.xml").exists());
sut.close();
sut.stop();
}
use of com.tvd12.ezyhttp.client.concurrent.DownloadCancellationToken in project ezyhttp by youngmonkeys.
the class HttpClient method download.
private String download(HttpURLConnection connection, String fileURL, File storeLocation, DownloadCancellationToken cancellationToken) throws Exception {
int responseCode = connection.getResponseCode();
if (responseCode >= 400) {
throw processDownloadError(connection, fileURL, responseCode);
}
String disposition = connection.getHeaderField("Content-Disposition");
String fileName = getDownloadFileName(fileURL, disposition);
Files.createDirectories(storeLocation.toPath());
File storeFile = Paths.get(storeLocation.toString(), fileName).toFile();
File downloadingFile = new File(storeFile + ".downloading");
Path downloadingFilePath = downloadingFile.toPath();
Files.deleteIfExists(downloadingFilePath);
Files.createFile(downloadingFilePath);
try (InputStream inputStream = connection.getInputStream()) {
try (FileOutputStream outputStream = new FileOutputStream(downloadingFile)) {
int bytesRead;
byte[] buffer = new byte[1024];
while ((bytesRead = inputStream.read(buffer)) != -1) {
if (cancellationToken.isCancelled()) {
break;
}
outputStream.write(buffer, 0, bytesRead);
}
}
}
if (cancellationToken.isCancelled()) {
Files.deleteIfExists(downloadingFilePath);
throw new DownloadCancelledException(fileURL);
}
Files.move(downloadingFile.toPath(), storeFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
return fileName;
}
Aggregations