Search in sources :

Example 26 with ForestResponse

use of com.dtflys.forest.http.ForestResponse in project forest by dromara.

the class HttpclientForestFuture method getResult.

private T getResult(R httpResponse) throws InterruptedException {
    if (httpResponse != null && innerType.isAssignableFrom(httpResponse.getClass())) {
        return (T) httpResponse;
    }
    ForestResponse response = forestResponseFactory.createResponse(request, httpResponse, this.lifeCycleHandler, null, requestTime);
    Object ret = lifeCycleHandler.handleResultType(request, response, innerType, innerType);
    return (T) ret;
}
Also used : ForestResponse(com.dtflys.forest.http.ForestResponse)

Example 27 with ForestResponse

use of com.dtflys.forest.http.ForestResponse in project forest by dromara.

the class OkHttp3Executor method execute.

public void execute(final LifeCycleHandler lifeCycleHandler, int retryCount) {
    OkHttpClient okHttpClient = getClient(request, lifeCycleHandler);
    URLBuilder urlBuilder = URL_BUILDER;
    String url = urlBuilder.buildUrl(request);
    Request.Builder builder = new Request.Builder().url(url);
    prepareHeaders(builder);
    prepareMethodAndBody(builder, lifeCycleHandler);
    final Request okRequest = builder.build();
    Call call = okHttpClient.newCall(okRequest);
    final OkHttp3ForestResponseFactory factory = new OkHttp3ForestResponseFactory();
    logRequest(retryCount, okRequest, okHttpClient);
    Date startDate = new Date();
    Response okResponse = null;
    ForestResponse response = null;
    try {
        okResponse = call.execute();
    } catch (Throwable e) {
        response = factory.createResponse(request, null, lifeCycleHandler, e, startDate);
        ForestRetryException retryException = new ForestRetryException(e, request, request.getMaxRetryCount(), retryCount);
        try {
            request.canRetry(response, retryException);
        } catch (Throwable throwable) {
            response = factory.createResponse(request, null, lifeCycleHandler, throwable, startDate);
            logResponse(response);
            lifeCycleHandler.handleSyncWithException(request, response, throwable);
            return;
        }
        response = factory.createResponse(request, null, lifeCycleHandler, e, startDate);
        logResponse(response);
        execute(lifeCycleHandler, retryCount + 1);
        return;
    } finally {
        if (response == null) {
            response = factory.createResponse(request, okResponse, lifeCycleHandler, null, startDate);
        }
        logResponse(response);
    }
    // 是否重试
    ForestRetryException retryEx = request.canRetry(response);
    if (retryEx != null && retryEx.isNeedRetry() && !retryEx.isMaxRetryCountReached()) {
        execute(lifeCycleHandler, retryCount + 1);
        return;
    }
    // 验证响应
    if (response.isError()) {
        retryOrDoError(response, okResponse, null, lifeCycleHandler, retryCount);
        return;
    }
    okHttp3ResponseHandler.handleSync(okResponse, response);
// }
}
Also used : Response(okhttp3.Response) ForestResponse(com.dtflys.forest.http.ForestResponse) Call(okhttp3.Call) ForestResponse(com.dtflys.forest.http.ForestResponse) OkHttpClient(okhttp3.OkHttpClient) ForestRequest(com.dtflys.forest.http.ForestRequest) Request(okhttp3.Request) Date(java.util.Date) ForestRetryException(com.dtflys.forest.exceptions.ForestRetryException) URLBuilder(com.dtflys.forest.backend.url.URLBuilder) QueryableURLBuilder(com.dtflys.forest.backend.url.QueryableURLBuilder) OkHttp3ForestResponseFactory(com.dtflys.forest.backend.okhttp3.response.OkHttp3ForestResponseFactory)

Example 28 with ForestResponse

use of com.dtflys.forest.http.ForestResponse in project forest by dromara.

the class TestBaseRedirectClient method testBaseNotAutoRedirect_306.

@Test
public void testBaseNotAutoRedirect_306() {
    server.enqueue(new MockResponse().addHeader("Location", "http://localhost:" + server.getPort() + "/b").setResponseCode(306));
    server.enqueue(new MockResponse().setBody(EXPECTED));
    AtomicReference<ForestRequest> atomicReq = new AtomicReference<>(null);
    ForestResponse<String> response = baseRedirectClient.testNotAutoRedirect(((redirectReq, prevReq, prevRes) -> {
        atomicReq.set(redirectReq);
    }));
    assertThat(response).isNotNull();
    assertThat(response.getStatusCode()).isEqualTo(306);
    assertThat(response.isRedirection()).isTrue();
    assertThat(response.getRedirectionLocation()).isEqualTo("http://localhost:" + server.getPort() + "/b");
    String result = response.redirectionRequest().execute(String.class);
    assertThat(result).isNotNull().isEqualTo(EXPECTED);
    mockRequest(server).assertPathEquals("/");
    mockRequest(server).assertPathEquals("/b").assertBodyEquals("body=" + RedirectInterceptor.BODY);
}
Also used : BeforeClass(org.junit.BeforeClass) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ForestResponse(com.dtflys.forest.http.ForestResponse) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) Rule(org.junit.Rule) MockWebServer(okhttp3.mockwebserver.MockWebServer) MockResponse(okhttp3.mockwebserver.MockResponse) MockServerRequest.mockRequest(com.dtflys.forest.mock.MockServerRequest.mockRequest) AssertionsForClassTypes.assertThat(org.assertj.core.api.AssertionsForClassTypes.assertThat) HttpBackend(com.dtflys.forest.backend.HttpBackend) BaseClientTest(com.dtflys.test.http.BaseClientTest) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 29 with ForestResponse

use of com.dtflys.forest.http.ForestResponse in project forest by dromara.

the class TestBaseRedirectClient method testBaseNotAutoRedirect_301.

/**
 * ====================================================== 测试Base拦截器非自动重定向 ======================================================
 */
@Test
public void testBaseNotAutoRedirect_301() {
    server.enqueue(new MockResponse().addHeader("Location", "http://localhost:" + server.getPort() + "/b").setResponseCode(301));
    server.enqueue(new MockResponse().setBody(EXPECTED));
    AtomicReference<ForestRequest> atomicReq = new AtomicReference<>(null);
    ForestResponse<String> response = baseRedirectClient.testNotAutoRedirect(((redirectReq, prevReq, prevRes) -> {
        atomicReq.set(redirectReq);
    }));
    assertThat(response).isNotNull();
    assertThat(response.getStatusCode()).isEqualTo(301);
    assertThat(response.isRedirection()).isTrue();
    assertThat(response.getRedirectionLocation()).isEqualTo("http://localhost:" + server.getPort() + "/b");
    String result = response.redirectionRequest().execute(String.class);
    assertThat(result).isNotNull().isEqualTo(EXPECTED);
    mockRequest(server).assertPathEquals("/");
    mockRequest(server).assertPathEquals("/b").assertBodyEquals("body=" + RedirectInterceptor.BODY);
}
Also used : BeforeClass(org.junit.BeforeClass) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ForestResponse(com.dtflys.forest.http.ForestResponse) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) Rule(org.junit.Rule) MockWebServer(okhttp3.mockwebserver.MockWebServer) MockResponse(okhttp3.mockwebserver.MockResponse) MockServerRequest.mockRequest(com.dtflys.forest.mock.MockServerRequest.mockRequest) AssertionsForClassTypes.assertThat(org.assertj.core.api.AssertionsForClassTypes.assertThat) HttpBackend(com.dtflys.forest.backend.HttpBackend) BaseClientTest(com.dtflys.test.http.BaseClientTest) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 30 with ForestResponse

use of com.dtflys.forest.http.ForestResponse in project forest by dromara.

the class TestBaseRedirectClient method testNotAutoRedirect_306.

@Test
public void testNotAutoRedirect_306() {
    server.enqueue(new MockResponse().addHeader("Location", "http://localhost:" + server.getPort() + "/b").setResponseCode(306));
    server.enqueue(new MockResponse().setBody(EXPECTED));
    AtomicReference<ForestRequest> atomicReq = new AtomicReference<>(null);
    ForestResponse<String> response = redirectClient.testNotAutoRedirect(((redirectReq, prevReq, prevRes) -> {
        atomicReq.set(redirectReq);
    }));
    assertThat(atomicReq.get()).isNull();
    assertThat(response).isNotNull();
    assertThat(response.getStatusCode()).isEqualTo(306);
    assertThat(response.isRedirection()).isTrue();
    assertThat(response.getRedirectionLocation()).isEqualTo("http://localhost:" + server.getPort() + "/b");
    String result = response.redirectionRequest().execute(String.class);
    assertThat(result).isNotNull().isEqualTo(EXPECTED);
    assertThat(atomicReq.get()).isNotNull();
    assertThat(atomicReq.get().path()).isEqualTo("/b");
}
Also used : BeforeClass(org.junit.BeforeClass) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ForestResponse(com.dtflys.forest.http.ForestResponse) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) Rule(org.junit.Rule) MockWebServer(okhttp3.mockwebserver.MockWebServer) MockResponse(okhttp3.mockwebserver.MockResponse) MockServerRequest.mockRequest(com.dtflys.forest.mock.MockServerRequest.mockRequest) AssertionsForClassTypes.assertThat(org.assertj.core.api.AssertionsForClassTypes.assertThat) HttpBackend(com.dtflys.forest.backend.HttpBackend) BaseClientTest(com.dtflys.test.http.BaseClientTest) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Aggregations

ForestResponse (com.dtflys.forest.http.ForestResponse)46 ForestRequest (com.dtflys.forest.http.ForestRequest)38 Test (org.junit.Test)37 MockResponse (okhttp3.mockwebserver.MockResponse)34 BaseClientTest (com.dtflys.test.http.BaseClientTest)31 HttpBackend (com.dtflys.forest.backend.HttpBackend)30 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)30 AtomicReference (java.util.concurrent.atomic.AtomicReference)30 MockWebServer (okhttp3.mockwebserver.MockWebServer)30 BeforeClass (org.junit.BeforeClass)30 Rule (org.junit.Rule)30 MockServerRequest.mockRequest (com.dtflys.forest.mock.MockServerRequest.mockRequest)29 CountDownLatch (java.util.concurrent.CountDownLatch)29 TimeUnit (java.util.concurrent.TimeUnit)29 AssertionsForClassTypes.assertThat (org.assertj.core.api.AssertionsForClassTypes.assertThat)29 ForestNetworkException (com.dtflys.forest.exceptions.ForestNetworkException)3 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)3 HttpclientForestResponseFactory (com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory)2 ForestRetryException (com.dtflys.forest.exceptions.ForestRetryException)2 Date (java.util.Date)2