Example usage for org.apache.http.client.methods HttpRequestBase getMethod

List of usage examples for org.apache.http.client.methods HttpRequestBase getMethod

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpRequestBase getMethod.

Prototype

public abstract String getMethod();

Source Link

Usage

From source file:org.wso2.carbon.apimgt.hybrid.gateway.common.util.HttpRequestUtil.java

/**
 * Executes the HTTPMethod with retry./* ww w. j  a va 2s  . c  o  m*/
 *
 * @param httpClient     HTTPClient
 * @param httpMethod     HTTPMethod
 * @param retryCount No of retries
 * @return response. it will return an empty string if response body is null
 * @throws OnPremiseGatewayException throws {@link OnPremiseGatewayException}
 */
public static String executeHTTPMethodWithRetry(HttpClient httpClient, HttpRequestBase httpMethod,
        int retryCount) throws OnPremiseGatewayException {

    String result = OnPremiseGatewayConstants.EMPTY_STRING;
    HttpResponse response;
    int executionCount = 0;
    String methodName = httpMethod.getMethod();
    String uri = getURI(httpMethod);

    //Add an unique identifier as a custom header for distinguishing requests from different micro gateways.
    String token = ConfigManager.getConfigManager()
            .getProperty(OnPremiseGatewayConstants.API_REQUEST_UNIQUE_IDENTIFIER);
    if (StringUtils.isNotBlank(token)
            && !(OnPremiseGatewayConstants.API_REQUEST_UNIQUE_IDENTIFIER_HOLDER.equals(token))) {
        if (log.isDebugEnabled()) {
            log.debug("Adding unique identifier as an header to the http " + methodName + " request.");
        }
        httpMethod.addHeader(OnPremiseGatewayConstants.APT_REQUEST_TOKEN_HEADER, token);
    }
    do {
        try {
            executionCount++;
            response = httpClient.execute(httpMethod);
            if (log.isDebugEnabled()) {
                log.debug("HTTP response code for the " + methodName + " request to URL: " + uri + " is "
                        + response);
            }
            result = handleResponse(response, methodName, true, executionCount, retryCount, uri);
            if (!OnPremiseGatewayConstants.EMPTY_STRING.equals(result)) {
                return result;
            }
        } catch (IOException e) {
            handleExceptionWithRetry(executionCount, retryCount, methodName, uri, e);
        } finally {
            httpMethod.releaseConnection();
        }
    } while (executionCount < retryCount);
    return result;
}

From source file:org.wso2.carbon.apimgt.hybrid.gateway.common.util.HttpRequestUtil.java

/**
 * Executes HTTPMethod without retry//from ww  w. j  a va  2  s  . c om
 *
 * @param httpClient HTTPClient
 * @param httpMethod HTTPMethod
 * @return response. it will return an empty string if response body is null
 * @throws OnPremiseGatewayException throws {@link OnPremiseGatewayException}
 */
public static String executeHTTPMethod(HttpClient httpClient, HttpRequestBase httpMethod)
        throws OnPremiseGatewayException {

    String result;
    HttpResponse response;
    String uri = getURI(httpMethod);
    String methodName = httpMethod.getMethod();

    //Add an unique identifier as an custom header for distinguishing requests from different micro gateways.
    String token = ConfigManager.getConfigManager()
            .getProperty(OnPremiseGatewayConstants.API_REQUEST_UNIQUE_IDENTIFIER);
    if (StringUtils.isNotBlank(token)
            && !(OnPremiseGatewayConstants.API_REQUEST_UNIQUE_IDENTIFIER_HOLDER.equals(token))) {
        if (log.isDebugEnabled()) {
            log.debug("Adding unique identifier as an header to the http " + methodName + " request.");
        }
        httpMethod.addHeader(OnPremiseGatewayConstants.APT_REQUEST_TOKEN_HEADER, token);
    }
    try {
        response = httpClient.execute(httpMethod);
        if (log.isDebugEnabled()) {
            log.debug("HTTP response code for the " + methodName + " request: " + uri + " is " + response);
        }
        result = handleResponse(response, methodName, false, 0, 0, uri);
    } catch (IOException e) {
        throw new OnPremiseGatewayException(methodName + " request failed for URI: " + uri, e);
    } finally {
        httpMethod.releaseConnection();
    }
    return result;
}