Example usage for org.apache.http.client.fluent Form build

List of usage examples for org.apache.http.client.fluent Form build

Introduction

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

Prototype

public List<NameValuePair> build() 

Source Link

Usage

From source file:eu.anynet.java.util.HttpClient.java

/**
 * Create POST request//  w  ww.j  a  v  a  2s .  c o  m
 * @param url The URL
 * @param form Formdata
 * @return The request
 */
public static Request Post(String url, Form form) {
    return defaults(Request.Post(url)).bodyForm(form.build());
}

From source file:com.jaspersoft.studio.server.utils.HttpUtils.java

public static Request post(String url, Form form, ServerProfile sp) throws HttpException, IOException {
    System.out.println(url);//  w  w  w  .j  av  a2 s . c  o  m
    return HttpUtils.setRequest(Request.Post(url).bodyForm(form.build()), sp);
}

From source file:es.uvigo.esei.dai.hybridserver.utils.TestUtils.java

private static List<NameValuePair> mapToNameValuePair(Map<String, String> map) {
    final Form form = Form.form();

    for (Map.Entry<String, String> entry : map.entrySet()) {
        form.add(entry.getKey(), entry.getValue());
    }/*  w w  w .  j  a va2  s  .c om*/

    return form.build();
}

From source file:com.mywork.framework.util.RemoteHttpUtil.java

/**
 * ?getpost????/*from  w w w .ja v  a 2  s.  co m*/
 */
public static String fetchSimpleHttpResponse(String method, String contentUrl, Map<String, String> headerMap,
        Map<String, String> bodyMap) throws IOException {
    Executor executor = Executor.newInstance(httpClient);
    if (HttpGet.METHOD_NAME.equalsIgnoreCase(method)) {
        String result = contentUrl;
        StringBuilder sb = new StringBuilder();
        sb.append(contentUrl);
        if (bodyMap != null && !bodyMap.isEmpty()) {
            if (contentUrl.indexOf("?") > 0) {
                sb.append("&");
            } else {
                sb.append("?");
            }
            result = Joiner.on("&").appendTo(sb, bodyMap.entrySet()).toString();
        }

        return executor.execute(Request.Get(result)).returnContent().asString();
    }
    if (HttpPost.METHOD_NAME.equalsIgnoreCase(method)) {
        Request request = Request.Post(contentUrl);
        if (headerMap != null && !headerMap.isEmpty()) {
            for (Map.Entry<String, String> m : headerMap.entrySet()) {
                request.addHeader(m.getKey(), m.getValue());
            }
        }
        if (bodyMap != null && !bodyMap.isEmpty()) {
            Form form = Form.form();
            for (Map.Entry<String, String> m : bodyMap.entrySet()) {
                form.add(m.getKey(), m.getValue());
            }
            request.bodyForm(form.build());
        }
        return executor.execute(request).returnContent().asString();
    }
    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();
    for (Entry<String, String> entry : formParams.entrySet()) {
        formContent.add(entry.getKey(), entry.getValue());
    }/*  ww  w.  j ava2  s . c o  m*/

    // @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.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();
    for (Entry<String, String> entry : formParams.entrySet()) {
        formContent.add(entry.getKey(), entry.getValue());
    }//from  w  w w.  ja  v  a  2  s . co  m

    // @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:edu.jhuapl.dorset.http.apache.ApacheHttpClient.java

private List<NameValuePair> buildFormBody(HttpParameter[] parameters) {
    Form form = Form.form();
    for (HttpParameter param : parameters) {
        form.add(param.getName(), param.getValue());
    }//from   w w  w. j a va 2 s.c om
    return form.build();
}

From source file:com.twosigma.beaker.NamespaceClient.java

private String runStringRequest(String urlfragment, Form postdata) throws ClientProtocolException, IOException {
    String reply = Request.Post(ctrlUrlBase + urlfragment).addHeader("Authorization", auth)
            .bodyForm(postdata.build()).execute().returnContent().asString();
    return objectMapper.get().readValue(reply, StringObject.class).getText();
}

From source file:com.twosigma.beaker.NamespaceClient.java

private boolean runBooleanRequest(String urlfragment, Form postdata)
        throws ClientProtocolException, IOException {
    String reply = Request.Post(ctrlUrlBase + urlfragment).addHeader("Authorization", auth)
            .bodyForm(postdata.build()).execute().returnContent().asString();
    return objectMapper.get().readValue(reply, Boolean.class);
}

From source file:com.twosigma.beaker.NamespaceClient.java

public Object evaluate(String filter) throws ClientProtocolException, IOException {
    Form form = Form.form().add("filter", filter).add("session", this.session);
    String reply = Request.Post(ctrlUrlBase + "/evaluate").addHeader("Authorization", auth)
            .bodyForm(form.build()).execute().returnContent().asString();
    Object r;//from  w w w. j a  va 2 s . c  o  m
    try {
        JsonNode n = objectMapper.get().readTree(reply);
        r = objectSerializerProvider.get().deserialize(n, objectMapper.get());
        if (r == null)
            r = deserializeObject(reply);
    } catch (Exception e) {
        r = null;
    }
    return r;
}