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.cloudfoundry.identity.uaa.integration.FormLoginIntegrationTests.java

@Test
public void testUnauthenticatedRedirect() throws Exception {
    String location = serverRunning.getBaseUrl() + "/";
    HttpGet httpget = new HttpGet(location);
    httpget.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build());
    CloseableHttpResponse response = httpclient.execute(httpget);
    assertEquals(FOUND.value(), response.getStatusLine().getStatusCode());
    location = response.getFirstHeader("Location").getValue();
    response.close();//w w  w.  jav a  2  s. c om
    httpget.completed();
    assertTrue(location.contains("/login"));
}

From source file:com.ibm.devops.dra.AbstractDevOpsAction.java

public static String getSpaceId(String token, String spaceName, String environment, Boolean debug_mode) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    String spaces_url = chooseSpacesUrl(environment);
    if (debug_mode) {
        LOGGER.info("GET SPACE_GUID URL:" + spaces_url + spaceName);
    }/*w ww .ja  va2 s  .co m*/

    try {
        HttpGet httpGet = new HttpGet(
                spaces_url + URLEncoder.encode(spaceName, "UTF-8").replaceAll("\\+", "%20"));

        httpGet = addProxyInformation(httpGet);

        httpGet.setHeader("Authorization", token);
        CloseableHttpResponse response = null;

        response = httpClient.execute(httpGet);
        String resStr = EntityUtils.toString(response.getEntity());

        if (debug_mode) {
            LOGGER.info("RESPONSE FROM SPACES API:" + response.getStatusLine().toString());
        }
        if (response.getStatusLine().toString().contains("200")) {
            // get 200 response
            JsonParser parser = new JsonParser();
            JsonElement element = parser.parse(resStr);
            JsonObject obj = element.getAsJsonObject();
            JsonArray resources = obj.getAsJsonArray("resources");

            if (resources.size() > 0) {
                JsonObject resource = resources.get(0).getAsJsonObject();
                JsonObject metadata = resource.getAsJsonObject("metadata");
                if (debug_mode) {
                    LOGGER.info("SPACE_ID:" + String.valueOf(metadata.get("guid")).replaceAll("\"", ""));
                }
                return String.valueOf(metadata.get("guid")).replaceAll("\"", "");
            } else {
                if (debug_mode) {
                    LOGGER.info("RETURNED NO SPACES.");
                }
                return null;
            }

        } else {
            if (debug_mode) {
                LOGGER.info("RETURNED STATUS CODE OTHER THAN 200. RESPONSE: "
                        + response.getStatusLine().toString());
            }
            return null;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.mirth.connect.client.core.ConnectServiceUtil.java

public static boolean sendStatistics(String serverId, String mirthVersion, boolean server, String data,
        String[] protocols, String[] cipherSuites) {
    if (data == null) {
        return false;
    }/*ww w .ja  v a  2  s.c  o m*/

    boolean isSent = false;

    CloseableHttpClient client = null;
    HttpPost post = new HttpPost();
    CloseableHttpResponse response = null;
    NameValuePair[] params = { new BasicNameValuePair("serverId", serverId),
            new BasicNameValuePair("version", mirthVersion),
            new BasicNameValuePair("server", Boolean.toString(server)), new BasicNameValuePair("data", data) };
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(TIMEOUT)
            .setConnectionRequestTimeout(TIMEOUT).setSocketTimeout(TIMEOUT).build();

    post.setURI(URI.create(URL_CONNECT_SERVER + URL_USAGE_SERVLET));
    post.setEntity(new UrlEncodedFormEntity(Arrays.asList(params), Charset.forName("UTF-8")));

    try {
        HttpClientContext postContext = HttpClientContext.create();
        postContext.setRequestConfig(requestConfig);
        client = getClient(protocols, cipherSuites);
        response = client.execute(post, postContext);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if ((statusCode == HttpStatus.SC_OK)) {
            isSent = true;
        }
    } catch (Exception e) {
    } finally {
        HttpClientUtils.closeQuietly(response);
        HttpClientUtils.closeQuietly(client);
    }
    return isSent;
}

From source file:com.ibm.devops.dra.AbstractDevOpsAction.java

public static String getAppId(String token, String appName, String orgName, String spaceName,
        String environment, Boolean debug_mode) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    String apps_url = chooseAppsUrl(environment);
    if (debug_mode) {
        LOGGER.info("GET APPS_GUID URL:" + apps_url + appName + ORG + orgName + SPACE + spaceName);
    }//from w  w  w  .  j  av  a2  s.  c o m

    try {
        HttpGet httpGet = new HttpGet(apps_url + URLEncoder.encode(appName, "UTF-8").replaceAll("\\+", "%20")
                + ORG + URLEncoder.encode(orgName, "UTF-8").replaceAll("\\+", "%20") + SPACE
                + URLEncoder.encode(spaceName, "UTF-8").replaceAll("\\+", "%20"));

        httpGet = addProxyInformation(httpGet);

        httpGet.setHeader("Authorization", token);
        CloseableHttpResponse response = null;

        response = httpClient.execute(httpGet);
        String resStr = EntityUtils.toString(response.getEntity());

        if (debug_mode) {
            LOGGER.info("RESPONSE FROM APPS API:" + response.getStatusLine().toString());
        }
        if (response.getStatusLine().toString().contains("200")) {
            // get 200 response
            JsonParser parser = new JsonParser();
            JsonElement element = parser.parse(resStr);
            JsonObject obj = element.getAsJsonObject();
            JsonArray resources = obj.getAsJsonArray("resources");

            if (resources.size() > 0) {
                JsonObject resource = resources.get(0).getAsJsonObject();
                JsonObject metadata = resource.getAsJsonObject("metadata");
                if (debug_mode) {
                    LOGGER.info("APP_ID:" + String.valueOf(metadata.get("guid")).replaceAll("\"", ""));
                }
                return String.valueOf(metadata.get("guid")).replaceAll("\"", "");
            } else {
                if (debug_mode) {
                    LOGGER.info("RETURNED NO APPS.");
                }
                return null;
            }

        } else {
            if (debug_mode) {
                LOGGER.info("RETURNED STATUS CODE OTHER THAN 200. RESPONSE: "
                        + response.getStatusLine().toString());
            }
            return null;
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.networknt.body.BodyHandlerTest.java

@Test
public void testGet() throws Exception {
    String url = "http://localhost:8080/get";
    CloseableHttpClient client = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(url);
    try {/*  ww w.j  a  v a 2  s .com*/
        CloseableHttpResponse response = client.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();
        Assert.assertEquals(200, statusCode);
        if (statusCode == 200) {
            String s = IOUtils.toString(response.getEntity().getContent(), "utf8");
            Assert.assertNotNull(s);
            Assert.assertEquals("nobody", s);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

void checkGlobalOutcome(ManagementClient managementClient, boolean mustBeUP, String probeName)
        throws IOException {

    final String healthURL = "http://" + managementClient.getMgmtAddress() + ":"
            + managementClient.getMgmtPort() + "/health";

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

        CloseableHttpResponse resp = client.execute(new HttpGet(healthURL));
        assertEquals(mustBeUP ? 200 : 503, resp.getStatusLine().getStatusCode());

        String content = getContent(resp);
        resp.close();// w  ww .ja v  a2 s .c  o  m

        try (JsonReader jsonReader = Json.createReader(new StringReader(content))) {
            JsonObject payload = jsonReader.readObject();
            String outcome = payload.getString("outcome");
            assertEquals(mustBeUP ? "UP" : "DOWN", outcome);

            if (probeName != null) {
                for (JsonValue check : payload.getJsonArray("checks")) {
                    if (probeName.equals(check.asJsonObject().getString("name"))) {
                        // probe name found
                        assertEquals(mustBeUP ? "UP" : "DOWN", check.asJsonObject().getString("state"));
                        return;
                    }
                }
                fail("Probe named " + probeName + " not found in " + content);
            }
        }
    }
}

From source file:com.ibm.devops.dra.AbstractDevOpsAction.java

public static String getOrgId(String token, String orgName, String environment, Boolean debug_mode) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    String organizations_url = chooseOrganizationsUrl(environment);
    if (debug_mode) {
        LOGGER.info("GET ORG_GUID URL:" + organizations_url + orgName);
    }//  w w w. j a va2  s.  c om

    try {
        HttpGet httpGet = new HttpGet(
                organizations_url + URLEncoder.encode(orgName, "UTF-8").replaceAll("\\+", "%20"));

        httpGet = addProxyInformation(httpGet);

        httpGet.setHeader("Authorization", token);
        CloseableHttpResponse response = null;

        response = httpClient.execute(httpGet);
        String resStr = EntityUtils.toString(response.getEntity());

        if (debug_mode) {
            LOGGER.info("RESPONSE FROM ORGANIZATIONS API:" + response.getStatusLine().toString());
        }
        if (response.getStatusLine().toString().contains("200")) {
            // get 200 response
            JsonParser parser = new JsonParser();
            JsonElement element = parser.parse(resStr);
            JsonObject obj = element.getAsJsonObject();
            JsonArray resources = obj.getAsJsonArray("resources");

            if (resources.size() > 0) {
                JsonObject resource = resources.get(0).getAsJsonObject();
                JsonObject metadata = resource.getAsJsonObject("metadata");
                if (debug_mode) {
                    LOGGER.info("ORG_ID:" + String.valueOf(metadata.get("guid")).replaceAll("\"", ""));
                }
                return String.valueOf(metadata.get("guid")).replaceAll("\"", "");
            } else {
                if (debug_mode) {
                    LOGGER.info("RETURNED NO ORGANIZATIONS.");
                }
                return null;
            }

        } else {
            if (debug_mode) {
                LOGGER.info("RETURNED STATUS CODE OTHER THAN 200. RESPONSE: "
                        + response.getStatusLine().toString());
            }
            return null;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

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

@Test
public void testAuditWithoutTrace() throws Exception {
    String url = "http://localhost:8080/pet";
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setHeader(Constants.AUTHORIZATION,
            "Bearer eyJraWQiOiIxMDAiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJ1cm46Y29tOm5ldHdvcmtudDpvYXV0aDI6djEiLCJhdWQiOiJ1cm46Y29tLm5ldHdvcmtudCIsImV4cCI6MTc5MDAzNTcwOSwianRpIjoiSTJnSmdBSHN6NzJEV2JWdUFMdUU2QSIsImlhdCI6MTQ3NDY3NTcwOSwibmJmIjoxNDc0Njc1NTg5LCJ2ZXJzaW9uIjoiMS4wIiwidXNlcl9pZCI6InN0ZXZlIiwidXNlcl90eXBlIjoiRU1QTE9ZRUUiLCJjbGllbnRfaWQiOiJmN2Q0MjM0OC1jNjQ3LT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re-fbR6_va95vwoUdCofLRa4IpDfXXx19ZlAtfiVO44nw6CS8O87eGfAm7rCMZIzkWlCOFWjNHnCeRsh7CVdEH34LF-B48beiG5lM7h4N12-EME8_VDefgMjZ8eqs1ICvJMxdIut58oYbdnkwTjkA");
    try {//from   ww  w  .ja v a 2 s . c  o  m
        StringEntity stringEntity = new StringEntity("post");
        httpPost.setEntity(stringEntity);
        CloseableHttpResponse response = client.execute(httpPost);
        int statusCode = response.getStatusLine().getStatusCode();
        Assert.assertEquals(200, statusCode);
        if (statusCode == 200) {
            String s = IOUtils.toString(response.getEntity().getContent(), "utf8");
            Assert.assertNotNull(s);
            Assert.assertEquals("OK", s);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.networknt.body.BodyHandlerTest.java

@Test
public void testPost() throws Exception {
    String url = "http://localhost:8080/post";
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    try {/*from   ww  w. j  a va 2 s  .c om*/
        StringEntity stringEntity = new StringEntity("post");
        httpPost.setEntity(stringEntity);
        CloseableHttpResponse response = client.execute(httpPost);
        int statusCode = response.getStatusLine().getStatusCode();
        Assert.assertEquals(200, statusCode);
        if (statusCode == 200) {
            String s = IOUtils.toString(response.getEntity().getContent(), "utf8");
            Assert.assertNotNull(s);
            Assert.assertEquals("body", s);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}