diff options
author | Artur <artur@vaadin.com> | 2017-04-24 12:04:15 +0300 |
---|---|---|
committer | Ilia Motornyi <elmot@vaadin.com> | 2017-04-24 11:04:15 +0200 |
commit | bb46fff4376095fb52443973577767026807a7ea (patch) | |
tree | 815dcd357f856174e52c21d554e1b1f9b98fad22 /server | |
parent | 70a3a105b22a01ee1114b40e50c18cdd5b194e50 (diff) | |
download | vaadin-framework-bb46fff4376095fb52443973577767026807a7ea.tar.gz vaadin-framework-bb46fff4376095fb52443973577767026807a7ea.zip |
Add support for frontend:// using separate es5 and es6 folders
Diffstat (limited to 'server')
4 files changed, 161 insertions, 9 deletions
diff --git a/server/src/main/java/com/vaadin/annotations/HtmlImport.java b/server/src/main/java/com/vaadin/annotations/HtmlImport.java index 81b4db87ca..dd20558737 100644 --- a/server/src/main/java/com/vaadin/annotations/HtmlImport.java +++ b/server/src/main/java/com/vaadin/annotations/HtmlImport.java @@ -40,8 +40,10 @@ import com.vaadin.server.ClientConnector; * <li>Absolute URLs including protocol and host are used as is on the * client-side. * </ul> - * Note that it is a good idea to use URLs starting with {@literal vaadin://} - * and place all HTML imports inside {@literal VAADIN/bower_components}. Polymer + * Note that you should (almost) always use URLs starting with + * {@literal frontend://} so that the framework can resolve the files to either + * {@literal VAADIN/es5} or {@literal VAADIN/es6} depending on if the browser + * supports ES6 classes (most browers) or not (IE11 and Safari <= 9). Polymer * elements rely on importing dependencies using relative paths * {@literal ../../other-element/other-element.html}, which will not work if * they are installed in different locations. @@ -50,10 +52,10 @@ import com.vaadin.server.ClientConnector; * added at the same time. * <p> * Example: - * <code>@HtmlImport("bower_components/paper-slider/paper-slider.html")</code> - * on the class com.example.MyConnector would load the file - * http://host.com/VAADIN/bower_components/paper-slider/paper-slider.html before - * the {@code init()} method of the client side connector is invoked. + * <code>@HtmlImport("frontend://paper-slider/paper-slider.html")</code> on the + * class com.example.MyConnector would load the file + * {@literal http://host.com/VAADIN/es[56]/paper-slider/paper-slider.html} + * before the {@code init()} method of the client side connector is invoked. * * @author Vaadin Ltd * @since 8.0 diff --git a/server/src/main/java/com/vaadin/server/BootstrapHandler.java b/server/src/main/java/com/vaadin/server/BootstrapHandler.java index 218ed394cb..2832ecb326 100644 --- a/server/src/main/java/com/vaadin/server/BootstrapHandler.java +++ b/server/src/main/java/com/vaadin/server/BootstrapHandler.java @@ -84,7 +84,7 @@ public abstract class BootstrapHandler extends SynchronizedRequestHandler { private String appId; private PushMode pushMode; private JsonObject applicationParameters; - private VaadinUriResolver uriResolver; + private BootstrapUriResolver uriResolver; private WidgetsetInfo widgetsetInfo; public BootstrapContext(VaadinResponse response, @@ -177,7 +177,7 @@ public abstract class BootstrapHandler extends SynchronizedRequestHandler { return applicationParameters; } - public VaadinUriResolver getUriResolver() { + public BootstrapUriResolver getUriResolver() { if (uriResolver == null) { uriResolver = new BootstrapUriResolver(this); } @@ -186,8 +186,9 @@ public abstract class BootstrapHandler extends SynchronizedRequestHandler { } } - private class BootstrapUriResolver extends VaadinUriResolver { + protected static class BootstrapUriResolver extends VaadinUriResolver { private final BootstrapContext context; + private String frontendUrl; public BootstrapUriResolver(BootstrapContext bootstrapContext) { context = bootstrapContext; @@ -250,6 +251,28 @@ public abstract class BootstrapHandler extends SynchronizedRequestHandler { assert root.endsWith("/"); return root; } + + @Override + protected String getFrontendUrl() { + if (frontendUrl == null) { + DeploymentConfiguration configuration = context.getSession() + .getConfiguration(); + if (context.getSession().getBrowser().isEs6Supported()) { + frontendUrl = configuration.getApplicationOrSystemProperty( + ApplicationConstants.FRONTEND_URL_ES6, + ApplicationConstants.FRONTEND_URL_ES6_DEFAULT_VALUE); + } else { + frontendUrl = configuration.getApplicationOrSystemProperty( + ApplicationConstants.FRONTEND_URL_ES5, + ApplicationConstants.FRONTEND_URL_ES5_DEFAULT_VALUE); + } + if (!frontendUrl.endsWith("/")) { + frontendUrl += "/"; + } + } + + return frontendUrl; + } } @Override @@ -708,6 +731,8 @@ public abstract class BootstrapHandler extends SynchronizedRequestHandler { String vaadinDir = vaadinService.getStaticFileLocation(request) + "/VAADIN/"; appConfig.put(ApplicationConstants.VAADIN_DIR_URL, vaadinDir); + appConfig.put(ApplicationConstants.FRONTEND_URL, + context.getUriResolver().getFrontendUrl()); if (!session.getConfiguration().isProductionMode()) { appConfig.put("debug", true); diff --git a/server/src/main/java/com/vaadin/server/WebBrowser.java b/server/src/main/java/com/vaadin/server/WebBrowser.java index f7fbea6513..5c387d0c14 100644 --- a/server/src/main/java/com/vaadin/server/WebBrowser.java +++ b/server/src/main/java/com/vaadin/server/WebBrowser.java @@ -538,4 +538,18 @@ public class WebBrowser implements Serializable { return browserDetails.isTooOldToFunctionProperly(); } + /** + * Checks if the browser supports ECMAScript 6, based on the user agent. + * + * @return <code>true</code> if the browser supports ES6, <code>false</code> + * otherwise. + */ + public boolean isEs6Supported() { + if (browserDetails == null) { + // Don't know, so assume no + return false; + } + return browserDetails.isEs6Supported(); + + } } diff --git a/server/src/test/java/com/vaadin/server/BootstrapHandlerTest.java b/server/src/test/java/com/vaadin/server/BootstrapHandlerTest.java new file mode 100644 index 0000000000..3530d105a3 --- /dev/null +++ b/server/src/test/java/com/vaadin/server/BootstrapHandlerTest.java @@ -0,0 +1,111 @@ +/* + * Copyright 2000-2016 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.server; + +import java.util.Properties; + +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import com.vaadin.server.BootstrapHandler.BootstrapContext; +import com.vaadin.server.BootstrapHandler.BootstrapUriResolver; + +public class BootstrapHandlerTest { + + private static final String VAADIN_URL = "http://host/VAADIN/"; + + public static class ES5Browser extends WebBrowser { + @Override + public boolean isEs6Supported() { + return false; + } + } + + public static class ES6Browser extends WebBrowser { + @Override + public boolean isEs6Supported() { + return true; + } + } + + @Test + public void resolveFrontendES5() { + testResolveFrontEnd("frontend://foobar.html", + "http://host/VAADIN/frontend/es5/foobar.html", + new ES5Browser()); + + } + + @Test + public void resolveFrontendES6() { + testResolveFrontEnd("frontend://foobar.html", + "http://host/VAADIN/frontend/es6/foobar.html", + new ES6Browser()); + + } + + @Test + public void resolveFrontendES5CustomUrl() { + Properties properties = new Properties(); + properties.setProperty("frontend.url.es5", + "https://cdn.somewhere.com/5"); + testResolveFrontEnd("frontend://foobar.html", + "https://cdn.somewhere.com/5/foobar.html", new ES5Browser(), + properties); + + } + + @Test + public void resolveFrontendES6CustomUrl() { + Properties properties = new Properties(); + properties.setProperty("frontend.url.es6", + "https://cdn.somewhere.com/6"); + testResolveFrontEnd("frontend://foobar.html", + "https://cdn.somewhere.com/6/foobar.html", new ES6Browser(), + properties); + + } + + private static void testResolveFrontEnd(String frontendUrl, + String expectedUrl, WebBrowser browser) { + testResolveFrontEnd(frontendUrl, expectedUrl, browser, + new Properties()); + } + + @SuppressWarnings("deprecation") + private static void testResolveFrontEnd(String frontendUrl, + String expectedUrl, WebBrowser browser, + Properties customProperties) { + + BootstrapContext context = Mockito.mock(BootstrapContext.class); + BootstrapUriResolver resolver = new BootstrapUriResolver(context) { + @Override + protected String getVaadinDirUrl() { + return VAADIN_URL; + } + }; + VaadinSession session = Mockito.mock(VaadinSession.class); + Mockito.when(context.getSession()).thenReturn(session); + DeploymentConfiguration configuration = new DefaultDeploymentConfiguration( + BootstrapHandlerTest.class, customProperties); + Mockito.when(session.getBrowser()).thenReturn(browser); + Mockito.when(session.getConfiguration()).thenReturn(configuration); + + Assert.assertEquals(expectedUrl, + resolver.resolveVaadinUri(frontendUrl)); + } +} |