Search in sources :

Example 91 with ProtocolVersion

use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project XobotOS by xamarin.

the class BasicLineParser method parseStatusLine.

// non-javadoc, see interface LineParser
public StatusLine parseStatusLine(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException {
    if (buffer == null) {
        throw new IllegalArgumentException("Char array buffer may not be null");
    }
    if (cursor == null) {
        throw new IllegalArgumentException("Parser cursor may not be null");
    }
    int indexFrom = cursor.getPos();
    int indexTo = cursor.getUpperBound();
    try {
        // handle the HTTP-Version
        ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
        // handle the Status-Code
        skipWhitespace(buffer, cursor);
        int i = cursor.getPos();
        int blank = buffer.indexOf(' ', i, indexTo);
        if (blank < 0) {
            blank = indexTo;
        }
        int statusCode = 0;
        try {
            statusCode = Integer.parseInt(buffer.substringTrimmed(i, blank));
        } catch (NumberFormatException e) {
            throw new ParseException("Unable to parse status code from status line: " + buffer.substring(indexFrom, indexTo));
        }
        //handle the Reason-Phrase
        i = blank;
        String reasonPhrase = null;
        if (i < indexTo) {
            reasonPhrase = buffer.substringTrimmed(i, indexTo);
        } else {
            reasonPhrase = "";
        }
        return createStatusLine(ver, statusCode, reasonPhrase);
    } catch (IndexOutOfBoundsException e) {
        throw new ParseException("Invalid status line: " + buffer.substring(indexFrom, indexTo));
    }
}
Also used : ParseException(org.apache.http.ParseException) ProtocolVersion(org.apache.http.ProtocolVersion)

Example 92 with ProtocolVersion

use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project XobotOS by xamarin.

the class BasicHttpResponse method setStatusCode.

// non-javadoc, see interface HttpResponse
public void setStatusCode(int code) {
    // argument checked in BasicStatusLine constructor
    ProtocolVersion ver = this.statusline.getProtocolVersion();
    this.statusline = new BasicStatusLine(ver, code, getReason(code));
}
Also used : ProtocolVersion(org.apache.http.ProtocolVersion)

Example 93 with ProtocolVersion

use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project XobotOS by xamarin.

the class HttpRequestBase method getRequestLine.

public RequestLine getRequestLine() {
    String method = getMethod();
    ProtocolVersion ver = getProtocolVersion();
    URI uri = getURI();
    String uritext = null;
    if (uri != null) {
        uritext = uri.toASCIIString();
    }
    if (uritext == null || uritext.length() == 0) {
        uritext = "/";
    }
    return new BasicRequestLine(method, uritext, ver);
}
Also used : BasicRequestLine(org.apache.http.message.BasicRequestLine) ProtocolVersion(org.apache.http.ProtocolVersion) URI(java.net.URI)

Example 94 with ProtocolVersion

use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project XobotOS by xamarin.

the class DefaultConnectionReuseStrategy method keepAlive.

// see interface ConnectionReuseStrategy
public boolean keepAlive(final HttpResponse response, final HttpContext context) {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null.");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null.");
    }
    HttpConnection conn = (HttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
    if (conn != null && !conn.isOpen())
        return false;
    // do NOT check for stale connection, that is an expensive operation
    // Check for a self-terminating entity. If the end of the entity will
    // be indicated by closing the connection, there is no keep-alive.
    HttpEntity entity = response.getEntity();
    ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
    if (entity != null) {
        if (entity.getContentLength() < 0) {
            if (!entity.isChunked() || ver.lessEquals(HttpVersion.HTTP_1_0)) {
                // encoded, the connection cannot be reused
                return false;
            }
        }
    }
    // Check for the "Connection" header. If that is absent, check for
    // the "Proxy-Connection" header. The latter is an unspecified and
    // broken but unfortunately common extension of HTTP.
    HeaderIterator hit = response.headerIterator(HTTP.CONN_DIRECTIVE);
    if (!hit.hasNext())
        hit = response.headerIterator("Proxy-Connection");
    if (hit.hasNext()) {
        try {
            TokenIterator ti = createTokenIterator(hit);
            boolean keepalive = false;
            while (ti.hasNext()) {
                final String token = ti.nextToken();
                if (HTTP.CONN_CLOSE.equalsIgnoreCase(token)) {
                    return false;
                } else if (HTTP.CONN_KEEP_ALIVE.equalsIgnoreCase(token)) {
                    // continue the loop, there may be a "close" afterwards
                    keepalive = true;
                }
            }
            if (keepalive)
                return true;
        // neither "close" nor "keep-alive", use default policy
        } catch (ParseException px) {
            // we don't have logging in HttpCore, so the exception is lost
            return false;
        }
    }
    // default since HTTP/1.1 is persistent, before it was non-persistent
    return !ver.lessEquals(HttpVersion.HTTP_1_0);
}
Also used : HttpEntity(org.apache.http.HttpEntity) TokenIterator(org.apache.http.TokenIterator) BasicTokenIterator(org.apache.http.message.BasicTokenIterator) HttpConnection(org.apache.http.HttpConnection) HeaderIterator(org.apache.http.HeaderIterator) ParseException(org.apache.http.ParseException) ProtocolVersion(org.apache.http.ProtocolVersion)

Example 95 with ProtocolVersion

use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project camel by apache.

the class HipchatComponentConsumerTest method sendInOnlyNoResponse.

@Test
public void sendInOnlyNoResponse() throws Exception {
    result.expectedMessageCount(0);
    HttpEntity mockHttpEntity = mock(HttpEntity.class);
    when(mockHttpEntity.getContent()).thenReturn(null);
    when(closeableHttpResponse.getEntity()).thenReturn(mockHttpEntity);
    when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, ""));
    assertMockEndpointsSatisfied();
}
Also used : HttpEntity(org.apache.http.HttpEntity) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Aggregations

ProtocolVersion (org.apache.http.ProtocolVersion)143 BasicStatusLine (org.apache.http.message.BasicStatusLine)67 Test (org.junit.Test)53 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)50 StatusLine (org.apache.http.StatusLine)44 HttpResponse (org.apache.http.HttpResponse)32 HttpEntity (org.apache.http.HttpEntity)31 Header (org.apache.http.Header)25 StringEntity (org.apache.http.entity.StringEntity)25 IOException (java.io.IOException)18 BasicHeader (org.apache.http.message.BasicHeader)15 URL (java.net.URL)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 HttpURLConnection (java.net.HttpURLConnection)13 HashMap (java.util.HashMap)13 List (java.util.List)13 ParseException (org.apache.http.ParseException)13 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)13 HttpHost (org.apache.http.HttpHost)12 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)11