| 1 | |
package org.webslinger.servlet; |
| 2 | |
|
| 3 | |
import java.io.BufferedReader; |
| 4 | |
import java.io.IOException; |
| 5 | |
import java.io.InputStream; |
| 6 | |
import java.io.InputStreamReader; |
| 7 | |
import java.io.UnsupportedEncodingException; |
| 8 | |
import java.util.Enumeration; |
| 9 | |
import java.util.HashMap; |
| 10 | |
import java.util.Locale; |
| 11 | |
import java.util.HashSet; |
| 12 | |
import java.util.Map; |
| 13 | |
import java.util.Set; |
| 14 | |
import javax.servlet.ServletInputStream; |
| 15 | |
import javax.servlet.ServletRequest; |
| 16 | |
import javax.servlet.RequestDispatcher; |
| 17 | |
|
| 18 | |
import org.apache.commons.collections.iterators.IteratorEnumeration; |
| 19 | |
|
| 20 | |
public class FakeServletRequest implements ServletRequest { |
| 21 | |
protected final FakeServletContext context; |
| 22 | |
protected final FakeResource resource; |
| 23 | 526 | protected final Map<String, Object> attributes = new HashMap<String, Object>(); |
| 24 | 526 | protected final Map<String, String[]> parameters = new HashMap<String, String[]>(); |
| 25 | |
|
| 26 | |
protected boolean getInputStreamCalled; |
| 27 | |
protected ServletInputStream servletInputStream; |
| 28 | |
protected boolean getReaderCalled; |
| 29 | |
protected BufferedReader bufferedReader; |
| 30 | |
|
| 31 | |
protected String encoding; |
| 32 | |
|
| 33 | |
protected final ServletRequestConfig config; |
| 34 | |
|
| 35 | |
public FakeServletRequest(FakeServletContext context, FakeResource resource) { |
| 36 | 526 | this(context, resource, new ServletRequestConfig()); |
| 37 | 526 | } |
| 38 | |
|
| 39 | 526 | public FakeServletRequest(FakeServletContext context, FakeResource resource, ServletRequestConfig config) { |
| 40 | 526 | this.context = context; |
| 41 | 526 | this.resource = resource; |
| 42 | 526 | this.config = config; |
| 43 | 526 | } |
| 44 | |
|
| 45 | |
public ServletRequestConfig getConfig() { |
| 46 | 0 | return config; |
| 47 | |
} |
| 48 | |
|
| 49 | |
public Object getAttribute(String name) { |
| 50 | 12 | Object value = attributes.get(name); |
| 51 | |
|
| 52 | 12 | return value; |
| 53 | |
} |
| 54 | |
|
| 55 | |
public Enumeration getAttributeNames() { |
| 56 | 0 | return new IteratorEnumeration(attributes.keySet().iterator()); |
| 57 | |
} |
| 58 | |
|
| 59 | |
public void setAttribute(String name, Object value) { |
| 60 | |
|
| 61 | 1041 | attributes.put(name, value); |
| 62 | 1046 | } |
| 63 | |
|
| 64 | |
public void removeAttribute(String name) { |
| 65 | 0 | attributes.remove(name); |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
public String getCharacterEncoding() { |
| 69 | 0 | return encoding; |
| 70 | |
} |
| 71 | |
|
| 72 | |
public int getContentLength() { |
| 73 | 0 | return -1; |
| 74 | |
} |
| 75 | |
|
| 76 | |
public String getContentType() { |
| 77 | 0 | return null; |
| 78 | |
} |
| 79 | |
|
| 80 | |
public ServletInputStream getInputStream() throws IOException { |
| 81 | 0 | if (getReaderCalled) throw new IllegalStateException(); |
| 82 | 0 | if (servletInputStream == null) { |
| 83 | 0 | getInputStreamCalled = true; |
| 84 | 0 | servletInputStream = getRealServletInputStream(); |
| 85 | |
} |
| 86 | 0 | return servletInputStream; |
| 87 | |
} |
| 88 | |
|
| 89 | |
public String getLocalAddr() { |
| 90 | 0 | return config.getLocalAddr(); |
| 91 | |
} |
| 92 | |
|
| 93 | |
public Locale getLocale() { |
| 94 | 0 | return config.getLocale(); |
| 95 | |
} |
| 96 | |
|
| 97 | |
public Enumeration getLocales() { |
| 98 | 0 | return new IteratorEnumeration(config.getLocales().iterator()); |
| 99 | |
} |
| 100 | |
|
| 101 | |
public String getLocalName() { |
| 102 | 0 | return config.getLocalName(); |
| 103 | |
} |
| 104 | |
|
| 105 | |
public int getLocalPort() { |
| 106 | 0 | return config.getLocalPort(); |
| 107 | |
} |
| 108 | |
|
| 109 | |
public String getParameter(String name) { |
| 110 | 0 | String[] values = getParameterValues(name); |
| 111 | 0 | return values == null ? null : values[0]; |
| 112 | |
} |
| 113 | |
|
| 114 | |
public Map getParameterMap() { |
| 115 | 0 | return parameters; |
| 116 | |
} |
| 117 | |
|
| 118 | |
public Enumeration<String> getParameterNames() { |
| 119 | 0 | return new IteratorEnumeration(parameters.keySet().iterator()); |
| 120 | |
} |
| 121 | |
|
| 122 | |
public String[] getParameterValues(String name) { |
| 123 | 0 | return parameters.get(name); |
| 124 | |
} |
| 125 | |
|
| 126 | |
public String getProtocol() { |
| 127 | 0 | return config.getProtocol(); |
| 128 | |
} |
| 129 | |
|
| 130 | |
public BufferedReader getReader() throws IOException { |
| 131 | 0 | if (getInputStreamCalled) throw new IllegalStateException(); |
| 132 | 0 | if (bufferedReader == null) { |
| 133 | 0 | getReaderCalled = true; |
| 134 | 0 | bufferedReader = getRealReader(); |
| 135 | |
} |
| 136 | 0 | return bufferedReader; |
| 137 | |
} |
| 138 | |
|
| 139 | |
|
| 140 | |
public String getRealPath(String path) { |
| 141 | 0 | return context.getRealPath(path); |
| 142 | |
} |
| 143 | |
|
| 144 | |
public String getRemoteAddr() { |
| 145 | 0 | return config.getRemoteAddr(); |
| 146 | |
} |
| 147 | |
|
| 148 | |
public String getRemoteHost() { |
| 149 | 0 | return config.getRemoteHost(); |
| 150 | |
} |
| 151 | |
|
| 152 | |
public int getRemotePort() { |
| 153 | 0 | return config.getRemotePort(); |
| 154 | |
} |
| 155 | |
|
| 156 | |
public RequestDispatcher getRequestDispatcher(String path) { |
| 157 | |
try { |
| 158 | 0 | return context.getRequestDispatcher(context.resolveResource(resource, path)); |
| 159 | 0 | } catch (IOException e){ |
| 160 | 0 | context.log(resource + ":getRequestDispatcher(" + path + ")", e); |
| 161 | 0 | return null; |
| 162 | |
} |
| 163 | |
} |
| 164 | |
|
| 165 | |
public String getScheme() { |
| 166 | 0 | return config.getScheme(); |
| 167 | |
} |
| 168 | |
|
| 169 | |
public String getServerName() { |
| 170 | 0 | return config.getServerName(); |
| 171 | |
} |
| 172 | |
|
| 173 | |
public int getServerPort() { |
| 174 | 0 | return config.getServerPort(); |
| 175 | |
} |
| 176 | |
|
| 177 | |
public boolean isSecure() { |
| 178 | 0 | return config.isSecure(); |
| 179 | |
} |
| 180 | |
|
| 181 | |
public void setCharacterEncoding(String encoding) throws UnsupportedEncodingException { |
| 182 | 0 | this.encoding = encoding; |
| 183 | 0 | } |
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
public void setLocalAddr(String localAddr) { |
| 188 | 0 | config.setLocalAddr(localAddr); |
| 189 | 0 | } |
| 190 | |
|
| 191 | |
public void setLocalName(String localName) { |
| 192 | 0 | config.setLocalName(localName); |
| 193 | 0 | } |
| 194 | |
|
| 195 | |
public void setLocalPort(int localPort) { |
| 196 | 0 | config.setLocalPort(localPort); |
| 197 | 0 | } |
| 198 | |
|
| 199 | |
public void setRemoteAddr(String remoteAddr) { |
| 200 | 0 | config.setRemoteAddr(remoteAddr); |
| 201 | 0 | } |
| 202 | |
|
| 203 | |
public void setRemoteHost(String remoteHost) { |
| 204 | 0 | config.setRemoteHost(remoteHost); |
| 205 | 0 | } |
| 206 | |
|
| 207 | |
public void setRemotePort(int remotePort) { |
| 208 | 0 | config.setRemotePort(remotePort); |
| 209 | 0 | } |
| 210 | |
|
| 211 | |
public void setServerName(String serverName) { |
| 212 | 0 | config.setServerName(serverName); |
| 213 | 0 | } |
| 214 | |
|
| 215 | |
public void setServerPort(int serverPort) { |
| 216 | 0 | config.setServerPort(serverPort); |
| 217 | 0 | } |
| 218 | |
|
| 219 | |
public void setScheme(String scheme) { |
| 220 | 0 | config.setScheme(scheme); |
| 221 | 0 | } |
| 222 | |
|
| 223 | |
public void setIsSecure(boolean isSecure) { |
| 224 | 0 | config.setIsSecure(isSecure); |
| 225 | 0 | } |
| 226 | |
|
| 227 | |
public void setLocale(Locale locale) { |
| 228 | 0 | config.setLocale(locale); |
| 229 | 0 | } |
| 230 | |
|
| 231 | |
public void setProtocol(String protocol) { |
| 232 | 0 | config.setProtocol(protocol); |
| 233 | 0 | } |
| 234 | |
|
| 235 | |
public void addLocale(Locale locale) { |
| 236 | 0 | config.addLocale(locale); |
| 237 | 0 | } |
| 238 | |
|
| 239 | |
public void removeLocale(Locale locale) { |
| 240 | 0 | config.removeLocale(locale); |
| 241 | 0 | } |
| 242 | |
|
| 243 | |
public Map getAttributes() { |
| 244 | 0 | return attributes; |
| 245 | |
} |
| 246 | |
|
| 247 | |
public void setParameter(String name, String value) { |
| 248 | 0 | parameters.put(name, new String[] {value}); |
| 249 | 0 | } |
| 250 | |
|
| 251 | |
public void addParameter(String name, String value) { |
| 252 | 0 | String[] values = parameters.get(name); |
| 253 | 0 | if (values == null) { |
| 254 | 0 | values = new String[] {value}; |
| 255 | |
} else { |
| 256 | 0 | String[] newValues = new String[values.length + 1]; |
| 257 | 0 | System.arraycopy(values, 0, newValues, 0, values.length); |
| 258 | 0 | newValues[values.length] = value; |
| 259 | 0 | values = newValues; |
| 260 | |
} |
| 261 | 0 | parameters.put(name, values); |
| 262 | 0 | } |
| 263 | |
|
| 264 | |
protected ServletInputStream getRealServletInputStream() throws IOException { |
| 265 | 0 | return new ServletInputStream() { |
| 266 | |
public int available() throws IOException { |
| 267 | 0 | return 0; |
| 268 | |
} |
| 269 | |
|
| 270 | |
public int read() throws IOException { |
| 271 | 0 | return -1; |
| 272 | |
} |
| 273 | |
|
| 274 | |
public int read(byte[] b, int off, int len) throws IOException { |
| 275 | 0 | return -1; |
| 276 | |
} |
| 277 | |
}; |
| 278 | |
} |
| 279 | |
|
| 280 | |
protected BufferedReader getRealReader() throws IOException { |
| 281 | 0 | return new BufferedReader(new InputStreamReader(getRealServletInputStream())); |
| 282 | |
} |
| 283 | |
} |