Search in sources :

Example 1 with DownloadRequest

use of com.tvd12.ezyhttp.client.request.DownloadRequest 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 DownloadRequest

use of com.tvd12.ezyhttp.client.request.DownloadRequest in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method downloadToOutputStreamByRequestTest.

@Test
public void downloadToOutputStreamByRequestTest() 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);
    // then
    Asserts.assertTrue(new File("test-output/no-commit/download-test.xml").exists());
    sut.close();
    sut.stop();
}
Also used : 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 3 with DownloadRequest

use of com.tvd12.ezyhttp.client.request.DownloadRequest in project ezyhttp by youngmonkeys.

the class DownloadWithTokenTest method main.

public static void main(String[] args) throws Exception {
    // given
    String fileUrl = "http://localhost:8083/api/v1/files/ansible.jpg";
    DownloadRequest request = new DownloadRequest().setFileURL(fileUrl).setConnectTimeout(5000).setReadTimeout(5000).setHeaders(MultiValueMap.builder().setValue("token", "").build());
    HttpClientProxy sut = HttpClientProxy.builder().requestQueueCapacity(1).threadPoolSize(1).build();
    // when
    String fileName = sut.download(request, new File("test-output/no-commit"));
    System.out.println(fileName);
    // then
    sut.close();
    sut.stop();
}
Also used : DownloadRequest(com.tvd12.ezyhttp.client.request.DownloadRequest) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) File(java.io.File)

Example 4 with DownloadRequest

use of com.tvd12.ezyhttp.client.request.DownloadRequest in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method downloadToFileByRequestTest.

@Test
public void downloadToFileByRequestTest() 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"));
    // 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 : 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 5 with DownloadRequest

use of com.tvd12.ezyhttp.client.request.DownloadRequest in project ezyhttp by youngmonkeys.

the class HttpClient method decorateConnection.

private void decorateConnection(HttpURLConnection connection, DownloadRequest request) {
    int connectTimeout = request.getReadTimeout();
    int readTimeout = request.getReadTimeout();
    connection.setConnectTimeout(connectTimeout > 0 ? connectTimeout : defaultConnectTimeout);
    connection.setReadTimeout(readTimeout > 0 ? readTimeout : defaultReadTimeout);
    MultiValueMap requestHeaders = request.getHeaders();
    if (requestHeaders != null) {
        Map<String, String> encodedHeaders = requestHeaders.toMap();
        for (Entry<String, String> requestHeader : encodedHeaders.entrySet()) {
            connection.setRequestProperty(requestHeader.getKey(), requestHeader.getValue());
        }
    }
}
Also used : MultiValueMap(com.tvd12.ezyhttp.core.data.MultiValueMap)

Aggregations

HttpClientProxy (com.tvd12.ezyhttp.client.HttpClientProxy)5 File (java.io.File)5 Test (org.testng.annotations.Test)5 BaseTest (com.tvd12.test.base.BaseTest)4 BeforeTest (org.testng.annotations.BeforeTest)4 DownloadCancellationToken (com.tvd12.ezyhttp.client.concurrent.DownloadCancellationToken)2 DownloadRequest (com.tvd12.ezyhttp.client.request.DownloadRequest)2 MultiValueMap (com.tvd12.ezyhttp.core.data.MultiValueMap)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 URL (java.net.URL)1