Search in sources :

Example 51 with HttpContext

use of org.apache.http.protocol.HttpContext in project stocator by CODAIT.

the class SwiftConnectionManager method getRetryHandler.

/**
 * Creates custom retry handler to be used if HTTP exception happens
 *
 * @return retry handler
 */
private HttpRequestRetryHandler getRetryHandler() {
    HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {

        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount >= connectionConfiguration.getExecutionCount()) {
                // Do not retry if over max retry count
                LOG.debug("Execution count {} is bigger then threashold. Stop", executionCount);
                return false;
            }
            if (exception instanceof NoHttpResponseException) {
                LOG.debug("NoHttpResponseException exception. Retry count {}", executionCount);
                return true;
            }
            if (exception instanceof UnknownHostException) {
                LOG.debug("UnknownHostException. Retry count {}", executionCount);
                return true;
            }
            if (exception instanceof ConnectTimeoutException) {
                LOG.debug("ConnectTimeoutException. Retry count {}", executionCount);
                return true;
            }
            if (exception instanceof SocketTimeoutException || exception.getClass() == SocketTimeoutException.class || exception.getClass().isInstance(SocketTimeoutException.class)) {
                // Connection refused
                LOG.debug("socketTimeoutException Retry count {}", executionCount);
                return true;
            }
            if (exception instanceof InterruptedIOException) {
                // Timeout
                LOG.debug("InterruptedIOException Retry count {}", executionCount);
                return true;
            }
            if (exception instanceof SSLException) {
                LOG.debug("SSLException Retry count {}", executionCount);
                return true;
            }
            HttpClientContext clientContext = HttpClientContext.adapt(context);
            HttpRequest request = clientContext.getRequest();
            boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
            if (idempotent) {
                LOG.debug("HttpEntityEnclosingRequest. Retry count {}", executionCount);
                return true;
            }
            LOG.debug("Retry stopped. Retry count {}", executionCount);
            return false;
        }
    };
    return myRetryHandler;
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) HttpRequest(org.apache.http.HttpRequest) InterruptedIOException(java.io.InterruptedIOException) UnknownHostException(java.net.UnknownHostException) HttpContext(org.apache.http.protocol.HttpContext) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) SocketTimeoutException(java.net.SocketTimeoutException) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 52 with HttpContext

use of org.apache.http.protocol.HttpContext in project k-9 by k9mail.

the class WebDavStoreTest method createOkResponseWithCookie.

private Answer<HttpResponse> createOkResponseWithCookie() {
    return new Answer<HttpResponse>() {

        @Override
        public HttpResponse answer(InvocationOnMock invocation) {
            HttpContext context = (HttpContext) invocation.getArguments()[1];
            if (context.getAttribute(ClientContext.COOKIE_STORE) != null) {
                BasicCookieStore cookieStore = (BasicCookieStore) context.getAttribute(ClientContext.COOKIE_STORE);
                BasicClientCookie cookie = new BasicClientCookie("cookie", "meLikeCookie");
                cookieStore.addCookie(cookie);
            }
            return OK_200_RESPONSE;
        }
    };
}
Also used : Answer(org.mockito.stubbing.Answer) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpContext(org.apache.http.protocol.HttpContext) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie)

Example 53 with HttpContext

use of org.apache.http.protocol.HttpContext in project seldon-core by SeldonIO.

the class InternalPredictionService method sendFeedbackREST.

public void sendFeedbackREST(String feedback, String serviceName) {
    long timeNow = System.currentTimeMillis();
    URI uri;
    try {
        URIBuilder builder = new URIBuilder().setScheme("http").setHost(serviceName).setPort(appProperties.getEngineContainerPort()).setPath("/api/v0.1/feedback");
        uri = builder.build();
    } catch (URISyntaxException e) {
        throw new SeldonAPIException(SeldonAPIException.ApiExceptionType.APIFE_INVALID_ENDPOINT_URL, "Host: " + serviceName + " port:" + appProperties.getEngineContainerPort());
    }
    StringEntity requestEntity = new StringEntity(feedback, ContentType.APPLICATION_JSON);
    HttpContext context = HttpClientContext.create();
    HttpPost httpPost = new HttpPost(uri);
    httpPost.setEntity(requestEntity);
    try {
        if (logger.isDebugEnabled())
            logger.debug("Requesting " + httpPost.getURI().toString());
        CloseableHttpResponse resp = httpClient.execute(httpPost, context);
        try {
            resp.getEntity();
        } finally {
            if (resp != null)
                resp.close();
            if (logger.isDebugEnabled())
                logger.debug("External prediction server took " + (System.currentTimeMillis() - timeNow) + "ms");
        }
    } catch (IOException e) {
        logger.error("Couldn't retrieve prediction from external prediction server - ", e);
        throw new SeldonAPIException(SeldonAPIException.ApiExceptionType.APIFE_MICROSERVICE_ERROR, e.toString());
    } catch (Exception e) {
        logger.error("Couldn't retrieve prediction from external prediction server - ", e);
        throw new SeldonAPIException(SeldonAPIException.ApiExceptionType.APIFE_MICROSERVICE_ERROR, e.toString());
    } finally {
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpPost(org.apache.http.client.methods.HttpPost) SeldonAPIException(io.seldon.apife.exception.SeldonAPIException) HttpContext(org.apache.http.protocol.HttpContext) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) SeldonAPIException(io.seldon.apife.exception.SeldonAPIException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 54 with HttpContext

use of org.apache.http.protocol.HttpContext in project Anki-Android by Ramblurr.

the class HttpFetcher method fetchThroughHttp.

public static String fetchThroughHttp(String address, String encoding) {
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet(address);
        HttpResponse response = httpClient.execute(httpGet, localContext);
        if (!response.getStatusLine().toString().contains("OK")) {
            return "FAILED";
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), Charset.forName(encoding)));
        StringBuilder stringBuilder = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
        }
        return stringBuilder.toString();
    } catch (Exception e) {
        return "FAILED with exception: " + e.getMessage();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) BufferedReader(java.io.BufferedReader) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 55 with HttpContext

use of org.apache.http.protocol.HttpContext in project robolectric by robolectric.

the class FakeHttpTest method makeRequest.

private void makeRequest(String uri) throws HttpException, IOException {
    FakeHttp.addPendingHttpResponse(200, "a happy response body");
    ConnectionKeepAliveStrategy connectionKeepAliveStrategy = new ConnectionKeepAliveStrategy() {

        @Override
        public long getKeepAliveDuration(HttpResponse httpResponse, HttpContext httpContext) {
            return 0;
        }
    };
    DefaultRequestDirector requestDirector = new DefaultRequestDirector(null, null, null, connectionKeepAliveStrategy, null, null, null, null, null, null, null, null);
    requestDirector.execute(null, new HttpGet(uri), null);
}
Also used : ConnectionKeepAliveStrategy(org.apache.http.conn.ConnectionKeepAliveStrategy) HttpGet(org.apache.http.client.methods.HttpGet) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) DefaultRequestDirector(org.apache.http.impl.client.DefaultRequestDirector)

Aggregations

HttpContext (org.apache.http.protocol.HttpContext)169 HttpResponse (org.apache.http.HttpResponse)77 IOException (java.io.IOException)72 HttpRequest (org.apache.http.HttpRequest)55 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)47 HttpException (org.apache.http.HttpException)30 Test (org.junit.Test)29 HttpGet (org.apache.http.client.methods.HttpGet)27 HttpHost (org.apache.http.HttpHost)26 HttpPost (org.apache.http.client.methods.HttpPost)26 ArrayList (java.util.ArrayList)22 Header (org.apache.http.Header)21 StringEntity (org.apache.http.entity.StringEntity)21 NameValuePair (org.apache.http.NameValuePair)18 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)18 ProtocolException (org.apache.http.ProtocolException)17 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)17 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)17 URI (java.net.URI)16 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)16