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:com.networknt.metrics.MetricsHandlerTest.java

@Test
public void testMetrics() throws Exception {
    String url = "http://localhost:8080/v2/pet/111";
    CloseableHttpClient client = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(url);
    httpGet.setHeader("Authorization",
            "Bearer eyJraWQiOiIxMDAiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJ1cm46Y29tOm5ldHdvcmtudDpvYXV0aDI6djEiLCJhdWQiOiJ1cm46Y29tLm5ldHdvcmtudCIsImV4cCI6MTc5MDAzNTcwOSwianRpIjoiSTJnSmdBSHN6NzJEV2JWdUFMdUU2QSIsImlhdCI6MTQ3NDY3NTcwOSwibmJmIjoxNDc0Njc1NTg5LCJ2ZXJzaW9uIjoiMS4wIiwidXNlcl9pZCI6InN0ZXZlIiwidXNlcl90eXBlIjoiRU1QTE9ZRUUiLCJjbGllbnRfaWQiOiJmN2Q0MjM0OC1jNjQ3LTRlZmItYTUyZC00YzU3ODc0MjFlNzIiLCJzY29wZSI6WyJ3cml0ZTpwZXRzIiwicmVhZDpwZXRzIl19.mue6eh70kGS3Nt2BCYz7ViqwO7lh_4JSFwcHYdJMY6VfgKTHhsIGKq2uEDt3zwT56JFAePwAxENMGUTGvgceVneQzyfQsJeVGbqw55E9IfM_uSM-YcHwTfR7eSLExN4pbqzVDI353sSOvXxA98ZtJlUZKgXNE1Ngun3XFORCRIB_eH8B0FY_nT_D1Dq2WJrR-re-fbR6_va95vwoUdCofLRa4IpDfXXx19ZlAtfiVO44nw6CS8O87eGfAm7rCMZIzkWlCOFWjNHnCeRsh7CVdEH34LF-B48beiG5lM7h4N12-EME8_VDefgMjZ8eqs1ICvJMxdIut58oYbdnkwTjkA");
    try {/*from  w w w  .ja v  a2  s  .  c o  m*/
        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("test", s);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.talend.dataprofiler.help.RegexExpeHelpUrlTest.java

private StatusLine execute(String helpUrl) throws ClientProtocolException, IOException {
    StatusLine statusLine = null;/*w w  w  .j  a  v a  2s  .co  m*/
    CloseableHttpClient httpclient = HttpClients.createMinimal();
    try {
        HttpGet httpget = new HttpGet(helpUrl);
        CloseableHttpResponse execute = httpclient.execute(httpget);
        statusLine = execute.getStatusLine();

    } finally {
        httpclient.close();
    }
    return statusLine;
}

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

private String uploadSmallBlob() throws IOException {
    String digest = "c520e6109835c876fd98636efec43dd61634b7d3";
    CloseableHttpResponse response = put(blobUri(digest), StringUtils.repeat("a", 1500));
    assertThat(response.getStatusLine().getStatusCode(), is(201));
    return digest;
}

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

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

    try (CloseableHttpClient client = HttpClients.createDefault()) {
        CloseableHttpResponse resp = client.execute(new HttpGet(healthURL));
        assertEquals(401, resp.getStatusLine().getStatusCode());
        String content = MicroProfileHealthHTTPEndpointTestCase.getContent(resp);
        resp.close();//  ww  w.ja v a  2s  . co  m
        assertTrue("'401 - Unauthorized' message is expected", content.contains("401 - Unauthorized"));
    }
}

From source file:com.anrisoftware.simplerest.core.AbstractSimplePostWorker.java

/**
 * Makes the request and sends the data and retrieves the response.
 *
 * @param entity//  w ww  .  j  a  v  a2  s .c om
 *            the {@link HttpEntity} entity.
 *
 * @return the response.
 *
 * @throws SimpleRestException
 */
public T sendData(HttpEntity entity) throws SimpleRestException {
    CloseableHttpClient httpclient = createHttpClient();
    HttpPost httpput = new HttpPost(requestUri);
    httpput.setEntity(entity);
    for (Header header : headers) {
        httpput.addHeader(header);
    }
    CloseableHttpResponse response = executeRequest(httpclient, httpput);
    StatusLine statusLine = response.getStatusLine();
    return parseResponse(response, httpput, statusLine);
}

From source file:net.xenqtt.httpgateway.HttpGatewayHandler.java

/**
 * The handler sets the response status, content-type, and marks the request as handled before it generates the body of the response using a writer. There
 * are a number of built in <a href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/server/handler/package-summary.html">handlers</a>
 * that might come in handy.//w  w w  .ja va 2s . c o  m
 * 
 * @param target
 *            the target of the request, which is either a URI or a name from a named dispatcher.
 * @param baseRequest
 *            the Jetty mutable request object, which is always unwrapped.
 * @param request
 *            the immutable request object, which may have been wrapped by a filter or servlet.
 * @param response
 *            the response, which may have been wrapped by a filter or servlet.
 * 
 * @see org.eclipse.jetty.server.Handler#handle(java.lang.String, org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse)
 */
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    // FIXME [jim] - make sure http 1.1 persistent connections are being used on both server and client sides

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet("http://www.google.com" + target);
    CloseableHttpResponse response1 = httpclient.execute(httpGet);
    try {
        response.setStatus(response1.getStatusLine().getStatusCode());
        HttpEntity entity = response1.getEntity();
        InputStream in = entity.getContent();
        OutputStream out = response.getOutputStream();
        for (int i = in.read(); i >= 0; i = in.read()) {
            out.write(i);
        }
    } finally {
        response1.close();
    }

    baseRequest.setHandled(true);

    // TODO Auto-generated method stub
}

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

@Test
public void localhostClientIsBasicAuthenticated() throws Exception {
    HttpUriRequest request = httpRequest();
    String credentials = "admin:admin_pw";
    request.setHeader("Authorization", "Basic " + Base64.encodeBytes(credentials.getBytes()));
    CloseableHttpResponse response = closeableHttpClient().execute(request);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
}

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

@Test
public void localhostClientNotBasicAuthenticated() throws Exception {
    HttpUriRequest request = httpRequest();
    String credentials = "admin:wrong";
    request.setHeader("Authorization", "Basic " + Base64.encodeBytes(credentials.getBytes()));
    CloseableHttpResponse response = closeableHttpClient().execute(request);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.UNAUTHORIZED.getStatus()));
}

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

@Test
public void localhostClientIsBasicAuthenticatedPassingXForward() throws Exception {
    HttpUriRequest request = httpRequest();
    String credentials = "admin:admin_pw";
    request.setHeader("Authorization", "Basic " + Base64.encodeBytes(credentials.getBytes()));
    request.setHeader("X-Forwarded-For", "1.1.1.1");
    CloseableHttpResponse response = closeableHttpClient().execute(request);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
}

From source file:model.WebServiceRequest.java

public String getResponse(String pais, String ciudad) {
    pais = pais.replace(" ", "%20");
    ciudad = ciudad.replace(" ", "%20");
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        HttpGet httpGet = new HttpGet("http://www.webservicex.net/globalweather.asmx/GetWeather?CityName="
                + ciudad + "&CountryName=" + pais);
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);

        System.out.println("GET Response Status: " + httpResponse.getStatusLine().getStatusCode());

        StringBuilder response;/*w w  w . j  av  a2  s  .c om*/
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(httpResponse.getEntity().getContent()))) {
            String inputLine;
            response = new StringBuilder();
            while ((inputLine = reader.readLine()) != null) {
                response.append(inputLine);
            }
        }
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            return response.toString();
        } else {
            return "Error.";
        }

    } catch (IOException ex) {
        Logger.getLogger(WebServiceRequest.class.getName()).log(Level.SEVERE, null, ex);
    }
    JOptionPane.showMessageDialog(null, "Se ha presentado un error al acceder al servicio web.");
    return null;
}