Search in sources :

Example 86 with ProtocolVersion

use of 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)

Example 87 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project camel by apache.

the class HipchatComponentConsumerTest method sendInOnlyMultipleUsers.

//TODO
@Test
public void sendInOnlyMultipleUsers() throws Exception {
    result.expectedMessageCount(1);
    String expectedResponse = "{\n" + "  \"items\" : [\n" + "    {\n" + "      \"date\" : \"2015-01-19T22:07:11.030740+00:00\",\n" + "      \"from\" : {\n" + "        \"id\" : 1647095,\n" + "        \"links\" : {\n" + "          \"self\" : \"https://api.hipchat.com/v2/user/1647095\"\n" + "        },\n" + "        \"mention_name\" : \"notifier\",\n" + "        \"name\" : \"Message Notifier\"\n" + "      },\n" + "      \"id\" : \"6567c6f7-7c1b-43cf-bed0-792b1d092919\",\n" + "      \"mentions\" : [ ],\n" + "      \"message\" : \"Unit test Alert\",\n" + "      \"type\" : \"message\"\n" + "    }\n" + "  ],\n" + "  \"links\" : {\n" + "    \"self\" : \"https://api.hipchat.com/v2/user/%40ShreyasPurohit/history/latest\"\n" + "  },\n" + "  \"maxResults\" : 1,\n" + "  \"startIndex\" : 0\n" + "}";
    HttpEntity mockHttpEntity = mock(HttpEntity.class);
    when(mockHttpEntity.getContent()).thenReturn(new ByteArrayInputStream(expectedResponse.getBytes(StandardCharsets.UTF_8)));
    when(closeableHttpResponse.getEntity()).thenReturn(mockHttpEntity);
    when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, ""));
    assertMockEndpointsSatisfied();
    assertCommonResultExchange(result.getExchanges().get(0));
}
Also used : HttpEntity(org.apache.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 88 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project robovm by robovm.

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 89 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project robovm by robovm.

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 90 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project robovm by robovm.

the class HttpService method handleRequest.

public void handleRequest(final HttpServerConnection conn, final HttpContext context) throws IOException, HttpException {
    context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
    HttpResponse response = null;
    try {
        HttpRequest request = conn.receiveRequestHeader();
        request.setParams(new DefaultedHttpParams(request.getParams(), this.params));
        ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
        if (!ver.lessEquals(HttpVersion.HTTP_1_1)) {
            // Downgrade protocol version if greater than HTTP/1.1 
            ver = HttpVersion.HTTP_1_1;
        }
        if (request instanceof HttpEntityEnclosingRequest) {
            if (((HttpEntityEnclosingRequest) request).expectContinue()) {
                response = this.responseFactory.newHttpResponse(ver, HttpStatus.SC_CONTINUE, context);
                response.setParams(new DefaultedHttpParams(response.getParams(), this.params));
                if (this.expectationVerifier != null) {
                    try {
                        this.expectationVerifier.verify(request, response, context);
                    } catch (HttpException ex) {
                        response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, HttpStatus.SC_INTERNAL_SERVER_ERROR, context);
                        response.setParams(new DefaultedHttpParams(response.getParams(), this.params));
                        handleException(ex, response);
                    }
                }
                if (response.getStatusLine().getStatusCode() < 200) {
                    // Send 1xx response indicating the server expections
                    // have been met
                    conn.sendResponseHeader(response);
                    conn.flush();
                    response = null;
                    conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
                }
            } else {
                conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
            }
        }
        if (response == null) {
            response = this.responseFactory.newHttpResponse(ver, HttpStatus.SC_OK, context);
            response.setParams(new DefaultedHttpParams(response.getParams(), this.params));
            context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
            context.setAttribute(ExecutionContext.HTTP_RESPONSE, response);
            this.processor.process(request, context);
            doService(request, response, context);
        }
        // Make sure the request content is fully consumed
        if (request instanceof HttpEntityEnclosingRequest) {
            HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
            if (entity != null) {
                entity.consumeContent();
            }
        }
    } catch (HttpException ex) {
        response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, HttpStatus.SC_INTERNAL_SERVER_ERROR, context);
        response.setParams(new DefaultedHttpParams(response.getParams(), this.params));
        handleException(ex, response);
    }
    this.processor.process(response, context);
    conn.sendResponseHeader(response);
    conn.sendResponseEntity(response);
    conn.flush();
    if (!this.connStrategy.keepAlive(response, context)) {
        conn.close();
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpEntity(org.apache.http.HttpEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpResponse(org.apache.http.HttpResponse) HttpException(org.apache.http.HttpException) ProtocolVersion(org.apache.http.ProtocolVersion) DefaultedHttpParams(org.apache.http.params.DefaultedHttpParams)

Aggregations

ProtocolVersion (org.apache.http.ProtocolVersion)113 BasicStatusLine (org.apache.http.message.BasicStatusLine)54 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)43 StatusLine (org.apache.http.StatusLine)42 Test (org.junit.Test)33 Header (org.apache.http.Header)26 HttpEntity (org.apache.http.HttpEntity)26 HttpResponse (org.apache.http.HttpResponse)22 StringEntity (org.apache.http.entity.StringEntity)20 URL (java.net.URL)16 BasicHeader (org.apache.http.message.BasicHeader)16 IOException (java.io.IOException)15 HttpURLConnection (java.net.HttpURLConnection)15 List (java.util.List)15 HashMap (java.util.HashMap)14 HttpHost (org.apache.http.HttpHost)12 ParseException (org.apache.http.ParseException)12 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)11 MockHttpStack (com.android.volley.mock.MockHttpStack)10 HttpRequest (org.apache.http.HttpRequest)10