diff options
author | Leif Åstrand <leif@vaadin.com> | 2012-10-08 08:11:51 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2012-10-08 10:35:02 +0000 |
commit | 13d5b3e98954c2ade382305f8d044b2b49fdbd0b (patch) | |
tree | 37559368471cbed3a9ea159261869bcf5c82e52a /client | |
parent | 6337f8f8ccb88014f7a3d269332661ec8183b758 (diff) | |
download | vaadin-framework-13d5b3e98954c2ade382305f8d044b2b49fdbd0b.tar.gz vaadin-framework-13d5b3e98954c2ade382305f8d044b2b49fdbd0b.zip |
Bootstrap UI using relative URLs with servlets (#6771)
* Configure widgetset using URLs relative to the requested page
* Provide a Util method for getting an absolute URL from a relative URL
* Test by using an embedded Jetty acting as a transparent proxy
* Make /embed1 use the Buttons test to enable testing UIDL requests
Change-Id: I4ef9b40e3954ae16b682d743a339f4360db40d4d
Diffstat (limited to 'client')
-rw-r--r-- | client/src/com/vaadin/client/ApplicationConfiguration.java | 75 | ||||
-rw-r--r-- | client/src/com/vaadin/client/ApplicationConnection.java | 24 | ||||
-rw-r--r-- | client/src/com/vaadin/client/ResourceLoader.java | 13 | ||||
-rw-r--r-- | client/src/com/vaadin/client/Util.java | 15 |
4 files changed, 79 insertions, 48 deletions
diff --git a/client/src/com/vaadin/client/ApplicationConfiguration.java b/client/src/com/vaadin/client/ApplicationConfiguration.java index f620a39a70..9d668121a7 100644 --- a/client/src/com/vaadin/client/ApplicationConfiguration.java +++ b/client/src/com/vaadin/client/ApplicationConfiguration.java @@ -189,8 +189,12 @@ public class ApplicationConfiguration implements EntryPoint { private static WidgetSet widgetSet = GWT.create(WidgetSet.class); private String id; - private String themeUri; - private String appUri; + /** + * The URL to the VAADIN directory containing themes and widgetsets. Should + * always end with a slash (/). + */ + private String vaadinDirUrl; + private String serviceUrl; private int uiId; private boolean standalone; private ErrorMessage communicationError; @@ -214,13 +218,20 @@ public class ApplicationConfiguration implements EntryPoint { private Map<Integer, Integer> componentInheritanceMap = new HashMap<Integer, Integer>(); private Map<Integer, String> tagToServerSideClassName = new HashMap<Integer, String>(); - public boolean usePortletURLs() { - return getPortletResourceUrl() != null; - } - - public String getPortletResourceUrl() { - return getJsoConfiguration(id).getConfigString( - ApplicationConstants.PORTLET_RESOUCE_URL_BASE); + /** + * Checks whether path info in requests to the server-side service should be + * in a request parameter (named + * {@value ApplicationConstants#V_RESOURCE_PATH}) or appended to the end of + * the service URL. + * + * @see #getServiceUrl() + * + * @return <code>true</code> if path info should be a request parameter; + * <code>false</code> if the path info goes after the service URL + */ + public boolean useServiceUrlPathParam() { + return getJsoConfiguration(id).getConfigBoolean( + ApplicationConstants.SERVICE_URL_PATH_AS_PARAMETER) == Boolean.TRUE; } public String getRootPanelId() { @@ -228,24 +239,28 @@ public class ApplicationConfiguration implements EntryPoint { } /** - * Gets the application base URI. Using this other than as the download - * action URI can cause problems in Portlet 2.0 deployments. + * Gets the URL to the server-side VaadinService. If + * {@link #useServiceUrlPathParam()} return <code>true</code>, the requested + * path info should be in the {@value ApplicationConstants#V_RESOURCE_PATH} + * query parameter; else the path info should be appended to the end of the + * URL. + * + * @see #useServiceUrlPathParam() * - * @return application base URI + * @return the URL to the server-side service as a string */ - public String getApplicationUri() { - return appUri; + public String getServiceUrl() { + return serviceUrl; } public String getThemeName() { - String uri = getThemeUri(); - String themeName = uri.substring(uri.lastIndexOf('/')); + String themeName = getJsoConfiguration(id).getConfigString("theme"); themeName = themeName.replaceAll("[^a-zA-Z0-9]", ""); return themeName; } public String getThemeUri() { - return themeUri; + return vaadinDirUrl + "themes/" + getThemeName(); } public void setAppId(String appId) { @@ -306,11 +321,29 @@ public class ApplicationConfiguration implements EntryPoint { */ private void loadFromDOM() { JsoConfiguration jsoConfiguration = getJsoConfiguration(id); - appUri = jsoConfiguration.getConfigString("appUri"); - if (appUri != null && !appUri.endsWith("/")) { - appUri += '/'; + serviceUrl = jsoConfiguration + .getConfigString(ApplicationConstants.SERVICE_URL); + if (serviceUrl == null || "".equals(serviceUrl)) { + /* + * Use the current url without query parameters and fragment as the + * default value. + */ + serviceUrl = Window.Location.getHref().replaceFirst("[?#].*", ""); + } else { + /* + * Resolve potentially relative URLs to ensure they point to the + * desired locations even if the base URL of the page changes later + * (e.g. with pushState) + */ + serviceUrl = Util.getAbsoluteUrl(serviceUrl); + } + // Ensure there's an ending slash (to make appending e.g. UIDL work) + if (!useServiceUrlPathParam() && !serviceUrl.endsWith("/")) { + serviceUrl += '/'; } - themeUri = jsoConfiguration.getConfigString("themeUri"); + + vaadinDirUrl = Util.getAbsoluteUrl(jsoConfiguration + .getConfigString(ApplicationConstants.VAADIN_DIR_URL)); uiId = jsoConfiguration.getConfigInteger(UIConstants.UI_ID_PARAMETER) .intValue(); diff --git a/client/src/com/vaadin/client/ApplicationConnection.java b/client/src/com/vaadin/client/ApplicationConnection.java index ab28ad291b..1e10762c28 100644 --- a/client/src/com/vaadin/client/ApplicationConnection.java +++ b/client/src/com/vaadin/client/ApplicationConnection.java @@ -528,16 +528,6 @@ public class ApplicationConnection { }-*/; /** - * Gets the application base URI. Using this other than as the download - * action URI can cause problems in Portlet 2.0 deployments. - * - * @return application base URI - */ - public String getAppUri() { - return configuration.getApplicationUri(); - }; - - /** * Indicates whether or not there are currently active UIDL requests. Used * internally to sequence requests properly, seldom needed in Widgets. * @@ -2604,29 +2594,29 @@ public class ApplicationConnection { String relativeUrl = uidlUri .substring(ApplicationConstants.APP_PROTOCOL_PREFIX .length()); - if (getConfiguration().usePortletURLs()) { + ApplicationConfiguration conf = getConfiguration(); + String serviceUrl = conf.getServiceUrl(); + if (conf.useServiceUrlPathParam()) { // Should put path in v-resourcePath parameter and append query // params to base portlet url String[] parts = relativeUrl.split("\\?", 2); String path = parts[0]; - String url = getConfiguration().getPortletResourceUrl(); - // If there's a "?" followed by something, append it as a query // string to the base URL if (parts.length > 1) { String appUrlParams = parts[1]; - url = addGetParameters(url, appUrlParams); + serviceUrl = addGetParameters(serviceUrl, appUrlParams); } if (!path.startsWith("/")) { path = '/' + path; } String pathParam = ApplicationConstants.V_RESOURCE_PATH + "=" + URL.encodeQueryString(path); - url = addGetParameters(url, pathParam); - uidlUri = url; + serviceUrl = addGetParameters(serviceUrl, pathParam); + uidlUri = serviceUrl; } else { - uidlUri = getAppUri() + relativeUrl; + uidlUri = serviceUrl + relativeUrl; } } return uidlUri; diff --git a/client/src/com/vaadin/client/ResourceLoader.java b/client/src/com/vaadin/client/ResourceLoader.java index f9925f89a4..eb718e24d0 100644 --- a/client/src/com/vaadin/client/ResourceLoader.java +++ b/client/src/com/vaadin/client/ResourceLoader.java @@ -26,7 +26,6 @@ import com.google.gwt.core.client.Duration; import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.Scheduler; import com.google.gwt.core.client.Scheduler.RepeatingCommand; -import com.google.gwt.dom.client.AnchorElement; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.LinkElement; @@ -205,7 +204,7 @@ public class ResourceLoader { */ public void loadScript(final String scriptUrl, final ResourceLoadListener resourceLoadListener) { - final String url = getAbsoluteUrl(scriptUrl); + final String url = Util.getAbsoluteUrl(scriptUrl); ResourceLoadEvent event = new ResourceLoadEvent(this, url, false); if (loadedResources.contains(url)) { if (resourceLoadListener != null) { @@ -252,12 +251,6 @@ public class ResourceLoader { } } - private static String getAbsoluteUrl(String url) { - AnchorElement a = Document.get().createAnchorElement(); - a.setHref(url); - return a.getHref(); - } - /** * Download a resource and notify a listener when the resource is loaded * without attempting to interpret the resource. When a resource has been @@ -278,7 +271,7 @@ public class ResourceLoader { */ public void preloadResource(String url, ResourceLoadListener resourceLoadListener) { - url = getAbsoluteUrl(url); + url = Util.getAbsoluteUrl(url); ResourceLoadEvent event = new ResourceLoadEvent(this, url, true); if (loadedResources.contains(url) || preloadedResources.contains(url)) { // Already loaded or preloaded -> just fire listener @@ -363,7 +356,7 @@ public class ResourceLoader { */ public void loadStylesheet(final String stylesheetUrl, final ResourceLoadListener resourceLoadListener) { - final String url = getAbsoluteUrl(stylesheetUrl); + final String url = Util.getAbsoluteUrl(stylesheetUrl); final ResourceLoadEvent event = new ResourceLoadEvent(this, url, false); if (loadedResources.contains(url)) { if (resourceLoadListener != null) { diff --git a/client/src/com/vaadin/client/Util.java b/client/src/com/vaadin/client/Util.java index 5ec5939c7b..7aea69a61d 100644 --- a/client/src/com/vaadin/client/Util.java +++ b/client/src/com/vaadin/client/Util.java @@ -24,6 +24,7 @@ import java.util.List; import com.google.gwt.core.client.Scheduler; import com.google.gwt.core.client.Scheduler.ScheduledCommand; +import com.google.gwt.dom.client.AnchorElement; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.NativeEvent; import com.google.gwt.dom.client.Node; @@ -1196,4 +1197,18 @@ public class Util { } return getSimpleName(p) + " (" + p.getConnectorId() + ")"; } + + /** + * Resolve a relative URL to an absolute URL based on the current document's + * location. + * + * @param url + * a string with the relative URL to resolve + * @return the corresponding absolute URL as a string + */ + public static String getAbsoluteUrl(String url) { + AnchorElement a = Document.get().createAnchorElement(); + a.setHref(url); + return a.getHref(); + } } |