Search in sources :

Example 6 with DownloadCancellationToken

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();
}
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 7 with DownloadCancellationToken

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();
}
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 8 with DownloadCancellationToken

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();
}
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 9 with DownloadCancellationToken

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

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