Example usage for org.apache.http.client.fluent Request execute

List of usage examples for org.apache.http.client.fluent Request execute

Introduction

In this page you can find the example usage for org.apache.http.client.fluent Request execute.

Prototype

public Response execute() throws ClientProtocolException, IOException 

Source Link

Usage

From source file:org.kie.remote.tests.base.RestUtil.java

public static <T> T post(URL deploymentUrl, String relativeUrl, String mediaType, int status, String user,
        String password, double timeoutInSecs, Class<T>... responseTypes) {

    String uriStr = createBaseUriString(deploymentUrl, relativeUrl);

    ResponseHandler<T> rh = createResponseHandler(mediaType, status, responseTypes);

    // @formatter:off
    Request request = Request.Post(uriStr).addHeader(HttpHeaders.CONTENT_TYPE, mediaType.toString())
            .addHeader(HttpHeaders.ACCEPT, mediaType.toString())
            .addHeader(HttpHeaders.AUTHORIZATION, basicAuthenticationHeader(user, password));
    // @formatter:on

    Response resp = null;/*from w w  w  .  j  a  v a2  s  .c om*/
    long before = 0, after = 0;
    try {
        logOp("POST", uriStr);
        before = System.currentTimeMillis();
        resp = request.execute();
        after = System.currentTimeMillis();
    } catch (Exception e) {
        failAndLog("[GET] " + uriStr, e);
    }

    long duration = after - before;
    assertTrue("Timeout exceeded " + timeoutInSecs + " secs: " + ((double) duration / 1000d) + " secs",
            duration < timeoutInSecs * 1000);

    try {
        return resp.handleResponse(rh);
    } catch (Exception e) {
        failAndLog("Failed retrieving response from [GET] " + uriStr, e);
    }

    // never happens
    return null;
}

From source file:org.fuin.owndeb.commons.DebUtils.java

/**
 * Downloads a file from an URL to a file and outputs progress in the log
 * (level 'info') every 1000 bytes.//w ww.  j  a  va  2s  . c  o m
 * 
 * @param url
 *            URL to download.
 * @param dir
 *            Target directory.
 * @param cookies
 *            Cookies for the request (Format: "name=value").
 */
public static void download(@NotNull final URL url, @NotNull final File dir, final String... cookies) {
    Contract.requireArgNotNull("url", url);
    Contract.requireArgNotNull("dir", dir);

    LOG.info("Download: {}", url);

    try {
        final Request request = Request.Get(url.toURI());
        if (cookies != null) {
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < cookies.length; i++) {
                if (i > 0) {
                    sb.append(";");
                }
                sb.append(cookies[i]);
            }
            request.addHeader("Cookie", sb.toString());
        }
        final File file = new File(dir, FilenameUtils.getName(url.getFile()));
        request.execute().saveContent(file);
    } catch (final IOException | URISyntaxException ex) {
        throw new RuntimeException("Error downloading: " + url, ex);
    }
}

From source file:org.kie.smoke.wb.util.RestUtil.java

public static <T> T postEntity(URL deploymentUrl, String relativeUrl, String mediaType, int status, String user,
        String password, double timeoutInSecs, Object entity, Class<T>... responseTypes) {

    String uriStr = createBaseUriString(deploymentUrl, relativeUrl);

    ResponseHandler<T> rh = createResponseHandler(mediaType, status, responseTypes);

    String entityStr = ((AbstractResponseHandler) rh).serialize(entity);
    HttpEntity bodyEntity = null;/*  w  ww. ja  v  a  2 s  .com*/
    try {
        bodyEntity = new StringEntity(entityStr);
    } catch (UnsupportedEncodingException uee) {
        logAndFail("Unable to encode serialized " + entity.getClass().getSimpleName() + " entity", uee);
    }

    // @formatter:off
    Request request = Request.Post(uriStr).body(bodyEntity)
            .addHeader(HttpHeaders.CONTENT_TYPE, mediaType.toString())
            .addHeader(HttpHeaders.ACCEPT, mediaType.toString())
            .addHeader(HttpHeaders.AUTHORIZATION, basicAuthenticationHeader(user, password));
    // @formatter:on

    Response resp = null;
    long before = 0, after = 0;
    try {
        logOp("POST", entity, uriStr);
        before = System.currentTimeMillis();
        resp = request.execute();
        after = System.currentTimeMillis();
    } catch (Exception e) {
        logAndFail("[GET] " + uriStr, e);
    }

    long duration = after - before;
    assertTrue("Timeout exceeded " + timeoutInSecs + " secs: " + ((double) duration / 1000d) + " secs",
            duration < timeoutInSecs * 1000);

    try {
        return resp.handleResponse(rh);
    } catch (Exception e) {
        logAndFail("Failed retrieving response from [GET] " + uriStr, e);
    }

    // never happens
    return null;
}

From source file:org.kie.remote.tests.base.RestUtil.java

public static <T> T postEntity(URL deploymentUrl, String relativeUrl, String mediaType, int status, String user,
        String password, double timeoutInSecs, Object entity, Class<T>... responseTypes) {

    String uriStr = createBaseUriString(deploymentUrl, relativeUrl);

    ResponseHandler<T> rh = createResponseHandler(mediaType, status, responseTypes);

    String entityStr = ((AbstractResponseHandler) rh).serialize(entity);
    HttpEntity bodyEntity = null;//from   ww  w.  j  a  v a  2 s  .  c om
    try {
        bodyEntity = new StringEntity(entityStr);
    } catch (UnsupportedEncodingException uee) {
        failAndLog("Unable to encode serialized " + entity.getClass().getSimpleName() + " entity", uee);
    }

    // @formatter:off
    Request request = Request.Post(uriStr).body(bodyEntity)
            .addHeader(HttpHeaders.CONTENT_TYPE, mediaType.toString())
            .addHeader(HttpHeaders.ACCEPT, mediaType.toString())
            .addHeader(HttpHeaders.AUTHORIZATION, basicAuthenticationHeader(user, password));
    // @formatter:on

    Response resp = null;
    long before = 0, after = 0;
    try {
        logOp("POST", entity, uriStr);
        before = System.currentTimeMillis();
        resp = request.execute();
        after = System.currentTimeMillis();
    } catch (Exception e) {
        failAndLog("[GET] " + uriStr, e);
    }

    long duration = after - before;
    assertTrue("Timeout exceeded " + timeoutInSecs + " secs: " + ((double) duration / 1000d) + " secs",
            duration < timeoutInSecs * 1000);

    try {
        return resp.handleResponse(rh);
    } catch (Exception e) {
        failAndLog("Failed retrieving response from [GET] " + uriStr, e);
    }

    // never happens
    return null;
}

From source file:com.twosigma.beaker.core.rest.PluginServiceLocatorRest.java

private static boolean spinCheck(String url) throws IOException, InterruptedException {

    int interval = RESTART_ENSURE_RETRY_INTERVAL;
    int totalTime = 0;

    while (totalTime < RESTART_ENSURE_RETRY_MAX_WAIT) {
        Request get = Request.Get(url);
        if (get.execute().returnResponse().getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            return true;
        }/*w  w  w  .  j a  v a2 s. c  o m*/
        Thread.sleep(interval);
        totalTime += interval;
        interval *= 1.5;
        if (interval > RESTART_ENSURE_RETRY_MAX_INTERVAL)
            interval = RESTART_ENSURE_RETRY_MAX_INTERVAL;
    }
    throw new RuntimeException("Spin check timed out: " + url);
}

From source file:io.gravitee.gateway.standalone.SimpleGatewayTest.java

@Test
public void call_get_started_api() throws Exception {
    Request request = Request.Get("http://localhost:8082/test/my_team");
    Response response = request.execute();
    HttpResponse returnResponse = response.returnResponse();

    assertEquals(HttpStatus.SC_OK, returnResponse.getStatusLine().getStatusCode());
}

From source file:io.gravitee.gateway.standalone.UnreachableTest.java

@Test
public void call_unreachable_api() throws Exception {
    Request request = Request.Post("http://localhost:8082/unreachable");
    Response response = request.execute();
    HttpResponse returnResponse = response.returnResponse();

    assertEquals(HttpStatus.SC_BAD_GATEWAY, returnResponse.getStatusLine().getStatusCode());
}

From source file:io.gravitee.gateway.standalone.ReadTimeoutTest.java

@Test
public void call_read_timeout_api() throws Exception {
    Request request = Request.Get("http://localhost:8082/team/my_team");
    Response response = request.execute();
    HttpResponse returnResponse = response.returnResponse();

    assertEquals(HttpStatus.SC_GATEWAY_TIMEOUT, returnResponse.getStatusLine().getStatusCode());
}

From source file:io.gravitee.gateway.standalone.ConnectionTimeoutTest.java

@Test
public void call_unreachable_api() throws Exception {
    Request request = Request.Post("http://localhost:8082/unreachable");
    Response response = request.execute();
    HttpResponse returnResponse = response.returnResponse();

    assertTrue(returnResponse.getStatusLine().getStatusCode() == HttpStatus.SC_GATEWAY_TIMEOUT
            || returnResponse.getStatusLine().getStatusCode() == HttpStatus.SC_BAD_GATEWAY);
}

From source file:io.gravitee.gateway.standalone.NotFoundGatewayTest.java

@Test
public void call_no_api() throws IOException {
    Request request = Request.Get("http://localhost:8082/unknow");
    Response response = request.execute();
    HttpResponse returnResponse = response.returnResponse();

    assertEquals(HttpStatusCode.NOT_FOUND_404, returnResponse.getStatusLine().getStatusCode());
}