Search in sources :

Example 1 with DownloadCancellationToken

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();
}
Also used : DownloadCancellationToken(com.tvd12.ezyhttp.client.concurrent.DownloadCancellationToken) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) File(java.io.File) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 2 with DownloadCancellationToken

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();
}
Also used : DownloadCancellationToken(com.tvd12.ezyhttp.client.concurrent.DownloadCancellationToken) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) File(java.io.File) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 3 with DownloadCancellationToken

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();
}
Also used : DownloadCancellationToken(com.tvd12.ezyhttp.client.concurrent.DownloadCancellationToken) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) File(java.io.File) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 4 with DownloadCancellationToken

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);
    }
}
Also used : InputStream(java.io.InputStream) DownloadCancelledException(com.tvd12.ezyhttp.client.exception.DownloadCancelledException)

Example 5 with DownloadCancellationToken

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());
}
Also used : DownloadCancellationToken(com.tvd12.ezyhttp.client.concurrent.DownloadCancellationToken) Test(org.testng.annotations.Test)

Aggregations

DownloadCancellationToken (com.tvd12.ezyhttp.client.concurrent.DownloadCancellationToken)7 File (java.io.File)7 Test (org.testng.annotations.Test)7 HttpClientProxy (com.tvd12.ezyhttp.client.HttpClientProxy)6 BaseTest (com.tvd12.test.base.BaseTest)6 BeforeTest (org.testng.annotations.BeforeTest)6 FileOutputStream (java.io.FileOutputStream)4 OutputStream (java.io.OutputStream)3 DownloadCancelledException (com.tvd12.ezyhttp.client.exception.DownloadCancelledException)2 InputStream (java.io.InputStream)2 Path (java.nio.file.Path)1