use of com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory in project forest by dromara.
the class HttpclientExecutor method execute.
@Override
public void execute(LifeCycleHandler lifeCycleHandler) {
prepare(lifeCycleHandler);
Date startDate = new Date();
ForestResponseFactory forestResponseFactory = new HttpclientForestResponseFactory();
try {
requestSender.sendRequest(request, this, httpclientResponseHandler, httpRequest, lifeCycleHandler, cookieStore, startDate);
} catch (IOException e) {
httpRequest.abort();
response = forestResponseFactory.createResponse(request, null, lifeCycleHandler, e, startDate);
lifeCycleHandler.handleSyncWithException(request, response, e);
return;
} catch (ForestRuntimeException e) {
httpRequest.abort();
throw e;
}
}
use of com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory 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.backend.httpclient.response.HttpclientForestResponseFactory in project forest by dromara.
the class TestResponseFactory method testHttpclientForestResponseFactory.
@Test
public void testHttpclientForestResponseFactory() {
ForestRequest request = new ForestRequest(ForestConfiguration.configuration());
Date requestTime = new Date();
HttpclientForestResponseFactory responseFactory = new HttpclientForestResponseFactory();
HttpResponse httpResponse = mock(HttpResponse.class);
StatusLine statusLine = mock(StatusLine.class);
when(statusLine.getStatusCode()).thenReturn(200);
when(httpResponse.getStatusLine()).thenReturn(statusLine);
LifeCycleHandler lifeCycleHandler = new NoneLifeCycleHandler();
ForestResponse response = responseFactory.createResponse(request, httpResponse, lifeCycleHandler, null, requestTime);
assertThat(response).isNotNull().extracting(ForestResponse::getStatusCode, ForestResponse::getContent, ForestResponse::getResult).contains(200, "", null);
}
Aggregations