Search in sources :

Example 1 with SimpleHtmlParser

use of org.olat.core.util.SimpleHtmlParser in project OpenOLAT by OpenOLAT.

the class IFrameDeliveryMapper method injectJavaScript.

/**
 * it would be possible to access the iframe.document but there is no event
 * sended when the content changes. Like this is is easier to inject the js
 * code and resize the iframe like this.
 *
 * @param page
 * @param addCheckForInlineEvents
 *            true: check if page is rendered in iframe, if yes send event
 *            to framework; false: don't do this check
 * @return
 */
/**
 * TODO:gs make more stable by only adding some js stuff to the end of the page. First check if document.height is ready
 * when puttings js to the end or menachism like ext.onReady is needed
 */
private String injectJavaScript(String page, String mimetype, boolean addCheckForInlineEvents, boolean anchorFirefoxWorkaround) {
    // do not use parser and just check for css and script stuff myself and append just before body and head
    SimpleHtmlParser parser = new SimpleHtmlParser(page);
    if (!parser.isValidHtml()) {
        return page;
    }
    String docType = parser.getHtmlDocType();
    HtmlOutput sb = new HtmlOutput(docType, themeBaseUri, page.length() + 1000);
    if (docType != null)
        sb.append(docType).append("\n");
    if (parser.getXhtmlNamespaces() == null)
        sb.append("<html><head>");
    else {
        sb.append(parser.getXhtmlNamespaces());
        // neded to allow body onload attribute
        sb.append("<head>\n<meta http-equiv=\"Content-Script-Type\" content=\"text/javascript\"/>");
    }
    // <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    sb.append("\n<meta http-equiv=\"content-type\" content=\"").append(mimetype).append("\"");
    // close tag only when xhtml to validate
    if (docType != null && docType.indexOf("XHTML") > 0)
        sb.append("/");
    sb.append(">");
    if (openolatCss != null && openolatCss.booleanValue()) {
        sb.appendOpenolatCss();
    }
    if (!parser.hasOwnCss()) {
        if (openolatCss == null || openolatCss.booleanValue()) {
            // add olat content css as used in html editor
            // css only loaded once in HtmlOutput
            sb.appendOpenolatCss();
        }
        if (customCssDelegate != null && customCssDelegate.getCustomCSS() != null && customCssDelegate.getCustomCSS().getCSSURLIFrame() != null) {
            String customCssURL = customCssDelegate.getCustomCSS().getCSSURLIFrame();
            sb.appendCss(customCssURL, "customcss");
        } else if (customCssURL != null) {
            // add the custom  CSS, e.g. the course css that overrides the standard content css
            sb.appendCss(customCssURL, "customcss");
        }
    }
    if (enableTextmarking) {
        if (log.isDebug()) {
            log.debug("Textmarking is enabled, including tooltips js files into iframe source...");
        }
        sb.appendJQuery();
        sb.appendGlossary();
    }
    if (jQueryEnabled != null && jQueryEnabled.booleanValue()) {
        sb.appendJQuery();
    }
    if (prototypeEnabled != null && prototypeEnabled.booleanValue()) {
        sb.appendPrototype();
    }
    // Load some iframe.js helper code
    sb.append("\n<script type=\"text/javascript\">\n/* <![CDATA[ */\n");
    // Set the iframe id, used by the resize function. Important to set before iframe.js is loaded
    sb.append("b_iframeid=\"").append(frameId).append("\";");
    sb.append("b_isInlineUri=").append(Boolean.valueOf(addCheckForInlineEvents).toString()).append(";");
    sb.append("\n/* ]]> */\n</script>");
    sb.appendStaticJs("js/openolat/iframe.js");
    // resizeing is meaningless anyway.
    if (parser.getHtmlContent().length() > 0) {
        sb.append("\n<script type=\"text/javascript\">\n/* <![CDATA[ */\n");
        // register the resize code to be executed on document load and click events
        if (adjusteightAutomatically) {
            sb.append("b_addOnloadEvent(b_sizeIframe);");
            sb.append("b_addOnclickEvent(b_sizeIframe);");
        }
        // register the tooltips enabling on document load event
        sb.append("b_addOnloadEvent(b_hideExtMessageBox);");
        if (addCheckForInlineEvents) {
            // iframe and ignore all other requests (files in framesets, sub-iframes, AJAX calls etc)
            if ((System.currentTimeMillis() - this.suppressEndlessReload) > 2000)
                sb.append("b_addOnloadEvent(b_sendNewUriEventToParent);");
            this.suppressEndlessReload = System.currentTimeMillis();
        }
        sb.append("b_addOnloadEvent(b_changeLinkTargets);");
        if (enableTextmarking) {
            sb.append("b_addOnloadEvent(b_glossaryHighlight);");
        }
        if (anchorFirefoxWorkaround) {
            sb.append("b_addOnloadEvent(b_anchorFirefoxWorkaround);");
        }
        sb.append("\n/* ]]> */\n</script>");
    }
    String origHTMLHead = parser.getHtmlHead();
    // the header of the page
    if ((page.indexOf("<math") > -1 || page.indexOf("class=\"math\"") != -1 || page.indexOf("class='math'") != -1) && (origHTMLHead == null || origHTMLHead.indexOf("jsMath/easy/load.js") == -1)) {
        sb.appendJsMath();
    }
    // add some custom header things like js code or css
    if (customHeaderContent != null) {
        sb.append(customHeaderContent);
    }
    // Add HTML header stuff from original page: css, javascript, title etc.
    if (origHTMLHead != null)
        sb.append(origHTMLHead);
    sb.append("\n</head>\n");
    // use the original body tag, may include all kind of attributes (class, style, onload, on...)
    sb.append(parser.getBodyTag());
    // finally add content and finish page
    sb.append(parser.getHtmlContent());
    sb.append("</body></html>");
    return sb.toString();
}
Also used : SimpleHtmlParser(org.olat.core.util.SimpleHtmlParser)

Example 2 with SimpleHtmlParser

use of org.olat.core.util.SimpleHtmlParser in project openolat by klemens.

the class IFrameDeliveryMapper method injectJavaScript.

/**
 * it would be possible to access the iframe.document but there is no event
 * sended when the content changes. Like this is is easier to inject the js
 * code and resize the iframe like this.
 *
 * @param page
 * @param addCheckForInlineEvents
 *            true: check if page is rendered in iframe, if yes send event
 *            to framework; false: don't do this check
 * @return
 */
/**
 * TODO:gs make more stable by only adding some js stuff to the end of the page. First check if document.height is ready
 * when puttings js to the end or menachism like ext.onReady is needed
 */
private String injectJavaScript(String page, String mimetype, boolean addCheckForInlineEvents, boolean anchorFirefoxWorkaround) {
    // do not use parser and just check for css and script stuff myself and append just before body and head
    SimpleHtmlParser parser = new SimpleHtmlParser(page);
    if (!parser.isValidHtml()) {
        return page;
    }
    String docType = parser.getHtmlDocType();
    HtmlOutput sb = new HtmlOutput(docType, themeBaseUri, page.length() + 1000);
    if (docType != null)
        sb.append(docType).append("\n");
    if (parser.getXhtmlNamespaces() == null)
        sb.append("<html><head>");
    else {
        sb.append(parser.getXhtmlNamespaces());
        // neded to allow body onload attribute
        sb.append("<head>\n<meta http-equiv=\"Content-Script-Type\" content=\"text/javascript\"/>");
    }
    // <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    sb.append("\n<meta http-equiv=\"content-type\" content=\"").append(mimetype).append("\"");
    // close tag only when xhtml to validate
    if (docType != null && docType.indexOf("XHTML") > 0)
        sb.append("/");
    sb.append(">");
    if (openolatCss != null && openolatCss.booleanValue()) {
        sb.appendOpenolatCss();
    }
    if (!parser.hasOwnCss()) {
        if (openolatCss == null || openolatCss.booleanValue()) {
            // add olat content css as used in html editor
            // css only loaded once in HtmlOutput
            sb.appendOpenolatCss();
        }
        if (customCssDelegate != null && customCssDelegate.getCustomCSS() != null && customCssDelegate.getCustomCSS().getCSSURLIFrame() != null) {
            String customCssURL = customCssDelegate.getCustomCSS().getCSSURLIFrame();
            sb.appendCss(customCssURL, "customcss");
        } else if (customCssURL != null) {
            // add the custom  CSS, e.g. the course css that overrides the standard content css
            sb.appendCss(customCssURL, "customcss");
        }
    }
    if (enableTextmarking) {
        if (log.isDebug()) {
            log.debug("Textmarking is enabled, including tooltips js files into iframe source...");
        }
        sb.appendJQuery();
        sb.appendGlossary();
    }
    if (jQueryEnabled != null && jQueryEnabled.booleanValue()) {
        sb.appendJQuery();
    }
    if (prototypeEnabled != null && prototypeEnabled.booleanValue()) {
        sb.appendPrototype();
    }
    // Load some iframe.js helper code
    sb.append("\n<script type=\"text/javascript\">\n/* <![CDATA[ */\n");
    // Set the iframe id, used by the resize function. Important to set before iframe.js is loaded
    sb.append("b_iframeid=\"").append(frameId).append("\";");
    sb.append("b_isInlineUri=").append(Boolean.valueOf(addCheckForInlineEvents).toString()).append(";");
    sb.append("\n/* ]]> */\n</script>");
    sb.appendStaticJs("js/openolat/iframe.js");
    // resizeing is meaningless anyway.
    if (parser.getHtmlContent().length() > 0) {
        sb.append("\n<script type=\"text/javascript\">\n/* <![CDATA[ */\n");
        // register the resize code to be executed on document load and click events
        if (adjusteightAutomatically) {
            sb.append("b_addOnloadEvent(b_sizeIframe);");
            sb.append("b_addOnclickEvent(b_sizeIframe);");
        }
        // register the tooltips enabling on document load event
        sb.append("b_addOnloadEvent(b_hideExtMessageBox);");
        if (addCheckForInlineEvents) {
            // iframe and ignore all other requests (files in framesets, sub-iframes, AJAX calls etc)
            if ((System.currentTimeMillis() - this.suppressEndlessReload) > 2000)
                sb.append("b_addOnloadEvent(b_sendNewUriEventToParent);");
            this.suppressEndlessReload = System.currentTimeMillis();
        }
        sb.append("b_addOnloadEvent(b_changeLinkTargets);");
        if (enableTextmarking) {
            sb.append("b_addOnloadEvent(b_glossaryHighlight);");
        }
        if (anchorFirefoxWorkaround) {
            sb.append("b_addOnloadEvent(b_anchorFirefoxWorkaround);");
        }
        sb.append("\n/* ]]> */\n</script>");
    }
    String origHTMLHead = parser.getHtmlHead();
    // the header of the page
    if ((page.indexOf("<math") > -1 || page.indexOf("class=\"math\"") != -1 || page.indexOf("class='math'") != -1) && (origHTMLHead == null || origHTMLHead.indexOf("jsMath/easy/load.js") == -1)) {
        sb.appendJsMath();
    }
    // add some custom header things like js code or css
    if (customHeaderContent != null) {
        sb.append(customHeaderContent);
    }
    // Add HTML header stuff from original page: css, javascript, title etc.
    if (origHTMLHead != null)
        sb.append(origHTMLHead);
    sb.append("\n</head>\n");
    // use the original body tag, may include all kind of attributes (class, style, onload, on...)
    sb.append(parser.getBodyTag());
    // finally add content and finish page
    sb.append(parser.getHtmlContent());
    sb.append("</body></html>");
    return sb.toString();
}
Also used : SimpleHtmlParser(org.olat.core.util.SimpleHtmlParser)

Example 3 with SimpleHtmlParser

use of org.olat.core.util.SimpleHtmlParser in project openolat by klemens.

the class TunnelComponent method getAsyncMediaResource.

/**
 * @see org.olat.core.gui.media.AsyncMediaResponsible#getAsyncMediaResource(org.olat.core.gui.UserRequest)
 */
public MediaResource getAsyncMediaResource(UserRequest ureq) {
    String moduleURI = ureq.getModuleURI();
    // ../index.php instead everything works as expected
    if (moduleURI == null) {
        // after a click on some other component e.g.
        if (!firstCall)
            return null;
        // reset first call
        firstCall = false;
    }
    TURequest tureq = new TURequest(config, ureq);
    fillTURequestWithUserInfo(tureq, ureq);
    HttpResponse response = fetch(tureq, httpClientInstance);
    if (response == null) {
        setFetchError();
        return null;
    }
    Header responseHeader = response.getFirstHeader("Content-Type");
    if (responseHeader == null) {
        setFetchError();
        return null;
    }
    String mimeType = responseHeader.getValue();
    if (mimeType != null && mimeType.startsWith("text/html")) {
        // we have html content, let doDispatch handle it for
        // inline rendering, update hreq for next content request
        String body;
        try {
            body = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            log.warn("Problems when tunneling URL::" + tureq.getUri(), e);
            return null;
        }
        SimpleHtmlParser parser = new SimpleHtmlParser(body);
        if (!parser.isValidHtml()) {
            // asynchronuous
            return new HttpRequestMediaResource(response);
        }
        htmlHead = parser.getHtmlHead();
        jsOnLoad = parser.getJsOnLoad();
        htmlContent = parser.getHtmlContent();
        setDirty(true);
    } else {
        // this is a async browser
        return new HttpRequestMediaResource(response);
    }
    // refetch
    return null;
}
Also used : TURequest(org.olat.course.nodes.tu.TURequest) HttpRequestMediaResource(org.olat.core.gui.media.HttpRequestMediaResource) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) SimpleHtmlParser(org.olat.core.util.SimpleHtmlParser)

Example 4 with SimpleHtmlParser

use of org.olat.core.util.SimpleHtmlParser in project OpenOLAT by OpenOLAT.

the class TunnelComponent method getAsyncMediaResource.

/**
 * @see org.olat.core.gui.media.AsyncMediaResponsible#getAsyncMediaResource(org.olat.core.gui.UserRequest)
 */
public MediaResource getAsyncMediaResource(UserRequest ureq) {
    String moduleURI = ureq.getModuleURI();
    // ../index.php instead everything works as expected
    if (moduleURI == null) {
        // after a click on some other component e.g.
        if (!firstCall)
            return null;
        // reset first call
        firstCall = false;
    }
    TURequest tureq = new TURequest(config, ureq);
    fillTURequestWithUserInfo(tureq, ureq);
    HttpResponse response = fetch(tureq, httpClientInstance);
    if (response == null) {
        setFetchError();
        return null;
    }
    Header responseHeader = response.getFirstHeader("Content-Type");
    if (responseHeader == null) {
        setFetchError();
        return null;
    }
    String mimeType = responseHeader.getValue();
    if (mimeType != null && mimeType.startsWith("text/html")) {
        // we have html content, let doDispatch handle it for
        // inline rendering, update hreq for next content request
        String body;
        try {
            body = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            log.warn("Problems when tunneling URL::" + tureq.getUri(), e);
            return null;
        }
        SimpleHtmlParser parser = new SimpleHtmlParser(body);
        if (!parser.isValidHtml()) {
            // asynchronuous
            return new HttpRequestMediaResource(response);
        }
        htmlHead = parser.getHtmlHead();
        jsOnLoad = parser.getJsOnLoad();
        htmlContent = parser.getHtmlContent();
        setDirty(true);
    } else {
        // this is a async browser
        return new HttpRequestMediaResource(response);
    }
    // refetch
    return null;
}
Also used : TURequest(org.olat.course.nodes.tu.TURequest) HttpRequestMediaResource(org.olat.core.gui.media.HttpRequestMediaResource) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) SimpleHtmlParser(org.olat.core.util.SimpleHtmlParser)

Example 5 with SimpleHtmlParser

use of org.olat.core.util.SimpleHtmlParser in project OpenOLAT by OpenOLAT.

the class TunnelComponent method fetchFirstResource.

private void fetchFirstResource(UserRequest ureq) {
    // config, ureq);
    TURequest tureq = new TURequest();
    // not used
    tureq.setContentType(null);
    tureq.setMethod("GET");
    tureq.setParameterMap(Collections.<String, String[]>emptyMap());
    tureq.setQueryString(query);
    if (startUri != null) {
        if (startUri.startsWith("/")) {
            tureq.setUri(startUri);
        } else {
            tureq.setUri("/" + startUri);
        }
    }
    fillTURequestWithUserInfo(tureq, ureq);
    HttpResponse response = fetch(tureq, httpClientInstance);
    if (response == null) {
        setFetchError();
    } else {
        Header responseHeader = response.getFirstHeader("Content-Type");
        String mimeType;
        if (responseHeader == null) {
            setFetchError();
            mimeType = null;
        } else {
            mimeType = responseHeader.getValue();
        }
        if (mimeType != null && mimeType.startsWith("text/html")) {
            // we have html content, let doDispatch handle it for
            // inline rendering, update hreq for next content request
            String body;
            try {
                body = EntityUtils.toString(response.getEntity());
            } catch (IOException e) {
                log.warn("Problems when tunneling URL::" + tureq.getUri(), e);
                htmlContent = "Error: cannot display inline :" + tureq.getUri() + ": Unknown transfer problem '";
                return;
            }
            SimpleHtmlParser parser = new SimpleHtmlParser(body);
            if (!parser.isValidHtml()) {
            // this is not valid HTML, deliver
            // asynchronuous
            }
            htmlHead = parser.getHtmlHead();
            jsOnLoad = parser.getJsOnLoad();
            htmlContent = parser.getHtmlContent();
        } else {
            htmlContent = "Error: cannot display inline :" + tureq.getUri() + ": mime type was '" + mimeType + "' but expected 'text/html'. Response header was '" + responseHeader + "'.";
        }
    }
}
Also used : TURequest(org.olat.course.nodes.tu.TURequest) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) SimpleHtmlParser(org.olat.core.util.SimpleHtmlParser)

Aggregations

SimpleHtmlParser (org.olat.core.util.SimpleHtmlParser)10 IOException (java.io.IOException)4 Header (org.apache.http.Header)4 HttpResponse (org.apache.http.HttpResponse)4 TURequest (org.olat.course.nodes.tu.TURequest)4 InputStream (java.io.InputStream)2 HttpRequestMediaResource (org.olat.core.gui.media.HttpRequestMediaResource)2 AssertException (org.olat.core.logging.AssertException)2