Java Code Examples for com.gargoylesoftware.htmlunit.html.HtmlPage#getFullyQualifiedUrl()

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlPage#getFullyQualifiedUrl() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XMLDocument.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Loads an XML document from the specified location.
 *
 * @param xmlSource a string containing a URL that specifies the location of the XML file
 * @return true if the load succeeded; false if the load failed
 */
@JsxFunction({FF68, FF60})
public boolean load(final String xmlSource) {
    if (async_) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("XMLDocument.load(): 'async' is true, currently treated as false.");
        }
    }
    try {
        final WebWindow ww = getWindow().getWebWindow();
        final HtmlPage htmlPage = (HtmlPage) ww.getEnclosedPage();
        final WebRequest request = new WebRequest(htmlPage.getFullyQualifiedUrl(xmlSource));
        final WebResponse webResponse = ww.getWebClient().loadWebResponse(request);
        final XmlPage page = new XmlPage(webResponse, ww, false);
        setDomNode(page);
        return true;
    }
    catch (final IOException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Error parsing XML from '" + xmlSource + "'", e);
        }
        return false;
    }
}
 
Example 2
Source File: History.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces a state.
 * @param object the state object
 * @param title the title
 * @param url an optional URL
 */
@JsxFunction
public void replaceState(final Object object, final String title, final String url) {
    final WebWindow w = getWindow().getWebWindow();
    try {
        URL newStateUrl = null;
        final HtmlPage page = (HtmlPage) w.getEnclosedPage();
        if (StringUtils.isNotBlank(url)) {
            newStateUrl = page.getFullyQualifiedUrl(url);
        }
        w.getHistory().replaceState(object, newStateUrl);
    }
    catch (final MalformedURLException e) {
        Context.throwAsScriptRuntimeEx(e);
    }
}
 
Example 3
Source File: History.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Pushes a state.
 * @param object the state object
 * @param title the title
 * @param url an optional URL
 */
@JsxFunction
public void pushState(final Object object, final String title, final String url) {
    final WebWindow w = getWindow().getWebWindow();
    try {
        URL newStateUrl = null;
        final HtmlPage page = (HtmlPage) w.getEnclosedPage();
        if (StringUtils.isNotBlank(url)) {
            newStateUrl = page.getFullyQualifiedUrl(url);
        }
        w.getHistory().pushState(object, newStateUrl);
    }
    catch (final IOException e) {
        Context.throwAsScriptRuntimeEx(e);
    }
}
 
Example 4
Source File: XMLDocument.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Loads an XML document from the specified location.
 *
 * @param xmlSource a string containing a URL that specifies the location of the XML file
 * @return true if the load succeeded; false if the load failed
 */
@JsxFunction(FF)
public boolean load(final String xmlSource) {
    if (async_) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("XMLDocument.load(): 'async' is true, currently treated as false.");
        }
    }
    try {
        final WebWindow ww = getWindow().getWebWindow();
        final HtmlPage htmlPage = (HtmlPage) ww.getEnclosedPage();
        final WebRequest request = new WebRequest(htmlPage.getFullyQualifiedUrl(xmlSource));
        final WebResponse webResponse = ww.getWebClient().loadWebResponse(request);
        final XmlPage page = new XmlPage(webResponse, ww, false);
        setDomNode(page);
        return true;
    }
    catch (final IOException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Error parsing XML from '" + xmlSource + "'", e);
        }
        return false;
    }
}
 
Example 5
Source File: History.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces a state.
 * @param object the state object
 * @param title the title
 * @param url an optional URL
 */
@JsxFunction
public void replaceState(final Object object, final String title, final String url) {
    final WebWindow w = getWindow().getWebWindow();
    try {
        URL newStateUrl = null;
        final HtmlPage page = (HtmlPage) w.getEnclosedPage();
        if (StringUtils.isNotBlank(url)) {
            newStateUrl = page.getFullyQualifiedUrl(url);
        }
        w.getHistory().replaceState(object, newStateUrl);
    }
    catch (final MalformedURLException e) {
        Context.throwAsScriptRuntimeEx(e);
    }
}
 
Example 6
Source File: History.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Pushes a state.
 * @param object the state object
 * @param title the title
 * @param url an optional URL
 */
@JsxFunction
public void pushState(final Object object, final String title, final String url) {
    final WebWindow w = getWindow().getWebWindow();
    try {
        URL newStateUrl = null;
        final HtmlPage page = (HtmlPage) w.getEnclosedPage();
        if (StringUtils.isNotBlank(url)) {
            newStateUrl = page.getFullyQualifiedUrl(url);
        }
        w.getHistory().pushState(object, newStateUrl);
    }
    catch (final IOException e) {
        Context.throwAsScriptRuntimeEx(e);
    }
}
 
Example 7
Source File: CSSStyleSheet.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the URL of the stylesheet.
 * @return the URL of the stylesheet
 */
@JsxGetter
public String getHref() {
    final BrowserVersion version = getBrowserVersion();

    if (ownerNode_ != null) {
        final DomNode node = ownerNode_.getDomNodeOrDie();
        if (node instanceof HtmlLink) {
            // <link rel="stylesheet" type="text/css" href="..." />
            final HtmlLink link = (HtmlLink) node;
            final HtmlPage page = (HtmlPage) link.getPage();
            final String href = link.getHrefAttribute();
            if ("".equals(href) && version.hasFeature(STYLESHEET_HREF_EMPTY_IS_NULL)) {
                return null;
            }
            // Expand relative URLs.
            try {
                final URL url = page.getFullyQualifiedUrl(href);
                return url.toExternalForm();
            }
            catch (final MalformedURLException e) {
                // Log the error and fall through to the return values below.
                LOG.warn(e.getMessage(), e);
            }
        }
    }

    return null;
}
 
Example 8
Source File: HTMLElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Implementation of the IE behavior #default#download.
 * @param uri the URI of the download source
 * @param callback the method which should be called when the download is finished
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms531406.aspx">MSDN documentation</a>
 * @throws MalformedURLException if the URL cannot be created
 */
public void startDownload(final String uri, final Function callback) throws MalformedURLException {
    final WebWindow ww = getWindow().getWebWindow();
    final HtmlPage page = (HtmlPage) ww.getEnclosedPage();
    final URL url = page.getFullyQualifiedUrl(uri);
    if (!page.getUrl().getHost().equals(url.getHost())) {
        throw Context.reportRuntimeError("Not authorized url: " + url);
    }
    final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().
            createDownloadBehaviorJob(url, callback, ww.getWebClient());
    page.getEnclosingWindow().getJobManager().addJob(job, page);
}
 
Example 9
Source File: Location.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the location URL to an entirely new value.
 * @param newLocation the new location URL
 * @throws IOException if loading the specified location fails
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms533867.aspx">MSDN Documentation</a>
 */
@JsxSetter
public void setHref(final String newLocation) throws IOException {
    final HtmlPage page = (HtmlPage) getWindow(getStartingScope()).getWebWindow().getEnclosedPage();
    if (newLocation.startsWith(JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
        final String script = newLocation.substring(11);
        page.executeJavaScript(script, "new location value", 1);
        return;
    }
    try {
        URL url = page.getFullyQualifiedUrl(newLocation);
        // fix for empty url
        if (StringUtils.isEmpty(newLocation)) {
            final boolean dropFilename = page.getWebClient().getBrowserVersion().
                    hasFeature(ANCHOR_EMPTY_HREF_NO_FILENAME);
            if (dropFilename) {
                String path = url.getPath();
                path = path.substring(0, path.lastIndexOf('/') + 1);
                url = UrlUtils.getUrlWithNewPath(url, path);
                url = UrlUtils.getUrlWithNewRef(url, null);
            }
            else {
                url = UrlUtils.getUrlWithNewRef(url, null);
            }
        }

        final WebRequest request = new WebRequest(url);
        request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm());

        final WebWindow webWindow = window_.getWebWindow();
        webWindow.getWebClient().download(webWindow, "", request, true, false, "JS set location");
    }
    catch (final MalformedURLException e) {
        if (LOG.isErrorEnabled()) {
            LOG.error("setHref('" + newLocation + "') got MalformedURLException", e);
        }
        throw e;
    }
}
 
Example 10
Source File: DedicatedWorkerGlobalScope.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
void loadAndExecute(final WebClient webClient, final String url,
        final Context context, final boolean checkMimeType) throws IOException {
    final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage();
    final URL fullUrl = page.getFullyQualifiedUrl(url);

    final WebRequest webRequest = new WebRequest(fullUrl);
    final WebResponse response = webClient.loadWebResponse(webRequest);
    if (checkMimeType && !MimeType.isJavascriptMimeType(response.getContentType())) {
        throw Context.reportRuntimeError(
                "NetworkError: importScripts response is not a javascript response");
    }

    final String scriptCode = response.getContentAsString();
    final JavaScriptEngine javaScriptEngine = (JavaScriptEngine) webClient.getJavaScriptEngine();

    final DedicatedWorkerGlobalScope thisScope = this;
    final ContextAction<Object> action = new ContextAction<Object>() {
        @Override
        public Object run(final Context cx) {
            final Script script = javaScriptEngine.compile(page, thisScope, scriptCode,
                    fullUrl.toExternalForm(), 1);
            return javaScriptEngine.execute(page, thisScope, script);
        }
    };

    final ContextFactory cf = javaScriptEngine.getContextFactory();

    if (context != null) {
        action.run(context);
    }
    else {
        final JavaScriptJob job = new WorkerJob(cf, action, "loadAndExecute " + url);

        owningWindow_.getWebWindow().getJobManager().addJob(job, page);
    }
}
 
Example 11
Source File: XMLDOMDocument.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Loads an XML document from the specified location.
 * @param xmlSource a URL that specifies the location of the XML file
 * @return {@code true} if the load succeeded; {@code false} if the load failed
 */
@JsxFunction
public boolean load(final String xmlSource) {
    if (async_ && LOG.isDebugEnabled()) {
        LOG.debug("XMLDOMDocument.load(): 'async' is true, currently treated as false.");
    }
    try {
        final WebWindow ww = getWindow().getWebWindow();
        final HtmlPage htmlPage = (HtmlPage) ww.getEnclosedPage();
        final URL fullyQualifiedURL = htmlPage.getFullyQualifiedUrl(xmlSource);
        final WebRequest request = new WebRequest(fullyQualifiedURL);
        final WebResponse webResponse = ww.getWebClient().loadWebResponse(request);
        final XmlPage page = new XmlPage(webResponse, ww, false, false);
        setDomNode(page);

        preserveWhiteSpaceDuringLoad_ = preserveWhiteSpace_;
        url_ = fullyQualifiedURL.toExternalForm();
        return true;
    }
    catch (final IOException e) {
        final XMLDOMParseError parseError = getParseError();
        parseError.setErrorCode(-1);
        parseError.setFilepos(1);
        parseError.setLine(1);
        parseError.setLinepos(1);
        parseError.setReason(e.getMessage());
        parseError.setSrcText("xml");
        parseError.setUrl(xmlSource);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Error parsing XML from '" + xmlSource + "'", e);
        }
        return false;
    }
}
 
Example 12
Source File: CSSStyleSheet.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the URL of the stylesheet.
 * @return the URL of the stylesheet
 */
@JsxGetter
public String getHref() {
    final BrowserVersion version = getBrowserVersion();

    if (ownerNode_ != null) {
        final DomNode node = ownerNode_.getDomNodeOrDie();
        if (node instanceof HtmlLink) {
            // <link rel="stylesheet" type="text/css" href="..." />
            final HtmlLink link = (HtmlLink) node;
            final HtmlPage page = (HtmlPage) link.getPage();
            final String href = link.getHrefAttribute();
            if ("".equals(href) && version.hasFeature(STYLESHEET_HREF_EMPTY_IS_NULL)) {
                return null;
            }
            // Expand relative URLs.
            try {
                final URL url = page.getFullyQualifiedUrl(href);
                return url.toExternalForm();
            }
            catch (final MalformedURLException e) {
                // Log the error and fall through to the return values below.
                LOG.warn(e.getMessage(), e);
            }
        }
    }

    return null;
}
 
Example 13
Source File: HTMLElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Implementation of the IE behavior #default#download.
 * @param uri the URI of the download source
 * @param callback the method which should be called when the download is finished
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms531406.aspx">MSDN documentation</a>
 * @throws MalformedURLException if the URL cannot be created
 */
public void startDownload(final String uri, final Function callback) throws MalformedURLException {
    final WebWindow ww = getWindow().getWebWindow();
    final HtmlPage page = (HtmlPage) ww.getEnclosedPage();
    final URL url = page.getFullyQualifiedUrl(uri);
    if (!page.getUrl().getHost().equals(url.getHost())) {
        throw Context.reportRuntimeError("Not authorized url: " + url);
    }
    final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().
            createDownloadBehaviorJob(url, callback, ww.getWebClient());
    page.getEnclosingWindow().getJobManager().addJob(job, page);
}
 
Example 14
Source File: Location.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the location URL to an entirely new value.
 * @param newLocation the new location URL
 * @throws IOException if loading the specified location fails
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms533867.aspx">MSDN Documentation</a>
 */
@JsxSetter
public void setHref(final String newLocation) throws IOException {
    final HtmlPage page = (HtmlPage) getWindow(getStartingScope()).getWebWindow().getEnclosedPage();
    if (newLocation.startsWith(JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
        final String script = newLocation.substring(11);
        page.executeJavaScript(script, "new location value", 1);
        return;
    }
    try {
        URL url = page.getFullyQualifiedUrl(newLocation);
        // fix for empty url
        if (StringUtils.isEmpty(newLocation)) {
            final boolean dropFilename = page.getWebClient().getBrowserVersion().
                    hasFeature(ANCHOR_EMPTY_HREF_NO_FILENAME);
            if (dropFilename) {
                String path = url.getPath();
                path = path.substring(0, path.lastIndexOf('/') + 1);
                url = UrlUtils.getUrlWithNewPath(url, path);
                url = UrlUtils.getUrlWithNewRef(url, null);
            }
            else {
                url = UrlUtils.getUrlWithNewRef(url, null);
            }
        }

        final WebRequest request = new WebRequest(url);
        request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm());

        final WebWindow webWindow = window_.getWebWindow();
        webWindow.getWebClient().download(webWindow, "", request, true, false, "JS set location");
    }
    catch (final MalformedURLException e) {
        LOG.error("setHref('" + newLocation + "') got MalformedURLException", e);
        throw e;
    }
}
 
Example 15
Source File: DedicatedWorkerGlobalScope.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
void loadAndExecute(final String url, final Context context) throws IOException {
    final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage();
    final URL fullUrl = page.getFullyQualifiedUrl(url);

    final WebClient webClient = owningWindow_.getWebWindow().getWebClient();

    final WebRequest webRequest = new WebRequest(fullUrl);
    final WebResponse response = webClient.loadWebResponse(webRequest);
    final String scriptCode = response.getContentAsString();
    final JavaScriptEngine javaScriptEngine = (JavaScriptEngine) webClient.getJavaScriptEngine();

    final DedicatedWorkerGlobalScope thisScope = this;
    final ContextAction action = new ContextAction() {
        @Override
        public Object run(final Context cx) {
            final Script script = javaScriptEngine.compile(page, thisScope, scriptCode,
                    fullUrl.toExternalForm(), 1);
            return javaScriptEngine.execute(page, thisScope, script);
        }
    };

    final ContextFactory cf = javaScriptEngine.getContextFactory();

    if (context != null) {
        action.run(context);
    }
    else {
        final JavaScriptJob job = new WorkerJob(cf, action, "loadAndExecute " + url);

        owningWindow_.getWebWindow().getJobManager().addJob(job, page);
    }
}
 
Example 16
Source File: XMLDOMDocument.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Loads an XML document from the specified location.
 * @param xmlSource a URL that specifies the location of the XML file
 * @return {@code true} if the load succeeded; {@code false} if the load failed
 */
@JsxFunction
public boolean load(final String xmlSource) {
    if (async_ && LOG.isDebugEnabled()) {
        LOG.debug("XMLDOMDocument.load(): 'async' is true, currently treated as false.");
    }
    try {
        final WebWindow ww = getWindow().getWebWindow();
        final HtmlPage htmlPage = (HtmlPage) ww.getEnclosedPage();
        final URL fullyQualifiedURL = htmlPage.getFullyQualifiedUrl(xmlSource);
        final WebRequest request = new WebRequest(fullyQualifiedURL);
        final WebResponse webResponse = ww.getWebClient().loadWebResponse(request);
        final XmlPage page = new XmlPage(webResponse, ww, false, false);
        setDomNode(page);

        preserveWhiteSpaceDuringLoad_ = preserveWhiteSpace_;
        url_ = fullyQualifiedURL.toExternalForm();
        return true;
    }
    catch (final IOException e) {
        final XMLDOMParseError parseError = getParseError();
        parseError.setErrorCode(-1);
        parseError.setFilepos(1);
        parseError.setLine(1);
        parseError.setLinepos(1);
        parseError.setReason(e.getMessage());
        parseError.setSrcText("xml");
        parseError.setUrl(xmlSource);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Error parsing XML from '" + xmlSource + "'", e);
        }
        return false;
    }
}