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 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
|
/*
* 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.compiler;
import java.util.Iterator;
import java.util.Vector;
import javax.servlet.ServletContext;
import org.apache.jasper.Constants;
import org.apache.jasper.JasperException;
import org.apache.jasper.xmlparser.ParserUtils;
import org.apache.jasper.xmlparser.TreeNode;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
/**
* Handles the jsp-config element in WEB_INF/web.xml. This is used
* for specifying the JSP configuration information on a JSP page
*
* @author Kin-man Chung
* @author Remy Maucherat
*/
public class JspConfig {
// Logger
private final Log log = LogFactory.getLog(JspConfig.class);
private Vector<JspPropertyGroup> jspProperties = null;
private ServletContext ctxt;
private volatile boolean initialized = false;
private static final String defaultIsXml = null; // unspecified
private String defaultIsELIgnored = null; // unspecified
private static final String defaultIsScriptingInvalid = null;
private String defaultDeferedSyntaxAllowedAsLiteral = null;
private static final String defaultTrimDirectiveWhitespaces = null;
private static final String defaultDefaultContentType = null;
private static final String defaultBuffer = null;
private static final String defaultErrorOnUndeclaredNamespace = "false";
private JspProperty defaultJspProperty;
public JspConfig(ServletContext ctxt) {
this.ctxt = ctxt;
}
private double getVersion(TreeNode webApp) {
String v = webApp.findAttribute("version");
if (v != null) {
try {
return Double.parseDouble(v);
} catch (NumberFormatException e) {
}
}
return 2.3;
}
private void processWebDotXml() throws JasperException {
WebXml webXml = null;
try {
webXml = new WebXml(ctxt);
boolean validate = Boolean.parseBoolean(
ctxt.getInitParameter(Constants.XML_VALIDATION_INIT_PARAM));
String blockExternalString =
ctxt.getInitParameter(Constants.XML_BLOCK_EXTERNAL_INIT_PARAM);
boolean blockExternal;
if (blockExternalString == null) {
blockExternal = true;
} else {
blockExternal = Boolean.parseBoolean(blockExternalString);
}
TreeNode webApp = null;
if (webXml.getInputSource() != null) {
ParserUtils pu = new ParserUtils(validate, blockExternal);
webApp = pu.parseXMLDocument(webXml.getSystemId(),
webXml.getInputSource());
}
if (webApp == null
|| getVersion(webApp) < 2.4) {
defaultIsELIgnored = "true";
defaultDeferedSyntaxAllowedAsLiteral = "true";
return;
}
if (getVersion(webApp) < 2.5) {
defaultDeferedSyntaxAllowedAsLiteral = "true";
}
TreeNode jspConfig = webApp.findChild("jsp-config");
if (jspConfig == null) {
return;
}
jspProperties = new Vector<JspPropertyGroup>();
Iterator<TreeNode> jspPropertyList =
jspConfig.findChildren("jsp-property-group");
while (jspPropertyList.hasNext()) {
TreeNode element = jspPropertyList.next();
Iterator<TreeNode> list = element.findChildren();
Vector<String> urlPatterns = new Vector<String>();
String pageEncoding = null;
String scriptingInvalid = null;
String elIgnored = null;
String isXml = null;
Vector<String> includePrelude = new Vector<String>();
Vector<String> includeCoda = new Vector<String>();
String deferredSyntaxAllowedAsLiteral = null;
String trimDirectiveWhitespaces = null;
String defaultContentType = null;
String buffer = null;
String errorOnUndeclaredNamespace = null;
while (list.hasNext()) {
element = list.next();
String tname = element.getName();
if ("url-pattern".equals(tname))
urlPatterns.addElement( element.getBody() );
else if ("page-encoding".equals(tname))
pageEncoding = element.getBody();
else if ("is-xml".equals(tname))
isXml = element.getBody();
else if ("el-ignored".equals(tname))
elIgnored = element.getBody();
else if ("scripting-invalid".equals(tname))
scriptingInvalid = element.getBody();
else if ("include-prelude".equals(tname))
includePrelude.addElement(element.getBody());
else if ("include-coda".equals(tname))
includeCoda.addElement(element.getBody());
else if ("deferred-syntax-allowed-as-literal".equals(tname))
deferredSyntaxAllowedAsLiteral = element.getBody();
else if ("trim-directive-whitespaces".equals(tname))
trimDirectiveWhitespaces = element.getBody();
else if ("default-content-type".equals(tname))
defaultContentType = element.getBody();
else if ("buffer".equals(tname))
buffer = element.getBody();
else if ("error-on-undeclared-namespace".equals(tname))
errorOnUndeclaredNamespace = element.getBody();
}
if (urlPatterns.size() == 0) {
continue;
}
// Add one JspPropertyGroup for each URL Pattern. This makes
// the matching logic easier.
for( int p = 0; p < urlPatterns.size(); p++ ) {
String urlPattern = urlPatterns.elementAt( p );
String path = null;
String extension = null;
if (urlPattern.indexOf('*') < 0) {
// Exact match
path = urlPattern;
} else {
int i = urlPattern.lastIndexOf('/');
String file;
if (i >= 0) {
path = urlPattern.substring(0,i+1);
file = urlPattern.substring(i+1);
} else {
file = urlPattern;
}
// pattern must be "*", or of the form "*.jsp"
if (file.equals("*")) {
extension = "*";
} else if (file.startsWith("*.")) {
extension = file.substring(file.indexOf('.')+1);
}
// The url patterns are reconstructed as the following:
// path != null, extension == null: / or /foo/bar.ext
// path == null, extension != null: *.ext
// path != null, extension == "*": /foo/*
boolean isStar = "*".equals(extension);
if ((path == null && (extension == null || isStar))
|| (path != null && !isStar)) {
if (log.isWarnEnabled()) {
log.warn(Localizer.getMessage(
"jsp.warning.bad.urlpattern.propertygroup",
urlPattern));
}
continue;
}
}
JspProperty property = new JspProperty(isXml,
elIgnored,
scriptingInvalid,
pageEncoding,
includePrelude,
includeCoda,
deferredSyntaxAllowedAsLiteral,
trimDirectiveWhitespaces,
defaultContentType,
buffer,
errorOnUndeclaredNamespace);
JspPropertyGroup propertyGroup =
new JspPropertyGroup(path, extension, property);
jspProperties.addElement(propertyGroup);
}
}
} catch (Exception ex) {
throw new JasperException(ex);
} finally {
if (webXml != null) {
webXml.close();
}
}
}
private void init() throws JasperException {
if (!initialized) {
synchronized (this) {
if (!initialized) {
processWebDotXml();
defaultJspProperty = new JspProperty(defaultIsXml,
defaultIsELIgnored,
defaultIsScriptingInvalid,
null, null, null,
defaultDeferedSyntaxAllowedAsLiteral,
defaultTrimDirectiveWhitespaces,
defaultDefaultContentType,
defaultBuffer,
defaultErrorOnUndeclaredNamespace);
initialized = true;
}
}
}
}
/**
* Select the property group that has more restrictive url-pattern.
* In case of tie, select the first.
*/
private JspPropertyGroup selectProperty(JspPropertyGroup prev,
JspPropertyGroup curr) {
if (prev == null) {
return curr;
}
if (prev.getExtension() == null) {
// exact match
return prev;
}
if (curr.getExtension() == null) {
// exact match
return curr;
}
String prevPath = prev.getPath();
String currPath = curr.getPath();
if (prevPath == null && currPath == null) {
// Both specifies a *.ext, keep the first one
return prev;
}
if (prevPath == null && currPath != null) {
return curr;
}
if (prevPath != null && currPath == null) {
return prev;
}
if (prevPath.length() >= currPath.length()) {
return prev;
}
return curr;
}
/**
* Find a property that best matches the supplied resource.
* @param uri the resource supplied.
* @return a JspProperty indicating the best match, or some default.
*/
public JspProperty findJspProperty(String uri) throws JasperException {
init();
// JSP Configuration settings do not apply to tag files
if (jspProperties == null || uri.endsWith(".tag")
|| uri.endsWith(".tagx")) {
return defaultJspProperty;
}
String uriPath = null;
int index = uri.lastIndexOf('/');
if (index >=0 ) {
uriPath = uri.substring(0, index+1);
}
String uriExtension = null;
index = uri.lastIndexOf('.');
if (index >=0) {
uriExtension = uri.substring(index+1);
}
Vector<String> includePreludes = new Vector<String>();
Vector<String> includeCodas = new Vector<String>();
JspPropertyGroup isXmlMatch = null;
JspPropertyGroup elIgnoredMatch = null;
JspPropertyGroup scriptingInvalidMatch = null;
JspPropertyGroup pageEncodingMatch = null;
JspPropertyGroup deferedSyntaxAllowedAsLiteralMatch = null;
JspPropertyGroup trimDirectiveWhitespacesMatch = null;
JspPropertyGroup defaultContentTypeMatch = null;
JspPropertyGroup bufferMatch = null;
JspPropertyGroup errorOnUndeclaredNamespaceMatch = null;
Iterator<JspPropertyGroup> iter = jspProperties.iterator();
while (iter.hasNext()) {
JspPropertyGroup jpg = iter.next();
JspProperty jp = jpg.getJspProperty();
// (arrays will be the same length)
String extension = jpg.getExtension();
String path = jpg.getPath();
if (extension == null) {
// exact match pattern: /a/foo.jsp
if (!uri.equals(path)) {
// not matched;
continue;
}
} else {
// Matching patterns *.ext or /p/*
if (path != null && uriPath != null &&
! uriPath.startsWith(path)) {
// not matched
continue;
}
if (!extension.equals("*") &&
!extension.equals(uriExtension)) {
// not matched
continue;
}
}
// We have a match
// Add include-preludes and include-codas
if (jp.getIncludePrelude() != null) {
includePreludes.addAll(jp.getIncludePrelude());
}
if (jp.getIncludeCoda() != null) {
includeCodas.addAll(jp.getIncludeCoda());
}
// If there is a previous match for the same property, remember
// the one that is more restrictive.
if (jp.isXml() != null) {
isXmlMatch = selectProperty(isXmlMatch, jpg);
}
if (jp.isELIgnored() != null) {
elIgnoredMatch = selectProperty(elIgnoredMatch, jpg);
}
if (jp.isScriptingInvalid() != null) {
scriptingInvalidMatch =
selectProperty(scriptingInvalidMatch, jpg);
}
if (jp.getPageEncoding() != null) {
pageEncodingMatch = selectProperty(pageEncodingMatch, jpg);
}
if (jp.isDeferedSyntaxAllowedAsLiteral() != null) {
deferedSyntaxAllowedAsLiteralMatch =
selectProperty(deferedSyntaxAllowedAsLiteralMatch, jpg);
}
if (jp.isTrimDirectiveWhitespaces() != null) {
trimDirectiveWhitespacesMatch =
selectProperty(trimDirectiveWhitespacesMatch, jpg);
}
if (jp.getDefaultContentType() != null) {
defaultContentTypeMatch =
selectProperty(defaultContentTypeMatch, jpg);
}
if (jp.getBuffer() != null) {
bufferMatch = selectProperty(bufferMatch, jpg);
}
if (jp.isErrorOnUndeclaredNamespace() != null) {
errorOnUndeclaredNamespaceMatch =
selectProperty(errorOnUndeclaredNamespaceMatch, jpg);
}
}
String isXml = defaultIsXml;
String isELIgnored = defaultIsELIgnored;
String isScriptingInvalid = defaultIsScriptingInvalid;
String pageEncoding = null;
String isDeferedSyntaxAllowedAsLiteral =
defaultDeferedSyntaxAllowedAsLiteral;
String isTrimDirectiveWhitespaces = defaultTrimDirectiveWhitespaces;
String defaultContentType = defaultDefaultContentType;
String buffer = defaultBuffer;
String errorOnUndelcaredNamespace = defaultErrorOnUndeclaredNamespace;
if (isXmlMatch != null) {
isXml = isXmlMatch.getJspProperty().isXml();
}
if (elIgnoredMatch != null) {
isELIgnored = elIgnoredMatch.getJspProperty().isELIgnored();
}
if (scriptingInvalidMatch != null) {
isScriptingInvalid =
scriptingInvalidMatch.getJspProperty().isScriptingInvalid();
}
if (pageEncodingMatch != null) {
pageEncoding = pageEncodingMatch.getJspProperty().getPageEncoding();
}
if (deferedSyntaxAllowedAsLiteralMatch != null) {
isDeferedSyntaxAllowedAsLiteral =
deferedSyntaxAllowedAsLiteralMatch.getJspProperty().isDeferedSyntaxAllowedAsLiteral();
}
if (trimDirectiveWhitespacesMatch != null) {
isTrimDirectiveWhitespaces =
trimDirectiveWhitespacesMatch.getJspProperty().isTrimDirectiveWhitespaces();
}
if (defaultContentTypeMatch != null) {
defaultContentType =
defaultContentTypeMatch.getJspProperty().getDefaultContentType();
}
if (bufferMatch != null) {
buffer = bufferMatch.getJspProperty().getBuffer();
}
if (errorOnUndeclaredNamespaceMatch != null) {
errorOnUndelcaredNamespace =
errorOnUndeclaredNamespaceMatch.getJspProperty().isErrorOnUndeclaredNamespace();
}
return new JspProperty(isXml, isELIgnored, isScriptingInvalid,
pageEncoding, includePreludes, includeCodas,
isDeferedSyntaxAllowedAsLiteral, isTrimDirectiveWhitespaces,
defaultContentType, buffer, errorOnUndelcaredNamespace);
}
/**
* To find out if an uri matches an url pattern in jsp config. If so,
* then the uri is a JSP page. This is used primarily for jspc.
*/
public boolean isJspPage(String uri) throws JasperException {
init();
if (jspProperties == null) {
return false;
}
String uriPath = null;
int index = uri.lastIndexOf('/');
if (index >=0 ) {
uriPath = uri.substring(0, index+1);
}
String uriExtension = null;
index = uri.lastIndexOf('.');
if (index >=0) {
uriExtension = uri.substring(index+1);
}
Iterator<JspPropertyGroup> iter = jspProperties.iterator();
while (iter.hasNext()) {
JspPropertyGroup jpg = iter.next();
String extension = jpg.getExtension();
String path = jpg.getPath();
if (extension == null) {
if (uri.equals(path)) {
// There is an exact match
return true;
}
} else {
if ((path == null || path.equals(uriPath)) &&
(extension.equals("*") || extension.equals(uriExtension))) {
// Matches *, *.ext, /p/*, or /p/*.ext
return true;
}
}
}
return false;
}
public static class JspPropertyGroup {
private String path;
private String extension;
private JspProperty jspProperty;
JspPropertyGroup(String path, String extension,
JspProperty jspProperty) {
this.path = path;
this.extension = extension;
this.jspProperty = jspProperty;
}
public String getPath() {
return path;
}
public String getExtension() {
return extension;
}
public JspProperty getJspProperty() {
return jspProperty;
}
}
public static class JspProperty {
private String isXml;
private String elIgnored;
private String scriptingInvalid;
private String pageEncoding;
private Vector<String> includePrelude;
private Vector<String> includeCoda;
private String deferedSyntaxAllowedAsLiteral;
private String trimDirectiveWhitespaces;
private String defaultContentType;
private String buffer;
private String errorOnUndeclaredNamespace;
public JspProperty(String isXml, String elIgnored,
String scriptingInvalid, String pageEncoding,
Vector<String> includePrelude, Vector<String> includeCoda,
String deferedSyntaxAllowedAsLiteral,
String trimDirectiveWhitespaces,
String defaultContentType,
String buffer,
String errorOnUndeclaredNamespace) {
this.isXml = isXml;
this.elIgnored = elIgnored;
this.scriptingInvalid = scriptingInvalid;
this.pageEncoding = pageEncoding;
this.includePrelude = includePrelude;
this.includeCoda = includeCoda;
this.deferedSyntaxAllowedAsLiteral = deferedSyntaxAllowedAsLiteral;
this.trimDirectiveWhitespaces = trimDirectiveWhitespaces;
this.defaultContentType = defaultContentType;
this.buffer = buffer;
this.errorOnUndeclaredNamespace = errorOnUndeclaredNamespace;
}
public String isXml() {
return isXml;
}
public String isELIgnored() {
return elIgnored;
}
public String isScriptingInvalid() {
return scriptingInvalid;
}
public String getPageEncoding() {
return pageEncoding;
}
public Vector<String> getIncludePrelude() {
return includePrelude;
}
public Vector<String> getIncludeCoda() {
return includeCoda;
}
public String isDeferedSyntaxAllowedAsLiteral() {
return deferedSyntaxAllowedAsLiteral;
}
public String isTrimDirectiveWhitespaces() {
return trimDirectiveWhitespaces;
}
public String getDefaultContentType() {
return defaultContentType;
}
public String getBuffer() {
return buffer;
}
public String isErrorOnUndeclaredNamespace() {
return errorOnUndeclaredNamespace;
}
}
}
|