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:functional.PathTraversingTest.java

@Test
public void testNoTraversal() throws Exception {
    HttpGet httpget = new HttpGet("http://localhost:8080/");

    CloseableHttpResponse execute = httpclient.execute(httpget);
    execute.close();//from   w  ww .  j a v a 2  s  .  c o m
    assertEquals(200, execute.getStatusLine().getStatusCode());
}

From source file:HelloWorld.HelloWorldTest.java

@Test
public void getPeopleNotExist() throws URISyntaxException, IOException {
    URI uri = new URI(baseURI + "api/person/sally");
    HttpGet get = new HttpGet(uri);
    CloseableHttpResponse response = client.execute(get);

    assertEquals(404, response.getStatusLine().getStatusCode());
    assertEquals("No person exists with specified name.", EntityUtils.toString(response.getEntity(), "UTF-8"));
}

From source file:HelloWorld.HelloWorldTest.java

@Test
public void getPeopleTest() throws URISyntaxException, IOException {
    URI uri = new URI(baseURI + "api/person");
    HttpGet get = new HttpGet(uri);
    CloseableHttpResponse response = client.execute(get);

    assertEquals(200, response.getStatusLine().getStatusCode());

    Person[] people = responseToEntity(response, Person[].class);
    assertEquals("Joe", people[0].firstName);
}

From source file:io.crate.integrationtests.RestSQLActionIntegrationTest.java

@Test
public void testWithoutBody() throws IOException {
    CloseableHttpResponse response = post(null);
    assertEquals(400, response.getStatusLine().getStatusCode());
    String bodyAsString = EntityUtils.toString(response.getEntity());
    assertThat(bodyAsString, startsWith("{\"error\":{\"message\":\"SQLActionException[missing request body]\","
            + "\"code\":4000},\"error_trace\":\"SQLActionException:"));
}

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

protected JsonNode loadCatalog(CloseableHttpClient client) throws IOException {
    final HttpUriRequest catalogRequest = RequestBuilder.get()
            .setUri("http://localhost:8080" + Constants.CATALOG_URI).build();
    final CloseableHttpResponse response = client.execute(catalogRequest);
    assertEquals(response.getStatusLine().getStatusCode(), 200);
    final String body = StreamUtils.copyToString(response.getEntity().getContent(), Charset.defaultCharset());

    final ObjectMapper mapper = new ObjectMapper();
    return mapper.readTree(body);
}

From source file:guru.nidi.loader.url.SimpleUrlFetcher.java

@Override
public InputStream fetchFromUrl(CloseableHttpClient client, String base, String name, long ifModifiedSince)
        throws IOException {
    final String suffix = (name == null || name.length() == 0) ? "" : ("/" + encodeUrl(name));
    final HttpGet get = postProcessGet(new HttpGet(base + suffix));
    if (ifModifiedSince > 0) {
        get.addHeader("if-modified-since", httpDate(ifModifiedSince));
    }//from   ww w . j a va2  s.co  m
    final CloseableHttpResponse getResult = client.execute(get);
    switch (getResult.getStatusLine().getStatusCode()) {
    case HttpStatus.SC_OK:
        // remove AutoCloseInputStream as soon as
        // https://github.com/raml-org/raml-java-parser/issues/72 is fixed
        return new AutoCloseInputStream(getResult.getEntity().getContent());
    case HttpStatus.SC_NOT_MODIFIED:
        EntityUtils.consume(getResult.getEntity());
        return null;
    default:
        throw new IOException("Http response status not ok: " + getResult.getStatusLine().toString());
    }
}

From source file:HelloWorld.HelloWorldTest.java

@Test
public void postPeopleTest() throws URISyntaxException, IOException {
    URI uri = new URI(baseURI + "api/person");
    HttpPost post = new HttpPost(uri);
    Person person = new Person();
    person.firstName = "Sally";
    person.lastName = "Smith";
    person.age = 25;/* www  . jav a 2 s .  c o m*/
    person.married = false;
    person.birthDate = new Date();

    Gson gson = new GsonBuilder().setDateFormat("MM/dd/yyyy").create();
    String json = gson.toJson(person);
    post.setEntity(new StringEntity(json));

    CloseableHttpResponse response = client.execute(post);
    assertEquals(201, response.getStatusLine().getStatusCode());
    assertEquals("Person creation success!", EntityUtils.toString(response.getEntity(), "UTF-8"));
}

From source file:com.microsoft.azure.hdinsight.common.task.LivyTask.java

@Override
public String call() throws Exception {
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider)
            .build();/* www . ja  v a  2s  . co  m*/
    HttpGet httpGet = new HttpGet(path);
    httpGet.addHeader("Content-Type", "application/json");
    CloseableHttpResponse response = httpclient.execute(httpGet);
    int code = response.getStatusLine().getStatusCode();

    HttpEntity httpEntity = response.getEntity();

    return IOUtils.toString(httpEntity.getContent(), Charset.forName("utf-8"));
}

From source file:kr.riotapi.core.ApiDomain.java

public String execute(ApiCall call) throws IOException, StatusCodeException {
    HttpGet get = new HttpGet(call.toUrlString());
    String body = null;/*from w  ww .j a  v  a2s.  c o m*/
    int statusCode = 0;
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        CloseableHttpResponse response = client.execute(get);
        statusCode = response.getStatusLine().getStatusCode();
        body = EntityUtils.toString(response.getEntity());
        response.close();
    } catch (IOException ex) {
        throw new IOException("There was a problem receiving or processing a server response", ex);
    }
    if (statusCode != HttpStatus.SC_OK)
        throw new StatusCodeException("status code was not 200", statusCode);
    return body;
}

From source file:vng.paygate.notify.NotifyHelper.java

public String postNotify(String url, JsonObject json, String dataType, String contentType)
        throws UnsupportedEncodingException, IOException {
    try {/*from  w  w w  . ja va  2s .  c om*/
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        if (StringUtils.isEmpty(contentType)) {
            contentType = "application/json";
        }
        if (StringUtils.isEmpty(dataType)) {
            dataType = "application/json";
        }
        httpPost.setHeader("Content-type", contentType);
        httpPost.setHeader("Accept-Content Type", dataType);
        httpPost.setEntity(new StringEntity(json.toString()));
        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(httpResponse.getEntity().getContent()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = reader.readLine()) != null) {
                response.append(inputLine);
            }
            reader.close();
            return response.toString();
        } else {
            return "";
        }
    } catch (Exception e) {
        return "";
    }
}