use of com.gargoylesoftware.htmlunit.WebRequest in project ats-framework by Axway.
the class AbstractHtmlEngine method reloadFrames.
@PublicAtsApi
public void reloadFrames() {
// real browsers reloads the frames automatically
if (webDriver instanceof HtmlUnitDriver) {
Field webClientField = null;
boolean fieldAccessibleState = false;
try {
// Retrieve current WebClient instance (with the current page) from the Selenium WebDriver
TargetLocator targetLocator = webDriver.switchTo();
webClientField = targetLocator.getClass().getDeclaringClass().getDeclaredField("webClient");
fieldAccessibleState = webClientField.isAccessible();
webClientField.setAccessible(true);
WebClient webClient = (WebClient) webClientField.get(targetLocator.defaultContent());
HtmlPage page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
for (final FrameWindow frameWindow : page.getFrames()) {
final BaseFrameElement frame = frameWindow.getFrameElement();
// use == and not equals(...) to identify initial content (versus URL set to "about:blank")
if (frame.getEnclosedPage().getWebResponse().getWebRequest().getUrl() == WebClient.URL_ABOUT_BLANK) {
String src = frame.getSrcAttribute();
if (src != null && !src.isEmpty()) {
final URL url;
try {
url = ((HtmlPage) frame.getEnclosedPage()).getFullyQualifiedUrl(src);
} catch (final MalformedURLException e) {
String message = "Invalid src attribute of " + frame.getTagName() + ": url=[" + src + "]. Ignored.";
final IncorrectnessListener incorrectnessListener = webClient.getIncorrectnessListener();
incorrectnessListener.notify(message, this);
return;
}
if (isAlreadyLoadedByAncestor(url, ((HtmlPage) frame.getEnclosedPage()))) {
String message = "Recursive src attribute of " + frame.getTagName() + ": url=[" + src + "]. Ignored.";
final IncorrectnessListener incorrectnessListener = webClient.getIncorrectnessListener();
incorrectnessListener.notify(message, this);
log.info("Frame already loaded: " + frame.toString());
return;
}
try {
final WebRequest request = new WebRequest(url);
request.setAdditionalHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9, text/*;q=0.7, */*;q=0.5");
if (frameWindow.getName() == null || frameWindow.getName().isEmpty()) {
frameWindow.setName("frame_" + page.getFrames().indexOf(frameWindow));
}
webClient.loadWebResponseInto(webClient.loadWebResponse(request), frameWindow);
log.info("Frame loaded: " + frame.toString());
} catch (IOException e) {
log.error("Error when getting content for " + frame.getTagName() + " with src=" + url, e);
}
}
} else {
log.info("Frame already loaded: " + frame.toString());
}
}
} catch (Exception e) {
throw new SeleniumOperationException("Error retrieving internal Selenium web client", e);
} finally {
if (webClientField != null) {
webClientField.setAccessible(fieldAccessibleState);
}
}
}
}
use of com.gargoylesoftware.htmlunit.WebRequest in project zeppelin by apache.
the class ScreenCaptureHtmlUnitDriver method downloadCssAndImages.
// http://stackoverflow.com/questions/2244272/how-can-i-tell-htmlunits-webclient-to-download-images-and-css
protected byte[] downloadCssAndImages(WebClient webClient, HtmlPage page) throws Exception {
WebWindow currentWindow = webClient.getCurrentWindow();
Map<String, String> urlMapping = new HashMap<>();
Map<String, byte[]> files = new HashMap<>();
WebWindow window = null;
try {
window = webClient.getWebWindowByName(page.getUrl().toString() + "_screenshot");
webClient.getPage(window, new WebRequest(page.getUrl()));
} catch (Exception e) {
LOGGER.error("Exception in ScreenCaptureHtmlUnitDriver while downloadCssAndImages ", e);
window = webClient.openWindow(page.getUrl(), page.getUrl().toString() + "_screenshot");
}
String xPathExpression = "//*[name() = 'img' or name() = 'link' and (@type = 'text/css' or @type = 'image/x-icon') or @type = 'text/javascript']";
List<?> resultList = page.getByXPath(xPathExpression);
Iterator<?> i = resultList.iterator();
while (i.hasNext()) {
try {
HtmlElement el = (HtmlElement) i.next();
String resourceSourcePath = el.getAttribute("src").equals("") ? el.getAttribute("href") : el.getAttribute("src");
if (resourceSourcePath == null || resourceSourcePath.equals(""))
continue;
URL resourceRemoteLink = page.getFullyQualifiedUrl(resourceSourcePath);
String resourceLocalPath = mapLocalUrl(page, resourceRemoteLink, resourceSourcePath, urlMapping);
urlMapping.put(resourceSourcePath, resourceLocalPath);
if (!resourceRemoteLink.toString().endsWith(".css")) {
byte[] image = downloadImage(webClient, window, resourceRemoteLink);
files.put(resourceLocalPath, image);
} else {
String css = downloadCss(webClient, window, resourceRemoteLink);
for (String cssImagePath : getLinksFromCss(css)) {
URL cssImagelink = page.getFullyQualifiedUrl(cssImagePath.replace("\"", "").replace("\'", "").replace(" ", ""));
String cssImageLocalPath = mapLocalUrl(page, cssImagelink, cssImagePath, urlMapping);
files.put(cssImageLocalPath, downloadImage(webClient, window, cssImagelink));
}
files.put(resourceLocalPath, replaceRemoteUrlsWithLocal(css, urlMapping).replace("resources/", "./").getBytes());
}
} catch (Exception e) {
LOGGER.error("Exception in ScreenCaptureHtmlUnitDriver while resultList.iterator ", e);
}
}
String pagesrc = replaceRemoteUrlsWithLocal(page.getWebResponse().getContentAsString(), urlMapping);
files.put("page.html", pagesrc.getBytes());
webClient.setCurrentWindow(currentWindow);
return createZip(files);
}
use of com.gargoylesoftware.htmlunit.WebRequest 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);
}
}
use of com.gargoylesoftware.htmlunit.WebRequest in project workflow-cps-plugin by jenkinsci.
the class CpsFlowExecutionTest method pause.
@Issue("JENKINS-25736")
@Test
public void pause() {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
story.j.jenkins.setSecurityRealm(story.j.createDummySecurityRealm());
story.j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy().grant(Jenkins.READ, Item.READ).everywhere().toEveryone().grant(Jenkins.ADMINISTER).everywhere().to("admin").grant(Item.BUILD).onItems(p).to("dev"));
story.j.jenkins.save();
p.setDefinition(new CpsFlowDefinition("echo 'before'; semaphore 'one'; echo 'after'", true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
SemaphoreStep.waitForStart("one/1", b);
final CpsFlowExecution e = (CpsFlowExecution) b.getExecution();
assertFalse(e.isPaused());
JenkinsRule.WebClient wc = story.j.createWebClient();
String toggleUrlRel = b.getUrl() + PauseUnpauseAction.URL + "/toggle";
WebRequest wrs = new WebRequest(wc.createCrumbedUrl(toggleUrlRel), HttpMethod.POST);
try {
// like JenkinsRule.assertFails but taking a WebRequest:
fail("should have been rejected but produced: " + wc.getPage(wrs).getWebResponse().getContentAsString());
} catch (FailingHttpStatusCodeException x) {
// link not even offered
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, x.getStatusCode());
}
wc.login("admin").getPage(wrs);
assertTrue(e.isPaused());
story.j.waitForMessage("before", b);
SemaphoreStep.success("one/1", null);
// not a very strong way of ensuring that the pause actually happens
Thread.sleep(1000);
assertTrue(b.isBuilding());
assertTrue(e.isPaused());
// link should only be displayed conditionally:
String toggleUrlAbs = story.j.contextPath + "/" + toggleUrlRel;
story.j.createWebClient().login("admin").getPage(b).getAnchorByHref(toggleUrlAbs);
try {
story.j.createWebClient().getPage(b).getAnchorByHref(toggleUrlAbs);
fail("link should not be present for anonymous user without CANCEL");
} catch (ElementNotFoundException x) {
// good
}
}
});
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.getItemByFullName("p", WorkflowJob.class);
WorkflowRun b = p.getLastBuild();
assertTrue(b.isBuilding());
CpsFlowExecution e = (CpsFlowExecution) b.getExecution();
assertTrue(e.isPaused());
JenkinsRule.WebClient wc = story.j.createWebClient();
WebRequest wrs = new WebRequest(wc.createCrumbedUrl(b.getUrl() + PauseUnpauseAction.URL + "/toggle"), HttpMethod.POST);
wc.login("dev").getPage(wrs);
assertFalse(e.isPaused());
story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));
assertFalse(e.isPaused());
}
});
}
use of com.gargoylesoftware.htmlunit.WebRequest in project archiva by apache.
the class RepositoryServletProxiedSnapshotPolicyTest method assertGetProxiedSnapshotsArtifactWithPolicy.
private void assertGetProxiedSnapshotsArtifactWithPolicy(int expectation, String snapshotsPolicy, boolean hasManagedCopy, long deltaManagedToRemoteTimestamp) throws Exception {
// --- Setup
setupSnapshotsRemoteRepo();
setupCleanInternalRepo();
String resourcePath = "org/apache/archiva/test/2.0-SNAPSHOT/test-2.0-SNAPSHOT.jar";
String expectedRemoteContents = "archiva-test-2.0-SNAPSHOT|jar-remote-contents";
String expectedManagedContents = null;
Path remoteFile = populateRepo(remoteSnapshots, resourcePath, expectedRemoteContents);
if (hasManagedCopy) {
expectedManagedContents = "archiva-test-2.0-SNAPSHOT|jar-managed-contents";
Path managedFile = populateRepo(repoRootInternal, resourcePath, expectedManagedContents);
Files.setLastModifiedTime(managedFile, FileTime.fromMillis(Files.getLastModifiedTime(remoteFile).toMillis() + deltaManagedToRemoteTimestamp));
}
setupSnapshotConnector(REPOID_INTERNAL, remoteSnapshots, snapshotsPolicy);
saveConfiguration();
// --- Execution
// process the response code later, not via an exception.
// HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
WebRequest request = new GetMethodWebRequest("http://machine.com/repository/internal/" + resourcePath);
WebResponse response = getServletUnitClient().getResponse(request);
switch(expectation) {
case EXPECT_MANAGED_CONTENTS:
assertResponseOK(response);
assertTrue("Invalid Test Case: Can't expect managed contents with " + "test that doesn't have a managed copy in the first place.", hasManagedCopy);
assertEquals("Expected managed file contents", expectedManagedContents, response.getContentAsString());
break;
case EXPECT_REMOTE_CONTENTS:
assertResponseOK(response);
assertEquals("Expected remote file contents", expectedRemoteContents, response.getContentAsString());
break;
case EXPECT_NOT_FOUND:
assertResponseNotFound(response);
assertManagedFileNotExists(repoRootInternal, resourcePath);
break;
}
}
Aggregations