1 5 package com.opensymphony.webwork.dispatcher; 6 7 import javax.servlet.http.HttpServletRequest ; 8 import java.io.Serializable ; 9 import java.util.AbstractMap ; 10 import java.util.Enumeration ; 11 import java.util.HashSet ; 12 import java.util.Set ; 13 14 15 21 public class RequestMap extends AbstractMap implements Serializable { 22 24 Set entries; 25 private HttpServletRequest request; 26 27 29 34 public RequestMap(final HttpServletRequest request) { 35 this.request = request; 36 } 37 38 40 43 public void clear() { 44 Enumeration keys = request.getAttributeNames(); 45 46 while (keys.hasMoreElements()) { 47 String key = (String ) keys.nextElement(); 48 request.removeAttribute(key); 49 } 50 } 51 52 57 public Set entrySet() { 58 if (entries == null) { 59 entries = new HashSet (); 60 61 Enumeration enumeration = request.getAttributeNames(); 62 63 while (enumeration.hasMoreElements()) { 64 final String key = enumeration.nextElement().toString(); 65 final Object value = request.getAttribute(key); 66 entries.add(new Entry() { 67 public boolean equals(Object obj) { 68 Entry entry = (Entry) obj; 69 70 return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue())); 71 } 72 73 public int hashCode() { 74 return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode()); 75 } 76 77 public Object getKey() { 78 return key; 79 } 80 81 public Object getValue() { 82 return value; 83 } 84 85 public Object setValue(Object obj) { 86 request.setAttribute(key.toString(), obj); 87 88 return value; 89 } 90 }); 91 } 92 } 93 94 return entries; 95 } 96 97 103 public Object get(Object key) { 104 return request.getAttribute(key.toString()); 105 } 106 107 114 public Object put(Object key, Object value) { 115 entries = null; 116 request.setAttribute(key.toString(), value); 117 118 return get(key); 119 } 120 121 127 public Object remove(Object key) { 128 entries = null; 129 130 Object value = get(key); 131 request.removeAttribute(key.toString()); 132 133 return value; 134 } 135 } 136 | Popular Tags |