Search in sources :

Example 86 with WebClient

use of com.gargoylesoftware.htmlunit.WebClient in project camel by apache.

the class BoxConnectionHelper method createStandardAuthenticatedConnection.

public static BoxAPIConnection createStandardAuthenticatedConnection(BoxConfiguration configuration) {
    // Create web client for first leg of OAuth2
    //
    final WebClient webClient = new WebClient();
    final WebClientOptions options = webClient.getOptions();
    options.setRedirectEnabled(true);
    options.setJavaScriptEnabled(false);
    options.setThrowExceptionOnFailingStatusCode(true);
    options.setThrowExceptionOnScriptError(true);
    options.setPrintContentOnFailingStatusCode(LOG.isDebugEnabled());
    try {
        // use default SSP to create supported non-SSL protocols list
        final SSLContext sslContext = new SSLContextParameters().createSSLContext(null);
        options.setSSLClientProtocols(sslContext.createSSLEngine().getEnabledProtocols());
    } catch (GeneralSecurityException e) {
        throw ObjectHelper.wrapRuntimeCamelException(e);
    } catch (IOException e) {
        throw ObjectHelper.wrapRuntimeCamelException(e);
    } finally {
        if (webClient != null) {
            webClient.close();
        }
    }
    // disable default gzip compression, as htmlunit does not negotiate
    // pages sent with no compression
    new WebConnectionWrapper(webClient) {

        @Override
        public WebResponse getResponse(WebRequest request) throws IOException {
            request.setAdditionalHeader(HttpHeaders.ACCEPT_ENCODING, "identity");
            return super.getResponse(request);
        }
    };
    // add HTTP proxy if set
    final Map<String, Object> httpParams = configuration.getHttpParams();
    if (httpParams != null && httpParams.get("http.route.default-proxy") != null) {
        final HttpHost proxyHost = (HttpHost) httpParams.get("http.route.default-proxy");
        final Boolean socksProxy = (Boolean) httpParams.get("http.route.socks-proxy");
        final ProxyConfig proxyConfig = new ProxyConfig(proxyHost.getHostName(), proxyHost.getPort(), socksProxy != null ? socksProxy : false);
        options.setProxyConfig(proxyConfig);
    }
    // authorize application on user's behalf
    try {
        // generate anti-forgery token to prevent/detect CSRF attack
        final String csrfToken = String.valueOf(new SecureRandom().nextLong());
        final HtmlPage authPage = webClient.getPage(authorizationUrl(configuration.getClientId(), csrfToken));
        // look for <div role="error_message">
        final HtmlDivision div = authPage.getFirstByXPath("//div[contains(concat(' ', @class, ' '), ' error_message ')]");
        if (div != null) {
            final String errorMessage = div.getTextContent().replaceAll("\\s+", " ").replaceAll(" Show Error Details", ":").trim();
            throw new IllegalArgumentException("Error authorizing application: " + errorMessage);
        }
        // submit login credentials
        final HtmlForm loginForm = authPage.getFormByName("login_form");
        final HtmlTextInput login = loginForm.getInputByName("login");
        login.setText(configuration.getUserName());
        final HtmlPasswordInput password = loginForm.getInputByName("password");
        password.setText(configuration.getUserPassword());
        final HtmlSubmitInput submitInput = loginForm.getInputByName("login_submit");
        // submit consent
        final HtmlPage consentPage = submitInput.click();
        final HtmlForm consentForm = consentPage.getFormByName("consent_form");
        final HtmlButton consentAccept = consentForm.getButtonByName("consent_accept");
        // disable redirect to avoid loading redirect URL
        webClient.getOptions().setRedirectEnabled(false);
        // validate CSRF and get authorization code
        String redirectQuery;
        try {
            final Page redirectPage = consentAccept.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();
        }
        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 (!csrfToken.equals(state)) {
            throw new SecurityException("Invalid CSRF code!");
        } else {
            // get authorization code
            final String authorizationCode = params.get("code");
            return new BoxAPIConnection(configuration.getClientId(), configuration.getClientSecret(), authorizationCode);
        }
    } catch (BoxAPIException e) {
        throw new RuntimeCamelException(String.format("Box API connection failed: API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e);
    } catch (Exception e) {
        throw new RuntimeCamelException(String.format("Box API connection failed: %s", e.getMessage()), e);
    }
}
Also used : WebClientOptions(com.gargoylesoftware.htmlunit.WebClientOptions) HtmlTextInput(com.gargoylesoftware.htmlunit.html.HtmlTextInput) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) HtmlPasswordInput(com.gargoylesoftware.htmlunit.html.HtmlPasswordInput) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Page(com.gargoylesoftware.htmlunit.Page) HtmlDivision(com.gargoylesoftware.htmlunit.html.HtmlDivision) BoxAPIException(com.box.sdk.BoxAPIException) URL(java.net.URL) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) HtmlSubmitInput(com.gargoylesoftware.htmlunit.html.HtmlSubmitInput) HttpHost(org.apache.http.HttpHost) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) GeneralSecurityException(java.security.GeneralSecurityException) BoxAPIConnection(com.box.sdk.BoxAPIConnection) SecureRandom(java.security.SecureRandom) GeneralSecurityException(java.security.GeneralSecurityException) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) ProxyConfig(com.gargoylesoftware.htmlunit.ProxyConfig) WebClient(com.gargoylesoftware.htmlunit.WebClient) BoxAPIException(com.box.sdk.BoxAPIException) GeneralSecurityException(java.security.GeneralSecurityException) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) SSLContextParameters(org.apache.camel.util.jsse.SSLContextParameters) HtmlButton(com.gargoylesoftware.htmlunit.html.HtmlButton) RuntimeCamelException(org.apache.camel.RuntimeCamelException) WebConnectionWrapper(com.gargoylesoftware.htmlunit.util.WebConnectionWrapper)

Example 87 with WebClient

use of com.gargoylesoftware.htmlunit.WebClient in project sling by apache.

the class ITWebConsoleRemote method prepareWebClient.

@Before
public void prepareWebClient() {
    webClient = new WebClient();
    ((DefaultCredentialsProvider) webClient.getCredentialsProvider()).addCredentials("admin", "admin");
}
Also used : DefaultCredentialsProvider(com.gargoylesoftware.htmlunit.DefaultCredentialsProvider) WebClient(com.gargoylesoftware.htmlunit.WebClient) Before(org.junit.Before)

Example 88 with WebClient

use of com.gargoylesoftware.htmlunit.WebClient in project sling by apache.

the class MetricWebConsolePluginTest method webConsolePlugin.

@Test
public void webConsolePlugin() throws Exception {
    MetricRegistry reg1 = new MetricRegistry();
    reg1.meter("test1").mark(5);
    reg1.timer("test2").time().close();
    reg1.histogram("test3").update(743);
    reg1.counter("test4").inc(9);
    reg1.registerAll(new JvmAttributeGaugeSet());
    context.registerService(MetricRegistry.class, reg1, regProps("foo"));
    activatePlugin();
    StringWriter sw = new StringWriter();
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(new PrintWriter(sw));
    plugin.doGet(mock(HttpServletRequest.class), response);
    WebClient client = new WebClient();
    WebResponse resp = new StringWebResponse(sw.toString(), WebClient.URL_ABOUT_BLANK);
    HtmlPage page = HTMLParser.parseHtml(resp, client.getCurrentWindow());
    assertTable("data-meters", page);
    assertTable("data-counters", page);
    assertTable("data-timers", page);
    assertTable("data-histograms", page);
    assertTable("data-gauges", page);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JvmAttributeGaugeSet(com.codahale.metrics.JvmAttributeGaugeSet) StringWebResponse(com.gargoylesoftware.htmlunit.StringWebResponse) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) StringWebResponse(com.gargoylesoftware.htmlunit.StringWebResponse) StringWriter(java.io.StringWriter) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) MetricRegistry(com.codahale.metrics.MetricRegistry) HttpServletResponse(javax.servlet.http.HttpServletResponse) WebClient(com.gargoylesoftware.htmlunit.WebClient) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 89 with WebClient

use of com.gargoylesoftware.htmlunit.WebClient in project nymph by Onnt.

the class HtmlUnit method getPage.

@SuppressWarnings("resource")
public String getPage(String path) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
    final WebClient webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setActiveXNative(false);
    webClient.getOptions().setCssEnabled(false);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.waitForBackgroundJavaScript(600 * 1000);
    webClient.setAjaxController(new NicelyResynchronizingAjaxController());
    final HtmlPage page = webClient.getPage(path);
    webClient.waitForBackgroundJavaScript(1000 * 3);
    webClient.setJavaScriptTimeout(0);
    webClient.getOptions().setJavaScriptEnabled(true);
    return page.asXml();
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) NicelyResynchronizingAjaxController(com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController) WebClient(com.gargoylesoftware.htmlunit.WebClient)

Example 90 with WebClient

use of com.gargoylesoftware.htmlunit.WebClient in project testcases by coheigea.

the class OIDCTest method setupWebClient.

private static WebClient setupWebClient(String user, String password, String idpPort) {
    final WebClient webClient = new WebClient();
    webClient.getOptions().setUseInsecureSSL(true);
    webClient.getCredentialsProvider().setCredentials(new AuthScope("localhost", Integer.parseInt(idpPort)), new UsernamePasswordCredentials(user, password));
    webClient.getOptions().setJavaScriptEnabled(false);
    return webClient;
}
Also used : AuthScope(org.apache.http.auth.AuthScope) WebClient(com.gargoylesoftware.htmlunit.WebClient) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

WebClient (com.gargoylesoftware.htmlunit.WebClient)182 Test (org.junit.Test)110 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)55 Page (com.gargoylesoftware.htmlunit.Page)33 TextPage (com.gargoylesoftware.htmlunit.TextPage)21 Before (org.junit.Before)20 HtmlSubmitInput (com.gargoylesoftware.htmlunit.html.HtmlSubmitInput)17 URL (java.net.URL)14 JsonObject (javax.json.JsonObject)13 RunAsClient (org.jboss.arquillian.container.test.api.RunAsClient)13 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)12 SpecAssertion (org.jboss.test.audit.annotations.SpecAssertion)11 HtmlSpan (com.gargoylesoftware.htmlunit.html.HtmlSpan)10 ReadContext (com.jayway.jsonpath.ReadContext)10 File (java.io.File)9 IOException (java.io.IOException)8 NicelyResynchronizingAjaxController (com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController)7 Test (org.junit.jupiter.api.Test)7 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)6 DefaultCredentialsProvider (com.gargoylesoftware.htmlunit.DefaultCredentialsProvider)5