1 18 19 20 package org.apache.struts.mock; 21 22 23 import java.util.Collections ; 24 import java.util.Enumeration ; 25 import java.util.HashMap ; 26 27 import javax.servlet.Servlet ; 28 import javax.servlet.ServletConfig ; 29 import javax.servlet.ServletContext ; 30 import javax.servlet.ServletRequest ; 31 import javax.servlet.ServletResponse ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpSession ; 34 import javax.servlet.jsp.JspWriter ; 35 import javax.servlet.jsp.PageContext ; 36 import javax.servlet.jsp.tagext.BodyContent ; 37 38 39 55 56 public class MockPageContext extends PageContext { 57 58 59 60 62 63 public MockPageContext() { 64 super(); 65 } 66 67 68 public MockPageContext(ServletConfig config, 69 ServletRequest request, 70 ServletResponse response) { 71 super(); 72 setValues(config, request, response); 73 } 74 75 76 78 79 protected ServletContext application = null; 80 protected HashMap attributes = new HashMap (); protected ServletConfig config = null; 82 protected ServletRequest request = null; 83 protected ServletResponse response = null; 84 protected HttpSession session = null; 85 86 87 89 90 public void setValues(ServletConfig config, 91 ServletRequest request, 92 ServletResponse response) { 93 this.config = config; 94 if (config != null) { 95 this.application = config.getServletContext(); 96 } else { 97 this.application = null; 98 } 99 this.request = request; 100 this.response = response; 101 if (request != null) { 102 session = ((HttpServletRequest ) request).getSession(false); 103 } else { 104 this.session = null; 105 } 106 } 107 108 109 110 112 113 public Object findAttribute(String name) { 114 Object value = getAttribute(name, PageContext.PAGE_SCOPE); 115 if (value == null) { 116 value = getAttribute(name, PageContext.REQUEST_SCOPE); 117 } 118 if (value == null) { 119 value = getAttribute(name, PageContext.SESSION_SCOPE); 120 } 121 if (value == null) { 122 value = getAttribute(name, PageContext.APPLICATION_SCOPE); 123 } 124 return (value); 125 } 126 127 128 public void forward(String path) { 129 throw new UnsupportedOperationException (); 130 } 131 132 133 public Object getAttribute(String name) { 134 return (getAttribute(name, PageContext.PAGE_SCOPE)); 135 } 136 137 138 public Object getAttribute(String name, int scope) { 139 if (scope == PageContext.PAGE_SCOPE) { 140 return (attributes.get(name)); 141 } else if (scope == PageContext.REQUEST_SCOPE) { 142 if (request != null) { 143 return (request.getAttribute(name)); 144 } else { 145 return (null); 146 } 147 } else if (scope == PageContext.SESSION_SCOPE) { 148 if (session != null) { 149 return (session.getAttribute(name)); 150 } else { 151 return (null); 152 } 153 } else if (scope == PageContext.APPLICATION_SCOPE) { 154 if (application != null) { 155 return (application.getAttribute(name)); 156 } else { 157 return (null); 158 } 159 } else { 160 throw new IllegalArgumentException ("Invalid scope " + scope); 161 } 162 } 163 164 165 public Enumeration getAttributeNamesInScope(int scope) { 166 if (scope == PageContext.PAGE_SCOPE) { 167 return (new MockEnumeration(attributes.keySet().iterator())); 168 } else if (scope == PageContext.REQUEST_SCOPE) { 169 if (request != null) { 170 return (request.getAttributeNames()); 171 } else { 172 return 173 (new MockEnumeration(Collections.EMPTY_LIST.iterator())); 174 } 175 } else if (scope == PageContext.SESSION_SCOPE) { 176 if (session != null) { 177 return (session.getAttributeNames()); 178 } else { 179 return 180 (new MockEnumeration(Collections.EMPTY_LIST.iterator())); 181 } 182 } else if (scope == PageContext.APPLICATION_SCOPE) { 183 if (application != null) { 184 return (application.getAttributeNames()); 185 } else { 186 return 187 (new MockEnumeration(Collections.EMPTY_LIST.iterator())); 188 } 189 } else { 190 throw new IllegalArgumentException ("Invalid scope " + scope); 191 } 192 } 193 194 195 public int getAttributesScope(String name) { 196 if (attributes.get(name) != null) { 197 return (PageContext.PAGE_SCOPE); 198 } else if ((request != null) && 199 (request.getAttribute(name) != null)) { 200 return (PageContext.REQUEST_SCOPE); 201 } else if ((session != null) && 202 (session.getAttribute(name) != null)) { 203 return (PageContext.SESSION_SCOPE); 204 } else if ((application != null) && 205 (application.getAttribute(name) != null)) { 206 return (PageContext.APPLICATION_SCOPE); 207 } else { 208 return (0); 209 } 210 } 211 212 213 public Exception getException() { 214 throw new UnsupportedOperationException (); 215 } 216 217 218 public JspWriter getOut() { 219 throw new UnsupportedOperationException (); 220 } 221 222 223 public Object getPage() { 224 throw new UnsupportedOperationException (); 225 } 226 227 228 public ServletRequest getRequest() { 229 return (this.request); 230 } 231 232 233 public ServletResponse getResponse() { 234 return (this.response); 235 } 236 237 238 public ServletConfig getServletConfig() { 239 return (this.config); 240 } 241 242 243 public ServletContext getServletContext() { 244 return (this.application); 245 } 246 247 248 public HttpSession getSession() { 249 return (this.session); 250 } 251 252 253 public void handlePageException(Exception e) { 254 throw new UnsupportedOperationException (); 255 } 256 257 258 public void handlePageException(Throwable t) { 259 throw new UnsupportedOperationException (); 260 } 261 262 263 public void include(String path) { 264 throw new UnsupportedOperationException (); 265 } 266 267 268 public void initialize(Servlet servlet, ServletRequest request, 269 ServletResponse response, String errorPageURL, 270 boolean needsSession, int bufferSize, 271 boolean autoFlush) { 272 throw new UnsupportedOperationException (); 273 } 274 275 276 public JspWriter popBody() { 277 throw new UnsupportedOperationException (); 278 } 279 280 281 public BodyContent pushBody() { 282 throw new UnsupportedOperationException (); 283 } 284 285 286 public void release() { 287 throw new UnsupportedOperationException (); 288 } 289 290 291 public void removeAttribute(String name) { 292 int scope = getAttributesScope(name); 293 if (scope != 0) { 294 removeAttribute(name, scope); 295 } 296 } 297 298 299 public void removeAttribute(String name, int scope) { 300 if (scope == PageContext.PAGE_SCOPE) { 301 attributes.remove(name); 302 } else if (scope == PageContext.REQUEST_SCOPE) { 303 if (request != null) { 304 request.removeAttribute(name); 305 } 306 } else if (scope == PageContext.SESSION_SCOPE) { 307 if (session != null) { 308 session.removeAttribute(name); 309 } 310 } else if (scope == PageContext.APPLICATION_SCOPE) { 311 if (application != null) { 312 application.removeAttribute(name); 313 } 314 } else { 315 throw new IllegalArgumentException ("Invalid scope " + scope); 316 } 317 } 318 319 320 public void setAttribute(String name, Object value) { 321 setAttribute(name, value, PageContext.PAGE_SCOPE); 322 } 323 324 325 public void setAttribute(String name, Object value, int scope) { 326 if (scope == PageContext.PAGE_SCOPE) { 327 attributes.put(name, value); 328 } else if (scope == PageContext.REQUEST_SCOPE) { 329 if (request != null) { 330 request.setAttribute(name, value); 331 } 332 } else if (scope == PageContext.SESSION_SCOPE) { 333 if (session != null) { 334 session.setAttribute(name, value); 335 } 336 } else if (scope == PageContext.APPLICATION_SCOPE) { 337 if (application != null) { 338 application.setAttribute(name, value); 339 } 340 } else { 341 throw new IllegalArgumentException ("Invalid scope " + scope); 342 } 343 } 344 345 346 } 347 | Popular Tags |