Search in sources :

Example 1 with ForestResponse

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

the class TestLogHandler method responseLoggingContent.

/**
 * 该方法生成Forest请求响应结果的日志内容字符串
 * @param responseLogMessage 请求响应日志字符串
 * @return 日志内容字符串
 */
@Override
protected String responseLoggingContent(ResponseLogMessage responseLogMessage) {
    ForestResponse response = responseLogMessage.getResponse();
    if (response != null && response.getException() != null) {
        return "[网络错误]: " + response.getException().getMessage();
    }
    int status = responseLogMessage.getStatus();
    if (status >= 0) {
        return "请求响应: 状态码: " + responseLogMessage.getStatus() + ", 耗时: " + responseLogMessage.getTime() + "ms";
    } else {
        return "[网络错误]: 未知的网络错误!";
    }
}
Also used : ForestResponse(com.dtflys.forest.http.ForestResponse)

Example 2 with ForestResponse

use of com.dtflys.forest.http.ForestResponse 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 3 with ForestResponse

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

the class TestBaseRedirectClient method testBaseAutoRedirect_305.

@Test
public void testBaseAutoRedirect_305() {
    server.enqueue(new MockResponse().addHeader("Location", "http://localhost:" + server.getPort() + "/b").setResponseCode(305));
    server.enqueue(new MockResponse().setBody(EXPECTED));
    AtomicReference<ForestRequest> atomicReq = new AtomicReference<>(null);
    ForestResponse<String> response = baseRedirectClient.testAutoRedirect(((redirectReq, prevReq, prevRes) -> {
        atomicReq.set(redirectReq);
    }));
    assertThat(atomicReq.get()).isNotNull();
    assertThat(atomicReq.get().path()).isEqualTo("/b");
    assertThat(response).isNotNull();
    assertThat(response.getStatusCode()).isEqualTo(200);
    String result = response.getResult();
    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 4 with ForestResponse

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

the class TestBaseRedirectClient method testBaseAutoRedirect_307.

@Test
public void testBaseAutoRedirect_307() {
    server.enqueue(new MockResponse().addHeader("Location", "http://localhost:" + server.getPort() + "/b").setResponseCode(307));
    server.enqueue(new MockResponse().setBody(EXPECTED));
    AtomicReference<ForestRequest> atomicReq = new AtomicReference<>(null);
    ForestResponse<String> response = baseRedirectClient.testAutoRedirect(((redirectReq, prevReq, prevRes) -> {
        atomicReq.set(redirectReq);
    }));
    assertThat(atomicReq.get()).isNotNull();
    assertThat(atomicReq.get().path()).isEqualTo("/b");
    assertThat(response).isNotNull();
    assertThat(response.getStatusCode()).isEqualTo(200);
    String result = response.getResult();
    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 5 with ForestResponse

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

the class TestBaseRedirectClient method testNotAutoRedirect_305.

@Test
public void testNotAutoRedirect_305() {
    server.enqueue(new MockResponse().addHeader("Location", "http://localhost:" + server.getPort() + "/b").setResponseCode(305));
    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(305);
    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().getPath()).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