Java Code Examples for org.apache.http.client.methods.HttpRequestBase#getMethod()

The following examples show how to use org.apache.http.client.methods.HttpRequestBase#getMethod() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpProxyClient.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
private String sendAndGetResponse(HttpRequestBase request) throws IOException {
  String data = StringUtils.EMPTY;
  try {
    HttpResponse response = client.execute(request, null).get(30, TimeUnit.SECONDS);
    int code = response.getStatusLine().getStatusCode();
    if (code == 200) {
      try (InputStream responseContent = response.getEntity().getContent()) {
        data = IOUtils.toString(responseContent, "UTF-8");
      }
    } else {
      LOG.error("ZeppelinHub {} {} returned with status {} ", request.getMethod(),
          request.getURI(), code);
      throw new IOException("Cannot perform " + request.getMethod() + " request to ZeppelinHub");
    }
  } catch (InterruptedException | ExecutionException | TimeoutException
      | NullPointerException e) {
    throw new IOException(e);
  }
  return data;
}
 
Example 2
Source File: HttpClientHelper.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public HttpResponse performRequest(HttpRequestBase request) {
    String method = request.getMethod();

    HttpResponse response;
    try {
        response = executeGetOrHead(request);
    } catch (IOException e) {
        throw new HttpRequestException(String.format("Could not %s '%s'.", method, request.getURI()), e);
    }

    return response;
}
 
Example 3
Source File: HttpClientHelper.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public HttpResponse performRequest(HttpRequestBase request) {
    String method = request.getMethod();

    HttpResponse response;
    try {
        response = executeGetOrHead(request);
    } catch (IOException e) {
        throw new HttpRequestException(String.format("Could not %s '%s'.", method, request.getURI()), e);
    }

    return response;
}
 
Example 4
Source File: ComplianceUtils.java    From scim2-compliance-test-suite with Apache License 2.0 5 votes vote down vote up
/**
 * method to get the wire.
 * @param method
 * @param responseBody
 * @param headerString
 * @param responseStatus
 * @param subTests
 * @return
 * @throws ComplianceException
 */
public static Wire getWire(HttpRequestBase method, String responseBody,
                           String headerString, String responseStatus,
                           ArrayList<String> subTests) throws ComplianceException {

    StringBuffer toServer = new StringBuffer();
    StringBuffer fromServer = new StringBuffer();
    StringBuffer subTestsPerformed = new StringBuffer();

    toServer.append(method.getRequestLine().getMethod()).append(" ");
    toServer.append(method.getRequestLine().getUri()+"\n");
    toServer.append(method.getRequestLine().getProtocolVersion().getProtocol());
    for (org.apache.http.Header header : method.getAllHeaders()) {
        toServer.append(header.getName()).append(": ").append(header.getValue()).append("\n");
    }

    if(method.getMethod() != "GET" && method.getMethod() != "DELETE"){
        try {
            HttpEntity entity = ((HttpEntityEnclosingRequest)method).getEntity();
            toServer.append(EntityUtils.toString(entity));
        } catch (Exception e) {
            throw new ComplianceException(500, "Error in getting the request payload");
        }
    }
    fromServer.append("\n" + "Headers : ");
    fromServer.append(headerString + "\n");
    fromServer.append("\n" + "Status : ");
    fromServer.append(responseStatus + "\n");
    fromServer.append("\n" + responseBody);
    for (String subTest : subTests) {
        subTestsPerformed.append(subTest).append("\n");
    }
    return new Wire(toServer.toString(), fromServer.toString(), subTestsPerformed.toString());
}
 
Example 5
Source File: HttpHandler.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
private ResponseInfo<T> sendRequestForCache(HttpRequestBase request) {
        requestMethod = request.getMethod();

        boolean isEnableCache = com.youzu.android.framework.HttpUtils.sHttpCache.isEnabled(requestMethod);

//        Log.e("APP", "requestMethod:" + requestMethod + " isEnableCache:" + isEnableCache);

        if (isEnableCache) {
            String result = com.youzu.android.framework.HttpUtils.sHttpCache.get(requestUrl + request.toString());
            if (result != null) {
                return new ResponseInfo<T>(null, (T) result, true);
            }
        }
        return null;
    }
 
Example 6
Source File: HttpClientHelper.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public HttpResponse performRequest(HttpRequestBase request) {
    String method = request.getMethod();

    HttpResponse response;
    try {
        response = executeGetOrHead(request);
    } catch (IOException e) {
        throw new HttpRequestException(String.format("Could not %s '%s'.", method, request.getURI()), e);
    }

    return response;
}
 
Example 7
Source File: HttpClientHelper.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public HttpResponse performRequest(HttpRequestBase request) {
    String method = request.getMethod();

    HttpResponse response;
    try {
        response = executeGetOrHead(request);
    } catch (IOException e) {
        throw new HttpRequestException(String.format("Could not %s '%s'.", method, request.getURI()), e);
    }

    return response;
}
 
Example 8
Source File: HttpMethodClientTest.java    From nem.core with MIT License 5 votes vote down vote up
@Override
public T coerce(final HttpRequestBase request, final HttpResponse response) {
	final HttpEntity entity = response.getEntity();
	this.requestMethod = request.getMethod();
	this.requestContentType = null == entity ? null : ContentType.get(entity).getMimeType();
	this.requestAcceptHeader = request.getFirstHeader("Accept").getValue();
	return null;
}
 
Example 9
Source File: HttpRequestUtil.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the HTTPMethod with retry.
 *
 * @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.getConfigurationDTO().getStatus_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;
}
 
Example 10
Source File: HttpRequestUtil.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Executes HTTPMethod without retry
 *
 * @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.getConfigurationDTO().getStatus_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;
}
 
Example 11
Source File: ElasticsearchClient.java    From elasticsearch-maven-plugin with Apache License 2.0 5 votes vote down vote up
protected String executeRequest(HttpRequestBase request) throws ElasticsearchClientException
{
    Validate.notNull(httpClient, "Has the ElasticsearchClient been initialized?");
    
    try
    {
        HttpResponse response = httpClient.execute(request);

        int statusCode = response.getStatusLine().getStatusCode();
        String content = readContent(response.getEntity());
        log.debug(String.format(
                "Response with status code %d and content: %s", statusCode, content));

        // some PUT requests return 200, some 201 :-O
        if (statusCode != 200 && statusCode != 201)
        {
            throw new ElasticsearchClientException(request.getMethod(), statusCode, content);
        }

        return content;
    }
    catch (IOException e)
    {
        throw new ElasticsearchClientException(e);
    }
    finally
    {
        request.releaseConnection();
    }
}
 
Example 12
Source File: KylinClient.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
private IOException asIOException(HttpRequestBase request, HttpResponse response) throws IOException {
    return new IOException(request.getMethod() + " failed, error code " + response.getStatusLine().getStatusCode()
            + " and response: " + EntityUtils.toString(response.getEntity()));
}
 
Example 13
Source File: KylinClient.java    From kylin with Apache License 2.0 4 votes vote down vote up
private IOException asIOException(HttpRequestBase request, HttpResponse response) throws IOException {
    return new IOException(request.getMethod() + " failed, error code " + response.getStatusLine().getStatusCode()
            + " and response: " + EntityUtils.toString(response.getEntity()));
}