use of com.gargoylesoftware.htmlunit.AppletConfirmHandler in project htmlunit by HtmlUnit.
the class HtmlAppletTest method appletConfirmHandler.
/**
* @throws Exception if the test fails
*/
@Test
public void appletConfirmHandler() throws Exception {
Assume.assumeFalse(SKIP_);
if (areAppletsNotSupported()) {
return;
}
final URL url = getClass().getResource("/applets/simpleAppletDoIt.html");
final WebClient webClient = getWebClient();
final List<String> collectedStatus = new ArrayList<>();
final StatusHandler statusHandler = new StatusHandler() {
@Override
public void statusMessageChanged(final Page page, final String message) {
collectedStatus.add(message);
}
};
webClient.setStatusHandler(statusHandler);
webClient.getOptions().setAppletEnabled(true);
webClient.setAppletConfirmHandler(new AppletConfirmHandler() {
@Override
public boolean confirm(final HtmlApplet applet) {
assertEquals("simpleAppletDoIt.jar", applet.getArchiveAttribute());
return true;
}
@Override
public boolean confirm(final HtmlObject applet) {
return false;
}
});
final HtmlPage page = webClient.getPage(url);
final DomNodeList<DomElement> applets = page.getElementsByTagName("applet");
assertEquals(1, applets.size());
final HtmlApplet htmlApplet = (HtmlApplet) applets.get(0);
assertTrue(htmlApplet.getApplet() != null);
}
use of com.gargoylesoftware.htmlunit.AppletConfirmHandler in project htmlunit by HtmlUnit.
the class HtmlObjectTest method appletConfirmHandlerPermit.
/**
* @throws Exception if the test fails
*/
@Test
public void appletConfirmHandlerPermit() throws Exception {
if (getBrowserVersion().isChrome()) {
return;
}
final URL url = getClass().getResource("/objects/simpleAppletDoIt.html");
final WebClient webClient = getWebClient();
final List<String> collectedStatus = new ArrayList<>();
final StatusHandler statusHandler = new StatusHandler() {
@Override
public void statusMessageChanged(final Page page, final String message) {
collectedStatus.add(message);
}
};
webClient.setStatusHandler(statusHandler);
webClient.getOptions().setAppletEnabled(true);
webClient.setAppletConfirmHandler(new AppletConfirmHandler() {
@Override
public boolean confirm(final HtmlApplet applet) {
return true;
}
@Override
public boolean confirm(final HtmlObject applet) {
assertEquals("simpleAppletDoIt.jar", applet.getArchiveAttribute());
return false;
}
});
final HtmlPage page = webClient.getPage(url);
final DomNodeList<DomElement> objects = page.getElementsByTagName("object");
assertEquals(1, objects.size());
final HtmlObject htmlObject = (HtmlObject) objects.get(0);
assertTrue(htmlObject.getApplet() == null);
}
use of com.gargoylesoftware.htmlunit.AppletConfirmHandler in project htmlunit by HtmlUnit.
the class HtmlApplet method setupAppletIfNeeded.
/**
* Download the associated content specified in the code attribute.
*
* @throws IOException if an error occurs while downloading the content
*/
@SuppressWarnings("unchecked")
private synchronized void setupAppletIfNeeded() throws IOException {
if (applet_ != null) {
return;
}
final HashMap<String, String> params = new HashMap<>();
params.put("name", getNameAttribute());
params.put("object", getObjectAttribute());
params.put("align", getAlignAttribute());
params.put("alt", getAltAttribute());
params.put("height", getHeightAttribute());
params.put("hspace", getHspaceAttribute());
params.put("vspace", getVspaceAttribute());
params.put("width", getWidthAttribute());
final DomNodeList<HtmlElement> paramTags = getElementsByTagName("param");
for (final HtmlElement paramTag : paramTags) {
final HtmlParameter parameter = (HtmlParameter) paramTag;
params.put(parameter.getNameAttribute(), parameter.getValueAttribute());
}
if (StringUtils.isEmpty(params.get(CODEBASE)) && StringUtils.isNotEmpty(getCodebaseAttribute())) {
params.put(CODEBASE, getCodebaseAttribute());
}
if (StringUtils.isEmpty(params.get(ARCHIVE)) && StringUtils.isNotEmpty(getArchiveAttribute())) {
params.put(ARCHIVE, getArchiveAttribute());
}
final HtmlPage page = (HtmlPage) getPage();
final WebClient webclient = page.getWebClient();
final AppletConfirmHandler handler = webclient.getAppletConfirmHandler();
if (null != handler && !handler.confirm(this)) {
return;
}
String appletClassName = getCodeAttribute();
if (appletClassName.endsWith(".class")) {
appletClassName = appletClassName.substring(0, appletClassName.length() - 6);
}
try (AppletClassLoader appletClassLoader = new AppletClassLoader(getPage().getEnclosingWindow().getScriptableObject(), Thread.currentThread().getContextClassLoader())) {
final String documentUrl = page.getUrl().toExternalForm();
String baseUrl = UrlUtils.resolveUrl(documentUrl, ".");
final String codebaseProperty = params.get(CODEBASE);
if (StringUtils.isNotEmpty(codebaseProperty)) {
// codebase can be relative to the page
baseUrl = UrlUtils.resolveUrl(baseUrl, codebaseProperty);
}
if (!baseUrl.endsWith("/")) {
baseUrl = baseUrl + "/";
}
// check archive
final List<URL> archiveUrls = new ArrayList<>();
String[] archives = StringUtils.split(params.get(ARCHIVE), ',');
if (null != archives) {
for (final String tmpArchive : archives) {
final String tempUrl = UrlUtils.resolveUrl(baseUrl, tmpArchive.trim());
final URL archiveUrl = UrlUtils.toUrlUnsafe(tempUrl);
appletClassLoader.addArchiveToClassPath(archiveUrl);
archiveUrls.add(archiveUrl);
}
}
archives = StringUtils.split(params.get(CACHE_ARCHIVE), ',');
if (null != archives) {
for (final String tmpArchive : archives) {
final String tempUrl = UrlUtils.resolveUrl(baseUrl, tmpArchive.trim());
final URL archiveUrl = UrlUtils.toUrlUnsafe(tempUrl);
appletClassLoader.addArchiveToClassPath(archiveUrl);
archiveUrls.add(archiveUrl);
}
}
archiveUrls_ = Collections.unmodifiableList(archiveUrls);
// no archive attribute, single class
if (archiveUrls_.isEmpty()) {
final String tempUrl = UrlUtils.resolveUrl(baseUrl, getCodeAttribute());
final URL classUrl = UrlUtils.toUrlUnsafe(tempUrl);
final WebResponse response = webclient.loadWebResponse(new WebRequest(classUrl));
try {
webclient.throwFailingHttpStatusCodeExceptionIfNecessary(response);
appletClassLoader.addClassToClassPath(appletClassName, response);
} catch (final FailingHttpStatusCodeException e) {
// that is what the browser does, the applet only fails, if
// the main class is not loadable
LOG.error(e.getMessage(), e);
}
}
try {
final Class<Applet> appletClass = (Class<Applet>) appletClassLoader.loadClass(appletClassName);
applet_ = appletClass.newInstance();
applet_.setStub(new AppletStubImpl(getHtmlPageOrNull(), params, UrlUtils.toUrlUnsafe(baseUrl), UrlUtils.toUrlUnsafe(documentUrl)));
applet_.init();
applet_.start();
} catch (final ClassNotFoundException | InstantiationException | IllegalAccessException e) {
if (LOG.isErrorEnabled()) {
LOG.error("Loading applet '" + appletClassName + "' failed\n" + " " + e + "\n Classpath:\n" + appletClassLoader.info());
}
throw new RuntimeException(e);
}
}
}
use of com.gargoylesoftware.htmlunit.AppletConfirmHandler in project htmlunit by HtmlUnit.
the class HtmlObject method setupAppletIfNeeded.
/**
* Download the associated content specified in the code attribute.
*
* @throws IOException if an error occurs while downloading the content
*/
@SuppressWarnings("unchecked")
private synchronized void setupAppletIfNeeded() throws IOException {
if (applet_ != null) {
return;
}
if (StringUtils.isBlank(getTypeAttribute()) || !getTypeAttribute().startsWith(APPLET_TYPE)) {
return;
}
final HashMap<String, String> params = new HashMap<>();
params.put("name", getNameAttribute());
params.put("height", getHeightAttribute());
params.put("width", getWidthAttribute());
final DomNodeList<HtmlElement> paramTags = getElementsByTagName("param");
for (final HtmlElement paramTag : paramTags) {
final HtmlParameter parameter = (HtmlParameter) paramTag;
params.put(parameter.getNameAttribute(), parameter.getValueAttribute());
}
if (StringUtils.isEmpty(params.get(CODEBASE)) && StringUtils.isNotEmpty(getCodebaseAttribute())) {
params.put(CODEBASE, getCodebaseAttribute());
}
if (StringUtils.isEmpty(params.get(ARCHIVE)) && StringUtils.isNotEmpty(getArchiveAttribute())) {
params.put(ARCHIVE, getArchiveAttribute());
}
final HtmlPage page = (HtmlPage) getPage();
final WebClient webclient = page.getWebClient();
final AppletConfirmHandler handler = webclient.getAppletConfirmHandler();
if (null != handler && !handler.confirm(this)) {
return;
}
String appletClassName = getAttributeDirect("code");
if (StringUtils.isEmpty(appletClassName)) {
appletClassName = params.get("code");
}
if (appletClassName.endsWith(".class")) {
appletClassName = appletClassName.substring(0, appletClassName.length() - 6);
}
try (AppletClassLoader appletClassLoader = new AppletClassLoader(getPage().getEnclosingWindow().getScriptableObject(), Thread.currentThread().getContextClassLoader())) {
final String documentUrl = page.getUrl().toExternalForm();
String baseUrl = UrlUtils.resolveUrl(documentUrl, ".");
final String codebaseProperty = params.get(CODEBASE);
if (StringUtils.isNotEmpty(codebaseProperty)) {
// codebase can be relative to the page
baseUrl = UrlUtils.resolveUrl(baseUrl, codebaseProperty);
}
if (!baseUrl.endsWith("/")) {
baseUrl = baseUrl + "/";
}
// check archive
List<URL> archiveUrls = new ArrayList<>();
String[] archives = StringUtils.split(params.get(ARCHIVE), ',');
if (null != archives) {
for (final String tmpArchive : archives) {
final String tempUrl = UrlUtils.resolveUrl(baseUrl, tmpArchive.trim());
final URL archiveUrl = UrlUtils.toUrlUnsafe(tempUrl);
appletClassLoader.addArchiveToClassPath(archiveUrl);
archiveUrls.add(archiveUrl);
}
}
archives = StringUtils.split(params.get(CACHE_ARCHIVE), ',');
if (null != archives) {
for (final String tmpArchive : archives) {
final String tempUrl = UrlUtils.resolveUrl(baseUrl, tmpArchive.trim());
final URL archiveUrl = UrlUtils.toUrlUnsafe(tempUrl);
appletClassLoader.addArchiveToClassPath(archiveUrl);
archiveUrls.add(archiveUrl);
}
}
archiveUrls = Collections.unmodifiableList(archiveUrls);
// no archive attribute, single class
if (archiveUrls.isEmpty()) {
final String tempUrl = UrlUtils.resolveUrl(baseUrl, getAttributeDirect("code"));
final URL classUrl = UrlUtils.toUrlUnsafe(tempUrl);
final WebResponse response = webclient.loadWebResponse(new WebRequest(classUrl));
try {
webclient.throwFailingHttpStatusCodeExceptionIfNecessary(response);
appletClassLoader.addClassToClassPath(appletClassName, response);
} catch (final FailingHttpStatusCodeException e) {
// that is what the browser does, the applet only fails, if
// the main class is not loadable
LOG.error(e.getMessage(), e);
}
}
try {
final Class<Applet> appletClass = (Class<Applet>) appletClassLoader.loadClass(appletClassName);
applet_ = appletClass.newInstance();
applet_.setStub(new AppletStubImpl(getHtmlPageOrNull(), params, UrlUtils.toUrlUnsafe(baseUrl), UrlUtils.toUrlUnsafe(documentUrl)));
applet_.init();
applet_.start();
} catch (final ClassNotFoundException | InstantiationException | IllegalAccessException e) {
if (LOG.isErrorEnabled()) {
LOG.error("Loading applet '" + appletClassName + "' failed\n" + " " + e + "\n Classpath:\n" + appletClassLoader.info());
}
throw new RuntimeException(e);
}
}
}
use of com.gargoylesoftware.htmlunit.AppletConfirmHandler in project htmlunit by HtmlUnit.
the class HtmlAppletTest method appletConfirmHandlerPermit.
/**
* @throws Exception if the test fails
*/
@Test
public void appletConfirmHandlerPermit() throws Exception {
if (areAppletsNotSupported()) {
return;
}
final URL url = getClass().getResource("/applets/simpleAppletDoIt.html");
final WebClient webClient = getWebClient();
final List<String> collectedStatus = new ArrayList<>();
final StatusHandler statusHandler = new StatusHandler() {
@Override
public void statusMessageChanged(final Page page, final String message) {
collectedStatus.add(message);
}
};
webClient.setStatusHandler(statusHandler);
webClient.getOptions().setAppletEnabled(true);
webClient.setAppletConfirmHandler(new AppletConfirmHandler() {
@Override
public boolean confirm(final HtmlApplet applet) {
assertEquals("simpleAppletDoIt.jar", applet.getArchiveAttribute());
return false;
}
@Override
public boolean confirm(final HtmlObject applet) {
return false;
}
});
final HtmlPage page = webClient.getPage(url);
final DomNodeList<DomElement> applets = page.getElementsByTagName("applet");
assertEquals(1, applets.size());
final HtmlApplet htmlApplet = (HtmlApplet) applets.get(0);
assertTrue(htmlApplet.getApplet() == null);
}
Aggregations