From: Jouni Koivuviita Date: Mon, 7 Apr 2008 09:30:40 +0000 (+0000) Subject: -Enhancement: Application.setSessionExpiredURL now allows the developer to set the... X-Git-Tag: 6.7.0.beta1~4914 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1b441ca92bba15de8ecae5a45fa7f632cce824b6;p=vaadin-framework.git -Enhancement: Application.setSessionExpiredURL now allows the developer to set the URL where to redirect the client after the application session has expired. The redirect is not pushed to the client, i.e. the client has to make a request to the expired application to get redirected. -Minor modification to the session expired notification (onclick="window.location.reload()" was not working properly). -Enhancement: "closeApplication" parameter allows Testing Tools to close the application after tests. svn changeset:4134/svn branch:trunk --- diff --git a/src/com/itmill/toolkit/Application.java b/src/com/itmill/toolkit/Application.java index 5a18903fa1..6a7758f880 100644 --- a/src/com/itmill/toolkit/Application.java +++ b/src/com/itmill/toolkit/Application.java @@ -163,11 +163,18 @@ public abstract class Application implements URIHandler, Terminal.ErrorListener private long lastResourceKeyNumber = 0; /** - * URL the user is redirected to on application close or null if application - * is just closed + * URL where the user is redirected to on application close, or null if + * application is just closed without redirection. */ private String logoutURL = null; + /** + * URL where the user is redirected to when the Toolkit ApplicationServlet + * session expires, or null if the application is just closed without + * redirection. + */ + private String expiredURL = null; + private Focusable pendingFocus; /** @@ -996,7 +1003,7 @@ public abstract class Application implements URIHandler, Terminal.ErrorListener } /** - * Returns the URL user is redirected to on application close.If the URL is + * Returns the URL user is redirected to on application close. If the URL is * null, the application is closed normally as defined by * the application running environment. *

@@ -1024,6 +1031,31 @@ public abstract class Application implements URIHandler, Terminal.ErrorListener this.logoutURL = logoutURL; } + /** + * Returns the URL where user is redirected to when the Toolkit + * ApplicationServlet session expires. If the URL is null, + * the application is closed normally and it shows a notification to the + * client. + * + * @return the URL. + */ + public String getSessionExpiredURL() { + return expiredURL; + } + + /** + * Sets the URL where user is redirected to when the Toolkit + * ApplicationServlet session expires. If the URL is null, + * the application is closed normally and it shows a notification to the + * client. + * + * @param expiredURL + * the expiredURL to set. + */ + public void setSessionExpiredURL(String expiredURL) { + this.expiredURL = expiredURL; + } + /** *

* Invoked by the terminal on any exception that occurs in application and diff --git a/src/com/itmill/toolkit/terminal/gwt/server/ApplicationServlet.java b/src/com/itmill/toolkit/terminal/gwt/server/ApplicationServlet.java index 1edd69fc72..56fab0803c 100644 --- a/src/com/itmill/toolkit/terminal/gwt/server/ApplicationServlet.java +++ b/src/com/itmill/toolkit/terminal/gwt/server/ApplicationServlet.java @@ -398,11 +398,11 @@ public class ApplicationServlet extends HttpServlet { // Get existing application application = getExistingApplication(request, response); if (application == null - || request.getParameter("restartApplication") != null) { - if (request.getParameter("restartApplication") != null - && application != null) { + || request.getParameter("restartApplication") != null + || request.getParameter("closeApplication") != null) { + if (application != null) { application.close(); - final HttpSession session = request.getSession(); + final HttpSession session = request.getSession(false); if (session != null) { application.close(); ApplicationServlet.applicationToAjaxAppMgrMap @@ -411,6 +411,9 @@ public class ApplicationServlet extends HttpServlet { .removeApplication(application); } } + if (request.getParameter("closeApplication") != null) { + return; + } // Not found, creating new application application = getNewApplication(request, response); } @@ -471,7 +474,32 @@ public class ApplicationServlet extends HttpServlet { } catch (final SessionExpired e) { // Session has expired - criticalNotification(request, response, "Your session has expired."); + // Get new application so we can fetch the sessionExpiredURL + Application app = null; + try { + app = (Application) applicationClass.newInstance(); + app.init(); + } catch (InstantiationException e1) { + // Should not happen + e1.printStackTrace(); + } catch (IllegalAccessException e1) { + // Should not happen + e1.printStackTrace(); + } + + // Redirect if expiredURL is found + if (app != null && app.getSessionExpiredURL() != null) { + if (UIDLrequest) { + redirectUidlRequest(request, response, app + .getSessionExpiredURL()); + } else { + response.sendRedirect(app.getSessionExpiredURL()); + } + } else { + // Else show a notification to the client + criticalNotification(request, response, + "Your session has expired."); + } } catch (final Throwable e) { e.printStackTrace(); // if this was an UIDL request, response UIDL back to client @@ -535,6 +563,31 @@ public class ApplicationServlet extends HttpServlet { } } + /** + * Redirect an UIDL request to move to a new URL. + * + * @param request + * the HTTP request instance. + * @param response + * the HTTP response to write to. + * @param url + * the URL to redirect to. + * @throws IOException + * if the writing failed due to input/output error. + */ + private void redirectUidlRequest(HttpServletRequest request, + HttpServletResponse response, String url) throws IOException { + // Set the response type + response.setContentType("application/json; charset=UTF-8"); + final ServletOutputStream out = response.getOutputStream(); + final PrintWriter outWriter = new PrintWriter(new BufferedWriter( + new OutputStreamWriter(out, "UTF-8"))); + outWriter.print("for(;;);[{\"redirect\":{\"url\":\"" + url + "\"}}]"); + outWriter.flush(); + outWriter.close(); + out.flush(); + } + /** * Send notification to client's application. Used to notify client of * critical errors and session expiration due to long inactivity. Server has @@ -566,10 +619,8 @@ public class ApplicationServlet extends HttpServlet { new OutputStreamWriter(out, "UTF-8"))); outWriter.print("for(;;);[{\"changes\":[], \"meta\" : {" + "\"appError\": {" + "\"caption\":\"" + caption + "\"," - + "\"message\" : \"
Please click " - + "here to restart your application.
" - + "You can also click your browser's refresh button.\"" + + "\"message\" : \"
You can click your browser's" + + " refresh button to restart your application.\"" + "}}, \"resources\": {}, \"locales\":[]}]"); outWriter.flush(); outWriter.close();