1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jasper.runtime;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.el.ELContext;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.el.ExpressionEvaluator;
import javax.servlet.jsp.el.VariableResolver;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.VariableInfo;
import org.apache.jasper.compiler.Localizer;
/**
* Implementation of a JSP Context Wrapper.
*
* The JSP Context Wrapper is a JspContext created and maintained by a tag
* handler implementation. It wraps the Invoking JSP Context, that is, the
* JspContext instance passed to the tag handler by the invoking page via
* setJspContext().
*
* @author Kin-man Chung
* @author Jan Luehe
* @author Jacob Hookom
*/
public class JspContextWrapper extends PageContext implements VariableResolver {
// Invoking JSP context
private PageContext invokingJspCtxt;
private transient HashMap<String, Object> pageAttributes;
// ArrayList of NESTED scripting variables
private ArrayList<String> nestedVars;
// ArrayList of AT_BEGIN scripting variables
private ArrayList<String> atBeginVars;
// ArrayList of AT_END scripting variables
private ArrayList<String> atEndVars;
private Map<String,String> aliases;
private HashMap<String, Object> originalNestedVars;
private ServletContext servletContext = null;
private ELContext elContext = null;
private PageContext rootJspCtxt;
public JspContextWrapper(JspContext jspContext,
ArrayList<String> nestedVars, ArrayList<String> atBeginVars,
ArrayList<String> atEndVars, Map<String,String> aliases) {
this.invokingJspCtxt = (PageContext) jspContext;
if (jspContext instanceof JspContextWrapper) {
rootJspCtxt = ((JspContextWrapper)jspContext).rootJspCtxt;
}
else {
rootJspCtxt = invokingJspCtxt;
}
this.nestedVars = nestedVars;
this.atBeginVars = atBeginVars;
this.atEndVars = atEndVars;
this.pageAttributes = new HashMap<String, Object>(16);
this.aliases = aliases;
if (nestedVars != null) {
this.originalNestedVars = new HashMap<String, Object>(nestedVars.size());
}
syncBeginTagFile();
}
@Override
public void initialize(Servlet servlet, ServletRequest request,
ServletResponse response, String errorPageURL,
boolean needsSession, int bufferSize, boolean autoFlush)
throws IOException, IllegalStateException, IllegalArgumentException {
}
@Override
public Object getAttribute(String name) {
if (name == null) {
throw new NullPointerException(Localizer
.getMessage("jsp.error.attribute.null_name"));
}
return pageAttributes.get(name);
}
@Override
public Object getAttribute(String name, int scope) {
if (name == null) {
throw new NullPointerException(Localizer
.getMessage("jsp.error.attribute.null_name"));
}
if (scope == PAGE_SCOPE) {
return pageAttributes.get(name);
}
return rootJspCtxt.getAttribute(name, scope);
}
@Override
public void setAttribute(String name, Object value) {
if (name == null) {
throw new NullPointerException(Localizer
.getMessage("jsp.error.attribute.null_name"));
}
if (value != null) {
pageAttributes.put(name, value);
} else {
removeAttribute(name, PAGE_SCOPE);
}
}
@Override
public void setAttribute(String name, Object value, int scope) {
if (name == null) {
throw new NullPointerException(Localizer
.getMessage("jsp.error.attribute.null_name"));
}
if (scope == PAGE_SCOPE) {
if (value != null) {
pageAttributes.put(name, value);
} else {
removeAttribute(name, PAGE_SCOPE);
}
} else {
rootJspCtxt.setAttribute(name, value, scope);
}
}
@Override
public Object findAttribute(String name) {
if (name == null) {
throw new NullPointerException(Localizer
.getMessage("jsp.error.attribute.null_name"));
}
Object o = pageAttributes.get(name);
if (o == null) {
o = rootJspCtxt.getAttribute(name, REQUEST_SCOPE);
if (o == null) {
if (getSession() != null) {
o = rootJspCtxt.getAttribute(name, SESSION_SCOPE);
}
if (o == null) {
o = rootJspCtxt.getAttribute(name, APPLICATION_SCOPE);
}
}
}
return o;
}
@Override
public void removeAttribute(String name) {
if (name == null) {
throw new NullPointerException(Localizer
.getMessage("jsp.error.attribute.null_name"));
}
pageAttributes.remove(name);
rootJspCtxt.removeAttribute(name, REQUEST_SCOPE);
if (getSession() != null) {
rootJspCtxt.removeAttribute(name, SESSION_SCOPE);
}
rootJspCtxt.removeAttribute(name, APPLICATION_SCOPE);
}
@Override
public void removeAttribute(String name, int scope) {
if (name == null) {
throw new NullPointerException(Localizer
.getMessage("jsp.error.attribute.null_name"));
}
if (scope == PAGE_SCOPE) {
pageAttributes.remove(name);
} else {
rootJspCtxt.removeAttribute(name, scope);
}
}
@Override
public int getAttributesScope(String name) {
if (name == null) {
throw new NullPointerException(Localizer
.getMessage("jsp.error.attribute.null_name"));
}
if (pageAttributes.get(name) != null) {
return PAGE_SCOPE;
} else {
return rootJspCtxt.getAttributesScope(name);
}
}
@Override
public Enumeration<String> getAttributeNamesInScope(int scope) {
if (scope == PAGE_SCOPE) {
return Collections.enumeration(pageAttributes.keySet());
}
return rootJspCtxt.getAttributeNamesInScope(scope);
}
@Override
public void release() {
invokingJspCtxt.release();
}
@Override
public JspWriter getOut() {
return rootJspCtxt.getOut();
}
@Override
public HttpSession getSession() {
return rootJspCtxt.getSession();
}
@Override
public Object getPage() {
return invokingJspCtxt.getPage();
}
@Override
public ServletRequest getRequest() {
return invokingJspCtxt.getRequest();
}
@Override
public ServletResponse getResponse() {
return rootJspCtxt.getResponse();
}
@Override
public Exception getException() {
return invokingJspCtxt.getException();
}
@Override
public ServletConfig getServletConfig() {
return invokingJspCtxt.getServletConfig();
}
@Override
public ServletContext getServletContext() {
if (servletContext == null) {
servletContext = rootJspCtxt.getServletContext();
}
return servletContext;
}
@Override
public void forward(String relativeUrlPath) throws ServletException,
IOException {
invokingJspCtxt.forward(relativeUrlPath);
}
@Override
public void include(String relativeUrlPath) throws ServletException,
IOException {
invokingJspCtxt.include(relativeUrlPath);
}
@Override
public void include(String relativeUrlPath, boolean flush)
throws ServletException, IOException {
invokingJspCtxt.include(relativeUrlPath, false);
}
@Override
@Deprecated
public VariableResolver getVariableResolver() {
return this;
}
@Override
public BodyContent pushBody() {
return invokingJspCtxt.pushBody();
}
@Override
public JspWriter pushBody(Writer writer) {
return invokingJspCtxt.pushBody(writer);
}
@Override
public JspWriter popBody() {
return invokingJspCtxt.popBody();
}
@Override
@Deprecated
public ExpressionEvaluator getExpressionEvaluator() {
return invokingJspCtxt.getExpressionEvaluator();
}
@Override
public void handlePageException(Exception ex) throws IOException,
ServletException {
// Should never be called since handleException() called with a
// Throwable in the generated servlet.
handlePageException((Throwable) ex);
}
@Override
public void handlePageException(Throwable t) throws IOException,
ServletException {
invokingJspCtxt.handlePageException(t);
}
/**
* VariableResolver interface
*/
@Override
@Deprecated
public Object resolveVariable(String pName) throws ELException {
ELContext ctx = this.getELContext();
return ctx.getELResolver().getValue(ctx, null, pName);
}
/**
* Synchronize variables at begin of tag file
*/
public void syncBeginTagFile() {
saveNestedVariables();
}
/**
* Synchronize variables before fragment invocation
*/
public void syncBeforeInvoke() {
copyTagToPageScope(VariableInfo.NESTED);
copyTagToPageScope(VariableInfo.AT_BEGIN);
}
/**
* Synchronize variables at end of tag file
*/
public void syncEndTagFile() {
copyTagToPageScope(VariableInfo.AT_BEGIN);
copyTagToPageScope(VariableInfo.AT_END);
restoreNestedVariables();
}
/**
* Copies the variables of the given scope from the virtual page scope of
* this JSP context wrapper to the page scope of the invoking JSP context.
*
* @param scope
* variable scope (one of NESTED, AT_BEGIN, or AT_END)
*/
private void copyTagToPageScope(int scope) {
Iterator<String> iter = null;
switch (scope) {
case VariableInfo.NESTED:
if (nestedVars != null) {
iter = nestedVars.iterator();
}
break;
case VariableInfo.AT_BEGIN:
if (atBeginVars != null) {
iter = atBeginVars.iterator();
}
break;
case VariableInfo.AT_END:
if (atEndVars != null) {
iter = atEndVars.iterator();
}
break;
}
while ((iter != null) && iter.hasNext()) {
String varName = iter.next();
Object obj = getAttribute(varName);
varName = findAlias(varName);
if (obj != null) {
invokingJspCtxt.setAttribute(varName, obj);
} else {
invokingJspCtxt.removeAttribute(varName, PAGE_SCOPE);
}
}
}
/**
* Saves the values of any NESTED variables that are present in the invoking
* JSP context, so they can later be restored.
*/
private void saveNestedVariables() {
if (nestedVars != null) {
Iterator<String> iter = nestedVars.iterator();
while (iter.hasNext()) {
String varName = iter.next();
varName = findAlias(varName);
Object obj = invokingJspCtxt.getAttribute(varName);
if (obj != null) {
originalNestedVars.put(varName, obj);
}
}
}
}
/**
* Restores the values of any NESTED variables in the invoking JSP context.
*/
private void restoreNestedVariables() {
if (nestedVars != null) {
Iterator<String> iter = nestedVars.iterator();
while (iter.hasNext()) {
String varName = iter.next();
varName = findAlias(varName);
Object obj = originalNestedVars.get(varName);
if (obj != null) {
invokingJspCtxt.setAttribute(varName, obj);
} else {
invokingJspCtxt.removeAttribute(varName, PAGE_SCOPE);
}
}
}
}
/**
* Checks to see if the given variable name is used as an alias, and if so,
* returns the variable name for which it is used as an alias.
*
* @param varName
* The variable name to check
* @return The variable name for which varName is used as an alias, or
* varName if it is not being used as an alias
*/
private String findAlias(String varName) {
if (aliases == null)
return varName;
String alias = aliases.get(varName);
if (alias == null) {
return varName;
}
return alias;
}
//private ELContextImpl elContext;
@Override
public ELContext getELContext() {
// instead decorate!!!
if (elContext == null) {
elContext = rootJspCtxt.getELContext();
}
return elContext;
/*
if (this.elContext != null) {
JspFactory jspFact = JspFactory.getDefaultFactory();
ServletContext servletContext = this.getServletContext();
JspApplicationContextImpl jspCtx = (JspApplicationContextImpl) jspFact
.getJspApplicationContext(servletContext);
this.elContext = jspCtx.createELContext(this);
}
return this.elContext;
*/
}
}
|