1 16 17 package org.springframework.mock.web; 18 19 import java.io.Serializable ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Hashtable ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 import javax.servlet.ServletContext ; 27 import javax.servlet.http.HttpSession ; 28 import javax.servlet.http.HttpSessionBindingEvent ; 29 import javax.servlet.http.HttpSessionBindingListener ; 30 import javax.servlet.http.HttpSessionContext ; 31 32 import org.springframework.util.Assert; 33 34 45 public class MockHttpSession implements HttpSession { 46 47 public static final String SESSION_COOKIE_NAME = "JSESSION"; 48 49 private static int nextId = 1; 50 51 52 private final String id = Integer.toString(nextId++); 53 54 private final long creationTime = System.currentTimeMillis(); 55 56 private int maxInactiveInterval; 57 58 private long lastAccessedTime = System.currentTimeMillis(); 59 60 private final ServletContext servletContext; 61 62 private final Hashtable attributes = new Hashtable (); 63 64 private boolean invalid = false; 65 66 private boolean isNew = true; 67 68 69 73 public MockHttpSession() { 74 this(null); 75 } 76 77 81 public MockHttpSession(ServletContext servletContext) { 82 this.servletContext = (servletContext != null ? servletContext : new MockServletContext()); 83 } 84 85 86 public long getCreationTime() { 87 return this.creationTime; 88 } 89 90 public String getId() { 91 return this.id; 92 } 93 94 public void access() { 95 this.lastAccessedTime = System.currentTimeMillis(); 96 this.isNew = false; 97 } 98 99 public long getLastAccessedTime() { 100 return this.lastAccessedTime; 101 } 102 103 public ServletContext getServletContext() { 104 return this.servletContext; 105 } 106 107 public void setMaxInactiveInterval(int interval) { 108 this.maxInactiveInterval = interval; 109 } 110 111 public int getMaxInactiveInterval() { 112 return this.maxInactiveInterval; 113 } 114 115 public HttpSessionContext getSessionContext() { 116 throw new UnsupportedOperationException ("getSessionContext"); 117 } 118 119 public Object getAttribute(String name) { 120 Assert.notNull(name, "Attribute name must not be null"); 121 return this.attributes.get(name); 122 } 123 124 public Object getValue(String name) { 125 return getAttribute(name); 126 } 127 128 public Enumeration getAttributeNames() { 129 return this.attributes.keys(); 130 } 131 132 public String [] getValueNames() { 133 return (String []) this.attributes.keySet().toArray(new String [this.attributes.size()]); 134 } 135 136 public void setAttribute(String name, Object value) { 137 Assert.notNull(name, "Attribute name must not be null"); 138 if (value != null) { 139 this.attributes.put(name, value); 140 if (value instanceof HttpSessionBindingListener ) { 141 ((HttpSessionBindingListener ) value).valueBound(new HttpSessionBindingEvent (this, name, value)); 142 } 143 } 144 else { 145 removeAttribute(name); 146 } 147 } 148 149 public void putValue(String name, Object value) { 150 setAttribute(name, value); 151 } 152 153 public void removeAttribute(String name) { 154 Assert.notNull(name, "Attribute name must not be null"); 155 Object value = this.attributes.remove(name); 156 if (value instanceof HttpSessionBindingListener ) { 157 ((HttpSessionBindingListener ) value).valueUnbound(new HttpSessionBindingEvent (this, name, value)); 158 } 159 } 160 161 public void removeValue(String name) { 162 removeAttribute(name); 163 } 164 165 168 public void clearAttributes() { 169 for (Iterator it = this.attributes.entrySet().iterator(); it.hasNext();) { 170 Map.Entry entry = (Map.Entry ) it.next(); 171 String name = (String ) entry.getKey(); 172 Object value = entry.getValue(); 173 it.remove(); 174 if (value instanceof HttpSessionBindingListener ) { 175 ((HttpSessionBindingListener ) value).valueUnbound(new HttpSessionBindingEvent (this, name, value)); 176 } 177 } 178 } 179 180 public void invalidate() { 181 this.invalid = true; 182 clearAttributes(); 183 } 184 185 public boolean isInvalid() { 186 return this.invalid; 187 } 188 189 public void setNew(boolean value) { 190 this.isNew = value; 191 } 192 193 public boolean isNew() { 194 return this.isNew; 195 } 196 197 198 203 public Serializable serializeState() { 204 HashMap state = new HashMap (); 205 for (Iterator it = this.attributes.entrySet().iterator(); it.hasNext();) { 206 Map.Entry entry = (Map.Entry ) it.next(); 207 String name = (String ) entry.getKey(); 208 Object value = entry.getValue(); 209 it.remove(); 210 if (value instanceof Serializable ) { 211 state.put(name, value); 212 } 213 else { 214 if (value instanceof HttpSessionBindingListener ) { 217 ((HttpSessionBindingListener ) value).valueUnbound(new HttpSessionBindingEvent (this, name, value)); 218 } 219 } 220 } 221 return state; 222 } 223 224 229 public void deserializeState(Serializable state) { 230 Assert.isTrue(state instanceof Map , "Serialized state needs to be of type [java.util.Map]"); 231 this.attributes.putAll((Map ) state); 232 } 233 234 } 235 | Popular Tags |