From a8f65623b64e64fbf4ccd885f1dfccf500e3dbf3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Tue, 4 Sep 2012 13:05:44 +0300 Subject: [PATCH] Get current request and response instead of explicitly setting (#9402) --- .../server/PortletApplicationContext2.java | 34 ++++++++----------- .../server/ServletApplicationContext.java | 21 +----------- .../src/com/vaadin/server/VaadinPortlet.java | 17 +++++++--- .../src/com/vaadin/server/VaadinServlet.java | 9 +++++ .../vaadin/server/WrappedPortletResponse.java | 8 +++-- 5 files changed, 42 insertions(+), 47 deletions(-) diff --git a/server/src/com/vaadin/server/PortletApplicationContext2.java b/server/src/com/vaadin/server/PortletApplicationContext2.java index c883e9ddfe..3efcc91c08 100644 --- a/server/src/com/vaadin/server/PortletApplicationContext2.java +++ b/server/src/com/vaadin/server/PortletApplicationContext2.java @@ -60,12 +60,8 @@ public class PortletApplicationContext2 extends ApplicationContext { protected Map> portletListeners = new HashMap>(); - protected transient PortletConfig portletConfig; - protected HashMap portletWindowIdToApplicationMap = new HashMap(); - private transient PortletResponse response; - private final Map eventActionDestinationMap = new HashMap(); private final Map eventActionValueMap = new HashMap(); @@ -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); diff --git a/server/src/com/vaadin/server/ServletApplicationContext.java b/server/src/com/vaadin/server/ServletApplicationContext.java index 1ac4a85df3..ecf6202917 100644 --- a/server/src/com/vaadin/server/ServletApplicationContext.java +++ b/server/src/com/vaadin/server/ServletApplicationContext.java @@ -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) diff --git a/server/src/com/vaadin/server/VaadinPortlet.java b/server/src/com/vaadin/server/VaadinPortlet.java index 4d6d7b84f0..199b8e1fc1 100644 --- a/server/src/com/vaadin/server/VaadinPortlet.java +++ b/server/src/com/vaadin/server/VaadinPortlet.java @@ -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 currentResponse = new ThreadLocal(); + @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; } diff --git a/server/src/com/vaadin/server/VaadinServlet.java b/server/src/com/vaadin/server/VaadinServlet.java index a10ad965c5..078263c623 100644 --- a/server/src/com/vaadin/server/VaadinServlet.java +++ b/server/src/com/vaadin/server/VaadinServlet.java @@ -157,6 +157,8 @@ public class VaadinServlet extends HttpServlet implements Constants { } } + private static ThreadLocal currentRequest = new ThreadLocal(); + // 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) { diff --git a/server/src/com/vaadin/server/WrappedPortletResponse.java b/server/src/com/vaadin/server/WrappedPortletResponse.java index f84e3619d2..e3010501b6 100644 --- a/server/src/com/vaadin/server/WrappedPortletResponse.java +++ b/server/src/com/vaadin/server/WrappedPortletResponse.java @@ -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 -- 2.39.5