]> source.dussan.org Git - vaadin-framework.git/commitdiff
Provide current request/response through VaadinRequest/VaadinResponse (#9642)
authorArtur <artur@vaadin.com>
Tue, 18 Jul 2017 10:59:19 +0000 (13:59 +0300)
committerHenri Sara <henri.sara@gmail.com>
Tue, 18 Jul 2017 10:59:19 +0000 (13:59 +0300)
This greatly improves discoverability of the feature and was made possible
by dropping Java 7 support

server/src/main/java/com/vaadin/server/VaadinRequest.java
server/src/main/java/com/vaadin/server/VaadinResponse.java
server/src/main/java/com/vaadin/server/VaadinService.java
server/src/main/java/com/vaadin/server/VaadinServletRequest.java
server/src/main/java/com/vaadin/server/VaadinServletResponse.java
server/src/main/java/com/vaadin/server/VaadinServletService.java

index 5dc58e769f43021ec76b2afaab863a0d2b7f5bc5..28dda963deebfbaeb777762954c700abb0ed3185 100644 (file)
@@ -32,6 +32,8 @@ import javax.servlet.ServletRequest;
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
 
+import com.vaadin.util.CurrentInstance;
+
 /**
  * A generic request to the server, wrapping a more specific request type, e.g.
  * HttpServletReqest or PortletRequest.
@@ -487,4 +489,17 @@ public interface VaadinRequest extends Serializable {
      */
     public Enumeration<String> getHeaders(String name);
 
+    /**
+     * Gets the currently processed Vaadin request. The current request is
+     * automatically defined when the request is started. The current request
+     * can not be used in e.g. background threads because of the way server
+     * implementations reuse request instances.
+     *
+     * @return the current Vaadin request instance if available, otherwise
+     *         <code>null</code>
+     * @since 8.1
+     */
+    public static VaadinRequest getCurrent() {
+        return CurrentInstance.get(VaadinRequest.class);
+    }
 }
index 90a9e6cbd751f9ba6fc61d3707c2b006dcd760b8..e3422d1d485cbb37d8971198f1e3b2b9878259af 100644 (file)
@@ -28,6 +28,8 @@ import javax.servlet.ServletResponse;
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletResponse;
 
+import com.vaadin.util.CurrentInstance;
+
 /**
  * A generic response from the server, wrapping a more specific response type,
  * e.g. HttpServletResponse or PortletResponse.
@@ -182,4 +184,18 @@ public interface VaadinResponse extends Serializable {
      * @since 7.3.8
      */
     public void setContentLength(int len);
+
+    /**
+     * Gets the currently processed Vaadin response. The current response is
+     * automatically defined when the request is started. The current response
+     * can not be used in e.g. background threads because of the way server
+     * implementations reuse response instances.
+     *
+     * @return the current Vaadin response instance if available, otherwise
+     *         <code>null</code>
+     * @since 8.1
+     */
+    public static VaadinResponse getCurrent() {
+        return CurrentInstance.get(VaadinResponse.class);
+    }
 }
index 524cb8aed877c788c1f8cf004757804557620564..c556cf8c9d6c9c486f955922b6d2e21aea7e713d 100644 (file)
@@ -1010,7 +1010,7 @@ public abstract class VaadinService implements Serializable {
      * @see #setCurrentInstances(VaadinRequest, VaadinResponse)
      */
     public static VaadinRequest getCurrentRequest() {
-        return CurrentInstance.get(VaadinRequest.class);
+        return VaadinRequest.getCurrent();
     }
 
     /**
@@ -1025,7 +1025,7 @@ public abstract class VaadinService implements Serializable {
      * @see #setCurrentInstances(VaadinRequest, VaadinResponse)
      */
     public static VaadinResponse getCurrentResponse() {
-        return CurrentInstance.get(VaadinResponse.class);
+        return VaadinResponse.getCurrent();
     }
 
     /**
@@ -1320,7 +1320,8 @@ public abstract class VaadinService implements Serializable {
      */
     private int getUidlRequestTimeout(VaadinSession session) {
         return getDeploymentConfiguration().isCloseIdleSessions()
-                ? session.getSession().getMaxInactiveInterval() : -1;
+                ? session.getSession().getMaxInactiveInterval()
+                : -1;
     }
 
     /**
@@ -1353,9 +1354,10 @@ public abstract class VaadinService implements Serializable {
     /**
      * Returns whether the given session is active or whether it can be closed.
      * <p>
-     * A session is active if and only if its {@link VaadinSession#getState()} returns {@link State#OPEN}
-     * and {@link #getUidlRequestTimeout(VaadinSession) getUidlRequestTimeout}
-     * is negative or has not yet expired.
+     * A session is active if and only if its {@link VaadinSession#getState()}
+     * returns {@link State#OPEN} and
+     * {@link #getUidlRequestTimeout(VaadinSession) getUidlRequestTimeout} is
+     * negative or has not yet expired.
      *
      * @param session
      *            The session whose status to check
@@ -1458,8 +1460,8 @@ public abstract class VaadinService implements Serializable {
      * <p>
      * The framework collects filters from the {@link SessionInitEvent} where
      * session init listeners can add them. This method is called with the
-     * combined list to optionally modify it, and the result is then stored
-     * by the caller as the final list to use.
+     * combined list to optionally modify it, and the result is then stored by
+     * the caller as the final list to use.
      * <p>
      * The filters are called in the order the session init listeners are
      * called, which is undefined. If you need a specific order, you can
index a01e4b7e3cb9e6b30de7a1f0db374368b54db110..9d166dbf8f6096250f34cde7a371c2d4aa10acc2 100644 (file)
@@ -76,4 +76,25 @@ public class VaadinServletRequest extends HttpServletRequestWrapper
     public VaadinServletService getService() {
         return vaadinService;
     }
+
+    /**
+     * Gets the currently processed Vaadin servlet request. The current request
+     * is automatically defined when the request is started. The current request
+     * can not be used in e.g. background threads because of the way server
+     * implementations reuse request instances.
+     *
+     *
+     * @return the current Vaadin servlet request instance if available,
+     *         otherwise <code>null</code>
+     * @since 8.1
+     */
+    public static VaadinServletRequest getCurrent() {
+        VaadinRequest currentRequest = VaadinRequest.getCurrent();
+        if (currentRequest instanceof VaadinServletRequest) {
+            return (VaadinServletRequest) currentRequest;
+        } else {
+            return null;
+        }
+    }
+
 }
index 7513dd4d9755f1f283e1ff07b9f4dceb68e675e7..6c37c7b440c0cbe62dcc952c0f8374822ed42a0d 100644 (file)
@@ -81,4 +81,24 @@ public class VaadinServletResponse extends HttpServletResponseWrapper
     public VaadinServletService getService() {
         return vaadinService;
     }
+
+    /**
+     * Gets the currently processed Vaadin servlet response. The current
+     * response is automatically defined when the request is started. The
+     * current response can not be used in e.g. background threads because of
+     * the way server implementations reuse response instances.
+     *
+     * @return the current Vaadin servlet response instance if available,
+     *         otherwise <code>null</code>
+     * @since 8.1
+     */
+    public static VaadinServletResponse getCurrent() {
+        VaadinResponse currentResponse = VaadinResponse.getCurrent();
+        if (currentResponse instanceof VaadinServletResponse) {
+            return (VaadinServletResponse) currentResponse;
+        } else {
+            return null;
+        }
+    }
+
 }
index 90328e78d96fafdf63b1c945b7c16b0fdadf5920..4675f3734041333f9c65f6d889c76a94bf91c72f 100644 (file)
@@ -209,16 +209,11 @@ public class VaadinServletService extends VaadinService {
     }
 
     public static HttpServletRequest getCurrentServletRequest() {
-        VaadinRequest currentRequest = VaadinService.getCurrentRequest();
-        if (currentRequest instanceof VaadinServletRequest) {
-            return (VaadinServletRequest) currentRequest;
-        } else {
-            return null;
-        }
+        return VaadinServletRequest.getCurrent();
     }
 
     public static VaadinServletResponse getCurrentResponse() {
-        return (VaadinServletResponse) VaadinService.getCurrentResponse();
+        return VaadinServletResponse.getCurrent();
     }
 
     @Override