use of com.gargoylesoftware.htmlunit.html.HtmlTextInput in project camel by apache.
the class LinkedInOAuthRequestFilter method getRefreshToken.
@SuppressWarnings("deprecation")
private String getRefreshToken() {
// disable redirect to avoid loading error redirect URL
webClient.getOptions().setRedirectEnabled(false);
try {
final String csrfId = String.valueOf(new SecureRandom().nextLong());
final String encodedRedirectUri = URLEncoder.encode(oAuthParams.getRedirectUri(), "UTF-8");
final OAuthScope[] scopes = oAuthParams.getScopes();
final String url;
if (scopes == null || scopes.length == 0) {
url = String.format(AUTHORIZATION_URL, oAuthParams.getClientId(), csrfId, encodedRedirectUri);
} else {
final int nScopes = scopes.length;
final StringBuilder builder = new StringBuilder();
int i = 0;
for (OAuthScope scope : scopes) {
builder.append(scope.getValue());
if (++i < nScopes) {
builder.append("%20");
}
}
url = String.format(AUTHORIZATION_URL_WITH_SCOPE, oAuthParams.getClientId(), csrfId, builder.toString(), encodedRedirectUri);
}
HtmlPage authPage;
try {
authPage = webClient.getPage(url);
} catch (FailingHttpStatusCodeException e) {
// only handle errors returned with redirects
if (e.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
final URL location = new URL(e.getResponse().getResponseHeaderValue(HttpHeaders.LOCATION));
final String locationQuery = location.getQuery();
if (locationQuery != null && locationQuery.contains("error=")) {
throw new IOException(URLDecoder.decode(locationQuery).replaceAll("&", ", "));
} else {
// follow the redirect to login form
authPage = webClient.getPage(location);
}
} else {
throw e;
}
}
// look for <div role="alert">
final HtmlDivision div = authPage.getFirstByXPath("//div[@role='alert']");
if (div != null) {
throw new IllegalArgumentException("Error authorizing application: " + div.getTextContent());
}
// submit login credentials
final HtmlForm loginForm = authPage.getFormByName("oauth2SAuthorizeForm");
final HtmlTextInput login = loginForm.getInputByName("session_key");
login.setText(oAuthParams.getUserName());
final HtmlPasswordInput password = loginForm.getInputByName("session_password");
password.setText(oAuthParams.getUserPassword());
final HtmlSubmitInput submitInput = loginForm.getInputByName("authorize");
// validate CSRF and get authorization code
String redirectQuery;
try {
final Page redirectPage = submitInput.click();
redirectQuery = redirectPage.getUrl().getQuery();
} catch (FailingHttpStatusCodeException e) {
// escalate non redirect errors
if (e.getStatusCode() != HttpStatus.SC_MOVED_TEMPORARILY) {
throw e;
}
final String location = e.getResponse().getResponseHeaderValue("Location");
redirectQuery = new URL(location).getQuery();
}
if (redirectQuery == null) {
throw new IllegalArgumentException("Redirect response query is null, check username, password and permissions");
}
final Map<String, String> params = new HashMap<String, String>();
final Matcher matcher = QUERY_PARAM_PATTERN.matcher(redirectQuery);
while (matcher.find()) {
params.put(matcher.group(1), matcher.group(2));
}
final String state = params.get("state");
if (!csrfId.equals(state)) {
throw new SecurityException("Invalid CSRF code!");
} else {
// TODO check results??
return params.get("code");
}
} catch (IOException e) {
throw new IllegalArgumentException("Error authorizing application: " + e.getMessage(), e);
}
}
use of com.gargoylesoftware.htmlunit.html.HtmlTextInput in project java-design-patterns by iluwatar.
the class AlbumPage method changeAlbumRating.
/**
* Sets the album rating input text field
*
* @param albumRating the new album rating value to set
* @return {@link AlbumPage}
*/
public AlbumPage changeAlbumRating(String albumRating) {
HtmlTextInput albumRatingInputTextField = (HtmlTextInput) page.getElementById("albumRating");
albumRatingInputTextField.setText(albumRating);
return this;
}
use of com.gargoylesoftware.htmlunit.html.HtmlTextInput in project tutorials by eugenp.
the class HtmlUnitAndSpringLiveTest method givenAMessage_whenSent_thenItShows.
@Test
public void givenAMessage_whenSent_thenItShows() throws Exception {
String text = "Hello world!";
HtmlPage page;
String url = "http://localhost/message/showForm";
page = webClient.getPage(url);
HtmlTextInput messageText = page.getHtmlElementById("message");
messageText.setValueAttribute(text);
HtmlForm form = page.getForms().get(0);
HtmlSubmitInput submit = form.getOneHtmlElementByAttribute("input", "type", "submit");
HtmlPage newPage = submit.click();
String receivedText = newPage.getHtmlElementById("received").getTextContent();
Assert.assertEquals(receivedText, text);
}
use of com.gargoylesoftware.htmlunit.html.HtmlTextInput in project testcases by coheigea.
the class OIDCTest method registerNewClient.
private static HtmlPage registerNewClient(WebClient webClient, String url, String clientName, String redirectURI, String clientAudience) throws Exception {
HtmlPage registerPage = webClient.getPage(url + "/register");
final HtmlForm form = registerPage.getForms().get(0);
// Set new client values
final HtmlTextInput clientNameInput = form.getInputByName("client_name");
clientNameInput.setValueAttribute(clientName);
final HtmlSelect clientTypeSelect = form.getSelectByName("client_type");
clientTypeSelect.setSelectedAttribute("confidential", true);
final HtmlTextInput redirectURIInput = form.getInputByName("client_redirectURI");
redirectURIInput.setValueAttribute(redirectURI);
final HtmlTextInput clientAudienceURIInput = form.getInputByName("client_audience");
clientAudienceURIInput.setValueAttribute(clientAudience);
final HtmlButton button = form.getButtonByName("submit_button");
return button.click();
}
use of com.gargoylesoftware.htmlunit.html.HtmlTextInput in project yyl_example by Relucent.
the class HtmlUnitTest1 method main.
public static void main(String[] args) {
// 得到浏览器对象
WebClient webClient = null;
try {
webClient = new WebClient();
webClient.waitForBackgroundJavaScript(60 * 1000);
WebClientOptions options = webClient.getOptions();
// 不加载css
options.setCssEnabled(true);
// 启用JS解释器,默认为true
options.setJavaScriptEnabled(false);
options.setUseInsecureSSL(true);
// options.setRedirectEnabled(true);
// 当出现HttpError时,是否抛出异常
options.setThrowExceptionOnFailingStatusCode(false);
// JS运行错误时,是否抛出异常
options.setThrowExceptionOnScriptError(false);
// 设置AJAX异步处理控制器(启用AJAX支持)
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
// 拿到网页
HtmlPage htmlpage = webClient.getPage("http://news.baidu.com/advanced_news.html");
// 根据名字得到表单(表单的名字叫“f”)
HtmlForm form = htmlpage.getFormByName("f");
System.out.println(form);
// 获取“百度一下”这个按钮
HtmlSubmitInput button = form.getInputByValue("百度一下");
System.out.println(button);
// 得到搜索框
HtmlTextInput textField = form.getInputByName("q1");
System.out.println(textField);
// 在搜索框内填入“HtmlUnit”
textField.setValueAttribute("HtmlUnit");
// 点击按钮
HtmlPage nextPage = button.click();
// 下一个页面
System.out.println(nextPage);
// 获得页面结果
String result = nextPage.asXml();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (webClient != null) {
webClient.close();
}
}
}
Aggregations