]> source.dussan.org Git - vaadin-framework.git/commitdiff
Remove synchronous AJAX support (#11254)
authorJohannes Dahlström <johannesd@vaadin.com>
Tue, 5 Mar 2013 13:16:05 +0000 (15:16 +0200)
committerVaadin Code Review <review@vaadin.com>
Wed, 27 Mar 2013 16:06:12 +0000 (16:06 +0000)
SJAX was used in Vaadin 6 only for window close events,
and those are not supported in Vaadin 7.

Change-Id: I3ca7137d5a16252f63051314743e0bee2890caf1

client/src/com/vaadin/client/ApplicationConnection.java
server/src/com/vaadin/server/VaadinPortlet.java
server/src/com/vaadin/server/VaadinServlet.java
shared/src/com/vaadin/shared/ApplicationConstants.java

index 4ddbd7c39bb1527866511026e60cedd88b104e8c..7a8e73e5716422b12bbb926e918eff1761fc649f 100644 (file)
@@ -656,8 +656,7 @@ public class ApplicationConnection {
     }-*/;
 
     protected void repaintAll() {
-        String repainAllParameters = getRepaintAllParameters();
-        makeUidlRequest("", repainAllParameters, false);
+        makeUidlRequest("", getRepaintAllParameters());
     }
 
     /**
@@ -667,7 +666,7 @@ public class ApplicationConnection {
     public void analyzeLayouts() {
         String params = getRepaintAllParameters() + "&"
                 + ApplicationConstants.PARAM_ANALYZE_LAYOUTS + "=1";
-        makeUidlRequest("", params, false);
+        makeUidlRequest("", params);
     }
 
     /**
@@ -681,7 +680,7 @@ public class ApplicationConnection {
         String params = getRepaintAllParameters() + "&"
                 + ApplicationConstants.PARAM_HIGHLIGHT_CONNECTOR + "="
                 + serverConnector.getConnectorId();
-        makeUidlRequest("", params, false);
+        makeUidlRequest("", params);
     }
 
     /**
@@ -694,11 +693,9 @@ public class ApplicationConnection {
      *            Contains key=value pairs joined by & characters or is empty if
      *            no parameters should be added. Should not start with any
      *            special character.
-     * @param forceSync
-     *            true if the request should be synchronous, false otherwise
      */
     protected void makeUidlRequest(final String requestData,
-            final String extraParams, final boolean forceSync) {
+            final String extraParams) {
         startRequest();
         // Security: double cookie submission pattern
         final String payload = uidlSecurityKey + VAR_BURST_SEPARATOR
@@ -713,7 +710,7 @@ public class ApplicationConnection {
         uri = addGetParameters(uri, UIConstants.UI_ID_PARAMETER + "="
                 + configuration.getUIId());
 
-        doUidlRequest(uri, payload, forceSync);
+        doUidlRequest(uri, payload);
 
     }
 
@@ -725,143 +722,124 @@ public class ApplicationConnection {
      *            The URI to use for the request. May includes GET parameters
      * @param payload
      *            The contents of the request to send
-     * @param synchronous
-     *            true if the request should be synchronous, false otherwise
      */
-    protected void doUidlRequest(final String uri, final String payload,
-            final boolean synchronous) {
-        if (!synchronous) {
-            RequestCallback requestCallback = new RequestCallback() {
-                @Override
-                public void onError(Request request, Throwable exception) {
-                    handleCommunicationError(exception.getMessage(), -1);
-                }
+    protected void doUidlRequest(final String uri, final String payload) {
+        RequestCallback requestCallback = new RequestCallback() {
+            @Override
+            public void onError(Request request, Throwable exception) {
+                handleCommunicationError(exception.getMessage(), -1);
+            }
 
-                private void handleCommunicationError(String details,
-                        int statusCode) {
-                    if (!handleErrorInDelegate(details, statusCode)) {
-                        showCommunicationError(details, statusCode);
-                    }
-                    endRequest();
+            private void handleCommunicationError(String details, int statusCode) {
+                if (!handleErrorInDelegate(details, statusCode)) {
+                    showCommunicationError(details, statusCode);
                 }
+                endRequest();
+            }
 
-                @Override
-                public void onResponseReceived(Request request,
-                        Response response) {
-                    VConsole.log("Server visit took "
-                            + String.valueOf((new Date()).getTime()
-                                    - requestStartTime.getTime()) + "ms");
-
-                    int statusCode = response.getStatusCode();
-
-                    switch (statusCode) {
-                    case 0:
-                        if (retryCanceledActiveRequest) {
-                            /*
-                             * Request was most likely canceled because the
-                             * browser is maybe navigating away from the page.
-                             * Just send the request again without displaying
-                             * any error in case the navigation isn't carried
-                             * through.
-                             */
-                            retryCanceledActiveRequest = false;
-                            doUidlRequest(uri, payload, synchronous);
-                        } else {
-                            handleCommunicationError(
-                                    "Invalid status code 0 (server down?)",
-                                    statusCode);
-                        }
-                        return;
+            @Override
+            public void onResponseReceived(Request request, Response response) {
+                VConsole.log("Server visit took "
+                        + String.valueOf((new Date()).getTime()
+                                - requestStartTime.getTime()) + "ms");
 
-                    case 401:
-                        /*
-                         * Authorization has failed. Could be that the session
-                         * has timed out and the container is redirecting to a
-                         * login page.
-                         */
-                        showAuthenticationError("");
-                        endRequest();
-                        return;
+                int statusCode = response.getStatusCode();
 
-                    case 503:
+                switch (statusCode) {
+                case 0:
+                    if (retryCanceledActiveRequest) {
                         /*
-                         * We'll assume msec instead of the usual seconds. If
-                         * there's no Retry-After header, handle the error like
-                         * a 500, as per RFC 2616 section 10.5.4.
+                         * Request was most likely canceled because the browser
+                         * is maybe navigating away from the page. Just send the
+                         * request again without displaying any error in case
+                         * the navigation isn't carried through.
                          */
-                        String delay = response.getHeader("Retry-After");
-                        if (delay != null) {
-                            VConsole.log("503, retrying in " + delay + "msec");
-                            (new Timer() {
-                                @Override
-                                public void run() {
-                                    doUidlRequest(uri, payload, synchronous);
-                                }
-                            }).schedule(Integer.parseInt(delay));
-                            return;
-                        }
+                        retryCanceledActiveRequest = false;
+                        doUidlRequest(uri, payload);
+                    } else {
+                        handleCommunicationError(
+                                "Invalid status code 0 (server down?)",
+                                statusCode);
                     }
+                    return;
 
-                    if ((statusCode / 100) == 4) {
-                        // Handle all 4xx errors the same way as (they are
-                        // all permanent errors)
-                        showCommunicationError(
-                                "UIDL could not be read from server. Check servlets mappings. Error code: "
-                                        + statusCode, statusCode);
-                        endRequest();
-                        return;
-                    } else if ((statusCode / 100) == 5) {
-                        // Something's wrong on the server, there's nothing the
-                        // client can do except maybe try again.
-                        handleCommunicationError("Server error. Error code: "
-                                + statusCode, statusCode);
+                case 401:
+                    /*
+                     * Authorization has failed. Could be that the session has
+                     * timed out and the container is redirecting to a login
+                     * page.
+                     */
+                    showAuthenticationError("");
+                    endRequest();
+                    return;
+
+                case 503:
+                    /*
+                     * We'll assume msec instead of the usual seconds. If
+                     * there's no Retry-After header, handle the error like a
+                     * 500, as per RFC 2616 section 10.5.4.
+                     */
+                    String delay = response.getHeader("Retry-After");
+                    if (delay != null) {
+                        VConsole.log("503, retrying in " + delay + "msec");
+                        (new Timer() {
+                            @Override
+                            public void run() {
+                                doUidlRequest(uri, payload);
+                            }
+                        }).schedule(Integer.parseInt(delay));
                         return;
                     }
+                }
 
-                    String contentType = response.getHeader("Content-Type");
-                    if (contentType == null
-                            || !contentType.startsWith("application/json")) {
-                        /*
-                         * A servlet filter or equivalent may have intercepted
-                         * the request and served non-UIDL content (for
-                         * instance, a login page if the session has expired.)
-                         * If the response contains a magic substring, do a
-                         * synchronous refresh. See #8241.
-                         */
-                        MatchResult refreshToken = RegExp.compile(
-                                UIDL_REFRESH_TOKEN + "(:\\s*(.*?))?(\\s|$)")
-                                .exec(response.getText());
-                        if (refreshToken != null) {
-                            redirect(refreshToken.getGroup(2));
-                            return;
-                        }
-                    }
+                if ((statusCode / 100) == 4) {
+                    // Handle all 4xx errors the same way as (they are
+                    // all permanent errors)
+                    showCommunicationError(
+                            "UIDL could not be read from server. Check servlets mappings. Error code: "
+                                    + statusCode, statusCode);
+                    endRequest();
+                    return;
+                } else if ((statusCode / 100) == 5) {
+                    // Something's wrong on the server, there's nothing the
+                    // client can do except maybe try again.
+                    handleCommunicationError("Server error. Error code: "
+                            + statusCode, statusCode);
+                    return;
+                }
 
-                    // for(;;);[realjson]
-                    final String jsonText = response.getText().substring(9,
-                            response.getText().length() - 1);
-                    handleJSONText(jsonText, statusCode);
+                String contentType = response.getHeader("Content-Type");
+                if (contentType == null
+                        || !contentType.startsWith("application/json")) {
+                    /*
+                     * A servlet filter or equivalent may have intercepted the
+                     * request and served non-UIDL content (for instance, a
+                     * login page if the session has expired.) If the response
+                     * contains a magic substring, do a synchronous refresh. See
+                     * #8241.
+                     */
+                    MatchResult refreshToken = RegExp.compile(
+                            UIDL_REFRESH_TOKEN + "(:\\s*(.*?))?(\\s|$)").exec(
+                            response.getText());
+                    if (refreshToken != null) {
+                        redirect(refreshToken.getGroup(2));
+                        return;
+                    }
                 }
 
-            };
-            try {
-                doAsyncUIDLRequest(uri, payload, requestCallback);
-            } catch (RequestException e) {
-                VConsole.error(e);
-                endRequest();
+                // for(;;);[realjson]
+                final String jsonText = response.getText().substring(9,
+                        response.getText().length() - 1);
+                handleJSONText(jsonText, statusCode);
             }
-        } else {
-            // Synchronized call, discarded response (leaving the page)
-            SynchronousXHR syncXHR = (SynchronousXHR) SynchronousXHR.create();
-            syncXHR.synchronousPost(uri + "&"
-                    + ApplicationConstants.PARAM_UNLOADBURST + "=1", payload);
-            /*
-             * Although we are in theory leaving the page, the page may still
-             * stay open. End request properly here too. See #3289
-             */
+
+        };
+        try {
+            doAjaxRequest(uri, payload, requestCallback);
+        } catch (RequestException e) {
+            VConsole.error(e);
             endRequest();
         }
-
     }
 
     /**
@@ -905,11 +883,12 @@ public class ApplicationConnection {
      * @throws RequestException
      *             if the request could not be sent
      */
-    protected void doAsyncUIDLRequest(String uri, String payload,
+    protected void doAjaxRequest(String uri, String payload,
             RequestCallback requestCallback) throws RequestException {
         RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, uri);
         // TODO enable timeout
         // rb.setTimeoutMillis(timeoutMillis);
+        // TODO this should be configurable
         rb.setHeader("Content-Type", "text/plain;charset=utf-8");
         rb.setRequestData(payload);
         rb.setCallback(requestCallback);
@@ -1179,7 +1158,7 @@ public class ApplicationConnection {
             }
             LinkedHashMap<String, MethodInvocation> nextBurst = pendingBursts
                     .remove(0);
-            buildAndSendVariableBurst(nextBurst, false);
+            buildAndSendVariableBurst(nextBurst);
         }
     }
 
@@ -2453,7 +2432,7 @@ public class ApplicationConnection {
                     lastInvocationTag = 0;
                 }
             } else {
-                buildAndSendVariableBurst(pendingInvocations, false);
+                buildAndSendVariableBurst(pendingInvocations);
             }
         }
     }
@@ -2467,12 +2446,9 @@ public class ApplicationConnection {
      * 
      * @param pendingInvocations
      *            List of RPC method invocations to send
-     * @param forceSync
-     *            Should we use synchronous request?
      */
     private void buildAndSendVariableBurst(
-            LinkedHashMap<String, MethodInvocation> pendingInvocations,
-            boolean forceSync) {
+            LinkedHashMap<String, MethodInvocation> pendingInvocations) {
         final StringBuffer req = new StringBuffer();
 
         while (!pendingInvocations.isEmpty()) {
@@ -2526,12 +2502,6 @@ public class ApplicationConnection {
             pendingInvocations.clear();
             // Keep tag string short
             lastInvocationTag = 0;
-            // Append all the bursts to this synchronous request
-            if (forceSync && !pendingBursts.isEmpty()) {
-                pendingInvocations = pendingBursts.get(0);
-                pendingBursts.remove(0);
-                req.append(VAR_BURST_SEPARATOR);
-            }
         }
 
         // Include the browser detail parameters if they aren't already sent
@@ -2552,7 +2522,7 @@ public class ApplicationConnection {
 
             getConfiguration().setWidgetsetVersionSent();
         }
-        makeUidlRequest(req.toString(), extraParams, forceSync);
+        makeUidlRequest(req.toString(), extraParams);
     }
 
     private boolean isJavascriptRpc(MethodInvocation invocation) {
index 2abf140a3db115c45f778e662dee992cfae3317a..cc92edf6b24ee2adc1a05329c776d6fd6b11373b 100644 (file)
@@ -417,9 +417,6 @@ public class VaadinPortlet extends GenericPortlet implements Constants,
                 VaadinPortletSession vaadinSession = null;
 
                 try {
-                    // TODO What about PARAM_UNLOADBURST &
-                    // redirectToApplication??
-
                     vaadinSession = (VaadinPortletSession) getService()
                             .findVaadinSession(vaadinRequest);
                     if (vaadinSession == null) {
index fefa8699e151798a50f0335fa9d65631014c0cd3..599c3d6becdcbd8e9a443a13406641645455a07a 100644 (file)
@@ -45,7 +45,6 @@ import com.vaadin.server.communication.HeartbeatHandler;
 import com.vaadin.server.communication.PublishedFileHandler;
 import com.vaadin.server.communication.UIInitHandler;
 import com.vaadin.server.communication.UidlRequestHandler;
-import com.vaadin.shared.ApplicationConstants;
 import com.vaadin.util.CurrentInstance;
 
 @SuppressWarnings("serial")
@@ -263,24 +262,6 @@ public class VaadinServlet extends HttpServlet implements Constants {
         VaadinSession vaadinSession = null;
 
         try {
-            // If a duplicate "close application" URL is received for an
-            // application that is not open, redirect to the application's main
-            // page.
-            // This is needed as e.g. Spring Security remembers the last
-            // URL from the application, which is the logout URL, and repeats
-            // it.
-            // We can tell apart a real onunload request from a repeated one
-            // based on the real one having content (at least the UIDL security
-            // key).
-            if (requestType == RequestType.UIDL
-                    && request.getParameterMap().containsKey(
-                            ApplicationConstants.PARAM_UNLOADBURST)
-                    && request.getContentLength() < 1
-                    && getService().getExistingSession(request, false) == null) {
-                redirectToApplication(request, response);
-                return;
-            }
-
             // Find out the service session this request is related to
             vaadinSession = getService().findVaadinSession(request);
             if (vaadinSession == null) {
@@ -618,14 +599,6 @@ public class VaadinServlet extends HttpServlet implements Constants {
             VaadinServletResponse response) throws IOException,
             ServletException {
 
-        if (isOnUnloadRequest(request)) {
-            /*
-             * Request was an unload request (e.g. window close event) and the
-             * client expects no response if it fails.
-             */
-            return;
-        }
-
         try {
             SystemMessages ci = getService().getSystemMessages(
                     ServletPortletHelper.findLocale(null, null, request),
@@ -665,13 +638,6 @@ public class VaadinServlet extends HttpServlet implements Constants {
     private void handleServiceSecurityException(VaadinServletRequest request,
             VaadinServletResponse response) throws IOException,
             ServletException {
-        if (isOnUnloadRequest(request)) {
-            /*
-             * Request was an unload request (e.g. window close event) and the
-             * client expects no response if it fails.
-             */
-            return;
-        }
 
         try {
             /*
@@ -1132,10 +1098,6 @@ public class VaadinServlet extends HttpServlet implements Constants {
         return false;
     }
 
-    private boolean isOnUnloadRequest(HttpServletRequest request) {
-        return request.getParameter(ApplicationConstants.PARAM_UNLOADBURST) != null;
-    }
-
     /**
      * Remove any heading or trailing "what" from the "string".
      * 
index 333833ab60f16f5be9467f60bdf84ce408492143..220679e69c16ce53f7e651adb1ea7bdf41498e30 100644 (file)
@@ -37,7 +37,6 @@ public class ApplicationConstants implements Serializable {
             + "://";
     public static final String UIDL_SECURITY_TOKEN_ID = "Vaadin-Security-Key";
 
-    public static final String PARAM_UNLOADBURST = "onunloadburst";
     public static final String PARAM_ANALYZE_LAYOUTS = "analyzeLayouts";
     public static final String PARAM_HIGHLIGHT_CONNECTOR = "highlightConnector";