1 16 17 package org.springframework.web.portlet.context; 18 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 23 import javax.portlet.PortletRequest; 24 import javax.portlet.PortletSession; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.springframework.util.Assert; 30 import org.springframework.web.context.request.AbstractRequestAttributes; 31 import org.springframework.web.context.request.RequestAttributes; 32 import org.springframework.web.portlet.util.PortletUtils; 33 34 50 public class PortletRequestAttributes extends AbstractRequestAttributes { 51 52 55 private static final Log logger = LogFactory.getLog(PortletRequestAttributes.class); 56 57 58 private final PortletRequest request; 59 60 private PortletSession session; 61 62 private final Map sessionAttributesToUpdate = new HashMap (); 63 64 private final Map globalSessionAttributesToUpdate = new HashMap (); 65 66 67 71 public PortletRequestAttributes(PortletRequest request) { 72 Assert.notNull(request, "Request must not be null"); 73 this.request = request; 74 this.session = request.getPortletSession(false); 77 } 78 79 80 83 protected final PortletRequest getRequest() { 84 return this.request; 85 } 86 87 91 protected final PortletSession getSession(boolean allowCreate) { 92 try { 93 this.session = this.request.getPortletSession(allowCreate); 94 return this.session; 95 } 96 catch (IllegalStateException ex) { 97 if (this.session == null) { 99 throw ex; 101 } 102 if (allowCreate) { 105 boolean canAskForExistingSession = false; 106 try { 107 this.session = this.request.getPortletSession(false); 108 canAskForExistingSession = true; 109 } 110 catch (IllegalStateException ex2) { 111 } 112 if (canAskForExistingSession) { 113 throw ex; 116 } 117 } 118 return this.session; 122 } 123 } 124 125 126 public Object getAttribute(String name, int scope) { 127 if (scope == SCOPE_REQUEST) { 128 return this.request.getAttribute(name); 129 } 130 else { 131 PortletSession session = getSession(false); 132 if (session != null) { 133 if (scope == SCOPE_GLOBAL_SESSION) { 134 Object value = session.getAttribute(name, PortletSession.APPLICATION_SCOPE); 135 if (value != null) { 136 this.globalSessionAttributesToUpdate.put(name, value); 137 } 138 return value; 139 } 140 else { 141 Object value = session.getAttribute(name); 142 if (value != null) { 143 this.sessionAttributesToUpdate.put(name, value); 144 } 145 return value; 146 } 147 } 148 else { 149 return null; 150 } 151 } 152 } 153 154 public void setAttribute(String name, Object value, int scope) { 155 if (scope == SCOPE_REQUEST) { 156 this.request.setAttribute(name, value); 157 } 158 else { 159 PortletSession session = getSession(true); 160 if (scope == SCOPE_GLOBAL_SESSION) { 161 session.setAttribute(name, value, PortletSession.APPLICATION_SCOPE); 162 this.globalSessionAttributesToUpdate.remove(name); 163 } 164 else { 165 session.setAttribute(name, value); 166 this.sessionAttributesToUpdate.remove(name); 167 } 168 } 169 } 170 171 public void removeAttribute(String name, int scope) { 172 if (scope == SCOPE_REQUEST) { 173 this.request.removeAttribute(name); 174 removeRequestDestructionCallback(name); 175 } 176 else { 177 PortletSession session = getSession(false); 178 if (session != null) { 179 if (scope == SCOPE_GLOBAL_SESSION) { 180 session.removeAttribute(name, PortletSession.APPLICATION_SCOPE); 181 this.globalSessionAttributesToUpdate.remove(name); 182 } 183 else { 184 session.removeAttribute(name); 185 this.sessionAttributesToUpdate.remove(name); 186 } 187 } 188 } 189 } 190 191 public void registerDestructionCallback(String name, Runnable callback, int scope) { 192 if (scope == SCOPE_REQUEST) { 193 registerRequestDestructionCallback(name, callback); 194 } 195 else { 196 registerSessionDestructionCallback(name, callback); 197 } 198 } 199 200 public String getSessionId() { 201 return getSession(true).getId(); 202 } 203 204 public Object getSessionMutex() { 205 return PortletUtils.getSessionMutex(getSession(true)); 206 } 207 208 209 213 protected void updateAccessedSessionAttributes() { 214 PortletSession session = this.request.getPortletSession(false); 215 if (session != null) { 216 for (Iterator it = this.sessionAttributesToUpdate.entrySet().iterator(); it.hasNext();) { 217 Map.Entry entry = (Map.Entry ) it.next(); 218 String name = (String ) entry.getKey(); 219 Object newValue = entry.getValue(); 220 Object oldValue = session.getAttribute(name); 221 if (oldValue == newValue) { 222 session.setAttribute(name, newValue); 223 } 224 } 225 for (Iterator it = this.globalSessionAttributesToUpdate.entrySet().iterator(); it.hasNext();) { 226 Map.Entry entry = (Map.Entry ) it.next(); 227 String name = (String ) entry.getKey(); 228 Object newValue = entry.getValue(); 229 Object oldValue = session.getAttribute(name, PortletSession.APPLICATION_SCOPE); 230 if (oldValue == newValue) { 231 session.setAttribute(name, newValue, PortletSession.APPLICATION_SCOPE); 232 } 233 } 234 } 235 this.sessionAttributesToUpdate.clear(); 236 this.globalSessionAttributesToUpdate.clear(); 237 } 238 239 244 private void registerSessionDestructionCallback(String name, Runnable callback) { 245 if (logger.isWarnEnabled()) { 246 logger.warn("Could not register destruction callback [" + callback + "] for attribute '" + name + 247 "' for session scope because Portlet API 1.0 does not support session attribute callbacks"); 248 } 249 } 250 251 } 252 | Popular Tags |