diff options
author | Artur Signell <artur@vaadin.com> | 2013-03-22 17:00:54 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2013-04-02 07:39:00 +0000 |
commit | d7708c5cc6c0a260598d373abe79024f5e5ee078 (patch) | |
tree | ba2bdb73b9d66a1e661ae40cd22ae7e782bc7b61 /server/src/com/vaadin | |
parent | f23f2533c669e25ae9a49ea8aa0e849576bdca2e (diff) | |
download | vaadin-framework-d7708c5cc6c0a260598d373abe79024f5e5ee078.tar.gz vaadin-framework-d7708c5cc6c0a260598d373abe79024f5e5ee078.zip |
Moved request start/end handling to VaadinService (#11400)
* VaadinService.requestStart/requestEnd is called for all requests, including static resource requests
Change-Id: Ic19f33f069d0b4f4127fdafc7b5c3a2fa1a3b5dc
Diffstat (limited to 'server/src/com/vaadin')
-rw-r--r-- | server/src/com/vaadin/server/RequestTimer.java | 55 | ||||
-rw-r--r-- | server/src/com/vaadin/server/VaadinPortlet.java | 21 | ||||
-rw-r--r-- | server/src/com/vaadin/server/VaadinService.java | 38 | ||||
-rw-r--r-- | server/src/com/vaadin/server/VaadinServlet.java | 106 |
4 files changed, 94 insertions, 126 deletions
diff --git a/server/src/com/vaadin/server/RequestTimer.java b/server/src/com/vaadin/server/RequestTimer.java deleted file mode 100644 index 2f91348ce5..0000000000 --- a/server/src/com/vaadin/server/RequestTimer.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2000-2013 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.server; - -import java.io.Serializable; - -/** - * Times the handling of requests and stores the information as an attribute in - * the request. The timing info is later passed on to the client in the UIDL and - * the client provides JavaScript API for accessing this data from e.g. - * TestBench. - * - * @author Jonatan Kronqvist / Vaadin Ltd - */ -public class RequestTimer implements Serializable { - private long requestStartTime = 0; - - /** - * Starts the timing of a request. This should be called before any - * processing of the request. - */ - public void start() { - requestStartTime = System.nanoTime(); - } - - /** - * Stops the timing of a request. This should be called when all processing - * of a request has finished. - * - * @param context - */ - public void stop(VaadinSession context) { - // Measure and store the total handling time. This data can be - // used in TestBench 3 tests. - long time = (System.nanoTime() - requestStartTime) / 1000000; - - // The timings must be stored in the context, since a new - // RequestTimer is created for every request. - context.setLastRequestDuration(time); - } -} diff --git a/server/src/com/vaadin/server/VaadinPortlet.java b/server/src/com/vaadin/server/VaadinPortlet.java index 2cbd83aaca..924310e9ed 100644 --- a/server/src/com/vaadin/server/VaadinPortlet.java +++ b/server/src/com/vaadin/server/VaadinPortlet.java @@ -386,15 +386,12 @@ public class VaadinPortlet extends GenericPortlet implements Constants, protected void handleRequest(VaadinPortletRequest request, VaadinPortletResponse response) throws PortletException, IOException { - RequestTimer requestTimer = new RequestTimer(); - requestTimer.start(); - - getService().setCurrentInstances(request, response); - - AbstractApplicationPortletWrapper portletWrapper = new AbstractApplicationPortletWrapper( - this); + getService().requestStart(request, response); + VaadinPortletSession vaadinSession = null; try { + AbstractApplicationPortletWrapper portletWrapper = new AbstractApplicationPortletWrapper( + this); RequestType requestType = getRequestType(request); @@ -417,8 +414,6 @@ public class VaadinPortlet extends GenericPortlet implements Constants, serveStaticResources((ResourceRequest) request, (ResourceResponse) response); } else { - VaadinPortletSession vaadinSession = null; - try { vaadinSession = (VaadinPortletSession) getService() .findVaadinSession(request); @@ -466,15 +461,11 @@ public class VaadinPortlet extends GenericPortlet implements Constants, getLogger().finest("A user session has expired"); } catch (final Throwable e) { handleServiceException(request, response, vaadinSession, e); - } finally { - if (vaadinSession != null) { - getService().cleanupSession(vaadinSession); - requestTimer.stop(vaadinSession); - } + } } } finally { - CurrentInstance.clearAll(); + getService().requestEnd(request, response, vaadinSession); } } diff --git a/server/src/com/vaadin/server/VaadinService.java b/server/src/com/vaadin/server/VaadinService.java index d3ac4bb1b7..baa455e69e 100644 --- a/server/src/com/vaadin/server/VaadinService.java +++ b/server/src/com/vaadin/server/VaadinService.java @@ -72,6 +72,8 @@ public abstract class VaadinService implements Serializable { @Deprecated public static final String URL_PARAMETER_CLOSE_APPLICATION = "closeApplication"; + private static final String REQUEST_START_TIME_ATTRIBUTE = "requestStartTime"; + private final DeploymentConfiguration deploymentConfiguration; private final EventRouter eventRouter = new EventRouter(); @@ -1124,4 +1126,40 @@ public abstract class VaadinService implements Serializable { private static final Logger getLogger() { return Logger.getLogger(VaadinService.class.getName()); } + + /** + * Called before the framework starts handling a request + * + * @param request + * The request + * @param response + * The response + */ + public void requestStart(VaadinRequest request, VaadinResponse response) { + setCurrentInstances(request, response); + request.setAttribute(REQUEST_START_TIME_ATTRIBUTE, System.nanoTime()); + } + + /** + * Called after the framework has handled a request and the response has + * been written. + * + * @param request + * The request object + * @param response + * The response object + * @param session + * The session which was used during the request or null if the + * request did not use a session + */ + public void requestEnd(VaadinRequest request, VaadinResponse response, + VaadinSession session) { + if (session != null) { + cleanupSession(session); + long duration = (System.nanoTime() - (Long) request + .getAttribute(REQUEST_START_TIME_ATTRIBUTE)) / 1000000; + session.setLastRequestDuration(duration); + } + CurrentInstance.clearAll(); + } } diff --git a/server/src/com/vaadin/server/VaadinServlet.java b/server/src/com/vaadin/server/VaadinServlet.java index bb5a4a8ab5..c47ce6bcf1 100644 --- a/server/src/com/vaadin/server/VaadinServlet.java +++ b/server/src/com/vaadin/server/VaadinServlet.java @@ -241,73 +241,67 @@ public class VaadinServlet extends HttpServlet implements Constants { private void service(VaadinServletRequest request, VaadinServletResponse response) throws ServletException, IOException { - RequestTimer requestTimer = new RequestTimer(); - requestTimer.start(); - - getService().setCurrentInstances(request, response); - - AbstractApplicationServletWrapper servletWrapper = new AbstractApplicationServletWrapper( - this); - - RequestType requestType = getRequestType(request); - if (!ensureCookiesEnabled(requestType, request, response)) { - return; - } - - if (requestType == RequestType.STATIC_FILE) { - serveStaticResources(request, response); - return; - } - + getService().requestStart(request, response); VaadinSession vaadinSession = null; try { - // Find out the service session this request is related to - vaadinSession = getService().findVaadinSession(request); - if (vaadinSession == null) { + AbstractApplicationServletWrapper servletWrapper = new AbstractApplicationServletWrapper( + this); + + RequestType requestType = getRequestType(request); + if (!ensureCookiesEnabled(requestType, request, response)) { return; } - if (requestType == RequestType.PUBLISHED_FILE) { - new PublishedFileHandler().handleRequest(vaadinSession, - request, response); - return; - } else if (requestType == RequestType.HEARTBEAT) { - new HeartbeatHandler().handleRequest(vaadinSession, request, - response); - return; - } else if (requestType == RequestType.FILE_UPLOAD) { - new FileUploadHandler().handleRequest(vaadinSession, request, - response); - return; - } else if (requestType == RequestType.UIDL) { - new UidlRequestHandler(servletWrapper).handleRequest( - vaadinSession, request, response); - return; - } else if (requestType == RequestType.BROWSER_DETAILS) { - // Browser details - not related to a specific UI - new UIInitHandler().handleRequest(vaadinSession, request, - response); - return; - } else if (vaadinSession.getCommunicationManager() - .handleOtherRequest(request, response)) { + if (requestType == RequestType.STATIC_FILE) { + serveStaticResources(request, response); return; } - // Request not handled by any RequestHandler -> 404 - response.sendError(HttpServletResponse.SC_NOT_FOUND); + try { + // Find out the service session this request is related to + vaadinSession = getService().findVaadinSession(request); + if (vaadinSession == null) { + return; + } + + if (requestType == RequestType.PUBLISHED_FILE) { + new PublishedFileHandler().handleRequest(vaadinSession, + request, response); + return; + } else if (requestType == RequestType.HEARTBEAT) { + new HeartbeatHandler().handleRequest(vaadinSession, + request, response); + return; + } else if (requestType == RequestType.FILE_UPLOAD) { + new FileUploadHandler().handleRequest(vaadinSession, + request, response); + return; + } else if (requestType == RequestType.UIDL) { + new UidlRequestHandler(servletWrapper).handleRequest( + vaadinSession, request, response); + return; + } else if (requestType == RequestType.BROWSER_DETAILS) { + // Browser details - not related to a specific UI + new UIInitHandler().handleRequest(vaadinSession, request, + response); + return; + } else if (vaadinSession.getCommunicationManager() + .handleOtherRequest(request, response)) { + return; + } + + // Request not handled by any RequestHandler -> 404 + response.sendError(HttpServletResponse.SC_NOT_FOUND); - } catch (final SessionExpiredException e) { - // Session has expired, notify user - handleServiceSessionExpired(request, response); - } catch (final Throwable e) { - handleServiceException(request, response, vaadinSession, e); - } finally { - if (vaadinSession != null) { - getService().cleanupSession(vaadinSession); - requestTimer.stop(vaadinSession); + } catch (final SessionExpiredException e) { + // Session has expired, notify user + handleServiceSessionExpired(request, response); + } catch (final Throwable e) { + handleServiceException(request, response, vaadinSession, e); } - CurrentInstance.clearAll(); + } finally { + getService().requestEnd(request, response, vaadinSession); } } |