Package org.apache.http.client.methods

Examples of org.apache.http.client.methods.CloseableHttpResponse


            LOG.error("Invalid version check URI.", e);
            return;
        }

        CloseableHttpClient http = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            response = http.execute(get);

            if (response.getStatusLine().getStatusCode() != 200) {
                LOG.error("Expected version check HTTP status code [200] but got [{}]", response.getStatusLine().getStatusCode());
                return;
            }

            HttpEntity entity = response.getEntity();

            StringWriter writer = new StringWriter();
            IOUtils.copy(entity.getContent(), writer, Charset.forName("UTF-8"));
            String body = writer.toString();

            VersionCheckResponse parsedResponse = parse(body);
            Version reportedVersion = new Version(parsedResponse.version.major, parsedResponse.version.minor, parsedResponse.version.patch);

            LOG.debug("Version check reports current version: " + parsedResponse);

            if (reportedVersion.greaterMinor(ServerVersion.VERSION)) {
                LOG.debug("Reported version is higher than ours ({}). Writing notification.", ServerVersion.VERSION);

                Notification notification = notificationService.buildNow()
                        .addSeverity(Notification.Severity.NORMAL)
                        .addType(Notification.Type.OUTDATED_VERSION)
                        .addDetail("current_version", parsedResponse.toString());
                notificationService.publishIfFirst(notification);
            } else {
                LOG.debug("Reported version is not higher than ours ({}).", ServerVersion.VERSION);
                notificationService.fixed(Notification.Type.OUTDATED_VERSION);
            }

            EntityUtils.consume(entity);
        } catch (IOException e) {
            LOG.warn("Could not perform version check.", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                LOG.warn("Could not close HTTP connection to version check API.", e);
            }
        }
View Full Code Here


            LOG.error("Invalid telemetry service endpoint URI.", e);
            return;
        }

        CloseableHttpClient http = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            response = http.execute(post);

            if (response.getStatusLine().getStatusCode() != 202) {
                LOG.error("Telemetry is activated: Expected HTTP response status code [202] but got [{}]", response.getStatusLine().getStatusCode());
                return;
            }
        } catch (IOException e) {
            LOG.warn("Telemetry is activated: Could not transmit metrics.", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                LOG.warn("Telemetry is activated: Could not close HTTP connection to monitoring service.", e);
            }
        }
View Full Code Here

        final CloseableHttpClient httpclient =
            HttpClients.custom().addInterceptorFirst(new BraveHttpRequestInterceptor(clientTracer, Optional.<String>absent()))
                .addInterceptorFirst(new BraveHttpResponseInterceptor(clientTracer)).build();
        try {
            final HttpGet httpGet = new HttpGet(REQUEST_WITH_QUERY_PARAMS);
            final CloseableHttpResponse httpClientResponse = httpclient.execute(httpGet);
            try {
                assertEquals(200, httpClientResponse.getStatusLine().getStatusCode());
            } finally {
                httpClientResponse.close();
            }
            mockServer.verify();

            final InOrder inOrder = inOrder(clientTracer);
            inOrder.verify(clientTracer).startNewSpan(PATH);
View Full Code Here

        final CloseableHttpClient httpclient =
                HttpClients.custom().addInterceptorFirst(new BraveHttpRequestInterceptor(clientTracer, Optional.of(SERVICE)))
                        .addInterceptorFirst(new BraveHttpResponseInterceptor(clientTracer)).build();
        try {
            final HttpGet httpGet = new HttpGet(REQUEST);
            final CloseableHttpResponse httpClientResponse = httpclient.execute(httpGet);
            try {
                assertEquals(200, httpClientResponse.getStatusLine().getStatusCode());
            } finally {
                httpClientResponse.close();
            }
            mockServer.verify();

            final InOrder inOrder = inOrder(clientTracer);
            inOrder.verify(clientTracer).startNewSpan(FULL_PATH);
View Full Code Here

        final CloseableHttpClient httpclient =
            HttpClients.custom().addInterceptorFirst(new BraveHttpRequestInterceptor(clientTracer, Optional.<String>absent()))
                .addInterceptorFirst(new BraveHttpResponseInterceptor(clientTracer)).build();
        try {
            final HttpGet httpGet = new HttpGet(REQUEST);
            final CloseableHttpResponse httpClientResponse = httpclient.execute(httpGet);
            try {
                assertEquals(200, httpClientResponse.getStatusLine().getStatusCode());
            } finally {
                httpClientResponse.close();
            }
            mockServer.verify();

            final InOrder inOrder = inOrder(clientTracer);
            inOrder.verify(clientTracer).startNewSpan(PATH);
View Full Code Here

        final CloseableHttpClient httpclient =
            HttpClients.custom().addInterceptorFirst(new BraveHttpRequestInterceptor(clientTracer, Optional.<String>absent()))
                .addInterceptorFirst(new BraveHttpResponseInterceptor(clientTracer)).build();
        try {
            final HttpGet httpGet = new HttpGet(REQUEST);
            final CloseableHttpResponse httpClientResponse = httpclient.execute(httpGet);
            try {
                assertEquals(200, httpClientResponse.getStatusLine().getStatusCode());
            } finally {
                httpClientResponse.close();
            }
            mockServer.verify();

            final InOrder inOrder = inOrder(clientTracer);
            inOrder.verify(clientTracer).startNewSpan(PATH);
View Full Code Here

public class ApacheHttpClientToMockServerResponseMapperTest {

    @Test
    public void shouldMapHttpClientResponseToHttpResponse() throws IOException {
        // given
        CloseableHttpResponse httpClientResponse = mock(CloseableHttpResponse.class);
        when(httpClientResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 500, "Server Error"));
        when(httpClientResponse.getAllHeaders()).thenReturn(new org.apache.http.Header[]{
                new BasicHeader("header_name", "header_value"),
                new BasicHeader("Set-Cookie", "cookie_name=cookie_value")
        });
        when(httpClientResponse.getEntity()).thenReturn(new StringEntity("some_other_body"));

        // when
        HttpResponse httpResponse = new ApacheHttpClientToMockServerResponseMapper().mapApacheHttpClientResponseToMockServerResponse(httpClientResponse, false);

        // then
View Full Code Here

    }

    @Test
    public void shouldFilterHeader() throws IOException {
        // given
        CloseableHttpResponse httpClientResponse = mock(CloseableHttpResponse.class);
        when(httpClientResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 500, "Server Error"));
        when(httpClientResponse.getAllHeaders()).thenReturn(new org.apache.http.Header[]{
                new BasicHeader("header_name", "header_value"),
                new BasicHeader("Content-Encoding", "gzip"),
                new BasicHeader("Content-Length", "1024"),
                new BasicHeader("Transfer-Encoding", "chunked")
        });
        when(httpClientResponse.getEntity()).thenReturn(new StringEntity(""));

        // when
        HttpResponse httpResponse = new ApacheHttpClientToMockServerResponseMapper().mapApacheHttpClientResponseToMockServerResponse(httpClientResponse, false);

        // then
View Full Code Here

    }

    @Test
    public void shouldIgnoreIncorrectlyFormattedCookies() throws IOException {
        // given
        CloseableHttpResponse httpClientResponse = mock(CloseableHttpResponse.class);
        when(httpClientResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 500, "Server Error"));
        when(httpClientResponse.getAllHeaders()).thenReturn(new org.apache.http.Header[]{
                new BasicHeader("Set-Cookie", "valid_name=valid_value"),
                new BasicHeader("Set-Cookie", "=invalid"),
                new BasicHeader("Set-Cookie", "valid_name="),
                new BasicHeader("Set-Cookie", "invalid"),
                new BasicHeader("Set-Cookie", "")
        });
        when(httpClientResponse.getEntity()).thenReturn(new StringEntity(""));

        // when
        HttpResponse httpResponse = new ApacheHttpClientToMockServerResponseMapper().mapApacheHttpClientResponseToMockServerResponse(httpClientResponse, false);

        // then
View Full Code Here

        return EasyMock.expect(resp).andReturn(Proxies.enhanceResponse(response));
    }

    protected IExpectationSetters<CloseableHttpResponse> backendExpectsRequestAndReturn(
        final HttpRequestWrapper request, final CloseableHttpResponse response) throws Exception {
        final CloseableHttpResponse resp = mockBackend.execute(EasyMock.isA(HttpRoute.class),
            EasyMock.eq(request), EasyMock.isA(HttpClientContext.class),
            EasyMock.<HttpExecutionAware> isNull());
        return EasyMock.expect(resp).andReturn(response);
    }
View Full Code Here

TOP

Related Classes of org.apache.http.client.methods.CloseableHttpResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.