Search in sources :

Example 36 with NoHttpResponseException

use of org.apache.http.NoHttpResponseException in project calcite-avatica by apache.

the class AvaticaCommonsHttpClientImpl method send.

@Override
public byte[] send(byte[] request) {
    while (true) {
        HttpClientContext context = HttpClientContext.create();
        context.setTargetHost(host);
        // Set the credentials if they were provided.
        if (null != this.credentialsProvider) {
            context.setCredentialsProvider(credentialsProvider);
            context.setAuthSchemeRegistry(authRegistry);
            context.setAuthCache(authCache);
        }
        if (null != userToken) {
            context.setUserToken(userToken);
        }
        ByteArrayEntity entity = new ByteArrayEntity(request, ContentType.APPLICATION_OCTET_STREAM);
        // Create the client with the AuthSchemeRegistry and manager
        HttpPost post = new HttpPost(uri);
        post.setEntity(entity);
        try (CloseableHttpResponse response = execute(post, context)) {
            final int statusCode = response.getStatusLine().getStatusCode();
            if (HttpURLConnection.HTTP_OK == statusCode || HttpURLConnection.HTTP_INTERNAL_ERROR == statusCode) {
                userToken = context.getUserToken();
                return EntityUtils.toByteArray(response.getEntity());
            } else if (HttpURLConnection.HTTP_UNAVAILABLE == statusCode) {
                LOG.debug("Failed to connect to server (HTTP/503), retrying");
                continue;
            }
            throw new RuntimeException("Failed to execute HTTP Request, got HTTP/" + statusCode);
        } catch (NoHttpResponseException e) {
            // This can happen when sitting behind a load balancer and a backend server dies
            LOG.debug("The server failed to issue an HTTP response, retrying");
            continue;
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            LOG.debug("Failed to execute HTTP request", e);
            throw new RuntimeException(e);
        }
    }
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) HttpPost(org.apache.http.client.methods.HttpPost) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) ClientProtocolException(org.apache.http.client.ClientProtocolException) URISyntaxException(java.net.URISyntaxException) NoHttpResponseException(org.apache.http.NoHttpResponseException) IOException(java.io.IOException)

Example 37 with NoHttpResponseException

use of org.apache.http.NoHttpResponseException in project platform_external_apache-http by android.

the class AndroidHttpClientConnection method parseResponseHeader.

/**
 * Parses the response headers and adds them to the
 * given {@code headers} object, and returns the response StatusLine
 * @param headers store parsed header to headers.
 * @throws IOException
 * @return StatusLine
 * @see HttpClientConnection#receiveResponseHeader()
 */
public StatusLine parseResponseHeader(Headers headers) throws IOException, ParseException {
    assertOpen();
    CharArrayBuffer current = new CharArrayBuffer(64);
    if (inbuffer.readLine(current) == -1) {
        throw new NoHttpResponseException("The target server failed to respond");
    }
    // Create the status line from the status string
    StatusLine statusline = BasicLineParser.DEFAULT.parseStatusLine(current, new ParserCursor(0, current.length()));
    if (HttpLog.LOGV)
        HttpLog.v("read: " + statusline);
    int statusCode = statusline.getStatusCode();
    // Parse header body
    CharArrayBuffer previous = null;
    int headerNumber = 0;
    while (true) {
        if (current == null) {
            current = new CharArrayBuffer(64);
        } else {
            // This must be he buffer used to parse the status
            current.clear();
        }
        int l = inbuffer.readLine(current);
        if (l == -1 || current.length() < 1) {
            break;
        }
        // Parse the header name and value
        // Check for folded headers first
        // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
        // discussion on folded headers
        char first = current.charAt(0);
        if ((first == ' ' || first == '\t') && previous != null) {
            // we have continuation folded header
            // so append value
            int start = 0;
            int length = current.length();
            while (start < length) {
                char ch = current.charAt(start);
                if (ch != ' ' && ch != '\t') {
                    break;
                }
                start++;
            }
            if (maxLineLength > 0 && previous.length() + 1 + current.length() - start > maxLineLength) {
                throw new IOException("Maximum line length limit exceeded");
            }
            previous.append(' ');
            previous.append(current, start, current.length() - start);
        } else {
            if (previous != null) {
                headers.parseHeader(previous);
            }
            headerNumber++;
            previous = current;
            current = null;
        }
        if (maxHeaderCount > 0 && headerNumber >= maxHeaderCount) {
            throw new IOException("Maximum header count exceeded");
        }
    }
    if (previous != null) {
        headers.parseHeader(previous);
    }
    if (statusCode >= 200) {
        this.metrics.incrementResponseCount();
    }
    return statusline;
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) StatusLine(org.apache.http.StatusLine) ParserCursor(org.apache.http.message.ParserCursor) CharArrayBuffer(org.apache.http.util.CharArrayBuffer) IOException(java.io.IOException)

Example 38 with NoHttpResponseException

use of org.apache.http.NoHttpResponseException in project gocd by gocd.

the class GoHttpClientHttpInvokerRequestExecutor method validateResponse.

private void validateResponse(HttpResponse response) throws IOException {
    StatusLine status = response.getStatusLine();
    if (status.getStatusCode() >= 400) {
        String messagePrefix = String.format("The server returned status code %d. Possible reasons include:", status.getStatusCode());
        List<String> reasons = Arrays.asList("This agent has been deleted from the configuration", "This agent is pending approval", "There is possibly a reverse proxy (or load balancer) that has been misconfigured. See " + docsUrl("/installation/configure-reverse-proxy.html#agents-and-reverse-proxies") + " for details.");
        String delimiter = "\n   - ";
        throw new ClientProtocolException(messagePrefix + delimiter + String.join(delimiter, reasons));
    }
    if (status.getStatusCode() >= 300) {
        throw new NoHttpResponseException("Did not receive successful HTTP response: status code = " + status.getStatusCode() + ", status message = [" + status.getReasonPhrase() + "]");
    }
}
Also used : StatusLine(org.apache.http.StatusLine) NoHttpResponseException(org.apache.http.NoHttpResponseException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Aggregations

NoHttpResponseException (org.apache.http.NoHttpResponseException)38 IOException (java.io.IOException)13 SocketException (java.net.SocketException)12 StatusLine (org.apache.http.StatusLine)12 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)11 SocketTimeoutException (java.net.SocketTimeoutException)10 ParserCursor (org.apache.http.message.ParserCursor)9 UnknownHostException (java.net.UnknownHostException)8 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)8 HttpRequest (org.apache.http.HttpRequest)7 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)7 ConnectException (java.net.ConnectException)6 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)6 InterruptedIOException (java.io.InterruptedIOException)5 SSLException (javax.net.ssl.SSLException)5 HashMap (java.util.HashMap)4 HttpEntity (org.apache.http.HttpEntity)4 RequestConfig (org.apache.http.client.config.RequestConfig)4 HttpPost (org.apache.http.client.methods.HttpPost)4 SolrException (org.apache.solr.common.SolrException)4