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 "[网络错误]: 未知的网络错误!";
}
}
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);
}
}
}
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);
}
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);
}
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");
}
Aggregations