Search in sources :

Example 11 with FormElement

use of org.jsoup.nodes.FormElement in project jsoup by jhy.

the class HtmlTreeBuilder method insertForm.

FormElement insertForm(Token.StartTag startTag, boolean onStack, boolean checkTemplateStack) {
    Tag tag = tagFor(startTag.name(), settings);
    FormElement el = new FormElement(tag, null, settings.normalizeAttributes(startTag.attributes));
    if (checkTemplateStack) {
        if (!onStack("template"))
            setFormElement(el);
    } else
        setFormElement(el);
    insertNode(el);
    if (onStack)
        stack.add(el);
    return el;
}
Also used : FormElement(org.jsoup.nodes.FormElement)

Example 12 with FormElement

use of org.jsoup.nodes.FormElement in project syndesis-qe by syndesisio.

the class BoxUtils method getAccessToken.

/**
 * From org.apache.camel.component.box.internal.BoxConnectionHelper
 *
 * @return box access token
 */
private String getAccessToken() throws Exception {
    final String csrfToken = String.valueOf(new SecureRandom().nextLong());
    final String url = OAUTH_URL + boxAccount.getProperty("clientId") + "&state=" + csrfToken;
    final Connection.Response loginPageResponse = Jsoup.connect(url).method(Connection.Method.GET).execute();
    Document loginPage = loginPageResponse.parse();
    validatePage(loginPage);
    final FormElement loginForm = (FormElement) loginPage.select("form[name=login_form]").first();
    final Element loginField = loginForm.select("input[name=login]").first();
    loginField.val(boxAccount.getProperty("userName"));
    final Element passwordField = loginForm.select("input[name=password]").first();
    passwordField.val(boxAccount.getProperty("userPassword"));
    final Map<String, String> cookies = new HashMap<>(loginPageResponse.cookies());
    Connection.Response response = loginForm.submit().cookies(cookies).execute();
    cookies.putAll(response.cookies());
    final Document consentPage = response.parse();
    validatePage(consentPage);
    final FormElement consentForm = (FormElement) consentPage.select("form[name=consent_form]").first();
    consentForm.elements().removeIf(e -> e.attr("name").equals("consent_reject"));
    // parse request_token from javascript from head, it is the first script in the header
    final String requestTokenScript = consentPage.select("script").first().html();
    final String requestToken = StringUtils.substringBetween(requestTokenScript, "'", "'");
    if (requestToken == null) {
        fail("Coudln't parse request token from " + requestTokenScript);
    }
    response = consentForm.submit().data("request_token", requestToken).followRedirects(false).cookies(cookies).execute();
    final String location = response.header("Location");
    final String code = StringUtils.substringAfterLast(location, "code=");
    if (code == null) {
        fail("Unable to get code from " + location);
    }
    return code;
}
Also used : HashMap(java.util.HashMap) Element(org.jsoup.nodes.Element) FormElement(org.jsoup.nodes.FormElement) Connection(org.jsoup.Connection) BoxAPIConnection(com.box.sdk.BoxAPIConnection) SecureRandom(java.security.SecureRandom) Document(org.jsoup.nodes.Document) FormElement(org.jsoup.nodes.FormElement)

Aggregations

FormElement (org.jsoup.nodes.FormElement)12 Document (org.jsoup.nodes.Document)9 Connection (org.jsoup.Connection)6 Element (org.jsoup.nodes.Element)5 MultipartBody (okhttp3.MultipartBody)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 FormBody (okhttp3.FormBody)2 JSONException (org.json.JSONException)2 Elements (org.jsoup.select.Elements)2 Test (org.junit.Test)2 BoxAPIConnection (com.box.sdk.BoxAPIConnection)1 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)1 Copy (de.geeksfactory.opacclient.objects.Copy)1 SearchRequestResult (de.geeksfactory.opacclient.objects.SearchRequestResult)1 SearchResult (de.geeksfactory.opacclient.objects.SearchResult)1