Example usage for org.apache.http.protocol ExecutionContext HTTP_REQUEST

List of usage examples for org.apache.http.protocol ExecutionContext HTTP_REQUEST

Introduction

In this page you can find the example usage for org.apache.http.protocol ExecutionContext HTTP_REQUEST.

Prototype

String HTTP_REQUEST

To view the source code for org.apache.http.protocol ExecutionContext HTTP_REQUEST.

Click Source Link

Usage

From source file:com.flicklib.service.HttpClientSourceLoader.java

private Source buildSource(String url, HttpResponse response, HttpRequestBase httpMethod, HttpContext ctx)
        throws IOException {
    LOGGER.info("Finished loading at " + httpMethod.getURI().toString());
    final HttpEntity entity = response.getEntity();
    String responseCharset = EntityUtils.getContentCharSet(entity);
    String contentType = EntityUtils.getContentMimeType(entity);
    LOGGER.info("Response charset = " + responseCharset);
    String content = EntityUtils.toString(entity);

    HttpUriRequest req = (HttpUriRequest) ctx.getAttribute(ExecutionContext.HTTP_REQUEST);
    HttpHost target = (HttpHost) ctx.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    URI resultUri;//w  ww  . ja va 2s  .  c  om
    try {
        resultUri = (target != null && req != null) ? new URI(target.toURI() + req.getURI().toString())
                : httpMethod.getURI();
    } catch (URISyntaxException e) {
        e.printStackTrace();
        resultUri = httpMethod.getURI();
    }
    // String contentType = URLConnection.guessContentTypeFromName(url)
    return new Source(resultUri.toString(), content, contentType, url);
}

From source file:com.fheebiy.http.lite.apache.DefaultHttpRequestRetryHandler.java

protected boolean retryRequest(final HttpContext context) {
    final HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
    final Object obj = context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    final boolean isSent = obj == null ? false : (Boolean) obj;

    if (requestIsAborted(request)) {
        return false;
    }/*  w w w.  j a v a 2s  .  com*/

    if (handleAsIdempotent(request)) {
        // Retry if the request is considered idempotent
        return true;
    }
    if (!isSent || 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;
    }
    return false;
}

From source file:br.ufsc.das.gtscted.shibbauth.Connection.java

public String[] httpGetWithEndpoint(String url) throws IOException {
    HttpGet httpget = new HttpGet(url);
    HttpContext context = new BasicHttpContext();
    HttpResponse response = httpClient.execute(httpget, context);
    String responseAsStr = readResponse(response.getEntity().getContent()).toString();
    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new IOException(response.getStatusLine().toString());
    }/*from   w  w  w .  j a  v  a2s.c  o m*/
    HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
    HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    String currentUrl = currentHost.toURI() + currentReq.getURI();

    URL endpointUrl = new URL(currentUrl);
    String endpointDomain = "https://" + endpointUrl.getHost();

    String[] returnArray = { endpointDomain, responseAsStr };
    return returnArray;
}

From source file:org.everit.authentication.http.session.ecm.tests.SessionAuthenticationComponentTest.java

private void logoutGet(final HttpContext httpContext) throws ClientProtocolException, IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(logoutUrl);
    HttpResponse httpResponse = httpClient.execute(httpGet, httpContext);
    Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, httpResponse.getStatusLine().getStatusCode());

    HttpUriRequest currentReq = (HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST);
    HttpHost currentHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    String currentUrl = (currentReq.getURI().isAbsolute()) ? currentReq.getURI().toString()
            : (currentHost.toURI() + currentReq.getURI());
    Assert.assertEquals(loggedOutUrl, currentUrl);
}

From source file:com.adavr.http.Client.java

public Client() {
    httpClient = new DefaultHttpClient(new BasicClientConnectionManager());
    localContext = new BasicHttpContext();
    byteStream = new ByteArrayOutputStream();

    CookieStore cookieStore = new BasicCookieStore();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    httpClient.addRequestInterceptor(new HttpRequestInterceptor() {

        @Override//www .  j  a  v  a2s  .  c  o  m
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }
        }
    });

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header ceheader = entity.getContentEncoding();
            if (ceheader != null) {
                HeaderElement[] codecs = ceheader.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }
    });

    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, DEFAULT_TIMEOUT);
    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, DEFAULT_USER_AGENT);
    HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {

        @Override
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount >= DEFAULT_RETRY) {
                // Do not retry if over max retry count
                return false;
            }
            System.out.println(exception.getClass().getName());
            if (exception instanceof NoHttpResponseException) {
                // Retry if the server dropped connection on us
                return true;
            }
            if (exception instanceof SSLHandshakeException) {
                // Do not retry on SSL handshake exception
                return false;
            }
            HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
            boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
            if (idempotent) {
                // Retry if the request is considered idempotent
                return true;
            }
            return false;
        }
    };

    httpClient.setHttpRequestRetryHandler(myRetryHandler);
}

From source file:eu.europeanaconnect.erds.HTTPResolverMultiThreaded.java

/**
 * TODO: ->Nuno: explain this part please
 *//*from  ww  w. ja  v  a2s  .  com*/
public HTTPResolverMultiThreaded() {
    HttpParams params = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(params, MAX_CONNECTIONS);
    ConnPerRouteBean connPerRoute = new ConnPerRouteBean(CONN_PER_ROUTE);
    ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

    ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);

    this.httpClient = new DefaultHttpClient(clientConnectionManager, params);
    HttpConnectionParams.setConnectionTimeout(this.httpClient.getParams(), CONECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(this.httpClient.getParams(), SOCKET_TIMEOUT);
    this.httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
    this.httpClient.getParams().setBooleanParameter(ClientPNames.REJECT_RELATIVE_REDIRECT, false);
    this.httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, this.userAgentString);

    HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
        @Override
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount >= MAXIMUM_REQUEST_RETRIES) {
                // Do not retry if over max retry count
                return false;
            }
            if (exception instanceof NoHttpResponseException) {
                // Retry if the server dropped connection on us
                return true;
            }
            if (exception instanceof SSLHandshakeException) {
                // Do not retry on SSL handshake exception
                return false;
            }
            HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
            boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
            if (idempotent) {
                // Retry if the request is considered idempotent 
                return true;
            }
            return false;
        }

    };
    this.httpClient.setHttpRequestRetryHandler(myRetryHandler);
}