com.gargoylesoftware.htmlunit.javascript.host.Location Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vaadin-client-compiler-deps Show documentation
Show all versions of vaadin-client-compiler-deps Show documentation
Vaadin is a web application framework for Rich Internet Applications (RIA).
Vaadin enables easy development and maintenance of fast and
secure rich web
applications with a stunning look and feel and a wide browser support.
It features a server-side architecture with the majority of the logic
running
on the server. Ajax technology is used at the browser-side to ensure a
rich
and interactive user experience.
/*
* Copyright (c) 2002-2011 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gargoylesoftware.htmlunit.javascript.host;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.gargoylesoftware.htmlunit.BrowserVersionFeatures;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebWindow;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable;
import com.gargoylesoftware.htmlunit.protocol.javascript.JavaScriptURLConnection;
import com.gargoylesoftware.htmlunit.util.UrlUtils;
/**
* A JavaScript object for a Location.
*
* @version $Revision: 6470 $
* @author Mike Bowler
* @author Michael Ottati
* @author Marc Guillemot
* @author Chris Erskine
* @author Daniel Gredler
* @author David K. Taylor
* @author Ahmed Ashour
* @author Ronald Brill
*
* @see MSDN Documentation
*/
public class Location extends SimpleScriptable {
private static final Pattern DECODE_HASH_PATTERN = Pattern.compile("%([\\dA-F]{2})");
private static final Log LOG = LogFactory.getLog(Location.class);
private static final String UNKNOWN = "null";
/**
* The window which owns this location object.
*/
private Window window_;
/**
* The current hash; we cache it locally because we don't want to modify the page's URL and
* force a page reload each time this changes.
*/
private String hash_;
/**
* Creates an instance. JavaScript objects must have a default constructor.
*/
public Location() {
// Empty.
}
/**
* Initializes the object.
*
* @param window the window that this location belongs to
*/
public void initialize(final Window window) {
window_ = window;
if (window_ != null && window_.getWebWindow().getEnclosedPage() != null) {
jsxSet_hash(window_.getWebWindow().getEnclosedPage().getWebResponse().getWebRequest().getUrl().getRef());
}
}
/**
* {@inheritDoc}
*/
@Override
public Object getDefaultValue(final Class< ? > hint) {
if (hint == null || String.class.equals(hint)) {
return jsxGet_href();
}
return super.getDefaultValue(hint);
}
/**
* Returns the string value of the location, which is the full URL string.
* @return the full URL string
*/
@Override
public String toString() {
if (window_ != null) {
return jsxGet_href();
}
return "[Uninitialized]";
}
/**
* Loads the new HTML document corresponding to the specified URL.
* @param url the location of the new HTML document to load
* @throws IOException if loading the specified location fails
* @see MSDN Documentation
*/
public void jsxFunction_assign(final String url) throws IOException {
jsxSet_href(url);
}
/**
* Reloads the current page, possibly forcing retrieval from the server even if
* the browser cache contains the latest version of the document.
* @param force if true, force reload from server; otherwise, may reload from cache
* @throws IOException if there is a problem reloading the page
* @see MSDN Documentation
*/
public void jsxFunction_reload(final boolean force) throws IOException {
final String url = jsxGet_href();
if (UNKNOWN.equals(url)) {
LOG.error("Unable to reload location: current URL is unknown.");
}
else {
jsxSet_href(url);
}
}
/**
* Reloads the window using the specified URL via a postponed action.
* @param url the new URL to use to reload the window
* @throws IOException if loading the specified location fails
* @see MSDN Documentation
*/
public void jsxFunction_replace(final String url) throws IOException {
window_.getWebWindow().getHistory().removeCurrent();
jsxSet_href(url);
}
/**
* Returns the location URL.
* @return the location URL
*/
public String jsxFunction_toString() {
return jsxGet_href();
}
/**
* Returns the location URL.
* @return the location URL
* @see MSDN Documentation
*/
public String jsxGet_href() {
final Page page = window_.getWebWindow().getEnclosedPage();
if (page == null) {
return UNKNOWN;
}
try {
URL url = page.getWebResponse().getWebRequest().getUrl();
final boolean encodeHash = getBrowserVersion().
hasFeature(BrowserVersionFeatures.JS_LOCATION_HASH_IS_DECODED);
final String hash = getHash(encodeHash);
if (hash != null) {
url = UrlUtils.getUrlWithNewRef(url, hash);
}
String s = url.toExternalForm();
if (s.startsWith("file:/") && !s.startsWith("file:///")) {
// Java (sometimes?) returns file URLs with a single slash; however, browsers return
// three slashes. See http://www.cyanwerks.com/file-url-formats.html for more info.
s = "file:///" + s.substring("file:/".length());
}
return s;
}
catch (final MalformedURLException e) {
LOG.error(e.getMessage(), e);
return page.getWebResponse().getWebRequest().getUrl().toExternalForm();
}
}
/**
* Sets the location URL to an entirely new value.
* @param newLocation the new location URL
* @throws IOException if loading the specified location fails
* @see MSDN Documentation
*/
public void jsxSet_href(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.executeJavaScriptIfPossible(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(BrowserVersionFeatures.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 WebWindow webWindow = getWindow().getWebWindow();
webWindow.getWebClient().download(webWindow, "", new WebRequest(url),
newLocation.endsWith("#"), "JS set location");
}
catch (final MalformedURLException e) {
LOG.error("jsxSet_location('" + newLocation + "') Got MalformedURLException", e);
throw e;
}
}
/**
* Returns the search portion of the location URL (the portion following the '?').
* @return the search portion of the location URL
* @see MSDN Documentation
*/
public String jsxGet_search() {
final String search = getUrl().getQuery();
if (search == null) {
return "";
}
return "?" + search;
}
/**
* Sets the search portion of the location URL (the portion following the '?').
* @param search the new search portion of the location URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public void jsxSet_search(final String search) throws Exception {
setUrl(UrlUtils.getUrlWithNewQuery(getUrl(), search));
}
/**
* Returns the hash portion of the location URL (the portion following the '#').
* @return the hash portion of the location URL
* @see MSDN Documentation
*/
public String jsxGet_hash() {
final boolean decodeHash = getBrowserVersion().hasFeature(BrowserVersionFeatures.JS_LOCATION_HASH_IS_DECODED);
String hash = hash_;
if (decodeHash && hash_ != null) {
hash = decodeHash(hash);
}
if (!StringUtils.isEmpty(hash)) {
return "#" + hash;
}
return "";
}
private String getHash(final boolean encoded) {
if (hash_ == null || hash_.length() == 0) {
return null;
}
if (encoded) {
return UrlUtils.encodeAnchor(hash_);
}
return hash_;
}
/**
* Sets the hash portion of the location URL (the portion following the '#').
*
* @param hash the new hash portion of the location URL
* @see MSDN Documentation
*/
public void jsxSet_hash(String hash) {
// IMPORTANT: This method must not call setUrl(), because
// we must not hit the server just to change the hash!
if (hash != null) {
if (hash.length() > 0 && ('#' == hash.charAt(0))) {
hash = hash.substring(1);
}
}
hash_ = hash;
}
private String decodeHash(final String hash) {
if (hash.indexOf('%') == -1) {
return hash;
}
final StringBuffer sb = new StringBuffer();
final Matcher m = DECODE_HASH_PATTERN.matcher(hash);
while (m.find()) {
final String code = m.group(1);
final int u = (char) Character.digit(code.charAt(0), 16);
final int l = (char) Character.digit(code.charAt(1), 16);
final char replacement = (char) ((u << 4) + l);
m.appendReplacement(sb, "");
sb.append(replacement);
}
m.appendTail(sb);
return sb.toString();
}
/**
* Returns the hostname portion of the location URL.
* @return the hostname portion of the location URL
* @see MSDN Documentation
*/
public String jsxGet_hostname() {
return getUrl().getHost();
}
/**
* Sets the hostname portion of the location URL.
* @param hostname the new hostname portion of the location URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public void jsxSet_hostname(final String hostname) throws Exception {
setUrl(UrlUtils.getUrlWithNewHost(getUrl(), hostname));
}
/**
* Returns the host portion of the location URL (the '[hostname]:[port]' portion).
* @return the host portion of the location URL
* @see MSDN Documentation
*/
public String jsxGet_host() {
final URL url = getUrl();
final int port = url.getPort();
final String host = url.getHost();
if (port == -1) {
return host;
}
return host + ":" + port;
}
/**
* Sets the host portion of the location URL (the '[hostname]:[port]' portion).
* @param host the new host portion of the location URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public void jsxSet_host(final String host) throws Exception {
final String hostname;
final int port;
final int index = host.indexOf(':');
if (index != -1) {
hostname = host.substring(0, index);
port = Integer.parseInt(host.substring(index + 1));
}
else {
hostname = host;
port = -1;
}
final URL url1 = UrlUtils.getUrlWithNewHost(getUrl(), hostname);
final URL url2 = UrlUtils.getUrlWithNewPort(url1, port);
setUrl(url2);
}
/**
* Returns the pathname portion of the location URL.
* @return the pathname portion of the location URL
* @see MSDN Documentation
*/
public String jsxGet_pathname() {
if (WebClient.URL_ABOUT_BLANK == getUrl()) {
if (getBrowserVersion().hasFeature(BrowserVersionFeatures.URL_ABOUT_BLANK_HAS_EMPTY_PATH)) {
return "";
}
return "/blank";
}
return getUrl().getPath();
}
/**
* Sets the pathname portion of the location URL.
* @param pathname the new pathname portion of the location URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public void jsxSet_pathname(final String pathname) throws Exception {
setUrl(UrlUtils.getUrlWithNewPath(getUrl(), pathname));
}
/**
* Returns the port portion of the location URL.
* @return the port portion of the location URL
* @see MSDN Documentation
*/
public String jsxGet_port() {
final int port = getUrl().getPort();
if (port == -1) {
return "";
}
return Integer.toString(port);
}
/**
* Sets the port portion of the location URL.
* @param port the new port portion of the location URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public void jsxSet_port(final String port) throws Exception {
setUrl(UrlUtils.getUrlWithNewPort(getUrl(), Integer.parseInt(port)));
}
/**
* Returns the protocol portion of the location URL, including the trailing ':'.
* @return the protocol portion of the location URL, including the trailing ':'
* @see MSDN Documentation
*/
public String jsxGet_protocol() {
return getUrl().getProtocol() + ":";
}
/**
* Sets the protocol portion of the location URL.
* @param protocol the new protocol portion of the location URL
* @throws Exception if an error occurs
* @see MSDN Documentation
*/
public void jsxSet_protocol(final String protocol) throws Exception {
setUrl(UrlUtils.getUrlWithNewProtocol(getUrl(), protocol));
}
/**
* Returns this location's current URL.
* @return this location's current URL
*/
private URL getUrl() {
return window_.getWebWindow().getEnclosedPage().getWebResponse().getWebRequest().getUrl();
}
/**
* Sets this location's URL, triggering a server hit and loading the resultant document
* into this location's window.
* @param url This location's new URL
* @throws IOException if there is a problem loading the new location
*/
private void setUrl(final URL url) throws IOException {
window_.getWebWindow().getWebClient().getPage(window_.getWebWindow(), new WebRequest(url));
}
}