1 13 package info.magnolia.context; 14 15 import info.magnolia.cms.beans.runtime.File; 16 import info.magnolia.cms.beans.runtime.MultipartForm; 17 import info.magnolia.cms.core.Aggregator; 18 import info.magnolia.cms.core.Content; 19 import info.magnolia.cms.core.HierarchyManager; 20 import info.magnolia.cms.core.search.QueryManager; 21 import info.magnolia.cms.security.AccessManager; 22 import info.magnolia.cms.security.Authenticator; 23 import info.magnolia.cms.security.Security; 24 import info.magnolia.cms.security.User; 25 import info.magnolia.cms.security.UserManager; 26 import info.magnolia.cms.util.DumperUtil; 27 28 import java.util.Enumeration ; 29 import java.util.HashMap ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 33 import javax.jcr.RepositoryException; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpSession ; 36 import javax.servlet.jsp.jstl.core.Config; 37 38 import org.slf4j.Logger; 39 import org.slf4j.LoggerFactory; 40 41 42 46 public class WebContextImpl extends AbstractContext implements WebContext { 47 48 51 private static final long serialVersionUID = 222L; 52 53 56 private static Logger log = LoggerFactory.getLogger(WebContextImpl.class); 57 58 61 private HttpServletRequest request; 62 63 66 public WebContextImpl() { 67 } 68 69 72 public void init(HttpServletRequest request) { 73 this.request = request; 74 } 75 76 80 public User getUser() { 81 if (this.user == null) { 82 if (Authenticator.getSubject(request) == null) { 83 log.debug("JAAS Subject is null, returning Anonymous user"); 84 this.user = Security.getUserManager().getUser(UserManager.ANONYMOUS_USER); 85 } 86 else { 87 this.user = Security.getUserManager().getUser(Authenticator.getSubject(request)); 88 } 89 } 90 return this.user; 91 } 92 93 96 public void setLocale(Locale locale) { 97 super.setLocale(locale); 98 this.setAttribute(Config.FMT_LOCALE + ".session", locale.getLanguage(), Context.SESSION_SCOPE); } 100 101 107 public HierarchyManager getHierarchyManager(String repositoryId, String workspaceId) { 108 HierarchyManager hm = SessionStore.getHierarchyManager(this.request, repositoryId, workspaceId); 109 110 if(this.request.getAttribute("jcr.session. " + repositoryId + ".checked")==null){ 113 this.request.setAttribute("jcr.session. " + repositoryId + ".checked", "true"); 114 try { 115 if(hm.getWorkspace().getSession().hasPendingChanges()){ 116 log.error("the current jcr session has pending changes but shouldn't please set to debug level to see the dumped details"); 117 if(log.isDebugEnabled()){ 118 DumperUtil.dumpChanges(hm); 119 } 120 log.warn("will refresh (cleanup) the session"); 121 hm.getWorkspace().getSession().refresh(false); 122 } 123 } 124 catch (RepositoryException e) { 125 log.error("wasn't able to check pending changes on the session", e); 126 } 127 } 128 return hm; 129 } 130 131 137 public AccessManager getAccessManager(String repositoryId, String workspaceId) { 138 return SessionStore.getAccessManager(this.request, repositoryId, workspaceId); 139 } 140 141 147 public QueryManager getQueryManager(String repositoryId, String workspaceId) { 148 try { 149 return SessionStore.getQueryManager(this.request, repositoryId, workspaceId); 150 } 151 catch (RepositoryException re) { 152 log.error(re.getMessage()); 153 log.debug("Exception caught", re); 154 return null; 155 } 156 } 157 158 162 public Content getActivePage() { 163 return (Content) this.request.getAttribute(Aggregator.ACTPAGE); 164 } 165 166 170 public File getFile() { 171 return (File) this.request.getAttribute(Aggregator.FILE); 172 } 173 174 178 public MultipartForm getPostedForm() { 179 return (MultipartForm) this.request.getAttribute("multipartform"); } 181 182 187 public String getParameter(String name) { 188 return this.request.getParameter(name); 189 } 190 191 195 public Map getParameters() { 196 Map map = new HashMap (); 198 Enumeration paramEnum = this.request.getParameterNames(); 199 while (paramEnum.hasMoreElements()) { 200 String name = (String ) paramEnum.nextElement(); 201 map.put(name, this.request.getParameter(name)); 202 } 203 return map; 204 } 205 206 212 public void setAttribute(String name, Object value, int scope) { 213 switch (scope) { 214 case Context.LOCAL_SCOPE: 215 this.request.setAttribute(name, value); 216 break; 217 case Context.SESSION_SCOPE: 218 219 HttpSession httpsession = request.getSession(false); 220 if (httpsession == null) { 221 log 222 .warn( 223 "Session initialized in oder to setting attribute '{}' to '{}'. You should avoid using session when possible!", 224 name, 225 value); 226 httpsession = request.getSession(true); 227 } 228 229 httpsession.setAttribute(name, value); 230 break; 231 case Context.APPLICATION_SCOPE: 232 MgnlContext.getSystemContext().setAttribute(name, value, Context.APPLICATION_SCOPE); 233 break; 234 default: 235 this.request.setAttribute(name, value); 236 if (log.isDebugEnabled()) { 237 log.debug("Undefined scope, setting attribute [{}] in request scope", name); 238 } 239 } 240 } 241 242 247 public Object getAttribute(String name, int scope) { 248 switch (scope) { 249 case Context.LOCAL_SCOPE: 250 Object obj = this.request.getAttribute(name); 251 if (obj == null) { 252 obj = this.getParameter(name); 253 } 254 return obj; 255 case Context.SESSION_SCOPE: 256 HttpSession httpsession = request.getSession(false); 257 if (httpsession == null) { 258 return null; 259 } 260 return httpsession.getAttribute(name); 261 case Context.APPLICATION_SCOPE: 262 return MgnlContext.getSystemContext().getAttribute(name, Context.APPLICATION_SCOPE); 263 default: 264 log.error("no illegal scope passed"); 265 return null; 266 } 267 } 268 269 272 public void removeAttribute(String name, int scope) { 273 switch (scope) { 274 case Context.LOCAL_SCOPE: 275 this.request.removeAttribute(name); 276 break; 277 case Context.SESSION_SCOPE: 278 HttpSession httpsession = request.getSession(false); 279 if (httpsession != null) { 280 httpsession.removeAttribute(name); 281 } 282 break; 283 case Context.APPLICATION_SCOPE: 284 MgnlContext.getSystemContext().removeAttribute(name, Context.APPLICATION_SCOPE); 285 break; 286 default: 287 log.error("no illegal scope passed"); 288 } 289 } 290 291 294 public final Map getAttributes(int scope) { 295 Map map = new HashMap (); 296 Enumeration keysEnum; 297 switch (scope) { 298 case Context.LOCAL_SCOPE: 299 map.putAll(this.getParameters()); 301 keysEnum = this.request.getAttributeNames(); 303 while (keysEnum.hasMoreElements()) { 304 String key = (String ) keysEnum.nextElement(); 305 Object value = getAttribute(key, scope); 306 map.put(key, value); 307 } 308 return map; 309 case Context.SESSION_SCOPE: 310 HttpSession httpsession = request.getSession(false); 311 if (httpsession == null) { 312 return null; 313 } 314 keysEnum = httpsession.getAttributeNames(); 315 while (keysEnum.hasMoreElements()) { 316 String key = (String ) keysEnum.nextElement(); 317 Object value = getAttribute(key, scope); 318 map.put(key, value); 319 } 320 return map; 321 case Context.APPLICATION_SCOPE: 322 return MgnlContext.getSystemContext().getAttributes(Context.APPLICATION_SCOPE); 323 default: 324 log.error("no illegal scope passed"); 325 return null; 326 } 327 328 } 329 330 334 public HttpServletRequest getRequest() { 335 return this.request; 336 } 337 338 public String getContextPath() { 339 return this.request.getContextPath(); 340 } 341 } 342 | Popular Tags |