Java Code Examples for com.gargoylesoftware.htmlunit.WebClient#URL_ABOUT_BLANK

The following examples show how to use com.gargoylesoftware.htmlunit.WebClient#URL_ABOUT_BLANK . 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: Window.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private URL makeUrlForOpenWindow(final String urlString) {
    if (urlString.isEmpty()) {
        return WebClient.URL_ABOUT_BLANK;
    }

    try {
        final Page page = getWebWindow().getEnclosedPage();
        if (page != null && page.isHtmlPage()) {
            return ((HtmlPage) page).getFullyQualifiedUrl(urlString);
        }
        return new URL(urlString);
    }
    catch (final MalformedURLException e) {
        if (LOG.isWarnEnabled()) {
            LOG.error("Unable to create URL for openWindow: relativeUrl=[" + urlString + "]", e);
        }
        return null;
    }
}
 
Example 2
Source File: Document.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the domain name of the server that served the document, or {@code null} if the server
 * cannot be identified by a domain name.
 * @return the domain name of the server that served the document
 * @see <a href="http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-2250147">
 * W3C documentation</a>
 */
@JsxGetter({CHROME, IE})
public String getDomain() {
    if (domain_ == null && getPage().getWebResponse() != null) {
        URL url = getPage().getUrl();
        if (url == WebClient.URL_ABOUT_BLANK) {
            final WebWindow w = getWindow().getWebWindow();
            if (w instanceof FrameWindow) {
                url = ((FrameWindow) w).getEnclosingPage().getUrl();
            }
            else {
                return null;
            }
        }
        domain_ = url.getHost().toLowerCase(Locale.ROOT);
    }

    return domain_;
}
 
Example 3
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
private URL makeUrlForOpenWindow(final String urlString) {
    if (urlString.isEmpty()) {
        return WebClient.URL_ABOUT_BLANK;
    }

    try {
        final Page page = getWebWindow().getEnclosedPage();
        if (page != null && page.isHtmlPage()) {
            return ((HtmlPage) page).getFullyQualifiedUrl(urlString);
        }
        return new URL(urlString);
    }
    catch (final MalformedURLException e) {
        LOG.error("Unable to create URL for openWindow: relativeUrl=[" + urlString + "]", e);
        return null;
    }
}
 
Example 4
Source File: Document.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the domain name of the server that served the document, or {@code null} if the server
 * cannot be identified by a domain name.
 * @return the domain name of the server that served the document
 * @see <a href="http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-2250147">
 * W3C documentation</a>
 */
@JsxGetter({CHROME, IE})
public String getDomain() {
    if (domain_ == null && getPage().getWebResponse() != null) {
        URL url = getPage().getUrl();
        if (url == WebClient.URL_ABOUT_BLANK) {
            final WebWindow w = getWindow().getWebWindow();
            if (w instanceof FrameWindow) {
                url = ((FrameWindow) w).getEnclosingPage().getUrl();
            }
            else {
                return null;
            }
        }
        domain_ = url.getHost().toLowerCase(Locale.ROOT);
    }

    return domain_;
}
 
Example 5
Source File: URLCreator.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
@Override
URL toUrlUnsafeClassic(final String url) throws MalformedURLException {
    final String protocol = StringUtils.substringBefore(url, ":")
            .toLowerCase(Locale.ROOT);

    if (protocol.isEmpty() || UrlUtils.isNormalUrlProtocol(protocol)) {
        return toNormalUrl(url);
    }
    else if (JavaScriptURLConnection.JAVASCRIPT_PREFIX.equals(protocol + ":")) {
        return new URL(null, url, JS_HANDLER);
    }
    else if ("about".equals(protocol)) {
        if (WebClient.URL_ABOUT_BLANK != null
                && StringUtils.equalsIgnoreCase(WebClient.URL_ABOUT_BLANK.toExternalForm(), url)) {
            return WebClient.URL_ABOUT_BLANK;
        }
        return new URL(null, url, ABOUT_HANDLER);
    }
    else if ("data".equals(protocol)) {
        return new URL(null, url, DATA_HANDLER);
    }
    else {
        return new URL(null, url, AnyHandler.INSTANCE);
    }
}
 
Example 6
Source File: URLCreator.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
@Override
URL toUrlUnsafeClassic(final String url) throws MalformedURLException {
    if (WebClient.URL_ABOUT_BLANK != null
            && StringUtils.equalsIgnoreCase(WebClient.URL_ABOUT_BLANK.toExternalForm(), url)) {
        return WebClient.URL_ABOUT_BLANK;
    }
    else if (StringUtils.startsWithIgnoreCase(url, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
        return new URL(PREFIX + url.replaceFirst(":", "/"));
    }
    else if (StringUtils.startsWithIgnoreCase(url, WebClient.ABOUT_SCHEME)) {
        return new URL(PREFIX + url.replaceFirst(":", "/"));
    }
    else if (StringUtils.startsWithIgnoreCase(url, "data:")) {
        return new URL(PREFIX + url.replaceFirst(":", "/"));
    }
    else {
        return toNormalUrl(url);
    }
}
 
Example 7
Source File: DOMImplementation.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an {@link HTMLDocument}.
 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createHTMLDocument">
 *   createHTMLDocument (MDN)</a>
 *
 * @param titleObj the document title
 * @return the newly created {@link HTMLDocument}
 */
@JsxFunction
public HTMLDocument createHTMLDocument(final Object titleObj) {
    if (Undefined.isUndefined(titleObj)
            && getBrowserVersion().hasFeature(JS_DOMIMPLEMENTATION_CREATE_HTMLDOCOMENT_REQUIRES_TITLE)) {
        throw Context.reportRuntimeError("Title is required");
    }

    final HTMLDocument document = new HTMLDocument();
    document.setParentScope(getParentScope());
    document.setPrototype(getPrototype(document.getClass()));

    // According to spec and behavior of function in browsers new document
    // has no location object and is not connected with any window
    final WebResponse resp = new StringWebResponse("", WebClient.URL_ABOUT_BLANK);
    final HtmlPage page = new HtmlPage(resp, getWindow().getWebWindow());
    page.setEnclosingWindow(null);
    document.setDomNode(page);

    final HTMLHtmlElement html = (HTMLHtmlElement) document.createElement("html");
    page.appendChild(html.getDomNodeOrDie());

    final HTMLHeadElement head = (HTMLHeadElement) document.createElement("head");
    html.appendChild(head);

    if (!Undefined.isUndefined(titleObj)) {
        final HTMLTitleElement title = (HTMLTitleElement) document.createElement("title");
        head.appendChild(title);
        title.setTextContent(Context.toString(titleObj));
    }

    final HTMLBodyElement body = (HTMLBodyElement) document.createElement("body");
    html.appendChild(body);

    return document;
}
 
Example 8
Source File: Document.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the domain of this document.
 *
 * Domains can only be set to suffixes of the existing domain
 * with the exception of setting the domain to itself.
 * <p>
 * The domain will be set according to the following rules:
 * <ol>
 * <li>If the newDomain.equalsIgnoreCase(currentDomain) the method returns with no error.</li>
 * <li>If the browser version is netscape, the newDomain is downshifted.</li>
 * <li>The change will take place if and only if the suffixes of the
 *       current domain and the new domain match AND there are at least
 *       two domain qualifiers e.g. the following transformations are legal
 *       d1.d2.d3.gargoylesoftware.com may be transformed to itself or:
 *          d2.d3.gargoylesoftware.com
 *             d3.gargoylesoftware.com
 *                gargoylesoftware.com
 *
 *        transformation to:        com
 *        will fail
 * </li>
 * </ol>
 * <p>
 * TODO This code could be modified to understand country domain suffixes.
 * The domain www.bbc.co.uk should be trimmable only down to bbc.co.uk
 * trimming to co.uk should not be possible.
 * @param newDomain the new domain to set
 */
@JsxSetter({CHROME, IE})
public void setDomain(String newDomain) {
    final BrowserVersion browserVersion = getBrowserVersion();

    // IE (at least 6) doesn't allow to set domain of about:blank
    if (WebClient.URL_ABOUT_BLANK == getPage().getUrl()
        && browserVersion.hasFeature(JS_DOCUMENT_SETTING_DOMAIN_THROWS_FOR_ABOUT_BLANK)) {
        throw Context.reportRuntimeError("Illegal domain value, cannot set domain from \""
                + WebClient.URL_ABOUT_BLANK + "\" to: \""
                + newDomain + "\".");
    }

    newDomain = newDomain.toLowerCase(Locale.ROOT);

    final String currentDomain = getDomain();
    if (currentDomain.equalsIgnoreCase(newDomain)) {
        return;
    }

    if (newDomain.indexOf('.') == -1) {
        throw Context.reportRuntimeError("Illegal domain value, cannot set domain from: \""
                + currentDomain + "\" to: \"" + newDomain + "\" (new domain has to contain a dot).");
    }

    if (currentDomain.indexOf('.') > -1
            && !currentDomain.toLowerCase(Locale.ROOT).endsWith("." + newDomain.toLowerCase(Locale.ROOT))) {
        throw Context.reportRuntimeError("Illegal domain value, cannot set domain from: \""
                + currentDomain + "\" to: \"" + newDomain + "\"");
    }

    domain_ = newDomain;
}
 
Example 9
Source File: Location.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the pathname portion of the location URL.
 * @return the pathname portion of the location URL
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms534332.aspx">MSDN Documentation</a>
 */
@JsxGetter
public String getPathname() {
    if (WebClient.URL_ABOUT_BLANK == getUrl()) {
        if (getBrowserVersion().hasFeature(URL_ABOUT_BLANK_HAS_BLANK_PATH)) {
            return "blank";
        }
        return "/blank";
    }
    return getUrl().getPath();
}
 
Example 10
Source File: UrlUtils.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Constructs a URL instance based on the specified URL string, taking into account the fact that the
 * specified URL string may represent an <tt>"about:..."</tt> URL, a <tt>"javascript:..."</tt> URL, or
 * a <tt>data:...</tt> URL.</p>
 *
 * <p>Unlike {@link #toUrlSafe(String)}, the caller need not be sure that URL strings passed to this
 * method will parse correctly as URLs.</p>
 *
 * @param url the URL string to convert into a URL instance
 * @return the constructed URL instance
 * @throws MalformedURLException if the URL string cannot be converted to a URL instance
 */
public static URL toUrlUnsafe(final String url) throws MalformedURLException {
    WebAssert.notNull("url", url);

    final String protocol = org.apache.commons.lang3.StringUtils.substringBefore(url, ":").toLowerCase(Locale.ROOT);

    if (protocol.isEmpty() || UrlUtils.isNormalUrlProtocol(protocol)) {
        final URL response = new URL(url);
        if (response.getProtocol().startsWith("http")
                && org.apache.commons.lang3.StringUtils.isEmpty(response.getHost())) {
            throw new MalformedURLException("Missing host name in url: " + url);
        }
        return response;
    }

    if (JavaScriptURLConnection.JAVASCRIPT_PREFIX.equals(protocol + ":")) {
        return new URL(null, url, JS_HANDLER);
    }

    if ("about".equals(protocol)) {
        if (WebClient.URL_ABOUT_BLANK != null
                && org.apache.commons.lang3.StringUtils.
                    equalsIgnoreCase(WebClient.URL_ABOUT_BLANK.toExternalForm(), url)) {
            return WebClient.URL_ABOUT_BLANK;
        }
        return new URL(null, url, ABOUT_HANDLER);
    }

    if ("data".equals(protocol)) {
        return new URL(null, url, DATA_HANDLER);
    }

    return new URL(null, url, AnyHandler.INSTANCE);
}
 
Example 11
Source File: HtmlPage.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Loads the content of the contained frames. This is done after the page is completely loaded, to allow script
 * contained in the frames to reference elements from the page located after the closing &lt;/frame&gt; tag.
 * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
 *         {@link WebClient#setThrowExceptionOnFailingStatusCode(boolean)} is set to {@code true}
 */
void loadFrames() throws FailingHttpStatusCodeException {
    for (final FrameWindow w : getFrames()) {
        final BaseFrameElement frame = w.getFrameElement();
        // test if the frame should really be loaded:
        // if a script has already changed its content, it should be skipped
        // use == and not equals(...) to identify initial content (versus URL set to "about:blank")
        if (frame.getEnclosedWindow() != null
                && WebClient.URL_ABOUT_BLANK == frame.getEnclosedPage().getUrl()
                && !frame.isContentLoaded()) {
            frame.loadInnerPage();
        }
    }
}
 
Example 12
Source File: DOMImplementation.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an {@link HTMLDocument}.
 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createHTMLDocument">
 *   createHTMLDocument (MDN)</a>
 *
 * @param titleObj the document title
 * @return the newly created {@link HTMLDocument}
 */
@JsxFunction
public HTMLDocument createHTMLDocument(final Object titleObj) {
    if (titleObj == Undefined.instance
            && getBrowserVersion().hasFeature(JS_DOMIMPLEMENTATION_CREATE_HTMLDOCOMENT_REQUIRES_TITLE)) {
        throw Context.reportRuntimeError("Title is required");
    }

    final HTMLDocument document = new HTMLDocument();
    document.setParentScope(getParentScope());
    document.setPrototype(getPrototype(document.getClass()));

    // According to spec and behavior of function in browsers new document
    // has no location object and is not connected with any window
    final WebResponse resp = new StringWebResponse("", WebClient.URL_ABOUT_BLANK);
    final HtmlPage page = new HtmlPage(resp, getWindow().getWebWindow());
    page.setEnclosingWindow(null);
    document.setDomNode(page);

    final HTMLHtmlElement html = (HTMLHtmlElement) document.createElement("html");
    page.appendChild(html.getDomNodeOrDie());

    final HTMLHeadElement head = (HTMLHeadElement) document.createElement("head");
    html.appendChild(head);

    if (titleObj != Undefined.instance) {
        final HTMLTitleElement title = (HTMLTitleElement) document.createElement("title");
        head.appendChild(title);
        title.setTextContent(Context.toString(titleObj));
    }

    final HTMLBodyElement body = (HTMLBodyElement) document.createElement("body");
    html.appendChild(body);

    return document;
}
 
Example 13
Source File: Document.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the domain of this document.
 *
 * Domains can only be set to suffixes of the existing domain
 * with the exception of setting the domain to itself.
 * <p>
 * The domain will be set according to the following rules:
 * <ol>
 * <li>If the newDomain.equalsIgnoreCase(currentDomain) the method returns with no error.</li>
 * <li>If the browser version is netscape, the newDomain is downshifted.</li>
 * <li>The change will take place if and only if the suffixes of the
 *       current domain and the new domain match AND there are at least
 *       two domain qualifiers e.g. the following transformations are legal
 *       d1.d2.d3.gargoylesoftware.com may be transformed to itself or:
 *          d2.d3.gargoylesoftware.com
 *             d3.gargoylesoftware.com
 *                gargoylesoftware.com
 *
 *        transformation to:        com
 *        will fail
 * </li>
 * </ol>
 * <p>
 * TODO This code could be modified to understand country domain suffixes.
 * The domain www.bbc.co.uk should be trimmable only down to bbc.co.uk
 * trimming to co.uk should not be possible.
 * @param newDomain the new domain to set
 */
@JsxSetter({CHROME, IE})
public void setDomain(String newDomain) {
    final BrowserVersion browserVersion = getBrowserVersion();

    // IE (at least 6) doesn't allow to set domain of about:blank
    if (WebClient.URL_ABOUT_BLANK == getPage().getUrl()
        && browserVersion.hasFeature(JS_DOCUMENT_SETTING_DOMAIN_THROWS_FOR_ABOUT_BLANK)) {
        throw Context.reportRuntimeError("Illegal domain value, cannot set domain from \""
                + WebClient.URL_ABOUT_BLANK + "\" to: \""
                + newDomain + "\".");
    }

    newDomain = newDomain.toLowerCase(Locale.ROOT);

    final String currentDomain = getDomain();
    if (currentDomain.equalsIgnoreCase(newDomain)) {
        return;
    }

    if (newDomain.indexOf('.') == -1) {
        throw Context.reportRuntimeError("Illegal domain value, cannot set domain from: \""
                + currentDomain + "\" to: \"" + newDomain + "\" (new domain has to contain a dot).");
    }

    if (currentDomain.indexOf('.') > -1
            && !currentDomain.toLowerCase(Locale.ROOT).endsWith("." + newDomain.toLowerCase(Locale.ROOT))) {
        throw Context.reportRuntimeError("Illegal domain value, cannot set domain from: \""
                + currentDomain + "\" to: \"" + newDomain + "\"");
    }

    domain_ = newDomain;
}
 
Example 14
Source File: Location.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the pathname portion of the location URL.
 * @return the pathname portion of the location URL
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms534332.aspx">MSDN Documentation</a>
 */
@JsxGetter
public String getPathname() {
    if (WebClient.URL_ABOUT_BLANK == getUrl()) {
        if (getBrowserVersion().hasFeature(URL_ABOUT_BLANK_HAS_EMPTY_PATH)) {
            return "";
        }
        if (getBrowserVersion().hasFeature(URL_ABOUT_BLANK_HAS_BLANK_PATH)) {
            return "blank";
        }
        return "/blank";
    }
    return getUrl().getPath();
}
 
Example 15
Source File: HtmlPage.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Loads the content of the contained frames. This is done after the page is completely loaded, to allow script
 * contained in the frames to reference elements from the page located after the closing &lt;/frame&gt; tag.
 * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
 *         {@link WebClient#setThrowExceptionOnFailingStatusCode(boolean)} is set to {@code true}
 */
void loadFrames() throws FailingHttpStatusCodeException {
    for (final FrameWindow w : getFrames()) {
        final BaseFrameElement frame = w.getFrameElement();
        // test if the frame should really be loaded:
        // if a script has already changed its content, it should be skipped
        // use == and not equals(...) to identify initial content (versus URL set to "about:blank")
        if (frame.getEnclosedWindow() != null
                && WebClient.URL_ABOUT_BLANK == frame.getEnclosedPage().getUrl()
                && !frame.isContentLoaded()) {
            frame.loadInnerPage();
        }
    }
}
 
Example 16
Source File: HtmlForm.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Gets the request for a submission of this form with the specified SubmittableElement.
 * @param submitElement the element that caused the submit to occur
 * @return the request
 */
public WebRequest getWebRequest(final SubmittableElement submitElement) {
    final HtmlPage htmlPage = (HtmlPage) getPage();
    final List<NameValuePair> parameters = getParameterListForSubmit(submitElement);
    final HttpMethod method;
    final String methodAttribute = getMethodAttribute();
    if ("post".equalsIgnoreCase(methodAttribute)) {
        method = HttpMethod.POST;
    }
    else {
        if (!"get".equalsIgnoreCase(methodAttribute) && StringUtils.isNotBlank(methodAttribute)) {
            notifyIncorrectness("Incorrect submit method >" + getMethodAttribute() + "<. Using >GET<.");
        }
        method = HttpMethod.GET;
    }

    final BrowserVersion browser = getPage().getWebClient().getBrowserVersion();
    String actionUrl = getActionAttribute();
    String anchor = null;
    String queryFromFields = "";
    if (HttpMethod.GET == method) {
        if (actionUrl.contains("#")) {
            anchor = StringUtils.substringAfter(actionUrl, "#");
        }
        final Charset enc = getPage().getCharset();
        queryFromFields =
            URLEncodedUtils.format(Arrays.asList(NameValuePair.toHttpClient(parameters)), enc);

        // action may already contain some query parameters: they have to be removed
        actionUrl = StringUtils.substringBefore(actionUrl, "#");
        actionUrl = StringUtils.substringBefore(actionUrl, "?");
        parameters.clear(); // parameters have been added to query
    }
    URL url;
    try {
        if (actionUrl.isEmpty()) {
            url = WebClient.expandUrl(htmlPage.getUrl(), actionUrl);
        }
        else {
            url = htmlPage.getFullyQualifiedUrl(actionUrl);
        }

        if (!queryFromFields.isEmpty()) {
            url = UrlUtils.getUrlWithNewQuery(url, queryFromFields);
        }

        if (HttpMethod.GET == method && browser.hasFeature(FORM_SUBMISSION_URL_WITHOUT_HASH)
                && WebClient.URL_ABOUT_BLANK != url) {
            url = UrlUtils.getUrlWithNewRef(url, null);
        }
        else if (HttpMethod.POST == method
                && browser.hasFeature(FORM_SUBMISSION_URL_WITHOUT_HASH)
                && WebClient.URL_ABOUT_BLANK != url
                && StringUtils.isEmpty(actionUrl)) {
            url = UrlUtils.getUrlWithNewRef(url, null);
        }
        else if (anchor != null
                && WebClient.URL_ABOUT_BLANK != url) {
            url = UrlUtils.getUrlWithNewRef(url, anchor);
        }
    }
    catch (final MalformedURLException e) {
        throw new IllegalArgumentException("Not a valid url: " + actionUrl);
    }

    final WebRequest request = new WebRequest(url, method);
    request.setAdditionalHeader(HttpHeader.ACCEPT, browser.getHtmlAcceptHeader());
    request.setAdditionalHeader(HttpHeader.ACCEPT_ENCODING, "gzip, deflate");
    request.setRequestParameters(parameters);
    if (HttpMethod.POST == method) {
        request.setEncodingType(FormEncodingType.getInstance(getEnctypeAttribute()));
    }
    request.setCharset(getSubmitCharset());

    String referer = htmlPage.getUrl().toExternalForm();
    request.setAdditionalHeader(HttpHeader.REFERER, referer);

    if (HttpMethod.POST == method
            && browser.hasFeature(FORM_SUBMISSION_HEADER_ORIGIN)) {
        referer = StringUtils.stripEnd(referer, "/");
        request.setAdditionalHeader(HttpHeader.ORIGIN, referer);
    }
    if (HttpMethod.POST == method
            && browser.hasFeature(FORM_SUBMISSION_HEADER_CACHE_CONTROL_MAX_AGE)) {
        request.setAdditionalHeader(HttpHeader.CACHE_CONTROL, "max-age=0");
    }
    if (browser.hasFeature(FORM_SUBMISSION_HEADER_CACHE_CONTROL_NO_CACHE)) {
        request.setAdditionalHeader(HttpHeader.CACHE_CONTROL, "no-cache");
    }

    return request;
}