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

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

Introduction

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

Prototype

public Request bodyForm(final Iterable<? extends NameValuePair> formParams, final Charset charset) 

Source Link

Usage

From source file:gate.tagger.tagme.TaggerTagMeWS.java

protected String retrieveServerResponse(String text) {
    Request req = Request.Post(getTagMeServiceUrl().toString());

    req.addHeader("Content-Type", "application/x-www-form-urlencoded");
    req.bodyForm(
            Form.form().add("text", text).add("gcube-token", getApiKey()).add("lang", getLanguageCode())
                    .add("tweet", getIsTweet().toString()).add("include_abstract", "false")
                    .add("include_categories", "false").add("include_all_spots", "false")
                    .add("long_text", getLongText().toString()).add("epsilon", getEpsilon().toString()).build(),
            Consts.UTF_8);/*from   www .  j av a  2s  .  c o m*/
    logger.debug("Request is " + req);
    Response res = null;
    try {
        res = req.execute();
    } catch (Exception ex) {
        throw new GateRuntimeException("Problem executing HTTP request: " + req, ex);
    }
    Content cont = null;
    try {
        cont = res.returnContent();
    } catch (Exception ex) {
        throw new GateRuntimeException("Problem getting HTTP response content: " + res, ex);
    }
    String ret = cont.asString();
    logger.debug("TagMe server response " + ret);
    return ret;
}

From source file:me.vertretungsplan.parser.LoginHandler.java

private String handleLogin(Executor executor, CookieStore cookieStore, boolean needsResponse)
        throws JSONException, IOException, CredentialInvalidException {
    if (auth == null)
        return null;
    if (!(auth instanceof UserPasswordCredential || auth instanceof PasswordCredential)) {
        throw new IllegalArgumentException("Wrong authentication type");
    }/*from  w  ww  .j a  va2  s . com*/

    String login;
    String password;
    if (auth instanceof UserPasswordCredential) {
        login = ((UserPasswordCredential) auth).getUsername();
        password = ((UserPasswordCredential) auth).getPassword();
    } else {
        login = null;
        password = ((PasswordCredential) auth).getPassword();
    }

    JSONObject data = scheduleData.getData();
    JSONObject loginConfig = data.getJSONObject(LOGIN_CONFIG);
    String type = loginConfig.optString(PARAM_TYPE, "post");
    switch (type) {
    case "post":
        List<Cookie> cookieList = cookieProvider != null ? cookieProvider.getCookies(auth) : null;
        if (cookieList != null && !needsResponse) {
            for (Cookie cookie : cookieList)
                cookieStore.addCookie(cookie);

            String checkUrl = loginConfig.optString(PARAM_CHECK_URL, null);
            String checkText = loginConfig.optString(PARAM_CHECK_TEXT, null);
            if (checkUrl != null && checkText != null) {
                String response = executor.execute(Request.Get(checkUrl)).returnContent().asString();
                if (!response.contains(checkText)) {
                    return null;
                }
            } else {
                return null;
            }
        }
        executor.clearCookies();
        Document preDoc = null;
        if (loginConfig.has(PARAM_PRE_URL)) {
            String preUrl = loginConfig.getString(PARAM_PRE_URL);
            String preHtml = executor.execute(Request.Get(preUrl)).returnContent().asString();
            preDoc = Jsoup.parse(preHtml);
        }

        String postUrl = loginConfig.getString(PARAM_URL);
        JSONObject loginData = loginConfig.getJSONObject(PARAM_DATA);
        List<NameValuePair> nvps = new ArrayList<>();

        String typo3Challenge = null;

        if (loginData.has("_hiddeninputs") && preDoc != null) {
            for (Element hidden : preDoc.select(loginData.getString("_hiddeninputs") + " input[type=hidden]")) {
                nvps.add(new BasicNameValuePair(hidden.attr("name"), hidden.attr("value")));
                if (hidden.attr("name").equals("challenge")) {
                    typo3Challenge = hidden.attr("value");
                }
            }
        }

        for (String name : JSONObject.getNames(loginData)) {
            String value = loginData.getString(name);

            if (name.equals("_hiddeninputs"))
                continue;

            switch (value) {
            case "_login":
                value = login;
                break;
            case "_password":
                value = password;
                break;
            case "_password_md5":
                value = DigestUtils.md5Hex(password);
                break;
            case "_password_md5_typo3":
                value = DigestUtils.md5Hex(login + ":" + DigestUtils.md5Hex(password) + ":" + typo3Challenge);
                break;
            }

            nvps.add(new BasicNameValuePair(name, value));
        }
        Request request = Request.Post(postUrl);
        if (loginConfig.optBoolean("form-data", false)) {
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            for (NameValuePair nvp : nvps) {
                builder.addTextBody(nvp.getName(), nvp.getValue());
            }
            request.body(builder.build());
        } else {
            request.bodyForm(nvps, Charset.forName("UTF-8"));
        }
        String html = executor.execute(request).returnContent().asString();
        if (cookieProvider != null)
            cookieProvider.saveCookies(auth, cookieStore.getCookies());

        String checkUrl = loginConfig.optString(PARAM_CHECK_URL, null);
        String checkText = loginConfig.optString(PARAM_CHECK_TEXT, null);
        if (checkUrl != null && checkText != null) {
            String response = executor.execute(Request.Get(checkUrl)).returnContent().asString();
            if (response.contains(checkText))
                throw new CredentialInvalidException();
        } else if (checkText != null) {
            if (html.contains(checkText))
                throw new CredentialInvalidException();
        }
        return html;
    case "basic":
        if (login == null)
            throw new IOException("wrong auth type");
        executor.auth(login, password);
        if (loginConfig.has(PARAM_URL)) {
            String url = loginConfig.getString(PARAM_URL);
            if (executor.execute(Request.Get(url)).returnResponse().getStatusLine().getStatusCode() != 200) {
                throw new CredentialInvalidException();
            }
        }
        break;
    case "ntlm":
        if (login == null)
            throw new IOException("wrong auth type");
        executor.auth(login, password, null, null);
        if (loginConfig.has(PARAM_URL)) {
            String url = loginConfig.getString(PARAM_URL);
            if (executor.execute(Request.Get(url)).returnResponse().getStatusLine().getStatusCode() != 200) {
                throw new CredentialInvalidException();
            }
        }
        break;
    case "fixed":
        String loginFixed = loginConfig.optString(PARAM_LOGIN, null);
        String passwordFixed = loginConfig.getString(PARAM_PASSWORD);
        if (!Objects.equals(loginFixed, login) || !Objects.equals(passwordFixed, password)) {
            throw new CredentialInvalidException();
        }
        break;
    }
    return null;
}

From source file:org.obm.sync.client.impl.AbstractClientImpl.java

@VisibleForTesting
void setPostRequestParameters(Request request, Multimap<String, String> parameters) {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    for (Entry<String, String> entry : parameters.entries()) {
        if (entry.getKey() != null && entry.getValue() != null) {
            nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }/*from  www.  jav  a 2  s  .  c o  m*/
    }
    request.bodyForm(nameValuePairs, Charsets.UTF_8);
}