Search in sources :

Example 96 with HttpRequest

use of org.apache.http.HttpRequest in project spark by perwendel.

the class SparkTestUtil method setFollowRedirectStrategy.

public void setFollowRedirectStrategy(Integer... codes) {
    final List<Integer> redirectCodes = Arrays.asList(codes);
    DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy() {

        public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
            boolean isRedirect = false;
            try {
                isRedirect = super.isRedirected(request, response, context);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (!isRedirect) {
                int responseCode = response.getStatusLine().getStatusCode();
                if (redirectCodes.contains(responseCode)) {
                    return true;
                }
            }
            return isRedirect;
        }
    };
    this.httpClient = httpClientBuilder().setRedirectStrategy(redirectStrategy).build();
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpContext(org.apache.http.protocol.HttpContext) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 97 with HttpRequest

use of org.apache.http.HttpRequest in project lucene-solr by apache.

the class SolrHttpRequestRetryHandler method retryRequest.

@Override
public boolean retryRequest(final IOException exception, final int executionCount, final HttpContext context) {
    log.debug("Retry http request {} out of {}", executionCount, this.retryCount);
    if (executionCount > this.retryCount) {
        log.debug("Do not retry, over max retry count");
        return false;
    }
    if (this.nonRetriableClasses.contains(exception.getClass())) {
        log.debug("Do not retry, non retriable class {}", exception.getClass().getName());
        return false;
    } else {
        for (final Class<? extends IOException> rejectException : this.nonRetriableClasses) {
            if (rejectException.isInstance(exception)) {
                log.debug("Do not retry, non retriable class {}", exception.getClass().getName());
                return false;
            }
        }
    }
    final HttpClientContext clientContext = HttpClientContext.adapt(context);
    final HttpRequest request = clientContext.getRequest();
    if (requestIsAborted(request)) {
        log.debug("Do not retry, request was aborted");
        return false;
    }
    if (handleAsIdempotent(clientContext)) {
        log.debug("Retry, request should be idempotent");
        return true;
    }
    log.debug("Do not retry, no allow rules matched");
    return false;
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpClientContext(org.apache.http.client.protocol.HttpClientContext)

Example 98 with HttpRequest

use of org.apache.http.HttpRequest in project lucene-solr by apache.

the class ConnectionReuseTest method headerRequest.

public void headerRequest(HttpHost target, HttpRoute route, HttpClientConnection conn, PoolingHttpClientConnectionManager cm) throws IOException, HttpException {
    HttpRequest req = new BasicHttpRequest("OPTIONS", "*", HttpVersion.HTTP_1_1);
    req.addHeader("Host", target.getHostName());
    if (!conn.isOpen()) {
        // establish connection based on its route info
        cm.connect(conn, route, 1000, context);
        // and mark it as route complete
        cm.routeComplete(conn, route, context);
    }
    conn.sendRequestHeader(req);
    conn.flush();
    conn.receiveResponseHeader();
}
Also used : BasicHttpRequest(org.apache.http.message.BasicHttpRequest) HttpRequest(org.apache.http.HttpRequest) BasicHttpRequest(org.apache.http.message.BasicHttpRequest)

Example 99 with HttpRequest

use of org.apache.http.HttpRequest in project uavstack by uavorg.

the class ApacheAsyncHttpClientIT method doAsyncStart.

/**
 * for async http client
 *
 * @param args
 * @return
 */
@SuppressWarnings({ "rawtypes", "unused", "unchecked" })
public FutureCallback doAsyncStart(Object[] args) {
    HttpAsyncRequestProducer requestProducer = null;
    HttpAsyncResponseConsumer responseConsumer = null;
    HttpContext context = null;
    FutureCallback callback = null;
    Map mObj = null;
    if (args.length == 4) {
        requestProducer = (HttpAsyncRequestProducer) args[0];
        responseConsumer = (HttpAsyncResponseConsumer) args[1];
        context = (HttpContext) args[2];
        callback = (FutureCallback) args[3];
    } else if (args.length == 5) {
        requestProducer = (HttpAsyncRequestProducer) args[1];
        responseConsumer = (HttpAsyncResponseConsumer) args[2];
        context = (HttpContext) args[3];
        callback = (FutureCallback) args[4];
    }
    String httpAction = null;
    try {
        HttpRequest hr = requestProducer.generateRequest();
        /**
         * 呵呵,就是把UAV的客户端标记加到http header里面,接收方就知道是哪个东东调用的了,便于实现来源快速匹配,这个模式只适合直连模式
         *
         * 对于代理模式,只匹配代理源即可
         */
        hr.addHeader("UAV-Client-Src", MonitorServerUtil.getUAVClientSrc(this.applicationId));
        RequestLine rl = hr.getRequestLine();
        httpAction = rl.getMethod();
        targetURL = rl.getUri();
    } catch (IOException e) {
        // ignore thie exception
        return null;
    } catch (HttpException e) {
        // ignore thie exception
        return null;
    }
    Map<String, Object> params = new HashMap<String, Object>();
    params.put(CaptureConstants.INFO_CLIENT_REQUEST_URL, targetURL);
    params.put(CaptureConstants.INFO_CLIENT_REQUEST_ACTION, httpAction);
    params.put(CaptureConstants.INFO_CLIENT_APPID, this.applicationId);
    params.put(CaptureConstants.INFO_CLIENT_TYPE, "apache.http.AsyncClient");
    if (logger.isDebugable()) {
        logger.debug("Invoke START:" + targetURL + "," + httpAction + "," + this.applicationId, null);
    }
    /**
     * for async, as not in the same thread
     */
    ccMap = UAVServer.instance().runMonitorAsyncCaptureOnServerCapPoint(CaptureConstants.CAPPOINT_APP_CLIENT, Monitor.CapturePhase.PRECAP, params, null);
    // register invokechain adapter
    UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter", "registerAdapter", ApacheAsyncHttpClientAdapter.class);
    ccMap4Chain = (Map<String, Object>) UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter", "runCap", InvokeChainConstants.CHAIN_APP_CLIENT, InvokeChainConstants.CapturePhase.PRECAP, params, ApacheAsyncHttpClientAdapter.class, args);
    if (callback == null) {
        return null;
    }
    callback = JDKProxyInvokeUtil.newProxyInstance(HttpContext.class.getClassLoader(), new Class<?>[] { FutureCallback.class }, new JDKProxyInvokeHandler<FutureCallback>(callback, new FutureCallbackProxyInvokeProcessor(ccMap4Chain)));
    return callback;
}
Also used : HttpRequest(org.apache.http.HttpRequest) HashMap(java.util.HashMap) JDKProxyInvokeHandler(com.creditease.monitor.proxy.spi.JDKProxyInvokeHandler) HttpContext(org.apache.http.protocol.HttpContext) HttpAsyncResponseConsumer(org.apache.http.nio.protocol.HttpAsyncResponseConsumer) IOException(java.io.IOException) RequestLine(org.apache.http.RequestLine) HttpAsyncRequestProducer(org.apache.http.nio.protocol.HttpAsyncRequestProducer) HttpException(org.apache.http.HttpException) HashMap(java.util.HashMap) Map(java.util.Map) FutureCallback(org.apache.http.concurrent.FutureCallback)

Example 100 with HttpRequest

use of org.apache.http.HttpRequest in project uavstack by uavorg.

the class ApacheAsyncHttpClientAdapter method afterPreCap.

@Override
public void afterPreCap(InvokeChainContext context, Object[] args) {
    HttpAsyncRequestProducer requestProducer = null;
    HttpRequest request = null;
    if (args.length == 4) {
        requestProducer = (HttpAsyncRequestProducer) args[0];
    } else if (args.length == 5) {
        requestProducer = (HttpAsyncRequestProducer) args[1];
    } else {
        return;
    }
    try {
        request = requestProducer.generateRequest();
    } catch (Exception e) {
    // ignore
    }
    if (request == null) {
        return;
    }
    /**
     * after precap the client's span is created, set the span meta into http request header
     */
    String url = (String) context.get(InvokeChainConstants.CLIENT_SPAN_THREADLOCAL_STOREKEY);
    Span span = this.spanFactory.getSpanFromContext(url);
    String spanMeta = this.spanFactory.getSpanMeta(span);
    request.removeHeaders(InvokeChainConstants.PARAM_HTTPHEAD_SPANINFO);
    request.addHeader(InvokeChainConstants.PARAM_HTTPHEAD_SPANINFO, spanMeta);
    /**
     * store span into context for async
     */
    context.put(InvokeChainConstants.PARAM_SPAN_KEY, span);
    handleSlowOperSupporter(request, span, context);
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpAsyncRequestProducer(org.apache.http.nio.protocol.HttpAsyncRequestProducer) Span(com.creditease.uav.apm.invokechain.span.Span)

Aggregations

HttpRequest (org.apache.http.HttpRequest)155 HttpResponse (org.apache.http.HttpResponse)57 HttpContext (org.apache.http.protocol.HttpContext)56 HttpHost (org.apache.http.HttpHost)52 Test (org.junit.Test)40 IOException (java.io.IOException)36 Header (org.apache.http.Header)33 HttpException (org.apache.http.HttpException)33 HttpEntity (org.apache.http.HttpEntity)27 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)25 BasicHttpRequest (org.apache.http.message.BasicHttpRequest)22 HttpGet (org.apache.http.client.methods.HttpGet)21 HttpPost (org.apache.http.client.methods.HttpPost)21 URI (java.net.URI)19 ProtocolException (org.apache.http.ProtocolException)18 AbortableHttpRequest (org.apache.http.client.methods.AbortableHttpRequest)16 StringEntity (org.apache.http.entity.StringEntity)16 ArrayList (java.util.ArrayList)14 NameValuePair (org.apache.http.NameValuePair)14 CredentialsProvider (org.apache.http.client.CredentialsProvider)14