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;
}
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);
// }
}
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);
}
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);
}
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");
}
Aggregations