1 20 package org.enhydra.barracuda.testbed.servlet; 21 22 import java.io.*; 23 import java.util.*; 24 import java.security.*; 25 import javax.servlet.*; 26 import javax.servlet.http.*; 27 28 import org.enhydra.barracuda.plankton.data.*; 29 30 38 public class MockHttpServletRequest implements HttpServletRequest { 39 40 public String paramStr = null; 41 public List paramList = new ArrayList(); 42 public Map hdrMap = new HashMap(); 43 44 47 public MockHttpServletRequest() { 48 this(null); 49 } 50 51 54 public MockHttpServletRequest(String paramStr) { 55 if (paramStr!=null) setParamStr(paramStr); 56 57 } 58 59 public void setParamStr(String paramStr) { 61 paramList.clear(); 63 if (paramStr==null) return; 64 65 String delimiter = "&"; 67 StringTokenizer st = new StringTokenizer(paramStr, delimiter); 68 while (st.hasMoreTokens()) { 69 String segment = st.nextToken(); 70 StringTokenizer stsub = new StringTokenizer(segment, "="); 71 String key = (String ) stsub.nextElement(); 72 String value = (String ) stsub.nextElement(); 73 paramList.add(new Param(key, value)); 74 } 75 } 76 77 public void setHeader(String hdrName, String hdrValue) { 78 List hdrs = new ArrayList(1); 79 hdrs.add(hdrValue); 80 hdrMap.put(hdrName, hdrs); 81 } 82 83 public void addHeader(String hdrName, String hdrValue) { 84 List hdrs = (List) hdrMap.get(hdrName); 85 if (hdrs==null) { 86 setHeader(hdrName, hdrValue); 87 } else { 88 hdrs.add(hdrValue); 89 } 90 } 91 92 93 94 public String getAuthType() {return null;} 96 public String getContextPath() {return null;} 97 public Cookie[] getCookies() {return null;} 98 public long getDateHeader(String name) {return -1;} 99 100 103 public String getHeader(String hdrName) { 104 List hdrs = (List) hdrMap.get(hdrName); 105 if (hdrs==null) { 106 return null; 107 } else { 108 return (String ) hdrs.get(0); 109 } 110 } 111 112 115 public Enumeration getHeaderNames() { 116 Iterator it = hdrMap.keySet().iterator(); 117 List keys = new ArrayList(); 118 while (it.hasNext()) { 119 keys.add(it.next()); 120 } 121 return new LocalEnumerator(keys); 122 } 123 124 127 public Enumeration getHeaders(String hdrName) { 128 return new LocalEnumerator((List) hdrMap.get(hdrName)); 129 } 130 131 public int getIntHeader(String name) {return -1;} 132 public String getMethod() {return null;} 133 public String getPathInfo() {return null;} 134 public String getPathTranslated() {return null;} 135 public String getQueryString() {return null;} 136 public String getRemoteUser() {return null;} 137 public String getRequestedSessionId() {return null;} 138 public String getRequestURI() {return null;} 139 public String getServletPath() {return null;} 140 public HttpSession getSession() {return null;} 141 public HttpSession getSession(boolean create) {return null;} 142 public Principal getUserPrincipal() {return null;} 143 public boolean isRequestedSessionIdFromCookie() {return false;} 144 public boolean isRequestedSessionIdFromUrl() {return false;} 145 public boolean isRequestedSessionIdFromURL() {return false;} 146 public boolean isRequestedSessionIdValid() {return false;} 147 public boolean isUserInRole(String role) {return false;} 148 149 public Object getAttribute(String name) {return null;} 151 public Enumeration getAttributeNames() {return null;} 152 public void setCharacterEncoding(String s) {} 153 public String getCharacterEncoding() {return null;} 154 public int getContentLength() {return -1;} 155 public String getContentType() {return null;} 156 public ServletInputStream getInputStream() throws IOException {return null;} 157 public Locale getLocale() {return null;} 158 public Enumeration getLocales() {return null;} 159 public Map getParameterMap() {return null;} public StringBuffer getRequestURL() {return null;} 161 162 165 public String getParameter(String name) { 166 Iterator it = paramList.iterator(); 167 while (it.hasNext()) { 168 Param param = (Param) it.next(); 169 if (param.getKey().equals(name)) { 170 return param.getValue(); 171 } 172 } 173 return null; 174 } 175 176 179 public Enumeration getParameterNames() { 180 return new LocalEnumerator(paramList); 181 } 182 183 186 public String [] getParameterValues(String name) { 187 List valueList = new ArrayList(paramList.size()); 188 Iterator it = paramList.iterator(); 189 while (it.hasNext()) { 190 Param param = (Param) it.next(); 191 if (param.getKey().equals(name)) valueList.add(param.getValue()); 192 } 193 int idx = -1; 194 String [] valueArr = new String [valueList.size()]; 195 it = valueList.iterator(); 196 while (it.hasNext()) { 197 valueArr[++idx] = (String ) it.next(); 198 } 199 if (valueArr.length==0) return null; 200 else return valueArr; 201 } 202 public String getProtocol() {return null;} 203 public BufferedReader getReader() throws IOException {return null;} 204 public String getRealPath(String path) {return null;} 205 public String getRemoteAddr() {return null;} 206 public String getRemoteHost() {return null;} 207 public RequestDispatcher getRequestDispatcher(String path) {return null;} 208 public String getScheme() {return null;} 209 public String getServerName() {return null;} 210 public int getServerPort() {return -1;} 211 public boolean isSecure() {return false;} 212 public void removeAttribute(String name) {} 213 public void setAttribute(String name, Object o) {} 214 215 216 217 222 class LocalEnumerator implements Enumeration { 223 List keyList = null; 224 Iterator it = null; 225 226 public LocalEnumerator(List iparamList) { 227 if (iparamList!=null) { 228 keyList = new ArrayList(iparamList.size()); 229 it = iparamList.iterator(); 230 while (it.hasNext()) { 231 Object o = it.next(); 232 if (o instanceof Param) { 233 Param param = (Param) o; 234 if (!keyList.contains(param.getKey())) keyList.add(param.getKey()); 235 } else { 236 keyList.add(o); 237 } 238 } 239 it = keyList.iterator(); 240 } 241 } 242 243 public boolean hasMoreElements() { 244 return (it!=null && it.hasNext()); 245 } 246 247 public Object nextElement() { 248 return it.next(); 249 } 250 251 public String toString() { 252 return super.toString()+" {"+keyList.size()+" items}"; 253 } 254 } 255 256 257 } 258 | Popular Tags |