1 7 package com.inversoft.junit; 8 9 10 import java.util.ArrayList ; 11 import java.util.Enumeration ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Map ; 16 import javax.servlet.http.Cookie ; 17 18 import com.inversoft.junit.internal.Constants; 19 20 21 27 public class Request { 28 31 protected Authentication authentication; 32 33 36 protected List cookies; 37 38 41 protected Map headers; 42 43 46 protected Map parameters; 47 48 51 protected String testLocation; 52 53 56 protected URL url; 57 58 59 62 public Request() { 63 cookies = new ArrayList (); 64 headers = new HashMap (); 65 parameters = new HashMap (); 66 } 67 68 69 74 public Authentication getAuthentication() { 75 return authentication; 76 } 77 78 83 public void setAuthentication(Authentication authentication) { 84 this.authentication = authentication; 85 } 86 87 93 public void addCookie(Cookie cookie) { 94 cookies.add(cookie); 95 } 96 97 104 public Cookie [] getCookies() { 105 return (Cookie []) cookies.toArray(new Cookie [0]); 106 } 107 108 115 public void addHeader(String name, String value) { 116 headers.put(name, value); 117 } 118 119 125 public String getHeader(String name) { 126 return (String ) headers.get(name); 127 } 128 129 134 public String [] getHeaderNames() { 135 return (String []) headers.keySet().toArray(new String [0]); 136 } 137 138 146 public void addParameter(String key, String value) { 147 148 List list = (List ) parameters.get(key); 149 if (list == null) { 150 list = new ArrayList (); 151 parameters.put(key, list); 152 } 153 154 list.add(value); 155 } 156 157 163 public String getParameter(String key) { 164 List list = (List ) parameters.get(key); 165 if (list == null || list.size() == 0) { 166 return null; 167 } 168 169 return (String ) list.get(0); 170 } 171 172 177 public void removeParameter(String key) { 178 parameters.remove(key); 179 } 180 181 186 public Enumeration getParameterNames() { 187 final Iterator iter = parameters.keySet().iterator(); 188 return new Enumeration () { 189 public boolean hasMoreElements() { 190 return iter.hasNext(); 191 } 192 public java.lang.Object nextElement() { 193 return iter.next(); 194 } 195 }; 196 } 197 198 203 public Map getParameterMap() { 204 Iterator iter = parameters.keySet().iterator(); 205 Object key; 206 Map newMap = new HashMap (); 207 while(iter.hasNext()) { 208 key = iter.next(); 209 newMap.put(key, 210 ((List ) parameters.get(key)).toArray(new String [0])); 211 } 212 213 return newMap; 214 } 215 216 221 public String [] getParameterValues(String name) { 222 List values = (List ) parameters.get(name); 223 if (values == null) { 224 return null; 225 } 226 227 return (String []) values.toArray(new String [0]); 228 } 229 230 233 public void clearParameters() { 234 parameters.clear(); 235 } 236 237 243 public String getTestLocation() { 244 return testLocation; 245 } 246 247 253 public void setTestLocation(String location) { 254 this.testLocation = location; 255 } 256 257 263 public URL getURL() { 264 return url; 265 } 266 267 274 public void setURL(URL url) { 275 this.url = url; 276 277 if (url.getContextPath() != null) { 279 addParameter(Constants.URL_CONTEXT_PARAM, url.getContextPath()); 280 } 281 282 if (url.getPathInfo() != null) { 283 addParameter(Constants.URL_PATHINFO_PARAM, url.getPathInfo()); 284 } 285 286 if (url.getProtocol() != null) { 287 addParameter(Constants.URL_PROTOCOL_PARAM, url.getProtocol()); 288 } 289 290 if (url.getQueryString() != null) { 291 addParameter(Constants.URL_QUERYSTRING_PARAM, url.getQueryString()); 292 } 293 294 if (url.getServerName() != null) { 295 String path = url.getServerName(); 296 297 if (url.getServerPort() != -1) { 298 path += ":" + url.getServerPort(); 299 } 300 301 addParameter(Constants.URL_SERVERNAME_PARAM, path); 302 } 303 304 if (url.getServletPath() != null) { 305 addParameter(Constants.URL_SERVLETPATH_PARAM, url.getServletPath()); 306 } 307 } 308 } 309 | Popular Tags |