From d2c959a0469061c62dbc0dc79c0be79f0779abac Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Tue, 22 Nov 2011 15:00:11 +0200 Subject: [PATCH] Initial migration of ajax page generation to a RequestHandler (#7888) --- .../server/AbstractApplicationServlet.java | 13 ++----- .../server/AbstractCommunicationManager.java | 27 +++++++++++-- .../terminal/gwt/server/AjaxPageHandler.java | 39 +++++++++++++++++++ .../gwt/server/WrappedHttpServletRequest.java | 4 ++ 4 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 src/com/vaadin/terminal/gwt/server/AjaxPageHandler.java diff --git a/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java b/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java index 94cd543a9b..d78c49c61c 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java @@ -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); diff --git a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java index b6ec7cc748..dd3304d21d 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java @@ -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 index 0000000000..be6b120b56 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/server/AjaxPageHandler.java @@ -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"); + } + +} diff --git a/src/com/vaadin/terminal/gwt/server/WrappedHttpServletRequest.java b/src/com/vaadin/terminal/gwt/server/WrappedHttpServletRequest.java index 8bd5b9b111..1ce97b10ba 100644 --- a/src/com/vaadin/terminal/gwt/server/WrappedHttpServletRequest.java +++ b/src/com/vaadin/terminal/gwt/server/WrappedHttpServletRequest.java @@ -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 -- 2.39.5