From e20f6fd54ae4b350ea654807b98fe442e546d1a8 Mon Sep 17 00:00:00 2001 From: Leif Åstrand Date: Sat, 10 Jan 2015 22:19:36 +0200 Subject: Include UI class @JavaScript and @StyleSheet in bootstrap html (#9045) Change-Id: I9d4243fa6f91ba5bc3449d0a3ec24f209e6360e6 --- .../com/vaadin/shared/ApplicationConstants.java | 6 +- .../src/com/vaadin/shared/VaadinUriResolver.java | 95 ++++++++++++++++++++++ shared/src/com/vaadin/shared/util/SharedUtil.java | 41 ++++++++++ 3 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 shared/src/com/vaadin/shared/VaadinUriResolver.java (limited to 'shared') 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..10edfe8aad --- /dev/null +++ b/shared/src/com/vaadin/shared/VaadinUriResolver.java @@ -0,0 +1,95 @@ +/* + * 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; + +public abstract class VaadinUriResolver implements Serializable { + + 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; + } + + protected abstract String getVaadinDirUrl(); + + protected abstract String getServiceUrlParameterName(); + + protected abstract String getServiceUrl(); + + protected abstract String getThemeUri(); + + protected abstract String encodeQueryStringParameterValue(String queryString); +} diff --git a/shared/src/com/vaadin/shared/util/SharedUtil.java b/shared/src/com/vaadin/shared/util/SharedUtil.java index 7276f418fa..cc98d11abd 100644 --- a/shared/src/com/vaadin/shared/util/SharedUtil.java +++ b/shared/src/com/vaadin/shared/util/SharedUtil.java @@ -60,4 +60,45 @@ public class SharedUtil implements Serializable { */ public static final String SIZE_PATTERN = "^(-?\\d*(?:\\.\\d+)?)(%|px|em|rem|ex|in|cm|mm|pt|pc)?$"; + /** + * 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; + } + } -- cgit v1.2.3 From 2877e42f0dbf706d5b1c79e93ee16320b7b5d8cc Mon Sep 17 00:00:00 2001 From: Leif Åstrand Date: Mon, 12 Jan 2015 20:10:28 +0200 Subject: Missing javadocs for VaadinUriResolver (#9045) Change-Id: I640d491c38c3fbd083a955669ec58ce294f158c4 --- .../src/com/vaadin/shared/VaadinUriResolver.java | 68 +++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) (limited to 'shared') diff --git a/shared/src/com/vaadin/shared/VaadinUriResolver.java b/shared/src/com/vaadin/shared/VaadinUriResolver.java index 10edfe8aad..9c2f4bcc76 100644 --- a/shared/src/com/vaadin/shared/VaadinUriResolver.java +++ b/shared/src/com/vaadin/shared/VaadinUriResolver.java @@ -19,8 +19,41 @@ 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 + * @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: + * + * Any other URI protocols, such as http:// or + * https:// are passed through this method unmodified. + * + * @since + * @param vaadinUri + * the uri to resolve + * @return the resolved uri + */ public String resolveVaadinUri(String vaadinUri) { if (vaadinUri == null) { return null; @@ -83,13 +116,46 @@ public abstract class VaadinUriResolver implements Serializable { 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 + * null is returned, the requested URL will instead be appended + * to the base service URL. + * + * @return the parameter name used for passing request URLs, or + * null 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(); - protected abstract String encodeQueryStringParameterValue(String queryString); + /** + * 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); } -- cgit v1.2.3 From 0fa60d0fceff89eefe28979f5ad44558047c223b Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 13 Jan 2015 22:42:46 +0200 Subject: Set @since tags to 7.4 Change-Id: I3846bea13444afbb72b54eaf8808273f88dddee1 --- shared/src/com/vaadin/shared/VaadinUriResolver.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'shared') diff --git a/shared/src/com/vaadin/shared/VaadinUriResolver.java b/shared/src/com/vaadin/shared/VaadinUriResolver.java index 9c2f4bcc76..b45d32f71a 100644 --- a/shared/src/com/vaadin/shared/VaadinUriResolver.java +++ b/shared/src/com/vaadin/shared/VaadinUriResolver.java @@ -25,7 +25,7 @@ import com.vaadin.shared.util.SharedUtil; * logic in {@link #resolveVaadinUri(String)} and using abstract methods in the * class for accessing information specific to the current environment. * - * @since + * @since 7.4 * @author Vaadin Ltd */ public abstract class VaadinUriResolver implements Serializable { @@ -49,7 +49,7 @@ public abstract class VaadinUriResolver implements Serializable { * Any other URI protocols, such as http:// or * https:// are passed through this method unmodified. * - * @since + * @since 7.4 * @param vaadinUri * the uri to resolve * @return the resolved uri -- cgit v1.2.3