1 33 34 package com.icesoft.faces.webapp.parser; 35 36 import com.icesoft.faces.util.IteratorEnumeration; 37 38 import javax.el.ELContext; 39 import javax.faces.context.ExternalContext; 40 import javax.servlet.*; 41 import javax.servlet.http.HttpSession ; 42 import javax.servlet.jsp.JspWriter ; 43 import javax.servlet.jsp.PageContext ; 44 import javax.servlet.jsp.el.ExpressionEvaluator ; 45 import javax.servlet.jsp.el.VariableResolver ; 46 import java.io.IOException ; 47 import java.io.PrintWriter ; 48 import java.util.Enumeration ; 49 import java.util.Hashtable ; 50 import java.util.Map ; 51 52 56 public class StubPageContext extends PageContext { 57 58 private Map attributes = new Hashtable (); 59 private HttpSession httpSession; 60 private ExternalContext externalContext; 61 private ServletRequest servletRequest; 62 private ServletResponse servletResponse; 63 64 67 public void initialize(Servlet servlet, ServletRequest servletRequest, 68 ServletResponse servletResponse, 69 String errorPageURL, boolean sessionSupport, 70 int bufferSize, boolean autoFlush) 71 throws IOException , IllegalStateException , 72 IllegalArgumentException { 73 this.servletResponse = servletResponse; 74 externalContext = javax.faces.context.FacesContext.getCurrentInstance() 75 .getExternalContext(); 76 } 77 78 81 public void setAttribute(String name, Object value) { 82 setAttribute(name, value, PAGE_SCOPE); 83 } 84 85 88 public void setAttribute(String name, Object value, int scope) { 89 switch (scope) { 90 case PAGE_SCOPE: 91 attributes.put(name, value); 92 break; 93 case REQUEST_SCOPE: 94 externalContext.getRequestMap().put(name, value); 95 break; 96 case SESSION_SCOPE: 97 if (httpSession == null) { 98 throw new IllegalArgumentException ( 99 "Session is not stablished for this request"); 100 } 101 httpSession.setAttribute(name, value); 102 break; 103 case APPLICATION_SCOPE: 104 externalContext.getApplicationMap().put(name, value); 105 break; 106 default: 107 throw new IllegalArgumentException ( 108 scope + " scope is not valid"); 109 } 110 } 111 112 115 public Object getAttribute(String name) { 116 return getAttribute(name, PAGE_SCOPE); 117 } 118 119 122 public Object getAttribute(String name, int scope) { 123 switch (scope) { 124 case PAGE_SCOPE: 125 return attributes.get(name); 126 case REQUEST_SCOPE: 127 return externalContext.getRequestMap().get(name); 128 case SESSION_SCOPE: 129 if (httpSession == null) { 130 throw new IllegalArgumentException ( 131 "Session is not stablished for this request"); 132 } 133 return httpSession.getAttribute(name); 134 case APPLICATION_SCOPE: 135 return (externalContext.getApplicationMap().get(name)); 136 default: 137 throw new IllegalArgumentException ( 138 scope + " scope is not valid"); 139 } 140 } 141 142 145 public void removeAttribute(String name) { 146 removeAttribute(name, PAGE_SCOPE); 147 } 148 149 152 public void removeAttribute(String name, int scope) { 153 switch (scope) { 154 case PAGE_SCOPE: 155 attributes.remove(name); 156 break; 157 case REQUEST_SCOPE: 158 externalContext.getRequestMap().remove(name); 159 break; 160 case SESSION_SCOPE: 161 if (httpSession == null) { 162 throw new IllegalArgumentException ( 163 "Session is not stablished for this request"); 164 } 165 httpSession.removeAttribute(name); 166 break; 167 case APPLICATION_SCOPE: 168 externalContext.getApplicationMap().remove(name); 169 break; 170 default: 171 throw new IllegalArgumentException ( 172 scope + " scope is not valid"); 173 } 174 } 175 176 179 public JspWriter getOut() { 180 return new JspWriterImpl(new PrintWriter (System.out)); 181 } 182 183 186 public Object getPage() { 187 throw new UnsupportedOperationException (); 188 } 189 190 193 public ServletConfig getServletConfig() { 194 throw new UnsupportedOperationException (); 195 } 196 197 200 public ServletContext getServletContext() { 201 throw new UnsupportedOperationException (); 202 } 203 204 public ELContext getELContext() { 205 throw new UnsupportedOperationException (); 206 } 207 208 211 public ServletRequest getRequest() { 212 return servletRequest; 213 } 214 215 218 public ServletResponse getResponse() { 219 return servletResponse; 220 } 221 222 225 public HttpSession getSession() { 226 return httpSession; 227 } 228 229 232 public void release() { 233 throw new UnsupportedOperationException (); 234 } 235 236 239 public Exception getException() { 240 throw new UnsupportedOperationException (); 241 } 242 243 246 public void handlePageException(Exception arg0) 247 throws ServletException, IOException { 248 throw new UnsupportedOperationException (); 249 } 250 251 254 public void forward(String arg0) throws ServletException, IOException { 255 throw new UnsupportedOperationException (); 256 } 257 258 261 public void include(String arg0) throws ServletException, IOException { 262 throw new UnsupportedOperationException (); 263 } 264 265 268 public void include(String arg0, boolean arg1) 269 throws ServletException, IOException { 270 throw new UnsupportedOperationException (); 271 } 272 273 276 public void handlePageException(Throwable arg0) 277 throws ServletException, IOException { 278 throw new UnsupportedOperationException (); 279 } 280 281 284 public int getAttributesScope(String arg0) { 285 throw new UnsupportedOperationException (); 286 } 287 288 289 292 public Enumeration getAttributeNamesInScope(int scope) { 293 switch (scope) { 294 case PAGE_SCOPE: 295 return new IteratorEnumeration( 296 ((Hashtable ) attributes).keySet().iterator()); 297 case REQUEST_SCOPE: 298 return new IteratorEnumeration( 299 externalContext.getRequestMap().keySet().iterator()); 300 case SESSION_SCOPE: 301 return new IteratorEnumeration( 302 externalContext.getSessionMap().keySet().iterator()); 303 case APPLICATION_SCOPE: 304 return new IteratorEnumeration( 305 externalContext.getApplicationMap() 306 .keySet().iterator()); 307 default: 308 throw new IllegalArgumentException ( 309 scope + " scope is not valid"); 310 } 311 312 } 313 314 317 public ExpressionEvaluator getExpressionEvaluator() { 318 throw new UnsupportedOperationException (); 319 } 320 321 324 public VariableResolver getVariableResolver() { 325 throw new UnsupportedOperationException (); 326 } 327 328 331 public Object findAttribute(String name) { 332 Object attribute; 333 334 attribute = attributes.get(name); 336 if (null != attribute) { 337 return attribute; 338 } 339 340 attribute = externalContext.getRequestMap().get(name); 342 if (null != attribute) { 343 return attribute; 344 } 345 346 if (httpSession != null) { 348 attribute = httpSession.getAttribute(name); 349 } 350 if (null != attribute) { 351 return attribute; 352 } 353 354 return (externalContext.getApplicationMap().get(name)); 356 } 357 } 358 | Popular Tags |