use of com.tvd12.ezyhttp.client.concurrent.DownloadCancellationToken in project ezyhttp by youngmonkeys.
the class HttpClientProxyTest method downloadToOutputStreamByRequestWithCancellationTokenTest.
@Test
public void downloadToOutputStreamByRequestWithCancellationTokenTest() 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();
File outFile = new File("test-output/no-commit/download-test.xml");
EzyFileUtil.createFileIfNotExists(outFile);
OutputStream outputStream = new FileOutputStream(outFile);
// when
sut.download(request, 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 HttpClientProxyTest method downloadToFileWithCancellationTokenTest.
@Test
public void downloadToFileWithCancellationTokenTest() throws Exception {
// given
String fileUrl = "https://resources.tvd12.com/ezy-settings-1.0.0.xsd";
HttpClientProxy sut = HttpClientProxy.builder().requestQueueCapacity(1).threadPoolSize(1).build();
// when
String fileName = sut.download(fileUrl, 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 downloadToOutputStreamButCancelTest.
@Test
public void downloadToOutputStreamButCancelTest() 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);
DownloadCancellationToken cancelledException = new DownloadCancellationToken();
cancelledException.cancel();
// when
Throwable e = Asserts.assertThrows(() -> sut.download(fileUrl, outputStream, cancelledException));
// 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 HttpClient method download.
private void download(HttpURLConnection connection, String fileURL, OutputStream outputStream, DownloadCancellationToken cancellationToken) throws Exception {
int responseCode = connection.getResponseCode();
if (responseCode >= 400) {
throw processDownloadError(connection, fileURL, responseCode);
}
try (InputStream inputStream = connection.getInputStream()) {
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()) {
throw new DownloadCancelledException(fileURL);
}
}
use of com.tvd12.ezyhttp.client.concurrent.DownloadCancellationToken in project ezyhttp by youngmonkeys.
the class DownloadCancellationTokenTest method cancelTest.
@Test
public void cancelTest() {
// given
DownloadCancellationToken sut = new DownloadCancellationToken();
// when
sut.cancel();
// then
Asserts.assertTrue(sut.isCancelled());
}
Aggregations