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

The following examples show how to use org.apache.http.client.methods.HttpUriRequest#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: HttpClientAop.java    From cicada with MIT License 6 votes vote down vote up
/**
 * 拦截HttpClient的Post与Get方法.
 * 
 */
@Around("execution(* org.apache.http.client.HttpClient.execute(..)) && args(httpUriRequest)")
public Object around(final ProceedingJoinPoint proceedingJoinPoint, final HttpUriRequest httpUriRequest)
    throws java.lang.Throwable {
  final long startTime = System.currentTimeMillis();
  final Object[] args = proceedingJoinPoint.getArgs();
  final Object result = proceedingJoinPoint.proceed(args);

  if (httpUriRequest instanceof HttpUriRequest) {
    final String methodName = httpUriRequest.getMethod();
    final String className = httpUriRequest.getURI().toString();

    Tracer.getInstance().addBinaryAnnotation(className, methodName, (int) (System.currentTimeMillis() - startTime));
  }

  return result;
}
 
Example 2
Source File: ApacheHttpClientAspect.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@OnBefore
public static @Nullable TraceEntry onBefore(ThreadContext context,
        @BindParameter @Nullable HttpUriRequest request) {
    if (request == null) {
        return null;
    }
    String method = request.getMethod();
    if (method == null) {
        method = "";
    } else {
        method += " ";
    }
    URI uriObj = request.getURI();
    String uri;
    if (uriObj == null) {
        uri = "";
    } else {
        uri = uriObj.toString();
    }
    return context.startServiceCallEntry("HTTP", method + Uris.stripQueryString(uri),
            MessageSupplier.create("http client request: {}{}", method, uri),
            timerName);
}
 
Example 3
Source File: ApacheHttpAsyncClientAspect.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@OnBefore
public static @Nullable AsyncTraceEntry onBefore(ThreadContext context,
        @BindParameter @Nullable HttpUriRequest request,
        @BindParameter ParameterHolder<FutureCallback<HttpResponse>> callback) {
    if (request == null) {
        return null;
    }
    String method = request.getMethod();
    if (method == null) {
        method = "";
    } else {
        method += " ";
    }
    URI uriObj = request.getURI();
    String uri;
    if (uriObj == null) {
        uri = "";
    } else {
        uri = uriObj.toString();
    }
    AsyncTraceEntry asyncTraceEntry = context.startAsyncServiceCallEntry("HTTP",
            method + Uris.stripQueryString(uri),
            MessageSupplier.create("http client request: {}{}", method, uri), timerName);
    callback.set(createWrapper(context, callback, asyncTraceEntry));
    return asyncTraceEntry;
}
 
Example 4
Source File: HttpRequestMatcher.java    From cosmic with Apache License 2.0 5 votes vote down vote up
private boolean checkMethod(final HttpUriRequest actual) {
    if (wanted instanceof HttpUriRequest) {
        final String wantedMethod = ((HttpUriRequest) wanted).getMethod();
        final String actualMethod = actual.getMethod();
        return equalsString(wantedMethod, actualMethod);
    } else {
        return wanted == actual;
    }
}
 
Example 5
Source File: RetryHandler.java    From sealtalk-android with MIT License 5 votes vote down vote up
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    boolean retry = true;

    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b);

    if (executionCount > maxRetries) {
        // Do not retry if over max retry count
        retry = false;
    } else if (isInList(exceptionBlacklist, exception)) {
        // immediately cancel retry if the error is blacklisted
        retry = false;
    } else if (isInList(exceptionWhitelist, exception)) {
        // immediately retry if error is whitelisted
        retry = true;
    } else if (!sent) {
        // for most other errors, retry only if request hasn't been fully sent yet
        retry = true;
    }

    if (retry) {
        // resend all idempotent requests
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        if (currentReq == null) {
            return false;
        }
        String requestType = currentReq.getMethod();
        retry = !requestType.equals("POST");
    }

    if (retry) {
        SystemClock.sleep(retrySleepTimeMS);
    } else {
        exception.printStackTrace();
    }

    return retry;
}
 
Example 6
Source File: ErrorResponseException.java    From nomad-java-sdk with Mozilla Public License 2.0 5 votes vote down vote up
private ErrorResponseException(HttpUriRequest request,
                               String errorLocation,
                               String serverErrorMessage,
                               int serverErrorCode,
                               @Nullable String rawEntity) {

    super(
            request.getMethod() + " " + request.getURI()
                    + " resulted in error " + errorLocation + ": " + serverErrorMessage,
            rawEntity,
            null);
    this.serverErrorMessage = serverErrorMessage;
    this.serverErrorCode = serverErrorCode;
}
 
Example 7
Source File: WrappedHttpClient.java    From soabase with Apache License 2.0 5 votes vote down vote up
@Override
public HttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException
{
    RequestRunner<HttpUriRequest> requestRunner = new RequestRunner<>(retryComponents, headerSetter, request.getURI(), request.getMethod());
    while ( requestRunner.shouldContinue() )
    {
        URI uri = requestRunner.prepareRequest(request);
        request = new WrappedHttpUriRequest(request, uri);
        try
        {
            HttpResponse response = implementation.execute(request, context);
            if ( requestRunner.isSuccessResponse(response.getStatusLine().getStatusCode()) )
            {
                return response;
            }
        }
        catch ( IOException e )
        {
            if ( !requestRunner.shouldBeRetried(e) )
            {
                throw e;
            }
        }
    }

    throw new IOException("Retries expired for " + requestRunner.getOriginalUri());
}
 
Example 8
Source File: HttpRequestMatcher.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
private boolean checkMethod(final HttpUriRequest actual) {
    if (wanted instanceof HttpUriRequest) {
        final String wantedMethod = ((HttpUriRequest) wanted).getMethod();
        final String actualMethod = actual.getMethod();
        return equalsString(wantedMethod, actualMethod);
    } else {
        return wanted == actual;
    }
}
 
Example 9
Source File: RetryHandler.java    From Roid-Library with Apache License 2.0 5 votes vote down vote up
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    boolean retry = true;

    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b.booleanValue());

    if (executionCount > maxRetries) {
        // Do not retry if over max retry count
        retry = false;
    } else if (isInList(exceptionBlacklist, exception)) {
        // immediately cancel retry if the error is blacklisted
        retry = false;
    } else if (isInList(exceptionWhitelist, exception)) {
        // immediately retry if error is whitelisted
        retry = true;
    } else if (!sent) {
        // for most other errors, retry only if request hasn't been fully
        // sent yet
        retry = true;
    }

    if (retry) {
        // resend all idempotent requests
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        String requestType = currentReq.getMethod();
        retry = !requestType.equals("POST");
    }

    if (retry) {
        SystemClock.sleep(RETRY_SLEEP_TIME_MILLIS);
    } else {
        exception.printStackTrace();
    }

    return retry;
}
 
Example 10
Source File: HttpUriRequestMethodMatcher.java    From cosmic with Apache License 2.0 4 votes vote down vote up
@Override
protected String featureValueOf(final HttpUriRequest actual) {
    return actual.getMethod();
}
 
Example 11
Source File: HttpUriRequestMethodMatcher.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
protected String featureValueOf(final HttpUriRequest actual) {
    return actual.getMethod();
}
 
Example 12
Source File: RequestRetryHandler.java    From fanfouapp-opensource with Apache License 2.0 4 votes vote down vote up
@Override
public boolean retryRequest(final IOException exception,
        final int executionCount, final HttpContext context) {
    boolean retry;

    final Boolean b = (Boolean) context
            .getAttribute(ExecutionContext.HTTP_REQ_SENT);
    final boolean sent = ((b != null) && b.booleanValue());

    if (executionCount > this.maxRetries) {
        // Do not retry if over max retry count
        retry = false;
    } else if (RequestRetryHandler.exceptionBlacklist.contains(exception
            .getClass())) {
        // immediately cancel retry if the error is blacklisted
        retry = false;
    } else if (RequestRetryHandler.exceptionWhitelist.contains(exception
            .getClass())) {
        // immediately retry if error is whitelisted
        retry = true;
    } else if (!sent) {
        // for most other errors, retry only if request hasn't been fully
        // sent yet
        retry = true;
    } else {
        // resend all idempotent requests
        final HttpUriRequest currentReq = (HttpUriRequest) context
                .getAttribute(ExecutionContext.HTTP_REQUEST);
        final String requestType = currentReq.getMethod();
        if (!requestType.equals("POST")) {
            retry = true;
        } else {
            retry = false;
        }
    }

    if (retry) {
        SystemClock.sleep(RequestRetryHandler.RETRY_SLEEP_TIME_MILLIS);
    } else {
        if (AppContext.DEBUG) {
            exception.printStackTrace();
        }
    }

    return retry;
}