From: Leif Åstrand Date: Tue, 4 Sep 2012 09:59:28 +0000 (+0300) Subject: Use centralized wrapped session in ApplicationContext (#9402) X-Git-Tag: 7.0.0.beta1~184^2~14 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a27c7ec9cc43f4cfd7333053903f9d8cf64812eb;p=vaadin-framework.git Use centralized wrapped session in ApplicationContext (#9402) --- diff --git a/server/src/com/vaadin/Application.java b/server/src/com/vaadin/Application.java index 36a8db83a1..d2313c0566 100644 --- a/server/src/com/vaadin/Application.java +++ b/server/src/com/vaadin/Application.java @@ -2152,7 +2152,7 @@ public class Application implements Terminal.ErrorListener, Serializable { */ protected int getUidlRequestTimeout() { return configuration.isIdleUICleanupEnabled() ? getContext() - .getMaxInactiveInterval() : -1; + .getSession().getMaxInactiveInterval() : -1; } /** diff --git a/server/src/com/vaadin/server/AbstractCommunicationManager.java b/server/src/com/vaadin/server/AbstractCommunicationManager.java index 02d0b0d791..253542db48 100644 --- a/server/src/com/vaadin/server/AbstractCommunicationManager.java +++ b/server/src/com/vaadin/server/AbstractCommunicationManager.java @@ -518,7 +518,8 @@ public abstract class AbstractCommunicationManager implements Serializable { checkWidgetsetVersion(request); requestThemeName = request.getParameter("theme"); - maxInactiveInterval = request.getSessionMaxInactiveInterval(); + maxInactiveInterval = request.getWrappedSession() + .getMaxInactiveInterval(); // repaint requested or session has timed out and new one is created boolean repaintAll; final OutputStream out; @@ -772,12 +773,13 @@ public abstract class AbstractCommunicationManager implements Serializable { */ protected String getSecurityKey(WrappedRequest request) { String seckey = null; - seckey = (String) request - .getSessionAttribute(ApplicationConstants.UIDL_SECURITY_TOKEN_ID); + WrappedSession session = request.getWrappedSession(); + seckey = (String) session + .getAttribute(ApplicationConstants.UIDL_SECURITY_TOKEN_ID); if (seckey == null) { seckey = UUID.randomUUID().toString(); - request.setSessionAttribute( - ApplicationConstants.UIDL_SECURITY_TOKEN_ID, seckey); + session.setAttribute(ApplicationConstants.UIDL_SECURITY_TOKEN_ID, + seckey); } return seckey; @@ -1559,7 +1561,9 @@ public abstract class AbstractCommunicationManager implements Serializable { // ApplicationServlet has stored the security token in the // session; check that it matched the one sent in the UIDL String sessId = (String) request - .getSessionAttribute(ApplicationConstants.UIDL_SECURITY_TOKEN_ID); + .getWrappedSession() + .getAttribute( + ApplicationConstants.UIDL_SECURITY_TOKEN_ID); if (sessId == null || !sessId.equals(bursts[0])) { throw new InvalidUIDLSecurityKeyException( diff --git a/server/src/com/vaadin/server/ApplicationContext.java b/server/src/com/vaadin/server/ApplicationContext.java index b698ea51c8..7577639a3f 100644 --- a/server/src/com/vaadin/server/ApplicationContext.java +++ b/server/src/com/vaadin/server/ApplicationContext.java @@ -102,6 +102,8 @@ public abstract class ApplicationContext implements HttpSessionBindingListener, private long lastRequestTime = -1; + private transient WrappedSession session; + /** * Adds a transaction listener to this context. The transaction listener is * called before and after each each request related to this session except @@ -300,9 +302,24 @@ public abstract class ApplicationContext implements HttpSessionBindingListener, public abstract File getBaseDirectory(); /** - * Returns the time between requests, in seconds, before this context is - * invalidated. A negative time indicates the context should never timeout. + * Gets the session to which this application context is currently + * associated. + * + * @return the wrapped session for this context + */ + public WrappedSession getSession() { + return session; + } + + /** + * Sets the session to which this application context is currently + * associated. + * + * @param session + * the wrapped session for this context */ - public abstract int getMaxInactiveInterval(); + public void setSession(WrappedSession session) { + this.session = session; + } } \ No newline at end of file diff --git a/server/src/com/vaadin/server/CombinedRequest.java b/server/src/com/vaadin/server/CombinedRequest.java index 0577c0098a..3432cda942 100644 --- a/server/src/com/vaadin/server/CombinedRequest.java +++ b/server/src/com/vaadin/server/CombinedRequest.java @@ -114,18 +114,8 @@ public class CombinedRequest implements WrappedRequest { } @Override - public int getSessionMaxInactiveInterval() { - return secondRequest.getSessionMaxInactiveInterval(); - } - - @Override - public Object getSessionAttribute(String name) { - return secondRequest.getSessionAttribute(name); - } - - @Override - public void setSessionAttribute(String name, Object attribute) { - secondRequest.setSessionAttribute(name, attribute); + public WrappedSession getWrappedSession() { + return secondRequest.getWrappedSession(); } @Override diff --git a/server/src/com/vaadin/server/PortletApplicationContext2.java b/server/src/com/vaadin/server/PortletApplicationContext2.java index cea97bc939..c883e9ddfe 100644 --- a/server/src/com/vaadin/server/PortletApplicationContext2.java +++ b/server/src/com/vaadin/server/PortletApplicationContext2.java @@ -60,7 +60,6 @@ public class PortletApplicationContext2 extends ApplicationContext { protected Map> portletListeners = new HashMap>(); - protected transient PortletSession session; protected transient PortletConfig portletConfig; protected HashMap portletWindowIdToApplicationMap = new HashMap(); @@ -75,6 +74,7 @@ public class PortletApplicationContext2 extends ApplicationContext { @Override public File getBaseDirectory() { + PortletSession session = getPortletSession(); String resultPath = session.getPortletContext().getRealPath("/"); if (resultPath != null) { return new File(resultPath); @@ -128,9 +128,7 @@ public class PortletApplicationContext2 extends ApplicationContext { cx = new PortletApplicationContext2(); session.setAttribute(PortletApplicationContext2.class.getName(), cx); } - if (cx.session == null) { - cx.session = session; - } + cx.setSession(new WrappedPortletSession(session)); return cx; } @@ -152,6 +150,9 @@ public class PortletApplicationContext2 extends ApplicationContext { } public PortletSession getPortletSession() { + WrappedSession wrappedSession = getSession(); + PortletSession session = ((WrappedPortletSession) wrappedSession) + .getPortletSession(); return session; } @@ -403,11 +404,6 @@ public class PortletApplicationContext2 extends ApplicationContext { } } - @Override - public int getMaxInactiveInterval() { - return getPortletSession().getMaxInactiveInterval(); - } - private Logger getLogger() { return Logger.getLogger(PortletApplicationContext2.class.getName()); } diff --git a/server/src/com/vaadin/server/ServletApplicationContext.java b/server/src/com/vaadin/server/ServletApplicationContext.java index a38c523254..1ac4a85df3 100644 --- a/server/src/com/vaadin/server/ServletApplicationContext.java +++ b/server/src/com/vaadin/server/ServletApplicationContext.java @@ -39,7 +39,6 @@ import com.vaadin.Application; @SuppressWarnings("serial") public class ServletApplicationContext extends ApplicationContext { - protected transient HttpSession session; private transient boolean reinitializingSession = false; /** @@ -113,7 +112,7 @@ public class ServletApplicationContext extends ApplicationContext { } // Update the "current session" variable - session = newSession; + setSession(new WrappedHttpSession(newSession)); } /** @@ -123,8 +122,8 @@ public class ServletApplicationContext extends ApplicationContext { */ @Override public File getBaseDirectory() { - final String realPath = VaadinServlet.getResourcePath( - session.getServletContext(), "/"); + final String realPath = VaadinServlet.getResourcePath(getHttpSession() + .getServletContext(), "/"); if (realPath == null) { return null; } @@ -137,7 +136,8 @@ public class ServletApplicationContext extends ApplicationContext { * @return HttpSession this application context resides in. */ public HttpSession getHttpSession() { - return session; + WrappedSession session = getSession(); + return ((WrappedHttpSession) session).getHttpSession(); } /** @@ -155,9 +155,7 @@ public class ServletApplicationContext extends ApplicationContext { cx = new ServletApplicationContext(); session.setAttribute(ServletApplicationContext.class.getName(), cx); } - if (cx.session == null) { - cx.session = session; - } + cx.setSession(new WrappedHttpSession(session)); return cx; } @@ -186,9 +184,4 @@ public class ServletApplicationContext extends ApplicationContext { } return mgr; } - - @Override - public int getMaxInactiveInterval() { - return getHttpSession().getMaxInactiveInterval(); - } } diff --git a/server/src/com/vaadin/server/WrappedHttpServletRequest.java b/server/src/com/vaadin/server/WrappedHttpServletRequest.java index b069235843..9285f92035 100644 --- a/server/src/com/vaadin/server/WrappedHttpServletRequest.java +++ b/server/src/com/vaadin/server/WrappedHttpServletRequest.java @@ -56,18 +56,8 @@ public class WrappedHttpServletRequest extends HttpServletRequestWrapper } @Override - public int getSessionMaxInactiveInterval() { - return getSession().getMaxInactiveInterval(); - } - - @Override - public Object getSessionAttribute(String name) { - return getSession().getAttribute(name); - } - - @Override - public void setSessionAttribute(String name, Object attribute) { - getSession().setAttribute(name, attribute); + public WrappedSession getWrappedSession() { + return new WrappedHttpSession(getSession()); } /** diff --git a/server/src/com/vaadin/server/WrappedHttpSession.java b/server/src/com/vaadin/server/WrappedHttpSession.java new file mode 100644 index 0000000000..1465588e08 --- /dev/null +++ b/server/src/com/vaadin/server/WrappedHttpSession.java @@ -0,0 +1,66 @@ +/* + * Copyright 2011 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 javax.servlet.http.HttpSession; + +/** + * Wrapper for {@link HttpSession}. + * + * @author Vaadin Ltd + * @version @VERSION@ + * @since 7.0.0 + */ +public class WrappedHttpSession implements WrappedSession { + + private final HttpSession session; + + /** + * Creates a new wrapped http session. + * + * @param session + * the http session to wrap. + */ + public WrappedHttpSession(HttpSession session) { + this.session = session; + } + + @Override + public int getMaxInactiveInterval() { + return session.getMaxInactiveInterval(); + } + + @Override + public Object getAttribute(String name) { + return session.getAttribute(name); + } + + @Override + public void setAttribute(String name, Object value) { + session.setAttribute(name, value); + } + + /** + * Gets the wrapped {@link HttpSession}. + * + * @return the wrapped http session + */ + public HttpSession getHttpSession() { + return session; + } + +} diff --git a/server/src/com/vaadin/server/WrappedPortletRequest.java b/server/src/com/vaadin/server/WrappedPortletRequest.java index 47a8e2c358..2d444d86ca 100644 --- a/server/src/com/vaadin/server/WrappedPortletRequest.java +++ b/server/src/com/vaadin/server/WrappedPortletRequest.java @@ -113,18 +113,8 @@ public class WrappedPortletRequest implements WrappedRequest { } @Override - public int getSessionMaxInactiveInterval() { - return request.getPortletSession().getMaxInactiveInterval(); - } - - @Override - public Object getSessionAttribute(String name) { - return request.getPortletSession().getAttribute(name); - } - - @Override - public void setSessionAttribute(String name, Object attribute) { - request.getPortletSession().setAttribute(name, attribute); + public WrappedSession getWrappedSession() { + return new WrappedPortletSession(request.getPortletSession()); } /** diff --git a/server/src/com/vaadin/server/WrappedPortletSession.java b/server/src/com/vaadin/server/WrappedPortletSession.java new file mode 100644 index 0000000000..eb07eb38f6 --- /dev/null +++ b/server/src/com/vaadin/server/WrappedPortletSession.java @@ -0,0 +1,65 @@ +/* + * Copyright 2011 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 javax.portlet.PortletSession; + +/** + * Wrapper for + * + * @author Vaadin Ltd + * @version @VERSION@ + * @since 7.0.0 + */ +public class WrappedPortletSession implements WrappedSession { + + private final PortletSession session; + + /** + * Creates a new wrapped portlet session. + * + * @param session + * the portlet session to wrap. + */ + public WrappedPortletSession(PortletSession session) { + this.session = session; + } + + @Override + public int getMaxInactiveInterval() { + return session.getMaxInactiveInterval(); + } + + @Override + public Object getAttribute(String name) { + return session.getAttribute(name); + } + + @Override + public void setAttribute(String name, Object value) { + session.setAttribute(name, value); + } + + /** + * Gets the wrapped {@link PortletSession}. + * + * @return the wrapped portlet session + */ + public PortletSession getPortletSession() { + return session; + } +} diff --git a/server/src/com/vaadin/server/WrappedRequest.java b/server/src/com/vaadin/server/WrappedRequest.java index 0714f73cad..5d7ece9ef1 100644 --- a/server/src/com/vaadin/server/WrappedRequest.java +++ b/server/src/com/vaadin/server/WrappedRequest.java @@ -32,7 +32,9 @@ import com.vaadin.ui.UI; * A generic request to the server, wrapping a more specific request type, e.g. * HttpServletReqest or PortletRequest. * - * @since 7.0 + * @author Vaadin Ltd + * @version @VERSION@ + * @since 7.0.0 */ public interface WrappedRequest extends Serializable { @@ -158,42 +160,15 @@ public interface WrappedRequest extends Serializable { public String getRequestPathInfo(); /** - * Returns the maximum time interval, in seconds, that the session - * associated with this request will be kept open between client accesses. + * Gets the session associated with this request. * - * @return an integer specifying the number of seconds the session - * associated with this request remains open between client requests + * @see WrappedSession + * @see HttpServletRequest#getSession() + * @see PortletRequest#getPortletSession() * - * @see javax.servlet.http.HttpSession#getMaxInactiveInterval() - * @see javax.portlet.PortletSession#getMaxInactiveInterval() + * @return the wrapped session for this request */ - public int getSessionMaxInactiveInterval(); - - /** - * Gets an attribute from the session associated with this request. - * - * @param name - * the name of the attribute - * @return the attribute value, or null if the attribute is not - * defined in the session - * - * @see javax.servlet.http.HttpSession#getAttribute(String) - * @see javax.portlet.PortletSession#getAttribute(String) - */ - public Object getSessionAttribute(String name); - - /** - * Saves an attribute value in the session associated with this request. - * - * @param name - * the name of the attribute - * @param attribute - * the attribute value - * - * @see javax.servlet.http.HttpSession#setAttribute(String, Object) - * @see javax.portlet.PortletSession#setAttribute(String, Object) - */ - public void setSessionAttribute(String name, Object attribute); + public WrappedSession getWrappedSession(); /** * Returns the MIME type of the body of the request, or null if the type is diff --git a/server/src/com/vaadin/server/WrappedSession.java b/server/src/com/vaadin/server/WrappedSession.java new file mode 100644 index 0000000000..3973c257c8 --- /dev/null +++ b/server/src/com/vaadin/server/WrappedSession.java @@ -0,0 +1,69 @@ +/* + * Copyright 2011 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 javax.portlet.PortletSession; +import javax.servlet.http.HttpSession; + +/** + * A generic session, wrapping a more specific session implementation, e.g. + * {@link HttpSession} or {@link PortletSession}. + * + * + * @author Vaadin Ltd + * @version @VERSION@ + * @since 7.0.0 + */ +public interface WrappedSession { + /** + * Returns the maximum time interval, in seconds, that this session will be + * kept open between client accesses. + * + * @return an integer specifying the number of seconds this session remains + * open between client requests + * + * @see javax.servlet.http.HttpSession#getMaxInactiveInterval() + * @see javax.portlet.PortletSession#getMaxInactiveInterval() + */ + public int getMaxInactiveInterval(); + + /** + * Gets an attribute from this session. + * + * @param name + * the name of the attribute + * @return the attribute value, or null if the attribute is not + * defined in the session + * + * @see javax.servlet.http.HttpSession#getAttribute(String) + * @see javax.portlet.PortletSession#getAttribute(String) + */ + public Object getAttribute(String name); + + /** + * Saves an attribute value in this session. + * + * @param name + * the name of the attribute + * @param value + * the attribute value + * + * @see javax.servlet.http.HttpSession#setAttribute(String, Object) + * @see javax.portlet.PortletSession#setAttribute(String, Object) + */ + public void setAttribute(String name, Object value); +}