Search in sources :

Example 1 with ForestNetworkException

use of com.dtflys.forest.exceptions.ForestNetworkException in project forest by dromara.

the class SyncHttpclientRequestSender method sendRequest.

@Override
public void sendRequest(ForestRequest request, AbstractHttpExecutor executor, HttpclientResponseHandler responseHandler, HttpUriRequest httpRequest, LifeCycleHandler lifeCycleHandler, CookieStore cookieStore, Date startDate) {
    HttpResponse httpResponse = null;
    ForestResponse response = null;
    HttpContext httpContext = new BasicHttpContext();
    httpContext.setAttribute("REQUEST", request);
    HttpClientContext httpClientContext = HttpClientContext.adapt(httpContext);
    client = getHttpClient(cookieStore);
    ForestResponseFactory forestResponseFactory = new HttpclientForestResponseFactory();
    try {
        logRequest(request.getCurrentRetryCount(), (HttpRequestBase) httpRequest);
        httpResponse = client.execute(httpRequest, httpClientContext);
    } catch (Throwable e) {
        httpRequest.abort();
        response = forestResponseFactory.createResponse(request, httpResponse, lifeCycleHandler, e, startDate);
        ForestRetryException retryException = new ForestRetryException(e, request, request.getRetryCount(), request.getCurrentRetryCount());
        try {
            request.canRetry(response, retryException);
        } catch (Throwable throwable) {
            response = forestResponseFactory.createResponse(request, httpResponse, lifeCycleHandler, throwable, startDate);
            lifeCycleHandler.handleSyncWithException(request, response, e);
            return;
        }
        response = forestResponseFactory.createResponse(request, httpResponse, lifeCycleHandler, null, startDate);
        logResponse(response);
        executor.execute(lifeCycleHandler);
        return;
    } finally {
        if (response == null) {
            response = forestResponseFactory.createResponse(request, httpResponse, lifeCycleHandler, null, startDate);
        }
        logResponse(response);
    }
    response = forestResponseFactory.createResponse(request, httpResponse, lifeCycleHandler, null, startDate);
    // 检查是否重试
    ForestRetryException retryEx = request.canRetry(response);
    if (retryEx != null && retryEx.isNeedRetry() && !retryEx.isMaxRetryCountReached()) {
        executor.execute(lifeCycleHandler);
        return;
    }
    // 检查响应是否失败
    if (response.isError()) {
        ForestNetworkException networkException = new ForestNetworkException("", response.getStatusCode(), response);
        ForestRetryException retryException = new ForestRetryException(networkException, request, request.getRetryCount(), request.getCurrentRetryCount());
        try {
            request.canRetry(response, retryException);
        } catch (Throwable throwable) {
            responseHandler.handleSync(httpResponse, response);
            return;
        }
        executor.execute(lifeCycleHandler);
        return;
    }
    try {
        lifeCycleHandler.handleSaveCookie(request, getCookiesFromHttpCookieStore(cookieStore));
        responseHandler.handleSync(httpResponse, response);
    } catch (Exception ex) {
        if (ex instanceof ForestRuntimeException) {
            throw ex;
        } else {
            throw new ForestRuntimeException(ex);
        }
    }
}
Also used : ForestResponse(com.dtflys.forest.http.ForestResponse) HttpclientForestResponseFactory(com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory) ForestNetworkException(com.dtflys.forest.exceptions.ForestNetworkException) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) ForestResponseFactory(com.dtflys.forest.http.ForestResponseFactory) HttpclientForestResponseFactory(com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory) ForestRetryException(com.dtflys.forest.exceptions.ForestRetryException) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestRetryException(com.dtflys.forest.exceptions.ForestRetryException) ForestNetworkException(com.dtflys.forest.exceptions.ForestNetworkException) IOException(java.io.IOException)

Example 2 with ForestNetworkException

use of com.dtflys.forest.exceptions.ForestNetworkException in project forest by dromara.

the class OkHttp3Executor method retryOrDoError.

private void retryOrDoError(ForestResponse response, Response okResponse, OkHttp3ResponseFuture future, LifeCycleHandler lifeCycleHandler, int retryCount) {
    ForestNetworkException networkException = new ForestNetworkException(okResponse.message(), okResponse.code(), response);
    ForestRetryException retryException = new ForestRetryException(networkException, request, request.getRetryCount(), retryCount);
    try {
        request.canRetry(response, retryException);
    } catch (Throwable throwable) {
        if (future != null) {
            future.failed(new ForestNetworkException(okResponse.message(), okResponse.code(), response));
        }
        logResponse(response);
        okHttp3ResponseHandler.handleSync(okResponse, response);
        return;
    }
    execute(lifeCycleHandler, retryCount + 1);
}
Also used : ForestNetworkException(com.dtflys.forest.exceptions.ForestNetworkException) ForestRetryException(com.dtflys.forest.exceptions.ForestRetryException)

Example 3 with ForestNetworkException

use of com.dtflys.forest.exceptions.ForestNetworkException in project forest by dromara.

the class TestAsyncGetClient method testAsyncVarParamGetError_404.

@Test
public void testAsyncVarParamGetError_404() throws InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(404).setBody(EXPECTED));
    final AtomicBoolean success = new AtomicBoolean(false);
    final AtomicBoolean error = new AtomicBoolean(false);
    CountDownLatch latch = new CountDownLatch(1);
    Future<String> future = getClient.asyncVarParamGet("error param", (data, request, response) -> {
        error.set(false);
        success.set(true);
        latch.countDown();
    }, (ex, request, response) -> {
        error.set(true);
        success.set(false);
        assertTrue(ex instanceof ForestNetworkException);
        int statusCode = ((ForestNetworkException) ex).getStatusCode();
        log.error("status code = " + statusCode);
        assertEquals(404, statusCode);
        latch.countDown();
    });
    log.info("send async get request");
    assertFalse(error.get());
    assertThat(future).isNotNull();
    latch.await(5, TimeUnit.SECONDS);
    assertFalse(success.get());
    assertTrue(error.get());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ForestNetworkException(com.dtflys.forest.exceptions.ForestNetworkException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with ForestNetworkException

use of com.dtflys.forest.exceptions.ForestNetworkException in project forest by dromara.

the class TestAsyncGetClient method testAsyncVarParamGetError_400.

@Test
public void testAsyncVarParamGetError_400() throws InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(400).setBody(EXPECTED));
    final AtomicBoolean success = new AtomicBoolean(false);
    final AtomicBoolean error = new AtomicBoolean(false);
    CountDownLatch latch = new CountDownLatch(1);
    Future<String> future = getClient.asyncVarParamGet("error param", (data, request, response) -> {
        error.set(false);
        success.set(true);
        latch.countDown();
    }, (ex, request, response) -> {
        error.set(true);
        success.set(false);
        assertTrue(ex instanceof ForestNetworkException);
        int statusCode = ((ForestNetworkException) ex).getStatusCode();
        log.error("status code = " + statusCode);
        assertEquals(400, statusCode);
        latch.countDown();
    });
    log.info("send async get request");
    assertFalse(error.get());
    assertThat(future).isNotNull();
    latch.await(5, TimeUnit.SECONDS);
    assertFalse(success.get());
    assertTrue(error.get());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ForestNetworkException(com.dtflys.forest.exceptions.ForestNetworkException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with ForestNetworkException

use of com.dtflys.forest.exceptions.ForestNetworkException in project forest by dromara.

the class TestErrorClient method testErrorGet3.

@Test
public void testErrorGet3() {
    server.enqueue(new MockResponse().setResponseCode(500).setBody(EXPECTED));
    boolean hasError = false;
    try {
        getClient.errorGet3();
    } catch (ForestNetworkException ex) {
        ex.printStackTrace();
        hasError = true;
        assertThat(ex.getStatusCode()).isEqualTo(500);
        assertThat(ex.getResponse()).isNotNull().extracting(ForestResponse::getContent).isEqualTo(EXPECTED);
    }
    assertThat(hasError).isTrue();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ForestResponse(com.dtflys.forest.http.ForestResponse) ForestNetworkException(com.dtflys.forest.exceptions.ForestNetworkException) Test(org.junit.Test)

Aggregations

ForestNetworkException (com.dtflys.forest.exceptions.ForestNetworkException)5 MockResponse (okhttp3.mockwebserver.MockResponse)3 Test (org.junit.Test)3 ForestRetryException (com.dtflys.forest.exceptions.ForestRetryException)2 ForestResponse (com.dtflys.forest.http.ForestResponse)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 HttpclientForestResponseFactory (com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory)1 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)1 ForestResponseFactory (com.dtflys.forest.http.ForestResponseFactory)1 IOException (java.io.IOException)1 HttpResponse (org.apache.http.HttpResponse)1 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)1 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)1 HttpContext (org.apache.http.protocol.HttpContext)1