1 40 41 package org.jahia.services.applications; 42 43 import javax.servlet.ServletContext ; 44 import javax.servlet.http.HttpSession ; 45 import javax.servlet.http.HttpSessionContext ; 46 import java.util.Enumeration ; 47 import java.util.Hashtable ; 48 49 59 public class HttpSessionWrapper implements HttpSession { 60 61 62 private static final org.apache.log4j.Logger logger = 63 org.apache.log4j.Logger.getLogger (HttpSessionWrapper.class); 64 65 private String appName; 66 private String contextID; 67 private HttpSession originalSession; 68 private final static String KEY_PREFIX = "org.jahia."; 69 private final static String KEY_SEPARATOR = "."; 70 private final static boolean debugOutput = false; 71 private boolean inheritJahiaSessionAttributes = false; 72 73 81 public HttpSessionWrapper (HttpSession origSession, 82 String applicationName, 83 String contextID, 84 boolean inheritJahiaSessionAttributes) { 85 if (debugOutput) { 86 logger.debug ("Creating session wrapper for application [" + 87 applicationName + "], contextID [" + contextID + 88 "] from original session object [" + origSession.toString () + "]"); 89 } 90 originalSession = origSession; 91 appName = applicationName; 92 this.contextID = contextID; 93 this.inheritJahiaSessionAttributes = inheritJahiaSessionAttributes; 94 if (originalSession.getAttribute (KEY_PREFIX + appName + KEY_SEPARATOR + contextID) == null) { 95 originalSession.setAttribute (KEY_PREFIX + appName + KEY_SEPARATOR + contextID, 96 new Hashtable ()); 97 } 98 } 99 100 public long getCreationTime () { 101 return originalSession.getCreationTime (); 102 } 103 104 public String getId () { 105 return originalSession.getId (); 106 } 107 108 public long getLastAccessedTime () { 109 return originalSession.getLastAccessedTime (); 110 } 111 112 public void setMaxInactiveInterval (int interval) { 113 originalSession.setMaxInactiveInterval (interval); 114 } 115 116 public int getMaxInactiveInterval () { 117 return originalSession.getMaxInactiveInterval (); 118 } 119 120 123 public HttpSessionContext getSessionContext () { 124 return originalSession.getSessionContext (); 125 } 126 127 public ServletContext getServletContext () { 128 try { 133 Class sessionClass = originalSession.getClass (); 134 java.lang.reflect.Method theMethod = sessionClass.getMethod ("getServletContext", 135 null); 136 if (theMethod == null) { 137 return null; 138 } else { 139 Object returnValue = theMethod.invoke (originalSession, null); 140 if (returnValue instanceof ServletContext ) { 141 return (ServletContext ) returnValue; 142 } else { 143 return null; 144 } 145 } 146 } catch (Throwable t) { 147 return null; 148 } 149 } 150 151 public Object getAttribute (String name) { 152 if (debugOutput) { 153 logger.debug ( 154 "emulatedSession.getAttribute(" + name + ") for app [" + KEY_PREFIX + appName + "]"); 155 } 156 Hashtable appAttributes = (Hashtable ) originalSession.getAttribute ( 157 KEY_PREFIX + appName + KEY_SEPARATOR + contextID); 158 if (debugOutput) { 159 logger.debug ("...result=[" + appAttributes.get (name) + "]"); 160 } 161 Object result = appAttributes.get (name); 164 if (inheritJahiaSessionAttributes) { 165 if (result == null) 166 result = originalSession.getAttribute (name); 167 } 168 return result; 169 } 170 171 174 public Object getValue (String name) { 175 return getAttribute (name); 176 } 177 178 public Enumeration getAttributeNames () { 179 Hashtable appAttributes = (Hashtable ) originalSession.getAttribute ( 180 KEY_PREFIX + appName + KEY_SEPARATOR + contextID); 181 return appAttributes.keys (); 182 } 183 184 187 public String [] getValueNames () { 188 Hashtable appAttributes = (Hashtable ) originalSession.getAttribute ( 189 KEY_PREFIX + appName + KEY_SEPARATOR + contextID); 190 return (String []) appAttributes.keySet ().toArray (); 191 } 192 193 public void setAttribute (String name, 194 Object value) { 195 Hashtable appAttributes = (Hashtable ) originalSession.getAttribute ( 196 KEY_PREFIX + appName + KEY_SEPARATOR + contextID); 197 appAttributes.put (name, value); 198 } 199 200 203 public void putValue (java.lang.String name, 204 java.lang.Object value) { 205 setAttribute (name, value); 206 } 207 208 public void removeAttribute (String name) { 209 Hashtable appAttributes = (Hashtable ) originalSession.getAttribute ( 210 KEY_PREFIX + appName + KEY_SEPARATOR + contextID); 211 appAttributes.remove (name); 212 } 213 214 217 public void removeValue (String name) { 218 removeAttribute (name); 219 } 220 221 public void invalidate () { 222 originalSession.removeAttribute (KEY_PREFIX + appName + KEY_SEPARATOR + contextID); 223 if (originalSession.getAttribute (KEY_PREFIX + appName + KEY_SEPARATOR + contextID) == null) { 225 originalSession.setAttribute (KEY_PREFIX + appName + KEY_SEPARATOR + contextID, 226 new Hashtable ()); 227 } 228 } 229 230 public boolean isNew () { 231 return originalSession.isNew (); 232 } 233 234 } 235 | Popular Tags |