| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.seasar.teeda.core.mock; |
| 17 | |
|
| 18 | |
import java.io.IOException; |
| 19 | |
import java.io.InputStream; |
| 20 | |
import java.net.MalformedURLException; |
| 21 | |
import java.net.URL; |
| 22 | |
import java.util.Enumeration; |
| 23 | |
import java.util.HashMap; |
| 24 | |
import java.util.Iterator; |
| 25 | |
import java.util.Locale; |
| 26 | |
import java.util.Map; |
| 27 | |
import java.util.Set; |
| 28 | |
|
| 29 | |
import javax.faces.FacesException; |
| 30 | |
import javax.servlet.http.Cookie; |
| 31 | |
import javax.servlet.http.HttpSession; |
| 32 | |
|
| 33 | |
import org.seasar.framework.log.Logger; |
| 34 | |
import org.seasar.framework.mock.servlet.MockHttpServletRequest; |
| 35 | |
import org.seasar.framework.mock.servlet.MockHttpServletRequestImpl; |
| 36 | |
import org.seasar.framework.mock.servlet.MockHttpServletResponse; |
| 37 | |
import org.seasar.framework.mock.servlet.MockHttpServletResponseImpl; |
| 38 | |
import org.seasar.framework.mock.servlet.MockServletContext; |
| 39 | |
import org.seasar.framework.mock.servlet.MockServletContextImpl; |
| 40 | |
|
| 41 | |
public class MockExternalContextImpl extends MockExternalContext { |
| 42 | |
|
| 43 | 1 | private static final Logger logger_ = Logger |
| 44 | 1 | .getLogger(MockExternalContextImpl.class); |
| 45 | |
|
| 46 | |
private MockServletContext mockServletContext_; |
| 47 | |
|
| 48 | |
private MockHttpServletRequest mockHttpServletRequest_; |
| 49 | |
|
| 50 | |
private MockHttpServletResponse mockHttpServletResponse_; |
| 51 | |
|
| 52 | |
private Map applicationMap_; |
| 53 | |
|
| 54 | |
private Map requestParameterMap_; |
| 55 | |
|
| 56 | 1761 | private Map requestCookieMap_ = new HashMap(); |
| 57 | |
|
| 58 | 1761 | private Map requestHeaderMap_ = new HashMap(); |
| 59 | |
|
| 60 | |
private Map sessionMap_; |
| 61 | |
|
| 62 | |
private Map requestMap_; |
| 63 | |
|
| 64 | |
private Map requestParameterValuesMap_; |
| 65 | |
|
| 66 | |
private String pathInfo; |
| 67 | |
|
| 68 | 420 | public MockExternalContextImpl() { |
| 69 | 420 | } |
| 70 | |
|
| 71 | |
public MockExternalContextImpl(MockServletContext context, |
| 72 | 1341 | MockHttpServletRequest request, MockHttpServletResponse response) { |
| 73 | 1341 | mockServletContext_ = context; |
| 74 | 1341 | mockHttpServletRequest_ = request; |
| 75 | 1341 | mockHttpServletResponse_ = response; |
| 76 | 1341 | applicationMap_ = new MockApplicationMap(mockServletContext_); |
| 77 | 1341 | } |
| 78 | |
|
| 79 | |
public void addRequestCookieMap(Cookie cookie) { |
| 80 | 0 | requestCookieMap_.put(cookie.getName(), cookie); |
| 81 | 0 | } |
| 82 | |
|
| 83 | |
public void setRequestCookieMap(Map map) { |
| 84 | 0 | requestParameterMap_ = map; |
| 85 | 0 | } |
| 86 | |
|
| 87 | |
public void addRequestParameterMap(String key, String value) { |
| 88 | 0 | requestParameterMap_.put(key, value); |
| 89 | 0 | } |
| 90 | |
|
| 91 | |
public void setRequestParameterMap(Map map) { |
| 92 | 0 | requestParameterMap_ = map; |
| 93 | 0 | } |
| 94 | |
|
| 95 | |
public void dispatch(String requestURI) throws IOException, FacesException { |
| 96 | 1 | if (logger_.isDebugEnabled()) { |
| 97 | 0 | logger_.debug("dispatch called."); |
| 98 | |
} |
| 99 | 1 | } |
| 100 | |
|
| 101 | |
public String encodeActionURL(String sb) { |
| 102 | 13 | return getMockHttpServletResponse().encodeURL(sb); |
| 103 | |
} |
| 104 | |
|
| 105 | |
public String encodeNamespace(String aValue) { |
| 106 | 937 | return aValue; |
| 107 | |
} |
| 108 | |
|
| 109 | |
public String encodeResourceURL(String url) { |
| 110 | 24 | return getMockHttpServletResponse().encodeURL(url); |
| 111 | |
} |
| 112 | |
|
| 113 | |
public Map getApplicationMap() { |
| 114 | 76 | if (applicationMap_ == null) { |
| 115 | 7 | applicationMap_ = new HashMap(); |
| 116 | |
} |
| 117 | 76 | return applicationMap_; |
| 118 | |
} |
| 119 | |
|
| 120 | |
public String getAuthType() { |
| 121 | 0 | return getMockHttpServletRequest().getAuthType(); |
| 122 | |
} |
| 123 | |
|
| 124 | |
public Object getContext() { |
| 125 | 0 | return getMockServletContext(); |
| 126 | |
} |
| 127 | |
|
| 128 | |
public MockServletContext getMockServletContext() { |
| 129 | 411 | if (mockServletContext_ == null) { |
| 130 | 391 | mockServletContext_ = new MockServletContextImpl("/mock-context"); |
| 131 | |
} |
| 132 | 411 | return mockServletContext_; |
| 133 | |
} |
| 134 | |
|
| 135 | |
public void setMockServletContext(MockServletContext mockServletContext) { |
| 136 | 1 | mockServletContext_ = mockServletContext; |
| 137 | 1 | } |
| 138 | |
|
| 139 | |
public String getInitParameter(String name) { |
| 140 | 21 | return getMockServletContext().getInitParameter(name); |
| 141 | |
} |
| 142 | |
|
| 143 | |
public Map getInitParameterMap() { |
| 144 | 0 | return getMockServletContext().getInitParameterMap(); |
| 145 | |
} |
| 146 | |
|
| 147 | |
public String getRemoteUser() { |
| 148 | 0 | return getMockHttpServletRequest().getRemoteUser(); |
| 149 | |
} |
| 150 | |
|
| 151 | |
public Object getRequest() { |
| 152 | 7 | return getMockHttpServletRequest(); |
| 153 | |
} |
| 154 | |
|
| 155 | |
public MockHttpServletRequest getMockHttpServletRequest() { |
| 156 | 531 | if (mockHttpServletRequest_ == null) { |
| 157 | 389 | mockHttpServletRequest_ = new MockHttpServletRequestImpl( |
| 158 | |
getMockServletContext(), "/mock-path.html"); |
| 159 | |
} |
| 160 | 531 | return mockHttpServletRequest_; |
| 161 | |
} |
| 162 | |
|
| 163 | |
public void setMockHttpServletRequest( |
| 164 | |
MockHttpServletRequest mockHttpServletRequest) { |
| 165 | 48 | mockHttpServletRequest_ = mockHttpServletRequest; |
| 166 | 48 | } |
| 167 | |
|
| 168 | |
public String getRequestContextPath() { |
| 169 | 3 | return getMockHttpServletRequest().getContextPath(); |
| 170 | |
} |
| 171 | |
|
| 172 | |
public Map getRequestCookieMap() { |
| 173 | 0 | return requestCookieMap_; |
| 174 | |
} |
| 175 | |
|
| 176 | |
public Map getRequestHeaderMap() { |
| 177 | 5 | return requestHeaderMap_; |
| 178 | |
} |
| 179 | |
|
| 180 | |
public Map getRequestHeaderValuesMap() { |
| 181 | 0 | throw new UnsupportedOperationException(); |
| 182 | |
} |
| 183 | |
|
| 184 | |
public Locale getRequestLocale() { |
| 185 | 0 | return getMockHttpServletRequest().getLocale(); |
| 186 | |
} |
| 187 | |
|
| 188 | |
public Iterator getRequestLocales() { |
| 189 | 7 | return new LocalesIterator(getMockHttpServletRequest().getLocales()); |
| 190 | |
} |
| 191 | |
|
| 192 | |
public Map getRequestMap() { |
| 193 | 227 | if (requestMap_ == null) { |
| 194 | 84 | requestMap_ = new HashMap(); |
| 195 | |
} |
| 196 | 227 | return requestMap_; |
| 197 | |
} |
| 198 | |
|
| 199 | |
public Map getRequestParameterMap() { |
| 200 | 83 | if (requestParameterMap_ == null) { |
| 201 | 50 | requestParameterMap_ = new MockServletRequestParameterMap( |
| 202 | |
getMockHttpServletRequest()); |
| 203 | |
} |
| 204 | 83 | return requestParameterMap_; |
| 205 | |
} |
| 206 | |
|
| 207 | |
public Iterator getRequestParameterNames() { |
| 208 | 0 | throw new UnsupportedOperationException(); |
| 209 | |
} |
| 210 | |
|
| 211 | |
public Map getRequestParameterValuesMap() { |
| 212 | 9 | if (requestParameterValuesMap_ == null) { |
| 213 | 6 | requestParameterValuesMap_ = new MockServletRequestParameterValuesMap( |
| 214 | |
getMockHttpServletRequest()); |
| 215 | |
} |
| 216 | 9 | return requestParameterValuesMap_; |
| 217 | |
} |
| 218 | |
|
| 219 | |
public String getRequestPathInfo() { |
| 220 | 40 | if (pathInfo != null) { |
| 221 | 7 | return pathInfo; |
| 222 | |
} |
| 223 | 33 | return getMockHttpServletRequest().getPathInfo(); |
| 224 | |
} |
| 225 | |
|
| 226 | |
public void setRequestPathInfo(String pathInfo) { |
| 227 | 9 | this.pathInfo = pathInfo; |
| 228 | 9 | } |
| 229 | |
|
| 230 | |
public String getRequestServletPath() { |
| 231 | 12 | return getMockHttpServletRequest().getServletPath(); |
| 232 | |
} |
| 233 | |
|
| 234 | |
public URL getResource(String path) throws MalformedURLException { |
| 235 | 0 | return getMockServletContext().getResource(path); |
| 236 | |
} |
| 237 | |
|
| 238 | |
public InputStream getResourceAsStream(String path) { |
| 239 | 0 | return getMockServletContext().getResourceAsStream(path); |
| 240 | |
} |
| 241 | |
|
| 242 | |
public Set getResourcePaths(String path) { |
| 243 | 0 | return getMockServletContext().getResourcePaths(path); |
| 244 | |
} |
| 245 | |
|
| 246 | |
public Object getResponse() { |
| 247 | 0 | return getMockHttpServletResponse(); |
| 248 | |
} |
| 249 | |
|
| 250 | |
public MockHttpServletResponse getMockHttpServletResponse() { |
| 251 | 405 | if (mockHttpServletResponse_ == null) { |
| 252 | 352 | mockHttpServletResponse_ = new MockHttpServletResponseImpl( |
| 253 | |
getMockHttpServletRequest()); |
| 254 | |
} |
| 255 | 405 | return mockHttpServletResponse_; |
| 256 | |
} |
| 257 | |
|
| 258 | |
public void setMockHttpServletResponse( |
| 259 | |
MockHttpServletResponse mockHttpServletResponse) { |
| 260 | 2 | mockHttpServletResponse_ = mockHttpServletResponse; |
| 261 | 2 | } |
| 262 | |
|
| 263 | |
public Object getSession(boolean create) { |
| 264 | 0 | return getMockHttpServletRequest().getSession(create); |
| 265 | |
} |
| 266 | |
|
| 267 | |
public Map getSessionMap() { |
| 268 | 83 | if (sessionMap_ == null) { |
| 269 | 26 | HttpSession session = getMockHttpServletRequest().getSession(true); |
| 270 | 26 | sessionMap_ = new MockSessionMap(session); |
| 271 | |
} |
| 272 | 83 | return sessionMap_; |
| 273 | |
} |
| 274 | |
|
| 275 | |
public java.security.Principal getUserPrincipal() { |
| 276 | 0 | return getMockHttpServletRequest().getUserPrincipal(); |
| 277 | |
} |
| 278 | |
|
| 279 | |
public boolean isUserInRole(String role) { |
| 280 | 0 | return getMockHttpServletRequest().isUserInRole(role); |
| 281 | |
} |
| 282 | |
|
| 283 | |
public void log(String message) { |
| 284 | 0 | getMockServletContext().log(message); |
| 285 | 0 | } |
| 286 | |
|
| 287 | |
public void log(String message, Throwable throwable) { |
| 288 | 1 | getMockServletContext().log(message, throwable); |
| 289 | 1 | } |
| 290 | |
|
| 291 | |
public void redirect(String requestURI) throws IOException { |
| 292 | 1 | getMockHttpServletResponse().sendRedirect(requestURI); |
| 293 | 1 | } |
| 294 | |
|
| 295 | |
private static class LocalesIterator implements Iterator { |
| 296 | 7 | public LocalesIterator(Enumeration locales) { |
| 297 | 7 | this.locales = locales; |
| 298 | 7 | } |
| 299 | |
|
| 300 | |
private Enumeration locales; |
| 301 | |
|
| 302 | |
public boolean hasNext() { |
| 303 | 10 | return locales.hasMoreElements(); |
| 304 | |
} |
| 305 | |
|
| 306 | |
public Object next() { |
| 307 | 7 | return locales.nextElement(); |
| 308 | |
} |
| 309 | |
|
| 310 | |
public void remove() { |
| 311 | 0 | throw new UnsupportedOperationException(); |
| 312 | |
} |
| 313 | |
|
| 314 | |
} |
| 315 | |
|
| 316 | |
} |