Search in sources :

Example 1 with ForestResponseFactory

use of com.dtflys.forest.http.ForestResponseFactory 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;
    }
}
Also used : HttpclientForestResponseFactory(com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) IOException(java.io.IOException) ForestResponseFactory(com.dtflys.forest.http.ForestResponseFactory) HttpclientForestResponseFactory(com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory) Date(java.util.Date)

Example 2 with ForestResponseFactory

use of com.dtflys.forest.http.ForestResponseFactory 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)

Aggregations

HttpclientForestResponseFactory (com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory)2 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)2 ForestResponseFactory (com.dtflys.forest.http.ForestResponseFactory)2 IOException (java.io.IOException)2 ForestNetworkException (com.dtflys.forest.exceptions.ForestNetworkException)1 ForestRetryException (com.dtflys.forest.exceptions.ForestRetryException)1 ForestResponse (com.dtflys.forest.http.ForestResponse)1 Date (java.util.Date)1 HttpResponse (org.apache.http.HttpResponse)1 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)1 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)1 HttpContext (org.apache.http.protocol.HttpContext)1