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.smoke.wb.util.RestUtil.java

public static <T, G> T getQuery(URL deploymentUrl, String relativeUrl, String mediaType, int status,
        String user, String password, Map<String, String> queryParams, Class... responseTypes) {
    URIBuilder uriBuilder = null;//from w ww . ja  v  a  2  s. co  m
    try {
        String uriStr = createBaseUriString(deploymentUrl, relativeUrl);
        uriBuilder = new URIBuilder(uriStr);
    } catch (URISyntaxException urise) {
        logAndFail("Invalid uri :" + deploymentUrl.toString(), urise);
    }

    for (Entry<String, String> paramEntry : queryParams.entrySet()) {
        uriBuilder.addParameter(paramEntry.getKey(), paramEntry.getValue());
    }

    URI uri = null;
    String uriStr = null;
    try {
        uri = uriBuilder.build();
        uriStr = uri.toString();
    } catch (URISyntaxException urise) {
        logAndFail("Invalid uri!", urise);
    }

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

    // @formatter:off
    Request request = Request.Get(uri).addHeader(HttpHeaders.ACCEPT, mediaType.toString())
            .addHeader(HttpHeaders.AUTHORIZATION, basicAuthenticationHeader(user, password));
    // @formatter:off

    Response resp = null;
    try {
        logOp("GET", uriStr);
        resp = request.execute();
    } catch (Exception e) {
        logAndFail("[GET] " + uriStr, e);
    }

    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, G> T getQuery(URL deploymentUrl, String relativeUrl, String mediaType, int status,
        String user, String password, Map<String, String> queryParams, Class... responseTypes) {
    URIBuilder uriBuilder = null;//from   w  w w . ja  v a2  s  .co m
    try {
        String uriStr = createBaseUriString(deploymentUrl, relativeUrl);
        uriBuilder = new URIBuilder(uriStr);
    } catch (URISyntaxException urise) {
        failAndLog("Invalid uri :" + deploymentUrl.toString(), urise);
    }

    for (Entry<String, String> paramEntry : queryParams.entrySet()) {
        String param = paramEntry.getKey();
        String value = paramEntry.getValue();
        uriBuilder.addParameter(paramEntry.getKey(), paramEntry.getValue());
    }

    URI uri = null;
    String uriStr = null;
    try {
        uri = uriBuilder.build();
        uriStr = uri.toString();
    } catch (URISyntaxException urise) {
        failAndLog("Invalid uri!", urise);
    }

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

    // @formatter:off
    Request request = Request.Get(uri).addHeader(HttpHeaders.ACCEPT, mediaType.toString())
            .addHeader(HttpHeaders.AUTHORIZATION, basicAuthenticationHeader(user, password));
    // @formatter:off

    Response resp = null;
    try {
        logOp("GET", uriStr);
        resp = request.execute();
    } catch (Exception e) {
        failAndLog("[GET] " + uriStr, e);
    }

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

    // never happens
    return null;
}

From source file:io.coala.capability.online.FluentHCOnlineCapability.java

/**
 * @param request//from  w  ww  .  jav  a 2  s.  c  o m
 * @param handler
 * @param sub
 * @param entity
 */
protected static void execute(final Request request, final MyResponseHandler handler,
        final Subscriber<? super ResourceStream> sub, final HttpEntity entity) {
    try {
        if (entity != null)
            request.body(entity);
        request.execute().handleResponse(handler);
    } catch (final Throwable e) {
        sub.onError(e);
    }
}

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, 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  .j a v a2  s.c om
    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;
    try {
        logOp("POST", entity, uriStr);
        resp = request.execute();
    } catch (Exception e) {
        logAndFail("[GET] " + uriStr, e);
    }

    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, 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 w w .  j a  v a2 s .  com
    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;
    try {
        logOp("POST", entity, uriStr);
        resp = request.execute();
    } catch (Exception e) {
        failAndLog("[GET] " + uriStr, e);
    }

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

    // never happens
    return null;
}

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

public static <T> T postForm(URL deploymentUrl, String relativeUrl, String mediaType, int status, String user,
        String password, Map<String, String> formParams, Class<T>... responseTypes) {

    String uriStr = createBaseUriString(deploymentUrl, relativeUrl);

    // form content
    Form formContent = Form.form();//from  w w  w.  ja v a 2  s .  com
    for (Entry<String, String> entry : formParams.entrySet()) {
        formContent.add(entry.getKey(), entry.getValue());
    }

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

    Response resp = null;
    try {
        logOp("POST", uriStr);
        resp = request.execute();
    } catch (Exception e) {
        logAndFail("[GET] " + uriStr, e);
    }

    ResponseHandler<T> rh = createResponseHandler(mediaType, status, responseTypes);
    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.smoke.wb.util.RestUtil.java

public static <T> T postEntity(URL deploymentUrl, String relativeUrl, int status, String user, String password,
        Class[] classes, Object entity, Class<T>... responseTypes) {

    String uriStr = createBaseUriString(deploymentUrl, relativeUrl);

    String mediaType = MediaType.APPLICATION_XML;
    ResponseHandler<T> rh = createResponseHandler(mediaType, status, responseTypes);
    XmlResponseHandler xrh = (XmlResponseHandler) rh;

    xrh.addExtraJaxbClasses(classes);//from  w  ww  .  j  av  a 2s  . com
    String entityStr = xrh.serialize(entity);

    HttpEntity bodyEntity = null;
    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;
    try {
        logOp("POST", entity, uriStr);
        resp = request.execute();
    } catch (Exception e) {
        logAndFail("[GET] " + uriStr, e);
    }

    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, int status, String user, String password,
        Class[] classes, Object entity, Class<T>... responseTypes) {

    String uriStr = createBaseUriString(deploymentUrl, relativeUrl);

    String mediaType = MediaType.APPLICATION_XML;
    ResponseHandler<T> rh = createResponseHandler(mediaType, status, responseTypes);
    XmlResponseHandler xrh = (XmlResponseHandler) rh;

    xrh.addExtraJaxbClasses(classes);/*from   w w  w . ja v  a2s.c  o m*/
    String entityStr = xrh.serialize(entity);

    HttpEntity bodyEntity = null;
    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;
    try {
        logOp("POST", entity, uriStr);
        resp = request.execute();
    } catch (Exception e) {
        failAndLog("[GET] " + uriStr, e);
    }

    try {
        return resp.handleResponse(rh);
    } catch (Exception e) {
        failAndLog("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 postForm(URL deploymentUrl, String relativeUrl, String mediaType, int status, String user,
        String password, Map<String, String> formParams, Class<T>... responseTypes) {

    String uriStr = createBaseUriString(deploymentUrl, relativeUrl);

    // form content
    Form formContent = Form.form();//from  w  w w .ja v a  2  s.  com
    for (Entry<String, String> entry : formParams.entrySet()) {
        formContent.add(entry.getKey(), entry.getValue());
    }

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

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

    ResponseHandler<T> rh = createResponseHandler(mediaType, status, responseTypes);
    try {
        return resp.handleResponse(rh);
    } catch (Exception e) {
        failAndLog("Failed retrieving response from [GET] " + uriStr, e);
    }

    // never happens
    return null;
}

From source file:org.kie.smoke.wb.util.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;// w ww . j a v  a 2 s  .c  o m
    long before = 0, after = 0;
    try {
        logOp("POST", 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;
}