diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/com/itmill/toolkit/Application.java | 38 | ||||
-rw-r--r-- | src/com/itmill/toolkit/terminal/gwt/server/ApplicationServlet.java | 69 |
2 files changed, 95 insertions, 12 deletions
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 * <code>null</code>, the application is closed normally as defined by * the application running environment. * <p> @@ -1025,6 +1032,31 @@ public abstract class Application implements URIHandler, Terminal.ErrorListener } /** + * Returns the URL where user is redirected to when the Toolkit + * ApplicationServlet session expires. If the URL is <code>null</code>, + * 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 <code>null</code>, + * 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; + } + + /** * <p> * Invoked by the terminal on any exception that occurs in application and * is thrown by the <code>setVariable</code> to the terminal. The default 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 @@ -536,6 +564,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 * no knowledge of what application client refers to. @@ -566,10 +619,8 @@ public class ApplicationServlet extends HttpServlet { new OutputStreamWriter(out, "UTF-8"))); outWriter.print("for(;;);[{\"changes\":[], \"meta\" : {" + "\"appError\": {" + "\"caption\":\"" + caption + "\"," - + "\"message\" : \"<br />Please click <a href=\\\"\\\"" - + "onclick=\\\"javascript:window.location.reload()\\\" >" - + "here</a> to restart your application.<br />" - + "You can also click your browser's refresh button.\"" + + "\"message\" : \"<br />You can click your browser's" + + " refresh button to restart your application.\"" + "}}, \"resources\": {}, \"locales\":[]}]"); outWriter.flush(); outWriter.close(); |