From 2a6a12e777d6ca89ac153a6f990874b137c145c1 Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Mon, 30 Nov 2009 15:28:10 +0000 Subject: [PATCH] fixes #3731, added HttpServletRequestListener and PortletRequestListener which can be used instead of ApplicationContext.TransactionListenr in most use cases svn changeset:10107/svn branch:6.2 --- .../server/AbstractApplicationPortlet.java | 38 +++-- .../server/AbstractApplicationServlet.java | 142 ++++++++++-------- .../server/HttpServletRequestListener.java | 51 +++++++ .../gwt/server/PortletRequestListener.java | 53 +++++++ 4 files changed, 209 insertions(+), 75 deletions(-) create mode 100644 src/com/vaadin/terminal/gwt/server/HttpServletRequestListener.java create mode 100644 src/com/vaadin/terminal/gwt/server/PortletRequestListener.java diff --git a/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java b/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java index b465bf2258..3d165c2d2e 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java @@ -49,7 +49,7 @@ import com.vaadin.ui.Window; /** * TODO Document me! - * + * * @author peholmst */ public abstract class AbstractApplicationPortlet extends GenericPortlet @@ -128,7 +128,7 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet /** * Gets an application property value. - * + * * @param parameterName * the Name or the parameter. * @return String value or null if not found @@ -149,7 +149,7 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet /** * Gets an system property value. - * + * * @param parameterName * the Name or the parameter. * @return String value or null if not found @@ -178,7 +178,7 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet /** * Gets an application or system property value. - * + * * @param parameterName * the Name or the parameter. * @param defaultValue @@ -209,7 +209,7 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet * Return the URL from where static files, e.g. the widgetset and the theme, * are served. In a standard configuration the VAADIN folder inside the * returned folder is what is used for widgetsets and themes. - * + * * @param request * @return The location of static resources (inside which there should be a * VAADIN directory). Does not end with a slash (/). @@ -281,7 +281,7 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet /** * Returns true if the servlet is running in production mode. Production * mode disables all debug facilities. - * + * * @return true if in production mode, false if in debug mode */ public boolean isProductionMode() { @@ -352,6 +352,15 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet updateBrowserProperties(applicationContext.getBrowser(), request); + /* + * Call application requestStart before Application.init() is + * called (bypasses the limitation in TransactionListener) + */ + if (application instanceof PortletRequestListener) { + ((PortletRequestListener) application).onRequestStart( + request, response); + } + /* Start the newly created application */ startApplication(request, application, applicationContext); @@ -365,7 +374,6 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet // TODO Should this happen before or after the transaction // starts? - if (request instanceof RenderRequest) { applicationContext.firePortletRenderRequest(application, (RenderRequest) request, (RenderResponse) response); @@ -455,6 +463,12 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet if (application != null) { ((PortletApplicationContext2) application.getContext()) .endTransaction(application, request); + + if (application instanceof PortletRequestListener) { + ((PortletRequestListener) application).onRequestEnd( + request, response); + } + } } } @@ -736,8 +750,8 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet protected String getWidgetsetURL(String widgetset, PortletRequest request) { return getStaticFilesLocation(request) + "/" + WIDGETSET_DIRECTORY_PATH - + widgetset + "/" - + widgetset + ".nocache.js?" + new Date().getTime(); + + widgetset + "/" + widgetset + ".nocache.js?" + + new Date().getTime(); } protected String getThemeURI(String themeName, PortletRequest request) { @@ -908,7 +922,7 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet /** * Returns the theme for given request/window - * + * * @param request * @param window * @return @@ -959,7 +973,7 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet /** * Get system messages from the current application class - * + * * @return */ protected SystemMessages getSystemMessages() { @@ -1032,7 +1046,7 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet * 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. - * + * * @param request * the Portlet request instance. * @param response diff --git a/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java b/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java index 46a4d8bb5b..fc64ede5bd 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java @@ -45,11 +45,11 @@ import com.vaadin.ui.Window; /** * Abstract implementation of the ApplicationServlet which handles all * communication between the client and the server. - * + * * It is possible to extend this class to provide own functionality but in most * cases this is unnecessary. - * - * + * + * * @author IT Mill Ltd. * @version * @VERSION@ @@ -57,9 +57,11 @@ import com.vaadin.ui.Window; */ @SuppressWarnings("serial") -public abstract class AbstractApplicationServlet extends HttpServlet implements Constants { +public abstract class AbstractApplicationServlet extends HttpServlet implements + Constants { - // TODO Move some (all?) of the constants to a separate interface (shared with portlet) + // TODO Move some (all?) of the constants to a separate interface (shared + // with portlet) /** * The version number of this release. For example "6.2.0". Always in the @@ -109,7 +111,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * If the attribute is present in the request, a html fragment will be * written instead of a whole page. - * + * * It is set to "true" by the {@link ApplicationPortlet} (Portlet 1.0) and * read by {@link AbstractApplicationServlet}. */ @@ -119,7 +121,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * This request attribute forces widgetsets to be loaded from under the * specified base path; e.g shared widgetset for all portlets in a portal. - * + * * It is set by the {@link ApplicationPortlet} (Portlet 1.0) based on * {@link Constants.PORTAL_PARAMETER_VAADIN_RESOURCE_PATH} and read by * {@link AbstractApplicationServlet}. @@ -130,7 +132,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * This request attribute forces widgetset used; e.g for portlets that can * not have different widgetsets. - * + * * It is set by the {@link ApplicationPortlet} (Portlet 1.0) based on * {@link ApplicationPortlet.PORTLET_PARAMETER_WIDGETSET} and read by * {@link AbstractApplicationServlet}. @@ -141,7 +143,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * This request attribute indicates the shared widgetset (e.g. portal-wide * default widgetset). - * + * * It is set by the {@link ApplicationPortlet} (Portlet 1.0) based on * {@link Constants.PORTAL_PARAMETER_VAADIN_WIDGETSET} and read by * {@link AbstractApplicationServlet}. @@ -152,7 +154,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * If set, do not load the default theme but assume that loading it is * handled e.g. by ApplicationPortlet. - * + * * It is set by the {@link ApplicationPortlet} (Portlet 1.0) based on * {@link Constants.PORTAL_PARAMETER_VAADIN_THEME} and read by * {@link AbstractApplicationServlet}. @@ -164,7 +166,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * This request attribute is used to add styles to the main element. E.g * "height:500px" generates a style="height:500px" to the main element, * useful from some embedding situations (e.g portlet include.) - * + * * It is typically set by the {@link ApplicationPortlet} (Portlet 1.0) based * on {@link ApplicationPortlet.PORTLET_PARAMETER_STYLE} and read by * {@link AbstractApplicationServlet}. @@ -182,7 +184,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Called by the servlet container to indicate to a servlet that the servlet * is being placed into service. - * + * * @param servletConfig * the object containing the servlet's configuration and * initialization parameters @@ -251,7 +253,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Gets an application property value. - * + * * @param parameterName * the Name or the parameter. * @return String value or null if not found @@ -272,7 +274,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Gets an system property value. - * + * * @param parameterName * the Name or the parameter. * @return String value or null if not found @@ -301,7 +303,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Gets an application or system property value. - * + * * @param parameterName * the Name or the parameter. * @param defaultValue @@ -331,7 +333,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Returns true if the servlet is running in production mode. Production * mode disables all debug facilities. - * + * * @return true if in production mode, false if in debug mode */ public boolean isProductionMode() { @@ -341,7 +343,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Receives standard HTTP requests from the public service method and * dispatches them. - * + * * @param request * the object that contains the request the client made of the * servlet. @@ -405,6 +407,15 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /* Update browser information from the request */ updateBrowserProperties(webApplicationContext.getBrowser(), request); + /* + * Call application requestStart before Application.init() is called + * (bypasses the limitation in TransactionListener) + */ + if (application instanceof HttpServletRequestListener) { + ((HttpServletRequestListener) application).onRequestStart( + request, response); + } + // Start the newly created application startApplication(request, application, webApplicationContext); @@ -472,6 +483,11 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements if (application != null) { ((WebApplicationContext) application.getContext()) .endTransaction(application, request); + + if (application instanceof HttpServletRequestListener) { + ((HttpServletRequestListener) application).onRequestEnd( + request, response); + } } } @@ -513,7 +529,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * 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. - * + * * @param request * the HTTP request instance. * @param response @@ -575,7 +591,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * Returns the application instance to be used for the request. If an * existing instance is not found a new one is created or null is returned * to indicate that the application is not available. - * + * * @param request * @param requestType * @return @@ -640,7 +656,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Check if the request should create an application if an existing * application is not found. - * + * * @param request * @param requestType * @return true if an application should be created, false otherwise @@ -671,7 +687,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * Gets resource path using different implementations. Required to * supporting different servlet container implementations (application * servers). - * + * * @param servletContext * @param path * the resource path. @@ -700,16 +716,16 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * processing, when a certain URI is requested. The handlers are invoked * before any windows URIs are processed and if a DownloadStream is returned * it is sent to the client. - * + * * @param stream * the download stream. - * + * * @param request * the HTTP request instance. * @param response * the HTTP response to write to. * @throws IOException - * + * * @see com.vaadin.terminal.URIHandler */ @SuppressWarnings("unchecked") @@ -790,7 +806,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * Creates a new application and registers it into WebApplicationContext * (aka session). This is not meant to be overridden. Override * getNewApplication to create the application instance in a custom way. - * + * * @param request * @return * @throws ServletException @@ -831,7 +847,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Returns the theme for given request/window - * + * * @param request * @param window * @return @@ -862,7 +878,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Returns the default theme. Must never return null. - * + * * @return */ public static String getDefaultTheme() { @@ -872,7 +888,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Calls URI handlers for the request. If an URI handler returns a * DownloadStream the stream is passed to the client for downloading. - * + * * @param applicationManager * @param window * @param request @@ -919,7 +935,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * Invalidate session (weird to have session if we're saying * that it's expired, and worse: portal integration will fail * since the session is not created by the portal. - * + * * Session must be invalidated before criticalNotification as it * commits the response. */ @@ -975,7 +991,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Creates a new application for the given request. - * + * * @param request * the HTTP request. * @return A new Application instance. @@ -986,7 +1002,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Starts the application if it is not already running. - * + * * @param request * @param application * @param webApplicationContext @@ -1013,7 +1029,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * Check if this is a request for a static resource and, if it is, serve the * resource to the client. Returns true if a file was served and the request * has been handled, false otherwise. - * + * * @param request * @param response * @return @@ -1045,7 +1061,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Serve resources from VAADIN directory. - * + * * @param request * @param response * @throws IOException @@ -1155,7 +1171,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Get system messages from the current application class - * + * * @return */ protected SystemMessages getSystemMessages() { @@ -1191,10 +1207,10 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * Return the URL from where static files, e.g. the widgetset and the theme, * are served. In a standard configuration the VAADIN folder inside the * returned folder is what is used for widgetsets and themes. - * + * * The returned folder is usually the same as the context path and * independent of the application. - * + * * @param request * @return The location of static resources (should contain the VAADIN * directory). Never ends with a slash (/). @@ -1216,7 +1232,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * The default method to fetch static files location. This method does not * check for request attribute {@value #REQUEST_VAADIN_STATIC_FILE_PATH}. - * + * * @param request * @return */ @@ -1259,7 +1275,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Remove any heading or trailing "what" from the "string". - * + * * @param string * @param what * @return @@ -1278,7 +1294,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Write a redirect response to the main page of the application. - * + * * @param request * @param response * @throws IOException @@ -1308,7 +1324,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * {@link #writeAjaxPageHtmlMainDiv(BufferedWriter, String, String, String)} *
  • {@link #writeAjaxPageHtmlBodyEnd(BufferedWriter)} * - * + * * @param request * the HTTP request. * @param response @@ -1417,9 +1433,9 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * Returns the application class identifier for use in the application CSS * class name in the root DIV. The application CSS class name is of form * "v-app-"+getApplicationCSSClassName(). - * + * * This method should normally not be overridden. - * + * * @return The CSS class name to use in combination with "v-app-". */ protected String getApplicationCSSClassName() { @@ -1433,10 +1449,10 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Get the URI for the application theme. - * + * * A portal-wide default theme is fetched from the portal shared resource * directory (if any), other themes from the portlet. - * + * * @param themeName * @param request * @return @@ -1466,7 +1482,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * Override this method if you want to add some custom html around around * the div element into which the actual vaadin application will be * rendered. - * + * * @param page * @param appId * @param classNames @@ -1482,12 +1498,12 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements } /** - * + * * * Method to write the script part of the page which loads needed vaadin * scripts and themes. *

    * Override this method if you want to add some custom html around scripts. - * + * * @param window * @param themeName * @param application @@ -1629,14 +1645,14 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements } /** - * + * * Method to open the body tag of the html kickstart page. *

    * This method is responsible for closing the head tag and opening the body * tag. *

    * Override this method if you want to add some custom html to the page. - * + * * @param page * @throws IOException */ @@ -1651,7 +1667,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements *

    * Override this method if you want to add some custom html to the header of * the page. - * + * * @param page * @param title * @param themeUri @@ -1683,7 +1699,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements *

    * Override this method if you want to add some custom html to the very * beginning of the page. - * + * * @param page * @throws IOException */ @@ -1703,7 +1719,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * Method to set http request headers for the Vaadin kickstart page. *

    * Override this method if you need to customize http headers of the page. - * + * * @param response */ protected void setAjaxPageHeaders(HttpServletResponse response) { @@ -1724,7 +1740,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Gets the current application URL from request. - * + * * @param request * the HTTP request. * @throws MalformedURLException @@ -1764,7 +1780,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Gets the existing application for given request. Looks for application * instance for given request based on the requested URL. - * + * * @param request * the HTTP request. * @param allowSessionCreation @@ -1824,7 +1840,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Ends the application. - * + * * @param request * the HTTP request. * @param response @@ -1855,7 +1871,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Gets the existing application or create a new one. Get a window within an * application based on the requested URI. - * + * * @param request * the HTTP Request. * @param application @@ -1901,7 +1917,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Returns the path info; note that this _can_ be different than * request.getPathInfo() (e.g application runner). - * + * * @param request * @return */ @@ -1911,7 +1927,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Gets relative location of a theme resource. - * + * * @param theme * the Theme name. * @param resource @@ -1956,7 +1972,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Gets the contained throwable. - * + * * @see com.vaadin.terminal.Terminal.ErrorEvent#getThrowable() */ public Throwable getThrowable() { @@ -1965,7 +1981,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Gets the source ParameterHandler. - * + * * @see com.vaadin.terminal.ParameterHandler.ErrorEvent#getParameterHandler() */ public ParameterHandler getParameterHandler() { @@ -1985,7 +2001,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements private final Throwable throwable; /** - * + * * @param owner * @param throwable */ @@ -1996,7 +2012,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Gets the contained throwable. - * + * * @see com.vaadin.terminal.Terminal.ErrorEvent#getThrowable() */ public Throwable getThrowable() { @@ -2005,7 +2021,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements /** * Gets the source URIHandler. - * + * * @see com.vaadin.terminal.URIHandler.ErrorEvent#getURIHandler() */ public URIHandler getURIHandler() { diff --git a/src/com/vaadin/terminal/gwt/server/HttpServletRequestListener.java b/src/com/vaadin/terminal/gwt/server/HttpServletRequestListener.java new file mode 100644 index 0000000000..f9c76121d9 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/server/HttpServletRequestListener.java @@ -0,0 +1,51 @@ +package com.vaadin.terminal.gwt.server; + +import java.io.Serializable; + +import javax.servlet.Filter; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import com.vaadin.Application; +import com.vaadin.service.ApplicationContext.TransactionListener; +import com.vaadin.terminal.Terminal; + +/** + * {@link Application} that implements this interface gets notified of request + * start and end by terminal. + *

    + * Interface can be used for several helper tasks including: + *

    + *

    + * Alternatives for implementing similar features are are Servlet {@link Filter} + * s and {@link TransactionListener}s in Vaadin. + * + * @since 6.2 + * @see PortletRequestListener + */ +public interface HttpServletRequestListener extends Serializable { + + /** + * This method is called before {@link Terminal} applies the request to + * Application. + * + * @param request + * @param response + */ + public void onRequestStart(HttpServletRequest request, + HttpServletResponse response); + + /** + * This method is called at the end if each request. + * + * @param request + * @param response + */ + public void onRequestEnd(HttpServletRequest request, + HttpServletResponse response); +} \ No newline at end of file diff --git a/src/com/vaadin/terminal/gwt/server/PortletRequestListener.java b/src/com/vaadin/terminal/gwt/server/PortletRequestListener.java new file mode 100644 index 0000000000..f49b4f5bc7 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/server/PortletRequestListener.java @@ -0,0 +1,53 @@ +package com.vaadin.terminal.gwt.server; + +import java.io.Serializable; + +import javax.portlet.PortletRequest; +import javax.portlet.PortletResponse; +import javax.servlet.Filter; + +import com.vaadin.Application; +import com.vaadin.service.ApplicationContext.TransactionListener; +import com.vaadin.terminal.Terminal; + +/** + * {@link Application} that implements this interface gets notified of request + * start and end by terminal. It is quite similar to + * {@link HttpServletRequestListener}, but the parameters are Portlet specific. + * If Application is used deployed as both Servlet and Portlet, one most likely + * needs to implement both. + *

    + * Only JSR 268 style Portlets are supported. + *

    + * Interface can be used for several helper tasks including: + *

    + *

    + * Alternatives for implementing similar features are are Servlet {@link Filter} + * s and {@link TransactionListener}s in Vaadin. + * + * @since 6.2 + * @see HttpServletRequestListener + */ +public interface PortletRequestListener extends Serializable { + + /** + * This method is called before {@link Terminal} applies the request to + * Application. + * + * @param requestData + * the {@link PortletRequest} about to change Application state + */ + public void onRequestStart(PortletRequest request, PortletResponse response); + + /** + * This method is called at the end if each request. + * + * @param requestData + * the {@link PortletRequest} + */ + public void onRequestEnd(PortletRequest request, PortletResponse response); +} \ No newline at end of file -- 2.39.5