]> source.dussan.org Git - vaadin-framework.git/commitdiff
#4129 - Show a message if cookie support is disabled in the browser
authorArtur Signell <artur.signell@itmill.com>
Mon, 1 Mar 2010 17:04:19 +0000 (17:04 +0000)
committerArtur Signell <artur.signell@itmill.com>
Mon, 1 Mar 2010 17:04:19 +0000 (17:04 +0000)
svn changeset:11570/svn branch:6.3

src/com/vaadin/Application.java
src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java

index 67bc5e9da6be4817d34ce6b5159760b6c57631a0..0d6c5721d93b5e86e28cf0fe49cdbf0b4d8d9709 100644 (file)
@@ -1303,6 +1303,13 @@ public abstract class Application implements URIHandler,
      * <li><b>outOfSyncMessage</b> = "Something has caused us to be out of sync
      * with the server.<br/>
      * Take note of any unsaved data, and <u>click here</u> to re-sync."</li>
+     * <li><b>cookiesDisabledURL</b> = null</li>
+     * <li><b>outOfSyncNotificationEnabled</b> = true</li>
+     * <li><b>outOfSyncCaption</b> = "Cookies disabled"</li>
+     * <li><b>outOfSyncMessage</b> = "This application requires cookies to
+     * function.<br/>
+     * Please enable cookies in your browser and <u>click here</u> to try again.
+     * </li>
      * </ul>
      * </p>
      * 
@@ -1328,6 +1335,11 @@ public abstract class Application implements URIHandler,
         protected String outOfSyncCaption = "Out of sync";
         protected String outOfSyncMessage = "Something has caused us to be out of sync with the server.<br/>Take note of any unsaved data, and <u>click here</u> to re-sync.";
 
+        protected String cookiesDisabledURL = null;
+        protected boolean cookiesDisabledNotificationEnabled = true;
+        protected String cookiesDisabledCaption = "Cookies disabled";
+        protected String cookiesDisabledMessage = "This application requires cookies to function.<br/>Please enable cookies in your browser and <u>click here</u> to try again.";
+
         /**
          * Use {@link CustomizedSystemMessages} to customize
          */
@@ -1462,6 +1474,52 @@ public abstract class Application implements URIHandler,
             return (outOfSyncNotificationEnabled ? outOfSyncMessage : null);
         }
 
+        /**
+         * Returns the URL the user should be redirected to after dismissing the
+         * "you have to enable your cookies" message. Typically null.
+         * 
+         * @return A URL the user should be redirected to after dismissing the
+         *         message or null to reload the current URL.
+         */
+        public String getCookiesDisabledURL() {
+            return cookiesDisabledURL;
+        }
+
+        /**
+         * Determines if "cookies disabled" messages should be shown to the end
+         * user or not. If the notification is disabled the user will be
+         * immediately redirected to the URL returned by
+         * {@link #getCookiesDisabledURL()}.
+         * 
+         * @return true to show "cookies disabled" messages to the end user,
+         *         false to redirect to the given URL directly
+         */
+        public boolean isCookiesDisabledNotificationEnabled() {
+            return cookiesDisabledNotificationEnabled;
+        }
+
+        /**
+         * Returns the caption of the message shown to the user when cookies are
+         * disabled in the browser.
+         * 
+         * @return The caption of the "cookies disabled" message
+         */
+        public String getCookiesDisabledCaption() {
+            return (cookiesDisabledNotificationEnabled ? cookiesDisabledCaption
+                    : null);
+        }
+
+        /**
+         * Returns the message shown to the user when cookies are disabled in
+         * the browser.
+         * 
+         * @return The "cookies disabled" message
+         */
+        public String getCookiesDisabledMessage() {
+            return (cookiesDisabledNotificationEnabled ? cookiesDisabledMessage
+                    : null);
+        }
+
     }
 
     /**
@@ -1688,6 +1746,54 @@ public abstract class Application implements URIHandler,
             this.outOfSyncMessage = outOfSyncMessage;
         }
 
+        /**
+         * Sets the URL to redirect to when the browser has cookies disabled.
+         * 
+         * @param cookiesDisabledURL
+         *            the URL to redirect to, or null to reload the current URL
+         */
+        public void setCookiesDisabledURL(String cookiesDisabledURL) {
+            this.cookiesDisabledURL = cookiesDisabledURL;
+        }
+
+        /**
+         * Enables or disables the notification for "cookies disabled" messages.
+         * If disabled, the URL returned by {@link #getCookiesDisabledURL()} is
+         * loaded directly.
+         * 
+         * @param cookiesDisabledNotificationEnabled
+         *            true to enable "cookies disabled" messages, false
+         *            otherwise
+         */
+        public void setCookiesDisabledNotificationEnabled(
+                boolean cookiesDisabledNotificationEnabled) {
+            this.cookiesDisabledNotificationEnabled = cookiesDisabledNotificationEnabled;
+        }
+
+        /**
+         * Sets the caption of the "cookies disabled" notification. Set to null
+         * for no caption. If both caption and message is null, the notification
+         * is disabled.
+         * 
+         * @param cookiesDisabledCaption
+         *            the caption for the "cookies disabled" notification
+         */
+        public void setCookiesDisableCaption(String cookiesDisabledCaption) {
+            this.cookiesDisabledCaption = cookiesDisabledCaption;
+        }
+
+        /**
+         * Sets the message of the "cookies disabled" notification. Set to null
+         * for no message. If both caption and message is null, the notification
+         * is disabled.
+         * 
+         * @param cookiesDisabledMessage
+         *            the message for the "cookies disabled" notification
+         */
+        public void setCookiesDisableMessage(String cookiesDisabledMessage) {
+            this.cookiesDisabledMessage = cookiesDisabledMessage;
+        }
+
     }
 
     /**
index 39a92484090c93bd7ae58b3036308ae16dc7f209..0fd4e46f8c2ec9fa12144918ff7d85f5eb12fca5 100644 (file)
@@ -362,6 +362,9 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements
             HttpServletResponse response) throws ServletException, IOException {
 
         RequestType requestType = getRequestType(request);
+        if (!ensureCookiesEnabled(requestType, request, response)) {
+            return;
+        }
 
         if (requestType == RequestType.STATIC_FILE) {
             serveStaticResources(request, response);
@@ -502,6 +505,39 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements
         }
     }
 
+    /**
+     * Check that cookie support is enabled in the browser. Only checks UIDL
+     * requests.
+     * 
+     * @param requestType
+     *            Type of the request as returned by
+     *            {@link #getRequestType(HttpServletRequest)}
+     * @param request
+     *            The request from the browser
+     * @param response
+     *            The response to which an error can be written
+     * @return false if cookies are disabled, true otherwise
+     * @throws IOException
+     */
+    private boolean ensureCookiesEnabled(RequestType requestType,
+            HttpServletRequest request, HttpServletResponse response)
+            throws IOException {
+        if (requestType == RequestType.UIDL && !isRepaintAll(request)) {
+            // In all other but the first UIDL request a cookie should be
+            // returned by the browser.
+            // This can be removed if cookieless mode (#3228) is supported
+            if (request.getRequestedSessionId() == null) {
+                // User has cookies disabled
+                criticalNotification(request, response, getSystemMessages()
+                        .getCookiesDisabledCaption(), getSystemMessages()
+                        .getCookiesDisabledMessage(), null, getSystemMessages()
+                        .getCookiesDisabledURL());
+                return false;
+            }
+        }
+        return true;
+    }
+
     private void updateBrowserProperties(WebBrowser browser,
             HttpServletRequest request) {
         browser.updateBrowserProperties(request.getLocale(), request