Example usage for org.apache.http.client.methods CloseableHttpResponse getStatusLine

List of usage examples for org.apache.http.client.methods CloseableHttpResponse getStatusLine

Introduction

In this page you can find the example usage for org.apache.http.client.methods CloseableHttpResponse getStatusLine.

Prototype

StatusLine getStatusLine();

Source Link

Usage

From source file:org.wildfly.test.integration.microprofile.health.MicroProfileHealthSecuredHTTPEndpointTestCase.java

@Test
public void securedHTTPEndpoint() throws Exception {
    final String healthURL = "http://" + managementClient.getMgmtAddress() + ":"
            + managementClient.getMgmtPort() + "/health";

    try (CloseableHttpClient client = HttpClients.createDefault()) {

        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("testSuite", "testSuitePassword"));
        HttpClientContext hcContext = HttpClientContext.create();
        hcContext.setCredentialsProvider(credentialsProvider);

        CloseableHttpResponse resp = client.execute(new HttpGet(healthURL), hcContext);
        assertEquals(200, resp.getStatusLine().getStatusCode());
        String content = MicroProfileHealthHTTPEndpointTestCase.getContent(resp);
        resp.close();//from  w w  w .  j ava2s .c  om
        assertTrue("'UP' message is expected", content.contains("UP"));
    }
}

From source file:io.crate.rest.AdminUIIntegrationTest.java

@Test
public void testNotFound() throws Exception {
    CloseableHttpResponse response = browserGet("/static/does/not/exist.html");
    assertThat(response.getStatusLine().getStatusCode(), is(404));
}

From source file:org.esigate.HttpErrorPage.java

/**
 * Create an HTTP error page exception from an Http response.
 * /*from w  w  w . j a  v a  2  s. c  om*/
 * @param httpResponse
 *            backend response.
 */
public HttpErrorPage(CloseableHttpResponse httpResponse) {
    super(httpResponse.getStatusLine().getStatusCode() + " " + httpResponse.getStatusLine().getReasonPhrase());
    this.httpResponse = httpResponse;
    // Consume the entity and replace it with an in memory Entity
    httpResponse.setEntity(toMemoryEntity(httpResponse.getEntity()));
}

From source file:cf.spring.servicebroker.ServiceBrokerErrorHandlingTest.java

@Test
public void returnsErrorMessage() throws Exception {
    final ServiceBrokerHandler.ProvisionBody provisionBody = new ServiceBrokerHandler.ProvisionBody(BROKER_ID,
            PLAN_ID, UUID.randomUUID(), UUID.randomUUID());
    final HttpUriRequest provisionRequest = RequestBuilder.put()
            .setUri("http://localhost:8080/v2/service_instances/" + UUID.randomUUID())
            .setEntity(new StringEntity(mapper.writeValueAsString(provisionBody), ContentType.APPLICATION_JSON))
            .build();// w  w w  .j av  a 2 s  . c o m
    final CloseableHttpResponse provisionResponse = client.execute(provisionRequest);
    assertEquals(provisionResponse.getStatusLine().getStatusCode(), HTTP_RESPONSE_CODE);
    final JsonNode responseJson = mapper.readTree(provisionResponse.getEntity().getContent());

    assertTrue(responseJson.has("description"));
    assertEquals(responseJson.get("description").asText(), HTTP_RESPONSE_MESSAGE);
}

From source file:org.geosdi.geoplatform.experimental.openam.support.config.connector.OpenAMConnector.java

/**
 * @param token//from   w ww. java  2  s  . c o m
 * @return {@link Boolean}
 * @throws Exception
 */
@Override
public IOpenAMValidateToken validateToken(String token) throws Exception {
    Preconditions.checkArgument((token != null) && !(token.isEmpty()),
            "The Token to validate " + "must not be null or an Empty String.");
    logger.debug(
            "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@TRYING TO VALIDATE_TOKEN WITH " + "OPENAM_CONNECTOR_SETTINGS : {}\n",
            this.openAMConnectorSettings);
    ValidateTokenRequest validateTokenRequest = this.openAMRequestMediator.getRequest(VALIDATE_TOKEN);
    URIBuilder uriBuilder = this.buildURI(this.openAMConnectorSettings,
            validateTokenRequest.setExtraPathParam(token));
    validateTokenRequest.addRequestParameter(uriBuilder,
            this.requestParameterMediator.getRequest(ACTION_VALIDATE));

    logger.debug("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@OPENAM_VALIDATE_TOKEN_CONNECTOR_URI : {}\n",
            URLDecoder.decode(uriBuilder.toString(), "UTF-8"));

    HttpPost httpPost = new HttpPost(uriBuilder.build());
    httpPost.addHeader("Content-Type", "application/json");
    CloseableHttpResponse response = this.httpClient.execute(httpPost);

    if (response.getStatusLine().getStatusCode() != 200) {
        throw new IllegalStateException(
                "OpenAMValidateToken Error Code : " + response.getStatusLine().getStatusCode());
    }

    return this.openAMReader.readValue(response.getEntity().getContent(), OpenAMValidateToken.class);
}

From source file:com.meplato.store2.ApacheHttpResponse.java

/**
 * Instantiates a new instance of ApacheHttpResponse,
 * then close the response./*  w  w  w . j a  va  2 s  .  c o  m*/
 *
 * @param response the HTTP response from the ApacheHttpClient.
 * @throws ServiceException if e.g. serialization of the response fails.
 */
public ApacheHttpResponse(CloseableHttpResponse response) throws ServiceException {
    this.statusCode = response.getStatusLine().getStatusCode();

    HttpEntity entity = response.getEntity();
    if (entity != null) {
        try {
            ContentType contentType = ContentType.getOrDefault(entity);
            Charset charset = contentType.getCharset();
            this.body = EntityUtils.toString(entity, charset);
        } catch (IOException e) {
            EntityUtils.consumeQuietly(entity);
            throw new ServiceException("Error deserializing data", null, e);
        } finally {
            try {
                response.close();
            } catch (IOException e) {
            }
        }
    } else {
        this.body = null;
    }
}

From source file:com.asquera.elasticsearch.plugins.http.auth.integration.IpAuthenticationIntegrationTest.java

@Test
public void proxyViaLocalhostIpAuthenticatesWhitelistedClients() throws Exception {
    HttpUriRequest request = httpRequest();
    request.setHeader("X-Forwarded-For", whitelistedIp);
    CloseableHttpResponse response = closeableHttpClient().execute(request);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
    request = httpRequest();/*from  w w w.j a v a2s .  c  o m*/
    request.setHeader("X-Forwarded-For", notWhitelistedIp + "," + whitelistedIp);
    response = closeableHttpClient().execute(request);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
    request = httpRequest();
    request.setHeader("X-Forwarded-For", notWhitelistedIp + "," + whitelistedIp + "," + trustedIp);
    response = closeableHttpClient().execute(request);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
}

From source file:com.asquera.elasticsearch.plugins.http.auth.integration.IpAuthenticationIntegrationTest.java

@Test
public void proxyViaLocalhostIpUnauthenticatesNonWhitelistedClients() throws Exception {
    HttpUriRequest request = httpRequest();
    request.setHeader("X-Forwarded-For", notWhitelistedIp);
    CloseableHttpResponse response = closeableHttpClient().execute(request);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.UNAUTHORIZED.getStatus()));
    request = httpRequest();/*from  w w  w .  ja v a  2 s  .c o  m*/
    request.setHeader("X-Forwarded-For", whitelistedIp + "," + notWhitelistedIp + "," + trustedIp);
    response = closeableHttpClient().execute(request);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.UNAUTHORIZED.getStatus()));
    request = httpRequest();
    request.setHeader("X-Forwarded-For", "");
    response = closeableHttpClient().execute(request);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.UNAUTHORIZED.getStatus()));
}

From source file:com.yahoo.gondola.container.LocalTestRoutingServerTest.java

@Test
public void testRoutingServer() throws Exception {
    server = new LocalTestRoutingServer(gondola, routingHelper, proxyClientProvider,
            new HashMap<String, RoutingService>() {
                {/*from  www  . j  a  v a2  s  .c om*/
                    put("shard1", routingService);
                    put("shard2", routingService);
                }
            }, changeLogProcessor);

    CloseableHttpClient client = HttpClients.createDefault();
    CloseableHttpResponse response = client.execute(new HttpGet(server.getHostUri()));
    assertEquals(response.getStatusLine().getStatusCode(), 503);
    assertEquals(EntityUtils.toString(response.getEntity()), "No leader is available");
}