]> source.dussan.org Git - vaadin-framework.git/commitdiff
Initial migration of ajax page generation to a RequestHandler (#7888)
authorLeif Åstrand <leif@vaadin.com>
Tue, 22 Nov 2011 13:00:11 +0000 (15:00 +0200)
committerLeif Åstrand <leif@vaadin.com>
Tue, 22 Nov 2011 13:00:11 +0000 (15:00 +0200)
src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java
src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java
src/com/vaadin/terminal/gwt/server/AjaxPageHandler.java [new file with mode: 0644]
src/com/vaadin/terminal/gwt/server/WrappedHttpServletRequest.java

index 94cd543a9b8d2b8a0c7eb41fc15e93f977594f0c..d78c49c61c553e8775f8b8664717756f66bb9256 100644 (file)
@@ -521,6 +521,9 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements
                 // Handles AJAX UIDL requests
                 Root root = applicationManager.getApplicationRoot(
                         wrappedRequest, application);
+                if (root == null) {
+                    throw new ServletException(ERROR_NO_WINDOW_FOUND);
+                }
                 applicationManager.handleUidlRequest(wrappedRequest,
                         wrappedResponse, servletWrapper, root);
                 return;
@@ -539,16 +542,6 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements
             }
             // TODO Should return 404 error here and not do anything more
 
-            // Finds the root within the application
-            Root root = applicationManager.getApplicationRoot(wrappedRequest,
-                    application);
-            if (root == null) {
-                throw new ServletException(ERROR_NO_WINDOW_FOUND);
-            }
-
-            // Send initial AJAX page that kickstarts a Vaadin application
-            writeAjaxPage(request, response, root, application);
-
         } catch (final SessionExpiredException e) {
             // Session has expired, notify user
             handleServiceSessionExpired(request, response);
index b6ec7cc748da95b063e11eb2520434bc7f1a5ec6..dd3304d21d8f2d9e1a988ee5f851d637477fdd36 100644 (file)
@@ -41,6 +41,8 @@ import java.util.UUID;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import javax.servlet.ServletException;
+
 import com.vaadin.Application;
 import com.vaadin.Application.SystemMessages;
 import com.vaadin.terminal.PaintException;
@@ -90,6 +92,27 @@ public abstract class AbstractCommunicationManager implements
             .getLogger(AbstractCommunicationManager.class.getName());
 
     private static final RequestHandler APP_RESOURCE_HANDLER = new ApplicationResourceHandler();
+    private static final RequestHandler AJAX_PAGE_HANDLER = new AjaxPageHandler() {
+
+        @Override
+        protected void writeAjaxPage(WrappedRequest request,
+                WrappedResponse response, Root root) throws IOException {
+            {
+                WrappedHttpServletRequest wrappedServletRequest = (WrappedHttpServletRequest) request;
+                try {
+                    wrappedServletRequest.getServlet().writeAjaxPage(
+                            wrappedServletRequest.getHttpServletRequest(),
+                            ((WrappedHttpServletResponse) response)
+                                    .getHttpServletResponse(), root,
+                            root.getApplication());
+                } catch (ServletException e) {
+                    logger.log(Level.WARNING, e.getLocalizedMessage(), e);
+                    writeError(response, e);
+                }
+            }
+        }
+
+    };
 
     /**
      * TODO Document me!
@@ -184,6 +207,7 @@ public abstract class AbstractCommunicationManager implements
      */
     public AbstractCommunicationManager(Application application) {
         this.application = application;
+        application.addRequestHandler(AJAX_PAGE_HANDLER);
         application.addRequestHandler(APP_RESOURCE_HANDLER);
         requireLocale(application.getLocale().toString());
     }
@@ -1635,9 +1659,6 @@ public abstract class AbstractCommunicationManager implements
                 int rootId = Integer.parseInt(rootIdString);
                 root = application.getRootById(rootId);
             }
-            if (root == null) {
-                root = application.getRoot(request);
-            }
         }
 
         Root.setCurrentRoot(root);
diff --git a/src/com/vaadin/terminal/gwt/server/AjaxPageHandler.java b/src/com/vaadin/terminal/gwt/server/AjaxPageHandler.java
new file mode 100644 (file)
index 0000000..be6b120
--- /dev/null
@@ -0,0 +1,39 @@
+package com.vaadin.terminal.gwt.server;
+
+import java.io.IOException;
+
+import com.vaadin.Application;
+import com.vaadin.terminal.RequestHandler;
+import com.vaadin.terminal.WrappedRequest;
+import com.vaadin.terminal.WrappedResponse;
+import com.vaadin.ui.Root;
+
+public abstract class AjaxPageHandler implements RequestHandler {
+
+    public boolean handleRequest(Application application,
+            WrappedRequest request, WrappedResponse response)
+            throws IOException {
+
+        // TODO Should all urls be handled here?
+        Root root = application.getRoot(request);
+
+        if (root == null) {
+            writeError(response, null);
+            return true;
+        }
+
+        writeAjaxPage(request, response, root);
+
+        return true;
+    }
+
+    protected abstract void writeAjaxPage(WrappedRequest request,
+            WrappedResponse response, Root root) throws IOException;
+
+    protected void writeError(WrappedResponse response, Throwable e)
+            throws IOException {
+        response.setStatus(500);
+        response.getWriter().println("Error");
+    }
+
+}
index 8bd5b9b1118580bf8d3bba6448d9bb6fe32fcd1b..1ce97b10ba991125e2cd24e75714d3dc81057202 100644 (file)
@@ -87,4 +87,8 @@ public class WrappedHttpServletRequest implements WrappedRequest {
     public String getContentType() {
         return request.getContentType();
     }
+
+    public AbstractApplicationServlet getServlet() {
+        return servlet;
+    }
 }
\ No newline at end of file