diff options
author | Artur Signell <artur@vaadin.com> | 2015-01-14 10:18:39 +0200 |
---|---|---|
committer | Artur Signell <artur@vaadin.com> | 2015-01-14 10:18:39 +0200 |
commit | 032bcef30f5ada7dc1ca6eade07ee596afa25bb3 (patch) | |
tree | a036581c2b7ae7e82fdd60ba92b2fe392b85df7a /shared | |
parent | 8a544b5a6e78337b24a5e56bcde1a21ff6087fae (diff) | |
parent | b356e7e3bba9b08ad9d1ecdfb4d628b8179ef20a (diff) | |
download | vaadin-framework-032bcef30f5ada7dc1ca6eade07ee596afa25bb3.tar.gz vaadin-framework-032bcef30f5ada7dc1ca6eade07ee596afa25bb3.zip |
Merge remote-tracking branch 'origin/master' into grid
Conflicts:
client/src/com/vaadin/client/communication/AtmospherePushConnection.java
server/tests/src/com/vaadin/data/fieldgroup/FieldGroupTests.java
shared/src/com/vaadin/shared/util/SharedUtil.java
Change-Id: I300b5a92bde562390a56b720adf9a37b795c9513
Diffstat (limited to 'shared')
-rw-r--r-- | shared/src/com/vaadin/shared/ApplicationConstants.java | 6 | ||||
-rw-r--r-- | shared/src/com/vaadin/shared/VaadinUriResolver.java | 161 | ||||
-rw-r--r-- | shared/src/com/vaadin/shared/util/SharedUtil.java | 41 |
3 files changed, 203 insertions, 5 deletions
diff --git a/shared/src/com/vaadin/shared/ApplicationConstants.java b/shared/src/com/vaadin/shared/ApplicationConstants.java index 44c972462a..d7aaee6267 100644 --- a/shared/src/com/vaadin/shared/ApplicationConstants.java +++ b/shared/src/com/vaadin/shared/ApplicationConstants.java @@ -48,11 +48,7 @@ public class ApplicationConstants implements Serializable { public static final String SERVICE_URL = "serviceUrl"; - public static final String SERVICE_URL_PATH_AS_PARAMETER = "usePathParameter"; - - // Denotes the namespace which parameters should be prefixed with when - // passed as GET parameters. Currently only used by Liferay. - public static final String SERVICE_URL_PARAMETER_NAMESPACE = "pathParameterNS"; + public static final String SERVICE_URL_PARAMETER_NAME = "pathParameterName"; // Javadocs in ApplicationConfiguration should be updated if this is changed public static final String V_RESOURCE_PATH = "v-resourcePath"; diff --git a/shared/src/com/vaadin/shared/VaadinUriResolver.java b/shared/src/com/vaadin/shared/VaadinUriResolver.java new file mode 100644 index 0000000000..b45d32f71a --- /dev/null +++ b/shared/src/com/vaadin/shared/VaadinUriResolver.java @@ -0,0 +1,161 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * 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.vaadin.shared; + +import java.io.Serializable; + +import com.vaadin.shared.util.SharedUtil; + +/** + * Utility for translating special Vaadin URIs like theme:// and app:// into + * URLs usable by the browser. This is an abstract class performing the main + * logic in {@link #resolveVaadinUri(String)} and using abstract methods in the + * class for accessing information specific to the current environment. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public abstract class VaadinUriResolver implements Serializable { + + /** + * Translates a Vaadin URI to a URL that can be loaded by the browser. The + * following URI schemes are supported: + * <ul> + * <li><code>theme://</code> - resolves to the URL of the currently active + * theme.</li> + * <li><code>published://</code> - resolves to resources on the classpath + * published by {@link com.vaadin.annotations.JavaScript @JavaScript} or + * {@link com.vaadin.annotations.StyleSheet @StyleSheet} annotations on + * connectors.</li> + * <li><code>app://</code> - resolves to a URL that will be routed to the + * currently registered {@link com.vaadin.server.RequestHandler + * RequestHandler} instances.</li> + * <li><code>vaadin://</code> - resolves to the location of static resouces + * in the VAADIN directory</li> + * </ul> + * Any other URI protocols, such as <code>http://</code> or + * <code>https://</code> are passed through this method unmodified. + * + * @since 7.4 + * @param vaadinUri + * the uri to resolve + * @return the resolved uri + */ + public String resolveVaadinUri(String vaadinUri) { + if (vaadinUri == null) { + return null; + } + if (vaadinUri.startsWith("theme://")) { + final String themeUri = getThemeUri(); + vaadinUri = themeUri + vaadinUri.substring(7); + } + + if (vaadinUri + .startsWith(ApplicationConstants.PUBLISHED_PROTOCOL_PREFIX)) { + // getAppUri *should* always end with / + // substring *should* always start with / (published:///foo.bar + // without published://) + vaadinUri = ApplicationConstants.APP_PROTOCOL_PREFIX + + ApplicationConstants.PUBLISHED_FILE_PATH + + vaadinUri + .substring(ApplicationConstants.PUBLISHED_PROTOCOL_PREFIX + .length()); + // Let translation of app:// urls take care of the rest + } + if (vaadinUri.startsWith(ApplicationConstants.APP_PROTOCOL_PREFIX)) { + String relativeUrl = vaadinUri + .substring(ApplicationConstants.APP_PROTOCOL_PREFIX + .length()); + String serviceUrl = getServiceUrl(); + String serviceUrlParameterName = getServiceUrlParameterName(); + if (serviceUrlParameterName != null) { + // Should put path in v-resourcePath parameter and append query + // params to base portlet url + String[] parts = relativeUrl.split("\\?", 2); + String path = parts[0]; + + // 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]; + serviceUrl = SharedUtil.addGetParameters(serviceUrl, + appUrlParams); + } + if (!path.startsWith("/")) { + path = '/' + path; + } + String pathParam = serviceUrlParameterName + "=" + + encodeQueryStringParameterValue(path); + serviceUrl = SharedUtil.addGetParameters(serviceUrl, pathParam); + vaadinUri = serviceUrl; + } else { + vaadinUri = serviceUrl + relativeUrl; + } + } + if (vaadinUri.startsWith(ApplicationConstants.VAADIN_PROTOCOL_PREFIX)) { + final String vaadinDirUri = getVaadinDirUrl(); + String relativeUrl = vaadinUri + .substring(ApplicationConstants.VAADIN_PROTOCOL_PREFIX + .length()); + vaadinUri = vaadinDirUri + relativeUrl; + } + + return vaadinUri; + } + + /** + * Gets the URL pointing to the VAADIN directory. + * + * @return the VAADIN directory URL + */ + protected abstract String getVaadinDirUrl(); + + /** + * Gets the name of the request parameter that should be used for sending + * the requested URL to the {@link #getServiceUrl() service URL}. If + * <code>null</code> is returned, the requested URL will instead be appended + * to the base service URL. + * + * @return the parameter name used for passing request URLs, or + * <code>null</code> to send the path as a part of the request path. + */ + protected abstract String getServiceUrlParameterName(); + + /** + * Gets the URL handled by {@link com.vaadin.server.VaadinService + * VaadinService} to handle application requests. + * + * @return the service URL + */ + protected abstract String getServiceUrl(); + + /** + * Gets the URI of the directory of the current theme. + * + * @return the URI of the current theme directory + */ + protected abstract String getThemeUri(); + + /** + * Encodes a value for safe inclusion as a parameter in the query string. + * + * @param parameterValue + * the value to encode + * @return the encoded value + */ + protected abstract String encodeQueryStringParameterValue( + String parameterValue); +} diff --git a/shared/src/com/vaadin/shared/util/SharedUtil.java b/shared/src/com/vaadin/shared/util/SharedUtil.java index b40d8f03bb..206041235a 100644 --- a/shared/src/com/vaadin/shared/util/SharedUtil.java +++ b/shared/src/com/vaadin/shared/util/SharedUtil.java @@ -196,4 +196,45 @@ public class SharedUtil implements Serializable { return camelCaseToHumanFriendly(string); } + /** + * Adds the get parameters to the uri and returns the new uri that contains + * the parameters. + * + * @param uri + * The uri to which the parameters should be added. + * @param extraParams + * One or more parameters in the format "a=b" or "c=d&e=f". An + * empty string is allowed but will not modify the url. + * @return The modified URI with the get parameters in extraParams added. + */ + public static String addGetParameters(String uri, String extraParams) { + if (extraParams == null || extraParams.length() == 0) { + return uri; + } + // RFC 3986: The query component is indicated by the first question + // mark ("?") character and terminated by a number sign ("#") character + // or by the end of the URI. + String fragment = null; + int hashPosition = uri.indexOf('#'); + if (hashPosition != -1) { + // Fragment including "#" + fragment = uri.substring(hashPosition); + // The full uri before the fragment + uri = uri.substring(0, hashPosition); + } + + if (uri.contains("?")) { + uri += "&"; + } else { + uri += "?"; + } + uri += extraParams; + + if (fragment != null) { + uri += fragment; + } + + return uri; + } + } |