Example usage for org.apache.http.protocol HttpContext getAttribute

List of usage examples for org.apache.http.protocol HttpContext getAttribute

Introduction

In this page you can find the example usage for org.apache.http.protocol HttpContext getAttribute.

Prototype

Object getAttribute(String str);

Source Link

Usage

From source file:cn.isif.util_plus.http.client.RetryHandler.java

@Override
public boolean retryRequest(IOException exception, int retriedTimes, HttpContext context) {
    boolean retry = true;

    if (exception == null || context == null) {
        return false;
    }/*from w ww  . ja  va 2  s.c  o  m*/

    Object isReqSent = context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = isReqSent == null ? false : (Boolean) isReqSent;

    if (retriedTimes > maxRetries) {
        retry = false;
    } else if (exceptionBlackList.contains(exception.getClass())) {
        retry = false;
    } else if (exceptionWhiteList.contains(exception.getClass())) {
        retry = true;
    } else if (!sent) {
        retry = true;
    }

    if (retry) {
        try {
            Object currRequest = context.getAttribute(ExecutionContext.HTTP_REQUEST);
            if (currRequest != null) {
                if (currRequest instanceof HttpRequestBase) {
                    HttpRequestBase requestBase = (HttpRequestBase) currRequest;
                    retry = "GET".equals(requestBase.getMethod());
                } else if (currRequest instanceof RequestWrapper) {
                    RequestWrapper requestWrapper = (RequestWrapper) currRequest;
                    retry = "GET".equals(requestWrapper.getMethod());
                }
            } else {
                retry = false;
                LogUtils.e("retry error, curr request is null");
            }
        } catch (Throwable e) {
            retry = false;
            LogUtils.e("retry error", e);
        }
    }

    if (retry) {
        SystemClock.sleep(RETRY_SLEEP_INTERVAL); // sleep a while and retry http request again.
    }

    return retry;
}

From source file:com.xc.framework.https.client.RetryHandler.java

@Override
public boolean retryRequest(IOException exception, int retriedTimes, HttpContext context) {
    boolean retry = true;

    if (exception == null || context == null) {
        return false;
    }/*from  w w  w. j  a  v a  2  s  . c om*/

    Object isReqSent = context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = isReqSent == null ? false : (Boolean) isReqSent;

    if (retriedTimes > maxRetries) {
        retry = false;
    } else if (exceptionBlackList.contains(exception.getClass())) {
        retry = false;
    } else if (exceptionWhiteList.contains(exception.getClass())) {
        retry = true;
    } else if (!sent) {
        retry = true;
    }

    if (retry) {
        try {
            Object currRequest = context.getAttribute(ExecutionContext.HTTP_REQUEST);
            if (currRequest != null) {
                if (currRequest instanceof HttpRequestBase) {
                    HttpRequestBase requestBase = (HttpRequestBase) currRequest;
                    retry = "GET".equals(requestBase.getMethod());
                } else if (currRequest instanceof RequestWrapper) {
                    RequestWrapper requestWrapper = (RequestWrapper) currRequest;
                    retry = "GET".equals(requestWrapper.getMethod());
                }
            } else {
                retry = false;
                Log.e("RetryHandler", "retry error, curr request is null");
            }
        } catch (Throwable e) {
            retry = false;
            Log.e("retry error", "" + e);
        }
    }

    if (retry) {
        SystemClock.sleep(RETRY_SLEEP_INTERVAL); // sleep a while and retry
        // http request again.
    }

    return retry;
}

From source file:com.drive.student.xutils.http.client.RetryHandler.java

@Override
public boolean retryRequest(IOException exception, int retriedTimes, HttpContext context) {
    boolean retry = true;

    if (exception == null || context == null) {
        return false;
    }// w  w  w .  j  ava  2s .  co  m

    Object isReqSent = context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = isReqSent == null ? false : (Boolean) isReqSent;

    if (retriedTimes > maxRetries) {
        retry = false;
    } else if (exceptionBlackList.contains(exception.getClass())) {
        retry = false;
    } else if (exceptionWhiteList.contains(exception.getClass())) {
        retry = true;
    } else if (!sent) {
        retry = true;
    }

    if (retry) {
        try {
            Object currRequest = context.getAttribute(ExecutionContext.HTTP_REQUEST);
            if (currRequest != null) {
                if (currRequest instanceof HttpRequestBase) {
                    HttpRequestBase requestBase = (HttpRequestBase) currRequest;
                    retry = "GET".equals(requestBase.getMethod());
                } else if (currRequest instanceof RequestWrapper) {
                    RequestWrapper requestWrapper = (RequestWrapper) currRequest;
                    retry = "GET".equals(requestWrapper.getMethod());
                }
            } else {
                retry = false;
                LogUtils.e("retry error, curr request is null");
            }
        } catch (Throwable e) {
            retry = false;
            LogUtils.e("retry error", e);
        }
    }

    if (retry) {
        SystemClock.sleep(RETRY_SLEEP_INTERVAL); // sleep a while and retry
        // http request again.
    }

    return retry;
}

From source file:ste.web.http.velocity.VelocityHandler.java

@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
        throws HttpException, IOException {
    String view = (String) context.getAttribute(ATTR_VIEW);
    if (view == null) {
        return;/*from   w w w.j  av  a  2 s.  c o  m*/
    }

    view = getViewPath(request.getRequestLine().getUri(), view);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Writer out = new OutputStreamWriter(baos);
    try {
        Template t = engine.getTemplate(view);
        t.merge(buildContext(request, (HttpSessionContext) context), out);
        out.flush();
    } catch (ResourceNotFoundException e) {
        response.setStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_FOUND, "View " + view + " not found.");
        return;
    } catch (ParseErrorException e) {
        throw new HttpException("Parse error evaluating " + view + ": " + e, e);
    } catch (MethodInvocationException e) {
        throw new HttpException("Method invocation error evaluating " + view + ": " + e, e);
    }

    BasicHttpEntity body = (BasicHttpEntity) response.getEntity();
    body.setContentLength(baos.size());
    body.setContent(new ByteArrayInputStream(baos.toByteArray()));
    if ((body.getContentType() == null) || StringUtils.isBlank(body.getContentType().getValue())) {
        body.setContentType(ContentType.TEXT_HTML.getMimeType());
    }
}

From source file:com.dongfang.net.http.RetryHandler.java

@Override
public boolean retryRequest(IOException exception, int retriedTimes, HttpContext context) {
    boolean retry = true;

    if (exception == null || context == null) {
        return false;
    }//from  ww  w .  j a  v  a 2 s  .co m

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

    if (retriedTimes > maxRetries) {
        retry = false;
    } else if (exceptionBlackList.contains(exception.getClass())) {
        retry = false;
    } else if (exceptionWhiteList.contains(exception.getClass())) {
        retry = true;
    } else if (!sent) {
        retry = true;
    }

    if (retry) {
        try {
            Object currRequest = context.getAttribute(ExecutionContext.HTTP_REQUEST);
            if (currRequest != null) {
                if (currRequest instanceof HttpRequestBase) {
                    HttpRequestBase requestBase = (HttpRequestBase) currRequest;
                    retry = "GET".equals(requestBase.getMethod());
                } else if (currRequest instanceof RequestWrapper) {
                    RequestWrapper requestWrapper = (RequestWrapper) currRequest;
                    retry = "GET".equals(requestWrapper.getMethod());
                }
            } else {
                retry = false;
                ULog.e("retry error, curr request is null");
            }
        } catch (Throwable e) {
            retry = false;
            ULog.e("retry error", e);
        }
    }

    if (retry) {
        SystemClock.sleep(RETRY_SLEEP_INTERVAL); // sleep a while and retry http request again.
    }

    return retry;
}

From source file:cn.wanghaomiao.seimi.core.SeimiProcessor.java

private String getRealUrl(HttpContext httpContext) {
    Object target = httpContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
    Object reqUri = httpContext.getAttribute(HttpCoreContext.HTTP_REQUEST);
    if (target == null || reqUri == null) {
        return null;
    }//from   w  ww.  j a  v  a  2s.  com
    HttpHost t = (HttpHost) target;
    HttpUriRequest r = (HttpUriRequest) reqUri;
    return r.getURI().isAbsolute() ? r.getURI().toString() : t.toString() + r.getURI().toString();
}

From source file:org.callimachusproject.server.AccessLog.java

private String getForensicId(HttpContext context) {
    String id = (String) context.getAttribute(FORENSIC_ATTR);
    if (id == null) {
        id = uid + seq.getAndIncrement();
        context.setAttribute(FORENSIC_ATTR, id);
    }/*from  w  w  w .ja  v  a2 s  .  c  o  m*/
    return id;
}

From source file:info.bonjean.beluga.connection.BelugaHTTPClient.java

private BelugaHTTPClient() {
    BelugaConfiguration configuration = BelugaConfiguration.getInstance();
    HttpClientBuilder clientBuilder = HttpClients.custom();

    // timeout//  w w  w.j a  va2 s  .  c o  m
    RequestConfig config = RequestConfig.custom().setConnectTimeout(TIMEOUT).setSocketTimeout(TIMEOUT)
            .setConnectionRequestTimeout(TIMEOUT).build();
    clientBuilder.setDefaultRequestConfig(config);

    switch (configuration.getConnectionType()) {
    case PROXY_DNS:
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", SSLConnectionSocketFactory.getSocketFactory()).build();
        BelugaDNSResolver dnsOverrider = new BelugaDNSResolver(DNSProxy.PROXY_DNS);
        connectionManager = new PoolingHttpClientConnectionManager(registry, dnsOverrider);
        break;
    case HTTP_PROXY:
        HttpHost proxy = new HttpHost(configuration.getProxyHost(), configuration.getProxyPort(), "http");
        clientBuilder.setProxy(proxy);
        break;
    default:
    }

    // limit the pool size
    connectionManager.setDefaultMaxPerRoute(2);

    // add interceptor, currently for debugging only
    clientBuilder.addInterceptorFirst(new HttpResponseInterceptor() {
        @Override
        public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
            HttpInetConnection connection = (HttpInetConnection) context
                    .getAttribute(HttpCoreContext.HTTP_CONNECTION);
            log.debug("Remote address: " + connection.getRemoteAddress());
            // TODO: reimplement blacklisting for DNS proxy by maintaining a
            // map [DNS IP,RESOLVED IP] in the DNS resolver for reverse
            // lookup
        }
    });

    // finally create the HTTP client
    clientBuilder.setConnectionManager(connectionManager);
    httpClient = clientBuilder.build();
}

From source file:org.dcache.srm.client.GsiConnectionSocketFactory.java

@Override
public Socket createLayeredSocket(Socket sock, String target, int port, HttpContext context)
        throws IOException {
    Socket socket = socketFactory.createLayeredSocket(sock, target, port, context);
    delegate(socket,/*from   w w w.  j av  a 2s. c o m*/
            (HttpClientTransport.Delegation) context
                    .getAttribute(HttpClientTransport.TRANSPORT_HTTP_DELEGATION),
            (X509Credential) context.getAttribute(HttpClientTransport.TRANSPORT_HTTP_CREDENTIALS));
    return socket;
}

From source file:httpfailover.DefaultFailoverRetryHandler.java

public boolean tryNextHost(final IOException exception, final HttpContext context) {

    if (exception == null) {
        throw new IllegalArgumentException("Exception parameter may not be null");
    }//from   ww w  .j  a  va2s.co m

    HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);

    if (requestIsAborted(request)) {
        return false;
    }

    if (handleAsIdempotent(request)) {
        // Retry if the request is considered idempotent
        return true;
    }

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

    if (!sent || this.requestSentRetryEnabled) {
        // Retry if the request has not been sent fully or
        // if it's OK to retry methods that have been sent
        return true;
    }
    // otherwise do not retry
    return false;
}