]> source.dussan.org Git - vaadin-framework.git/commitdiff
Get current request and response instead of explicitly setting (#9402)
authorLeif Åstrand <leif@vaadin.com>
Tue, 4 Sep 2012 10:05:44 +0000 (13:05 +0300)
committerLeif Åstrand <leif@vaadin.com>
Wed, 5 Sep 2012 08:39:32 +0000 (11:39 +0300)
server/src/com/vaadin/server/PortletApplicationContext2.java
server/src/com/vaadin/server/ServletApplicationContext.java
server/src/com/vaadin/server/VaadinPortlet.java
server/src/com/vaadin/server/VaadinServlet.java
server/src/com/vaadin/server/WrappedPortletResponse.java

index c883e9ddfebf145c321b696e9fca7ffa4db6c3f6..3efcc91c0812666c2039c4a4da712717ed27bf70 100644 (file)
@@ -60,12 +60,8 @@ public class PortletApplicationContext2 extends ApplicationContext {
 
     protected Map<Application, Set<PortletListener>> portletListeners = new HashMap<Application, Set<PortletListener>>();
 
-    protected transient PortletConfig portletConfig;
-
     protected HashMap<String, Application> portletWindowIdToApplicationMap = new HashMap<String, Application>();
 
-    private transient PortletResponse response;
-
     private final Map<String, QName> eventActionDestinationMap = new HashMap<String, QName>();
     private final Map<String, Serializable> eventActionValueMap = new HashMap<String, Serializable>();
 
@@ -156,12 +152,19 @@ public class PortletApplicationContext2 extends ApplicationContext {
         return session;
     }
 
-    public PortletConfig getPortletConfig() {
-        return portletConfig;
+    private PortletResponse getCurrentResponse() {
+        WrappedPortletResponse currentResponse = VaadinPortlet
+                .getCurrentResponse();
+        if (currentResponse != null) {
+            return currentResponse.getPortletResponse();
+        } else {
+            return null;
+        }
     }
 
-    public void setPortletConfig(PortletConfig config) {
-        portletConfig = config;
+    public PortletConfig getPortletConfig() {
+        return VaadinPortlet.getCurrentResponse().getDeploymentConfiguration()
+                .getPortlet().getPortletConfig();
     }
 
     public void addPortletListener(Application app, PortletListener listener) {
@@ -254,17 +257,6 @@ public class PortletApplicationContext2 extends ApplicationContext {
                 ResourceResponse response, UI uI);
     }
 
-    /**
-     * This is for use by {@link VaadinPortlet} only.
-     * 
-     * TODO cleaner implementation, now "semi-static"!
-     * 
-     * @param mimeResponse
-     */
-    void setResponse(PortletResponse response) {
-        this.response = response;
-    }
-
     /**
      * Creates a new action URL.
      * 
@@ -274,6 +266,7 @@ public class PortletApplicationContext2 extends ApplicationContext {
      */
     public PortletURL generateActionURL(String action) {
         PortletURL url = null;
+        PortletResponse response = getCurrentResponse();
         if (response instanceof MimeResponse) {
             url = ((MimeResponse) response).createActionURL();
             url.setParameter("javax.portlet.action", action);
@@ -306,6 +299,7 @@ public class PortletApplicationContext2 extends ApplicationContext {
      */
     public void sendPortletEvent(UI uI, QName name, Serializable value)
             throws IllegalStateException {
+        PortletResponse response = getCurrentResponse();
         if (response instanceof MimeResponse) {
             String actionKey = "" + System.currentTimeMillis();
             while (eventActionDestinationMap.containsKey(actionKey)) {
@@ -352,6 +346,7 @@ public class PortletApplicationContext2 extends ApplicationContext {
      */
     public void setSharedRenderParameter(UI uI, String name, String value)
             throws IllegalStateException {
+        PortletResponse response = getCurrentResponse();
         if (response instanceof MimeResponse) {
             String actionKey = "" + System.currentTimeMillis();
             while (sharedParameterActionNameMap.containsKey(actionKey)) {
@@ -391,6 +386,7 @@ public class PortletApplicationContext2 extends ApplicationContext {
      */
     public void setPortletMode(UI uI, PortletMode portletMode)
             throws IllegalStateException, PortletModeException {
+        PortletResponse response = getCurrentResponse();
         if (response instanceof MimeResponse) {
             PortletURL url = ((MimeResponse) response).createRenderURL();
             url.setPortletMode(portletMode);
index 1ac4a85df39c6a1b9d16623ea2a9252fc308ca81..ecf6202917360f1b9cbe350adc3a25acd1c450f3 100644 (file)
@@ -20,7 +20,6 @@ import java.io.File;
 import java.util.Enumeration;
 import java.util.HashMap;
 
-import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
 import javax.servlet.http.HttpSessionBindingEvent;
 import javax.servlet.http.HttpSessionBindingListener;
@@ -41,11 +40,6 @@ public class ServletApplicationContext extends ApplicationContext {
 
     private transient boolean reinitializingSession = false;
 
-    /**
-     * Stores a reference to the currentRequest. Null it not inside a request.
-     */
-    private transient Object currentRequest = null;
-
     /**
      * Creates a new Web Application Context.
      * 
@@ -54,18 +48,6 @@ public class ServletApplicationContext extends ApplicationContext {
 
     }
 
-    @Override
-    protected void startTransaction(Application application, Object request) {
-        currentRequest = request;
-        super.startTransaction(application, request);
-    }
-
-    @Override
-    protected void endTransaction(Application application, Object request) {
-        super.endTransaction(application, request);
-        currentRequest = null;
-    }
-
     @Override
     public void valueUnbound(HttpSessionBindingEvent event) {
         if (!reinitializingSession) {
@@ -102,8 +84,7 @@ public class ServletApplicationContext extends ApplicationContext {
         reinitializingSession = false;
 
         // Create a new session
-        HttpSession newSession = ((HttpServletRequest) currentRequest)
-                .getSession();
+        HttpSession newSession = VaadinServlet.getCurrentRequest().getSession();
 
         // Restores all attributes (security key, reference to this context
         // instance)
index 4d6d7b84f0c7919f05329d2269a9630d24ebf67b..199b8e1fc127058fe10e6aa3b28a1cdca55b7b4b 100644 (file)
@@ -293,9 +293,11 @@ public class VaadinPortlet extends GenericPortlet implements Constants {
     // TODO Can we close the application when the portlet is removed? Do we know
     // when the portlet is removed?
 
-    private DeploymentConfiguration deploymentConfiguration;
+    private PortletDeploymentConfiguration deploymentConfiguration;
     private AddonContext addonContext;
 
+    private static ThreadLocal<WrappedPortletResponse> currentResponse = new ThreadLocal<WrappedPortletResponse>();
+
     @Override
     public void init(PortletConfig config) throws PortletException {
         super.init(config);
@@ -324,7 +326,7 @@ public class VaadinPortlet extends GenericPortlet implements Constants {
         addonContext.init();
     }
 
-    protected DeploymentConfiguration createDeploymentConfiguration(
+    protected PortletDeploymentConfiguration createDeploymentConfiguration(
             Properties applicationProperties) {
         return new PortletDeploymentConfiguration(this, applicationProperties);
     }
@@ -393,6 +395,10 @@ public class VaadinPortlet extends GenericPortlet implements Constants {
         return deploymentConfiguration.isProductionMode();
     }
 
+    public static WrappedPortletResponse getCurrentResponse() {
+        return currentResponse.get();
+    }
+
     protected void handleRequest(PortletRequest request,
             PortletResponse response) throws PortletException, IOException {
         RequestTimer requestTimer = new RequestTimer();
@@ -406,6 +412,8 @@ public class VaadinPortlet extends GenericPortlet implements Constants {
         WrappedPortletResponse wrappedResponse = new WrappedPortletResponse(
                 response, getDeploymentConfiguration());
 
+        currentResponse.set(wrappedResponse);
+
         RequestType requestType = getRequestType(wrappedRequest);
 
         if (requestType == RequestType.UNKNOWN) {
@@ -449,8 +457,6 @@ public class VaadinPortlet extends GenericPortlet implements Constants {
                  */
                 PortletApplicationContext2 applicationContext = getApplicationContext(request
                         .getPortletSession());
-                applicationContext.setResponse(response);
-                applicationContext.setPortletConfig(getPortletConfig());
 
                 PortletCommunicationManager applicationManager = applicationContext
                         .getApplicationManager(application);
@@ -604,6 +610,7 @@ public class VaadinPortlet extends GenericPortlet implements Constants {
                     } finally {
                         UI.setCurrent(null);
                         Application.setCurrent(null);
+                        currentResponse.set(null);
 
                         PortletSession session = request
                                 .getPortletSession(false);
@@ -640,7 +647,7 @@ public class VaadinPortlet extends GenericPortlet implements Constants {
 
     }
 
-    protected DeploymentConfiguration getDeploymentConfiguration() {
+    protected PortletDeploymentConfiguration getDeploymentConfiguration() {
         return deploymentConfiguration;
     }
 
index a10ad965c5a6751edd9db07ed6ab9d4ed506b40f..078263c623d628f3ad1cda9e40d6638d60e335e5 100644 (file)
@@ -157,6 +157,8 @@ public class VaadinServlet extends HttpServlet implements Constants {
         }
     }
 
+    private static ThreadLocal<WrappedHttpServletRequest> currentRequest = new ThreadLocal<WrappedHttpServletRequest>();
+
     // TODO Move some (all?) of the constants to a separate interface (shared
     // with portlet)
 
@@ -261,12 +263,18 @@ public class VaadinServlet extends HttpServlet implements Constants {
         service(createWrappedRequest(request), createWrappedResponse(response));
     }
 
+    public static WrappedHttpServletRequest getCurrentRequest() {
+        return currentRequest.get();
+    }
+
     private void service(WrappedHttpServletRequest request,
             WrappedHttpServletResponse response) throws ServletException,
             IOException {
         RequestTimer requestTimer = new RequestTimer();
         requestTimer.start();
 
+        currentRequest.set(request);
+
         AbstractApplicationServletWrapper servletWrapper = new AbstractApplicationServletWrapper(
                 this);
 
@@ -417,6 +425,7 @@ public class VaadinServlet extends HttpServlet implements Constants {
                 } finally {
                     UI.setCurrent(null);
                     Application.setCurrent(null);
+                    currentRequest.set(null);
 
                     HttpSession session = request.getSession(false);
                     if (session != null) {
index f84e3619d2d922d3b605482c2866ebd5310325d0..e3010501b62803b2caf53475019d7d3ab469a67f 100644 (file)
@@ -29,6 +29,8 @@ import javax.portlet.MimeResponse;
 import javax.portlet.PortletResponse;
 import javax.portlet.ResourceResponse;
 
+import com.vaadin.server.VaadinPortlet.PortletDeploymentConfiguration;
+
 /**
  * Wrapper for {@link PortletResponse} and its subclasses.
  * 
@@ -46,7 +48,7 @@ public class WrappedPortletResponse implements WrappedResponse {
     }
 
     private final PortletResponse response;
-    private DeploymentConfiguration deploymentConfiguration;
+    private PortletDeploymentConfiguration deploymentConfiguration;
 
     /**
      * Wraps a portlet response and an associated deployment configuration
@@ -57,7 +59,7 @@ public class WrappedPortletResponse implements WrappedResponse {
      *            the associated deployment configuration
      */
     public WrappedPortletResponse(PortletResponse response,
-            DeploymentConfiguration deploymentConfiguration) {
+            PortletDeploymentConfiguration deploymentConfiguration) {
         this.response = response;
         this.deploymentConfiguration = deploymentConfiguration;
     }
@@ -114,7 +116,7 @@ public class WrappedPortletResponse implements WrappedResponse {
     }
 
     @Override
-    public DeploymentConfiguration getDeploymentConfiguration() {
+    public PortletDeploymentConfiguration getDeploymentConfiguration() {
         return deploymentConfiguration;
     }
 }
\ No newline at end of file