Search in sources :

Example 1 with AppletStubImpl

use of com.gargoylesoftware.htmlunit.html.applets.AppletStubImpl 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);
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URL(java.net.URL) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) AppletClassLoader(com.gargoylesoftware.htmlunit.html.applets.AppletClassLoader) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) AppletStubImpl(com.gargoylesoftware.htmlunit.html.applets.AppletStubImpl) AppletConfirmHandler(com.gargoylesoftware.htmlunit.AppletConfirmHandler) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) WebClient(com.gargoylesoftware.htmlunit.WebClient) Applet(java.applet.Applet)

Example 2 with AppletStubImpl

use of com.gargoylesoftware.htmlunit.html.applets.AppletStubImpl 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);
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URL(java.net.URL) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) AppletClassLoader(com.gargoylesoftware.htmlunit.html.applets.AppletClassLoader) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) AppletStubImpl(com.gargoylesoftware.htmlunit.html.applets.AppletStubImpl) AppletConfirmHandler(com.gargoylesoftware.htmlunit.AppletConfirmHandler) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) WebClient(com.gargoylesoftware.htmlunit.WebClient) Applet(java.applet.Applet)

Aggregations

AppletConfirmHandler (com.gargoylesoftware.htmlunit.AppletConfirmHandler)2 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)2 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)2 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)2 AppletClassLoader (com.gargoylesoftware.htmlunit.html.applets.AppletClassLoader)2 AppletStubImpl (com.gargoylesoftware.htmlunit.html.applets.AppletStubImpl)2 Applet (java.applet.Applet)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2