summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Englund <marc@vaadin.com>2013-05-08 09:31:09 +0300
committerMarc Englund <marc@vaadin.com>2013-05-08 09:31:09 +0300
commit1b188073c46c5c21bb8ddeb21674452c9bc414fd (patch)
treeffd26aebca49b4372cdbeb472e6a916e8249167f
parent7d9f544d584edb90b1c9231987e356dfaf0b7b35 (diff)
downloadvaadin-framework-1b188073c46c5c21bb8ddeb21674452c9bc414fd.tar.gz
vaadin-framework-1b188073c46c5c21bb8ddeb21674452c9bc414fd.zip
Remove pre-loader element after pre-loading, fixes #10863
Change-Id: Ibd2009315aadb25e574c82f3b4b54c501d9b1395 Ticket: 10863
-rw-r--r--client/src/com/vaadin/client/ResourceLoader.java18
-rw-r--r--uitest/src/com/vaadin/launcher/DevelopmentServerLauncher.java109
2 files changed, 123 insertions, 4 deletions
diff --git a/client/src/com/vaadin/client/ResourceLoader.java b/client/src/com/vaadin/client/ResourceLoader.java
index b3b9ac7256..7fc8b4f9f3 100644
--- a/client/src/com/vaadin/client/ResourceLoader.java
+++ b/client/src/com/vaadin/client/ResourceLoader.java
@@ -50,8 +50,8 @@ public class ResourceLoader {
* Event fired when a resource has been loaded.
*/
public static class ResourceLoadEvent {
- private ResourceLoader loader;
- private String resourceUrl;
+ private final ResourceLoader loader;
+ private final String resourceUrl;
private final boolean preload;
/**
@@ -286,25 +286,35 @@ public class ResourceLoader {
// Inject loader element if this is the first time this is preloaded
// AND the resources isn't already being loaded in the normal way
- Element element = getPreloadElement(url);
+ final Element element = getPreloadElement(url);
addOnloadHandler(element, new ResourceLoadListener() {
@Override
public void onLoad(ResourceLoadEvent event) {
fireLoad(event);
+ Document.get().getBody().removeChild(element);
}
@Override
public void onError(ResourceLoadEvent event) {
fireError(event);
+ Document.get().getBody().removeChild(element);
}
}, event);
- // TODO Remove object when loaded (without causing spinner in FF)
Document.get().getBody().appendChild(element);
}
}
private static Element getPreloadElement(String url) {
+ /*-
+ * TODO
+ * In Chrome, FF:
+ * <object> does not fire event if resource is 404 -> eternal spinner.
+ * <img> always fires onerror -> no way to know if it loaded -> eternal spinner
+ * <script type="text/javascript> fires, but also executes -> not preloading
+ * <script type="text/cache"> does not fire events
+ * XHR not tested - should work, probably causes other issues
+ -*/
if (BrowserInfo.get().isIE()) {
ScriptElement element = Document.get().createScriptElement();
element.setSrc(url);
diff --git a/uitest/src/com/vaadin/launcher/DevelopmentServerLauncher.java b/uitest/src/com/vaadin/launcher/DevelopmentServerLauncher.java
index 381091206a..ec060bc73a 100644
--- a/uitest/src/com/vaadin/launcher/DevelopmentServerLauncher.java
+++ b/uitest/src/com/vaadin/launcher/DevelopmentServerLauncher.java
@@ -22,9 +22,20 @@ import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
@@ -160,6 +171,25 @@ public class DevelopmentServerLauncher {
webappcontext.setWar(serverArgs.get("webroot"));
server.setHandler(webappcontext);
+ // --slowdown=/run/APP/PUBLISHED/*,/other/path/asd.jpg
+ // slows down specified paths
+ if (serverArgs.containsKey("slowdown")) {
+ String[] paths = serverArgs.get("slowdown").split(",");
+ for (String p : paths) {
+ System.out.println("Slowing down: " + p);
+ webappcontext.addFilter(SlowFilter.class, p, 1);
+ }
+ }
+ // --cache=/run/APP/PUBLISHED/*,/other/path/asd.jpg
+ // caches specified paths
+ if (serverArgs.containsKey("cache")) {
+ String[] paths = serverArgs.get("cache").split(",");
+ for (String p : paths) {
+ System.out.println("Enabling cache for: " + p);
+ webappcontext.addFilter(CacheFilter.class, p, 1);
+ }
+ }
+
try {
server.start();
@@ -240,4 +270,83 @@ public class DevelopmentServerLauncher {
return map;
}
+ /**
+ * Sleeps for 2-5 seconds when serving resources that matches given
+ * pathSpec. --slowdown=/run/APP/PUBLISHED/*,/other/path/asd.jpg
+ */
+ public static class SlowFilter implements Filter {
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ // TODO Auto-generated method stub
+ }
+
+ @Override
+ public void doFilter(ServletRequest request, ServletResponse response,
+ FilterChain chain) throws IOException, ServletException {
+
+ String path = ((HttpServletRequest) request).getPathInfo();
+ long delay = Math.round(Math.random() * 3000) + 2000;
+ System.out.println("Delaying " + path + " for " + delay);
+
+ try {
+ Thread.sleep(delay);
+ } catch (InterruptedException e) {
+ System.out.println("Delay interrupted for " + path);
+ } finally {
+ System.out.println("Resuming " + path);
+ }
+
+ chain.doFilter(request, response);
+ }
+
+ @Override
+ public void destroy() {
+ // TODO Auto-generated method stub
+ }
+
+ }
+
+ /**
+ * Adds "Expires" and "Cache-control" headers when serving resources that
+ * match given pathSpec, in order to cache resource for CACHE_MINUTES.
+ * --cache=/run/APP/PUBLISHED/*,/other/path/asd.jpg
+ */
+ public static class CacheFilter implements Filter {
+
+ private static final int CACHE_MINUTES = 1;
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ // TODO Auto-generated method stub
+ }
+
+ @Override
+ public void doFilter(ServletRequest request, ServletResponse response,
+ FilterChain chain) throws IOException, ServletException {
+
+ String path = ((HttpServletRequest) request).getPathInfo();
+ System.out.println("Caching " + path + " for " + CACHE_MINUTES
+ + " minutes");
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.add(Calendar.MINUTE, CACHE_MINUTES);
+
+ String expires = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z")
+ .format(calendar.getTime());
+
+ ((HttpServletResponse) response).setHeader("Expires", expires);
+ ((HttpServletResponse) response).setHeader("Cache-Control",
+ "max-age=" + (CACHE_MINUTES * 60));
+
+ chain.doFilter(request, response);
+ }
+
+ @Override
+ public void destroy() {
+ // TODO Auto-generated method stub
+ }
+
+ }
+
}