From e2574afbe9c0267710b63d6f21a59c05eb6e6d88 Mon Sep 17 00:00:00 2001 From: Marc Englund Date: Thu, 22 Dec 2011 14:51:46 +0200 Subject: [PATCH] External UnsupportedBrowserHandler --- .../server/AbstractCommunicationManager.java | 1 + .../terminal/gwt/server/BootstrapHandler.java | 58 +--------- .../gwt/server/UnsupportedBrowserHandler.java | 103 ++++++++++++++++++ 3 files changed, 105 insertions(+), 57 deletions(-) create mode 100644 src/com/vaadin/terminal/gwt/server/UnsupportedBrowserHandler.java diff --git a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java index 97d31df827..942f128862 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java @@ -187,6 +187,7 @@ public abstract class AbstractCommunicationManager implements this.application = application; application.addRequestHandler(getBootstrapHandler()); application.addRequestHandler(APP_RESOURCE_HANDLER); + application.addRequestHandler(UnsupportedBrowserHandler.getInstance()); requireLocale(application.getLocale().toString()); } diff --git a/src/com/vaadin/terminal/gwt/server/BootstrapHandler.java b/src/com/vaadin/terminal/gwt/server/BootstrapHandler.java index 13a4ea3e1f..6bdcc0cff5 100644 --- a/src/com/vaadin/terminal/gwt/server/BootstrapHandler.java +++ b/src/com/vaadin/terminal/gwt/server/BootstrapHandler.java @@ -26,10 +26,8 @@ import com.vaadin.ui.Root; public abstract class BootstrapHandler implements RequestHandler { - /** Cookie used to ignore browser checks */ - private static final String FORCE_LOAD_COOKIE = "vaadinforceload=1"; - protected class BootstrapContext implements Serializable { + private final WrappedResponse response; private final WrappedRequest request; private final Application application; @@ -117,21 +115,6 @@ public abstract class BootstrapHandler implements RequestHandler { WrappedRequest request, WrappedResponse response) throws IOException { - if (request.getBrowserDetails() != null) { - // Check if the browser is supported; we'll activate Chrome Frame - // silently if available. - WebBrowser b = request.getBrowserDetails().getWebBrowser(); - if (b.isTooOldToFunctionProperly() && !b.isChromeFrameCapable()) { - // bypass if cookie set - String c = request.getHeader("Cookie"); - if (c == null || !c.contains(FORCE_LOAD_COOKIE)) { - writeBrowserTooOldPage(request, response); - return true; - } - - } - } - // TODO Should all urls be handled here? int rootId; try { @@ -155,45 +138,6 @@ public abstract class BootstrapHandler implements RequestHandler { return true; } - /** - * Writes a page encouraging the user to upgrade to a more current browser. - * - * @param request - * @param response - * @throws IOException - */ - protected void writeBrowserTooOldPage(WrappedRequest request, - WrappedResponse response) throws IOException { - Writer page = response.getWriter(); - WebBrowser b = request.getBrowserDetails().getWebBrowser(); - - page.write("

I'm sorry, but your browser is not supported

" - + "

The version (" - + b.getBrowserMajorVersion() - + "." - + b.getBrowserMinorVersion() - + ") of the browser you are using " - + " is outdated and not supported.

" - + "

You should consider upgrading to a more up-to-date browser.

" - + "

The most popular browsers are " - + " Chrome," - + " Firefox," - + (b.isWindows() ? " Internet Explorer," - : "") - + " Opera" - + " and Safari.
" - + "Upgrading to the latest version of one of these will make the web safer, faster and better looking.

" - + (b.isIE() ? "" - + "

If you can not upgrade your browser, please consider trying Chrome Frame.

" - : "") // - + "

Continue without updating (not recommended)

" - + "\n" + ""); - - page.close(); - } - protected final void writeBootstrapPage(WrappedRequest request, WrappedResponse response, Application application, int rootId) throws IOException, JSONException { diff --git a/src/com/vaadin/terminal/gwt/server/UnsupportedBrowserHandler.java b/src/com/vaadin/terminal/gwt/server/UnsupportedBrowserHandler.java new file mode 100644 index 0000000000..42da74bf65 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/server/UnsupportedBrowserHandler.java @@ -0,0 +1,103 @@ +package com.vaadin.terminal.gwt.server; + +import java.io.IOException; +import java.io.Writer; + +import com.vaadin.Application; +import com.vaadin.terminal.RequestHandler; +import com.vaadin.terminal.WrappedRequest; +import com.vaadin.terminal.WrappedResponse; + +/** + * A {@link RequestHandler} that presents an informative page if the browser in + * use is unsupported. Recognizes Chrome Frame and allow it to be used. + * + *

+ * This handler is a usually used as a singleton added to the application by + * {@link AbstractCommunicationManager}. It can be removed/replaced by doing + * something like this:

+ * application.removeRequestHandler(UnsupportedBrowserHandler.getInstance());
+ * 
+ *

+ */ +@SuppressWarnings("serial") +public class UnsupportedBrowserHandler implements RequestHandler { + + /** Cookie used to ignore browser checks */ + public static final String FORCE_LOAD_COOKIE = "vaadinforceload=1"; + + private static UnsupportedBrowserHandler instance; + + /** + * Get the {@link UnsupportedBrowserHandler} singleton instance. + * + * @return the {@link UnsupportedBrowserHandler} singleton + */ + public static synchronized UnsupportedBrowserHandler getInstance() { + if (instance == null) { + instance = new UnsupportedBrowserHandler(); + } + return instance; + } + + @Override + public boolean handleRequest(Application application, + WrappedRequest request, WrappedResponse response) + throws IOException { + + if (request.getBrowserDetails() != null) { + // Check if the browser is supported + // If Chrome Frame is available we'll assume it's ok + WebBrowser b = request.getBrowserDetails().getWebBrowser(); + if (b.isTooOldToFunctionProperly() && !b.isChromeFrameCapable()) { + // bypass if cookie set + String c = request.getHeader("Cookie"); + if (c == null || !c.contains(FORCE_LOAD_COOKIE)) { + writeBrowserTooOldPage(request, response); + return true; // request handled + } + } + } + + return false; // pass to next handler + } + + /** + * Writes a page encouraging the user to upgrade to a more current browser. + * + * @param request + * @param response + * @throws IOException + */ + protected void writeBrowserTooOldPage(WrappedRequest request, + WrappedResponse response) throws IOException { + Writer page = response.getWriter(); + WebBrowser b = request.getBrowserDetails().getWebBrowser(); + + page.write("

I'm sorry, but your browser is not supported

" + + "

The version (" + + b.getBrowserMajorVersion() + + "." + + b.getBrowserMinorVersion() + + ") of the browser you are using " + + " is outdated and not supported.

" + + "

You should consider upgrading to a more up-to-date browser.

" + + "

The most popular browsers are " + + " Chrome," + + " Firefox," + + (b.isWindows() ? " Internet Explorer," + : "") + + " Opera" + + " and Safari.
" + + "Upgrading to the latest version of one of these will make the web safer, faster and better looking.

" + + (b.isIE() ? "" + + "

If you can not upgrade your browser, please consider trying Chrome Frame.

" + : "") // + + "

Continue without updating (not recommended)

" + + "\n" + ""); + + page.close(); + } +} \ No newline at end of file -- 2.39.5