Search in sources :

Example 61 with Request

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

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

Example 63 with Request

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

the class HttpClientProxy method execute.

public void execute(Request request, RequestCallback<ResponseEntity> callback) {
    EzyFuture future = new RequestFutureTask(callback);
    futures.addFuture(request, future);
    try {
        addRequest(request);
    } catch (Exception e) {
        futures.removeFuture(request);
        throw e;
    }
}
Also used : EzyFuture(com.tvd12.ezyfox.concurrent.EzyFuture) RequestFutureTask(com.tvd12.ezyhttp.client.concurrent.RequestFutureTask) EzyProcessor.processWithException(com.tvd12.ezyfox.util.EzyProcessor.processWithException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) IOException(java.io.IOException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException)

Example 64 with Request

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

the class HttpClientProxy method handleRequests.

protected void handleRequests() {
    Request request = null;
    EzyFuture future = null;
    Exception exception = null;
    ResponseEntity response = null;
    try {
        request = requestQueue.take();
        future = futures.removeFuture(request);
        response = client.request(request);
    } catch (Exception e) {
        exception = e;
    }
    try {
        if (future != null) {
            if (exception != null) {
                future.setException(exception);
            } else {
                future.setResult(response);
            }
        } else {
            if (exception != null) {
                logger.info("handled request: {} with exception, but there is no future", request, exception);
            } else {
                logger.info("handled request: {} with response: {}, but there is no future", request, response);
            }
        }
    } catch (Throwable e) {
        logger.info("handle request result error", e);
    }
}
Also used : ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) Request(com.tvd12.ezyhttp.client.request.Request) DownloadRequest(com.tvd12.ezyhttp.client.request.DownloadRequest) EzyFuture(com.tvd12.ezyfox.concurrent.EzyFuture) EzyProcessor.processWithException(com.tvd12.ezyfox.util.EzyProcessor.processWithException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) IOException(java.io.IOException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException)

Example 65 with Request

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

the class HttpClientTest method postMethodPaymentRequired.

@Test
public void postMethodPaymentRequired() {
    // given
    HttpClient sut = HttpClient.builder().build();
    PostRequest request = new PostRequest().setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL(URI.create("http://127.0.0.1:18081/402"));
    // when
    Throwable e = Asserts.assertThrows(() -> sut.call(request));
    // then
    Asserts.assertThat(e).isEqualsType(HttpPaymentRequiredException.class);
}
Also used : PostRequest(com.tvd12.ezyhttp.client.request.PostRequest) HttpClient(com.tvd12.ezyhttp.client.HttpClient) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

Test (org.testng.annotations.Test)128 HttpServletResponse (javax.servlet.http.HttpServletResponse)48 HttpServletRequest (javax.servlet.http.HttpServletRequest)45 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)37 BeforeTest (org.testng.annotations.BeforeTest)36 BaseTest (com.tvd12.test.base.BaseTest)31 EzyArray (com.tvd12.ezyfox.entity.EzyArray)30 ComponentManager (com.tvd12.ezyhttp.server.core.manager.ComponentManager)29 BlockingServlet (com.tvd12.ezyhttp.server.core.servlet.BlockingServlet)29 ToString (lombok.ToString)29 RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)27 ServletOutputStream (javax.servlet.ServletOutputStream)25 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)24 RequestHandlerManager (com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager)24 Cookie (javax.servlet.http.Cookie)24 HttpClientProxy (com.tvd12.ezyhttp.client.HttpClientProxy)22 RequestCookie (com.tvd12.ezyhttp.server.core.annotation.RequestCookie)22 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)21 HttpClient (com.tvd12.ezyhttp.client.HttpClient)21 PostRequest (com.tvd12.ezyhttp.client.request.PostRequest)21