]> source.dussan.org Git - vaadin-framework.git/commitdiff
Moved request start/end handling to VaadinService (#11400)
authorArtur Signell <artur@vaadin.com>
Fri, 22 Mar 2013 15:00:54 +0000 (17:00 +0200)
committerVaadin Code Review <review@vaadin.com>
Tue, 2 Apr 2013 07:39:00 +0000 (07:39 +0000)
* VaadinService.requestStart/requestEnd is called for all requests, including static resource requests

Change-Id: Ic19f33f069d0b4f4127fdafc7b5c3a2fa1a3b5dc

server/src/com/vaadin/server/RequestTimer.java [deleted file]
server/src/com/vaadin/server/VaadinPortlet.java
server/src/com/vaadin/server/VaadinService.java
server/src/com/vaadin/server/VaadinServlet.java

diff --git a/server/src/com/vaadin/server/RequestTimer.java b/server/src/com/vaadin/server/RequestTimer.java
deleted file mode 100644 (file)
index 2f91348..0000000
+++ /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);
-    }
-}
index 2cbd83aacaff1e0bb896c4e61e79b618d3305abd..924310e9edabf01ddc18047c714c2864f5316f8b 100644 (file)
@@ -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);
         }
     }
 
index d3ac4bb1b71a59bd66b4ca2e14bd282f4033bbe8..baa455e69e3b661d748141e173ee7e170a37df66 100644 (file)
@@ -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();
+    }
 }
index bb5a4a8ab58f0f7570c61355e9ae9391d440585b..c47ce6bcf10f6cfdb20c653bdcec3b68f0d5621d 100644 (file)
@@ -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);
         }
     }