From fdb5a384fdde3f46247bc381aa80a5d5fa481ae6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Thu, 20 Sep 2012 10:23:49 +0300 Subject: [PATCH] Clear current instances after all portlet requests (#9505) --- .../src/com/vaadin/server/VaadinPortlet.java | 325 +++++++++--------- 1 file changed, 168 insertions(+), 157 deletions(-) diff --git a/server/src/com/vaadin/server/VaadinPortlet.java b/server/src/com/vaadin/server/VaadinPortlet.java index a156c9b79d..678308485b 100644 --- a/server/src/com/vaadin/server/VaadinPortlet.java +++ b/server/src/com/vaadin/server/VaadinPortlet.java @@ -526,182 +526,193 @@ public class VaadinPortlet extends GenericPortlet implements Constants { CurrentInstance.clearAll(); setCurrent(this); - AbstractApplicationPortletWrapper portletWrapper = new AbstractApplicationPortletWrapper( - this); - - WrappedPortletRequest wrappedRequest = createWrappedRequest(request); - - WrappedPortletResponse wrappedResponse = new WrappedPortletResponse( - response, getVaadinService()); - - getVaadinService().setCurrentInstances(wrappedRequest, wrappedResponse); - - RequestType requestType = getRequestType(wrappedRequest); - - if (requestType == RequestType.UNKNOWN) { - handleUnknownRequest(request, response); - } else if (requestType == RequestType.DUMMY) { - /* - * This dummy page is used by action responses to redirect to, in - * order to prevent the boot strap code from being rendered into - * strange places such as iframes. - */ - ((ResourceResponse) response).setContentType("text/html"); - final OutputStream out = ((ResourceResponse) response) - .getPortletOutputStream(); - final PrintWriter outWriter = new PrintWriter(new BufferedWriter( - new OutputStreamWriter(out, "UTF-8"))); - outWriter.print("dummy page"); - outWriter.close(); - } else if (requestType == RequestType.STATIC_FILE) { - serveStaticResources((ResourceRequest) request, - (ResourceResponse) response); - } else { - VaadinSession application = null; - boolean applicationRunning = false; + try { + AbstractApplicationPortletWrapper portletWrapper = new AbstractApplicationPortletWrapper( + this); - try { - // TODO What about PARAM_UNLOADBURST & redirectToApplication?? + WrappedPortletRequest wrappedRequest = createWrappedRequest(request); - /* Find out which application this request is related to */ - application = getVaadinService().findVaadinSession( - wrappedRequest); - if (application == null) { - return; - } - VaadinSession.setCurrent(application); - request.setAttribute(VaadinSession.class.getName(), application); + WrappedPortletResponse wrappedResponse = new WrappedPortletResponse( + response, getVaadinService()); + + getVaadinService().setCurrentInstances(wrappedRequest, + wrappedResponse); + RequestType requestType = getRequestType(wrappedRequest); + + if (requestType == RequestType.UNKNOWN) { + handleUnknownRequest(request, response); + } else if (requestType == RequestType.DUMMY) { /* - * Get or create an application context and an application - * manager for the session + * This dummy page is used by action responses to redirect to, + * in order to prevent the boot strap code from being rendered + * into strange places such as iframes. */ - VaadinPortletSession applicationContext = (VaadinPortletSession) application; - - PortletCommunicationManager applicationManager = (PortletCommunicationManager) applicationContext - .getApplicationManager(); - - if (requestType == RequestType.CONNECTOR_RESOURCE) { - applicationManager.serveConnectorResource(wrappedRequest, - wrappedResponse); - return; - } else if (requestType == RequestType.HEARTBEAT) { - applicationManager.handleHeartbeatRequest(wrappedRequest, - wrappedResponse, application); - return; - } + ((ResourceResponse) response).setContentType("text/html"); + final OutputStream out = ((ResourceResponse) response) + .getPortletOutputStream(); + final PrintWriter outWriter = new PrintWriter( + new BufferedWriter(new OutputStreamWriter(out, "UTF-8"))); + outWriter.print("dummy page"); + outWriter.close(); + } else if (requestType == RequestType.STATIC_FILE) { + serveStaticResources((ResourceRequest) request, + (ResourceResponse) response); + } else { + VaadinSession application = null; + boolean applicationRunning = false; - /* Update browser information from request */ - applicationContext.getBrowser().updateRequestDetails( - wrappedRequest); + try { + // TODO What about PARAM_UNLOADBURST & + // redirectToApplication?? - applicationRunning = true; + /* Find out which application this request is related to */ + application = getVaadinService().findVaadinSession( + wrappedRequest); + if (application == null) { + return; + } + VaadinSession.setCurrent(application); + request.setAttribute(VaadinSession.class.getName(), + application); - /* Notify listeners */ + /* + * Get or create an application context and an application + * manager for the session + */ + VaadinPortletSession applicationContext = (VaadinPortletSession) application; - // Finds the window within the application - UI uI = null; - application.getLock().lock(); - try { - if (application.isRunning()) { - switch (requestType) { - case RENDER: - case ACTION: - // Both action requests and render requests are ok - // without a UI as they render the initial HTML - // and then do a second request - uI = application.getUIForRequest(wrappedRequest); - break; - case BROWSER_DETAILS: - // Should not try to find a UI here as the - // combined request details might change the UI - break; - case FILE_UPLOAD: - // no window - break; - case APPLICATION_RESOURCE: - // use main window - should not need any window - // UI = application.getUI(); - break; - default: - uI = application.getUIForRequest(wrappedRequest); + PortletCommunicationManager applicationManager = (PortletCommunicationManager) applicationContext + .getApplicationManager(); + + if (requestType == RequestType.CONNECTOR_RESOURCE) { + applicationManager.serveConnectorResource( + wrappedRequest, wrappedResponse); + return; + } else if (requestType == RequestType.HEARTBEAT) { + applicationManager.handleHeartbeatRequest( + wrappedRequest, wrappedResponse, application); + return; + } + + /* Update browser information from request */ + applicationContext.getBrowser().updateRequestDetails( + wrappedRequest); + + applicationRunning = true; + + /* Notify listeners */ + + // Finds the window within the application + UI uI = null; + application.getLock().lock(); + try { + if (application.isRunning()) { + switch (requestType) { + case RENDER: + case ACTION: + // Both action requests and render requests are + // ok + // without a UI as they render the initial HTML + // and then do a second request + uI = application + .getUIForRequest(wrappedRequest); + break; + case BROWSER_DETAILS: + // Should not try to find a UI here as the + // combined request details might change the UI + break; + case FILE_UPLOAD: + // no window + break; + case APPLICATION_RESOURCE: + // use main window - should not need any window + // UI = application.getUI(); + break; + default: + uI = application + .getUIForRequest(wrappedRequest); + } + // if window not found, not a problem - use null } - // if window not found, not a problem - use null + } finally { + application.getLock().unlock(); } - } finally { - application.getLock().unlock(); - } - // TODO Should this happen before or after the transaction - // starts? - if (request instanceof RenderRequest) { - applicationContext.firePortletRenderRequest(uI, - (RenderRequest) request, (RenderResponse) response); - } else if (request instanceof ActionRequest) { - applicationContext.firePortletActionRequest(uI, - (ActionRequest) request, (ActionResponse) response); - } else if (request instanceof EventRequest) { - applicationContext.firePortletEventRequest(uI, - (EventRequest) request, (EventResponse) response); - } else if (request instanceof ResourceRequest) { - applicationContext.firePortletResourceRequest(uI, - (ResourceRequest) request, - (ResourceResponse) response); - } + // TODO Should this happen before or after the transaction + // starts? + if (request instanceof RenderRequest) { + applicationContext.firePortletRenderRequest(uI, + (RenderRequest) request, + (RenderResponse) response); + } else if (request instanceof ActionRequest) { + applicationContext.firePortletActionRequest(uI, + (ActionRequest) request, + (ActionResponse) response); + } else if (request instanceof EventRequest) { + applicationContext.firePortletEventRequest(uI, + (EventRequest) request, + (EventResponse) response); + } else if (request instanceof ResourceRequest) { + applicationContext.firePortletResourceRequest(uI, + (ResourceRequest) request, + (ResourceResponse) response); + } - /* Handle the request */ - if (requestType == RequestType.FILE_UPLOAD) { - // UI is resolved in handleFileUpload by - // PortletCommunicationManager - applicationManager.handleFileUpload(application, - wrappedRequest, wrappedResponse); - return; - } else if (requestType == RequestType.BROWSER_DETAILS) { - applicationManager.handleBrowserDetailsRequest( - wrappedRequest, wrappedResponse, application); - return; - } else if (requestType == RequestType.UIDL) { - // Handles AJAX UIDL requests - applicationManager.handleUidlRequest(wrappedRequest, - wrappedResponse, portletWrapper, uI); - return; - } else { - /* - * Removes the application if it has stopped - */ - if (!application.isRunning()) { - endApplication(request, response, application); + /* Handle the request */ + if (requestType == RequestType.FILE_UPLOAD) { + // UI is resolved in handleFileUpload by + // PortletCommunicationManager + applicationManager.handleFileUpload(application, + wrappedRequest, wrappedResponse); return; - } + } else if (requestType == RequestType.BROWSER_DETAILS) { + applicationManager.handleBrowserDetailsRequest( + wrappedRequest, wrappedResponse, application); + return; + } else if (requestType == RequestType.UIDL) { + // Handles AJAX UIDL requests + applicationManager.handleUidlRequest(wrappedRequest, + wrappedResponse, portletWrapper, uI); + return; + } else { + /* + * Removes the application if it has stopped + */ + if (!application.isRunning()) { + endApplication(request, response, application); + return; + } - handleOtherRequest(wrappedRequest, wrappedResponse, - requestType, application, applicationContext, - applicationManager); - } - } catch (final SessionExpiredException e) { - // TODO Figure out a better way to deal with - // SessionExpiredExceptions - getLogger().finest("A user session has expired"); - } catch (final GeneralSecurityException e) { - // TODO Figure out a better way to deal with - // GeneralSecurityExceptions - getLogger() - .fine("General security exception, the security key was probably incorrect."); - } catch (final Throwable e) { - handleServiceException(wrappedRequest, wrappedResponse, - application, e); - } finally { - - if (applicationRunning) { - application.cleanupInactiveUIs(); - } + handleOtherRequest(wrappedRequest, wrappedResponse, + requestType, application, applicationContext, + applicationManager); + } + } catch (final SessionExpiredException e) { + // TODO Figure out a better way to deal with + // SessionExpiredExceptions + getLogger().finest("A user session has expired"); + } catch (final GeneralSecurityException e) { + // TODO Figure out a better way to deal with + // GeneralSecurityExceptions + getLogger() + .fine("General security exception, the security key was probably incorrect."); + } catch (final Throwable e) { + handleServiceException(wrappedRequest, wrappedResponse, + application, e); + } finally { - CurrentInstance.clearAll(); + if (applicationRunning) { + application.cleanupInactiveUIs(); + } - if (application != null) { - requestTimer.stop(application); + if (application != null) { + requestTimer.stop(application); + } } } + } finally { + CurrentInstance.clearAll(); } } -- 2.39.5