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:io.liveoak.keycloak.AuthInterceptorTest.java

private void sendRequestAndCheckStatus(HttpRequestBase req, int expectedStatusCode) throws IOException {
    CloseableHttpResponse resp = httpClient.execute(req);
    assertThat(resp.getStatusLine().getStatusCode()).isEqualTo(expectedStatusCode);
    resp.close();/*from ww w .j  a v  a  2 s.c  om*/
}

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

@Test
@InSequence(2)/*from   ww w . j  av  a2 s . c  o m*/
@OperateOnDeployment("MicroProfileHealthTestCase")
public void testHealthCheckAfterDeployment(@ArquillianResource URL url) throws Exception {
    try (CloseableHttpClient client = HttpClientBuilder.create().build()) {

        checkGlobalOutcome(managementClient, true, "myProbe");

        HttpPost request = new HttpPost(url + "microprofile/myApp");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("up", "false"));
        request.setEntity(new UrlEncodedFormEntity(nvps));

        CloseableHttpResponse response = client.execute(request);
        assertEquals(200, response.getStatusLine().getStatusCode());

        checkGlobalOutcome(managementClient, false, "myProbe");
    }
}

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

@Test
public void handlesUnexpectedException() throws Exception {
    final ServiceBrokerHandler.BindBody bindBody = new ServiceBrokerHandler.BindBody(BROKER_ID, PLAN_ID,
            UUID.randomUUID());/*w  w  w  .  j a  v a2  s  .c o m*/
    final HttpUriRequest bindRequest = RequestBuilder.put()
            .setUri("http://localhost:8080/v2/service_instances/" + UUID.randomUUID() + "/service_bindings/"
                    + UUID.randomUUID())
            .setEntity(new StringEntity(mapper.writeValueAsString(bindBody), ContentType.APPLICATION_JSON))
            .build();
    final CloseableHttpResponse bindResponse = client.execute(bindRequest);
    assertEquals(bindResponse.getStatusLine().getStatusCode(), 500);
    final JsonNode responseJson = mapper.readTree(bindResponse.getEntity().getContent());

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

From source file:org.wuspba.ctams.ws.ITSoloContestController.java

protected static void delete() throws Exception {
    String id;/* w  ww  . j a  v  a2 s . c om*/

    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build();

    HttpGet httpGet = new HttpGet(uri);

    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING);

        HttpEntity entity = response.getEntity();

        CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity);

        id = doc.getSoloContests().get(0).getId();

        EntityUtils.consume(entity);
    }

    httpclient = HttpClients.createDefault();

    uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).setParameter("id", id)
            .build();

    HttpDelete httpDelete = new HttpDelete(uri);

    CloseableHttpResponse response = null;

    try {
        response = httpclient.execute(httpDelete);

        assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString());

        HttpEntity responseEntity = response.getEntity();

        EntityUtils.consume(responseEntity);
    } catch (UnsupportedEncodingException ex) {
        LOG.error("Unsupported coding", ex);
    } catch (IOException ioex) {
        LOG.error("IOException", ioex);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException ex) {
                LOG.error("Could not close response", ex);
            }
        }
    }

    ITVenueController.delete();
    ITHiredJudgeController.delete();
    ITJudgeController.delete();
}

From source file:com.networknt.audit.ServerInfoDisabledTest.java

@Test
public void testServerInfo() throws Exception {
    String url = "http://localhost:8080/v1/server/info";
    CloseableHttpClient client = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(url);
    try {/*from w  w w.  j  a v a  2 s . c  o  m*/
        CloseableHttpResponse response = client.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();
        Assert.assertEquals(404, statusCode);
        if (statusCode == 404) {
            Status status = Config.getInstance().getMapper().readValue(response.getEntity().getContent(),
                    Status.class);
            Assert.assertNotNull(status);
            Assert.assertEquals("ERR10013", status.getCode());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.openstreetmap.josm.plugins.mapillary.actions.MapillarySubmitCurrentChangesetAction.java

@Override
public void actionPerformed(ActionEvent event) {
    String token = Main.pref.get("mapillary.access-token");
    if (token == null || token.trim().isEmpty()) {
        PluginState.notLoggedInToMapillaryDialog();
        return;//from  w  w  w .j  a  v  a 2s.  c  o m
    }
    PluginState.setSubmittingChangeset(true);
    MapillaryUtils.updateHelpText();
    HttpClientBuilder builder = HttpClientBuilder.create();
    HttpPost httpPost = new HttpPost(MapillaryURL.submitChangesetURL().toString());
    httpPost.addHeader("content-type", "application/json");
    httpPost.addHeader("Authorization", "Bearer " + token);
    JsonArrayBuilder changes = Json.createArrayBuilder();
    MapillaryLocationChangeset locationChangeset = MapillaryLayer.getInstance().getLocationChangeset();
    for (MapillaryImage image : locationChangeset) {
        changes.add(Json.createObjectBuilder().add("image_key", image.getKey()).add("values",
                Json.createObjectBuilder()
                        .add("from", Json.createObjectBuilder().add("ca", image.getCa())
                                .add("lat", image.getLatLon().getY()).add("lon", image.getLatLon().getX()))
                        .add("to",
                                Json.createObjectBuilder().add("ca", image.getTempCa())
                                        .add("lat", image.getTempLatLon().getY())
                                        .add("lon", image.getTempLatLon().getX()))));
    }
    String json = Json.createObjectBuilder().add("change_type", "location").add("changes", changes)
            .add("request_comment", "JOSM-created").build().toString();
    try (CloseableHttpClient httpClient = builder.build()) {
        httpPost.setEntity(new StringEntity(json));
        CloseableHttpResponse response = httpClient.execute(httpPost);
        if (response.getStatusLine().getStatusCode() == 200) {
            String key = Json.createReader(response.getEntity().getContent()).readObject().getString("key");
            synchronized (MapillaryUtils.class) {
                Main.map.statusLine.setHelpText(
                        String.format("%s images submitted, Changeset key: %s", locationChangeset.size(), key));
            }
            locationChangeset.cleanChangeset();

        }

    } catch (IOException e) {
        logger.error("got exception", e);
        synchronized (MapillaryUtils.class) {
            Main.map.statusLine.setHelpText("Error submitting Mapillary changeset: " + e.getMessage());
        }
    } finally {
        PluginState.setSubmittingChangeset(false);
    }
}

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

/**
 * @return {@link IOpenAMServerInfo}/*from   ww  w. java2  s .  com*/
 * @throws Exception
 */
@Override
public IOpenAMServerInfo serverInfo() throws Exception {
    logger.debug("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@TRYING TO HAVE SERVER_INFO WITH "
            + "OPENAM_CONNECTOR_SETTINGS : {}\n", this.openAMConnectorSettings);
    OpenAMServerInfoRequest serverInfoRequest = this.openAMRequestMediator.getRequest(SERVER_INFO);
    URI serverInfoURI = this.buildURI(this.openAMConnectorSettings, serverInfoRequest).build();

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

    HttpGet httpGet = new HttpGet(serverInfoURI);
    httpGet.addHeader("Content-Type", "application/json");
    CloseableHttpResponse response = this.httpClient.execute(httpGet);

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

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

From source file:org.sikuli.script.App.java

/**
 * issue a http(s) request//from w  w  w .j a  v  a  2 s.com
 * @param url a valid url as used in a browser
 * @return textual content of the response or empty (UTF-8)
 * @throws IOException
 */
public static String wwwGet(String url) throws IOException {
    HttpGet httpget = new HttpGet(url);
    CloseableHttpResponse response = null;
    ResponseHandler rh = new ResponseHandler() {
        @Override
        public String handleResponse(final HttpResponse response) throws IOException {
            StatusLine statusLine = response.getStatusLine();
            HttpEntity entity = response.getEntity();
            if (statusLine.getStatusCode() >= 300) {
                throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
            }
            if (entity == null) {
                throw new ClientProtocolException("Response has no content");
            }
            InputStream is = entity.getContent();
            ByteArrayOutputStream result = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) != -1) {
                result.write(buffer, 0, length);
            }
            return result.toString("UTF-8");
        }
    };
    boolean oneTime = false;
    if (httpclient == null) {
        wwwStart();
        oneTime = true;
    }
    Object content = httpclient.execute(httpget, rh);
    if (oneTime) {
        wwwStop();
    }
    return (String) content;
}

From source file:io.undertow.js.test.cdi.CDIInjectionProviderTestCase.java

@Test
public void testInjection() throws ClientProtocolException, IOException {
    final TestHttpClient client = new TestHttpClient();
    try {//from  ww w  .j a  v a  2 s. co m
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/cdi/foo");
        CloseableHttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        assertEquals("Foopong", HttpClientUtils.readResponse(result));
    } finally {
        client.getConnectionManager().shutdown();
    }
}