1 23 24 package com.sun.enterprise.web; 25 26 import java.io.IOException ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import java.util.List ; 34 import java.util.Vector ; 35 import java.util.logging.Logger ; 36 import javax.servlet.ServletException ; 37 import com.sun.enterprise.deployment.runtime.web.SunWebApp; 38 import com.sun.enterprise.deployment.runtime.web.LocaleCharsetInfo; 39 import com.sun.enterprise.deployment.runtime.web.LocaleCharsetMap; 40 import com.sun.logging.LogDomains; 41 import org.apache.catalina.LifecycleException; 42 import org.apache.catalina.Pipeline; 43 import org.apache.catalina.Request; 44 import org.apache.catalina.Response; 45 import org.apache.catalina.Valve; 46 import org.apache.catalina.Wrapper; 47 import org.apache.catalina.core.StandardWrapper; 48 import org.apache.catalina.core.StandardPipeline; 49 import org.apache.catalina.deploy.FilterMap; 50 import com.sun.enterprise.deployment.web.ServletFilterMapping; 51 52 55 56 public class WebModule extends PwcWebModule { 57 58 60 private static Logger logger = LogDomains.getLogger(LogDomains.WEB_LOGGER); 61 62 64 private SunWebApp iasBean = null; 66 67 private LocaleCharsetMap[] _lcMap = null; 69 70 73 private boolean hasBeenXmlConfigured = false; 74 75 private WebContainer webContainer; 76 77 private HashMap <String ,AdHocServletInfo> adHocPaths; 78 private boolean hasAdHocPaths; 79 80 private HashMap <String ,AdHocServletInfo> adHocSubtrees; 81 private boolean hasAdHocSubtrees; 82 83 private StandardPipeline adHocPipeline; 84 85 private String fileEncoding; 87 88 91 protected Object [] cachedFinds; 92 93 94 99 public WebModule(WebContainer webContainer) { 100 101 this.webContainer = webContainer; 102 103 this.adHocPaths = new HashMap <String ,AdHocServletInfo>(); 104 this.adHocSubtrees = new HashMap <String ,AdHocServletInfo>(); 105 106 this.adHocPipeline = new StandardPipeline(this); 107 this.adHocPipeline.setBasic(new AdHocContextValve(this)); 108 } 109 110 111 114 public void setIasWebAppConfigBean(SunWebApp iasBean) { 115 this.iasBean = iasBean; 116 } 117 118 119 122 public SunWebApp getIasWebAppConfigBean() { 123 return iasBean; 124 } 125 126 127 130 public void setI18nInfo() { 131 132 if (iasBean == null) { 133 return; 134 } 135 136 if (iasBean.isParameterEncoding()) { 137 formHintField = (String ) iasBean.getAttributeValue( 138 SunWebApp.PARAMETER_ENCODING, 139 SunWebApp.FORM_HINT_FIELD); 140 defaultCharset = (String ) iasBean.getAttributeValue( 141 SunWebApp.PARAMETER_ENCODING, 142 SunWebApp.DEFAULT_CHARSET); 143 } 144 145 LocaleCharsetInfo lcinfo = iasBean.getLocaleCharsetInfo(); 146 if (lcinfo != null) { 147 if (lcinfo.getAttributeValue( 148 LocaleCharsetInfo.DEFAULT_LOCALE) != null) { 149 logger.warning("webmodule.default_locale_deprecated"); 150 } 151 155 if (lcinfo.isParameterEncoding() 156 && !iasBean.isParameterEncoding()) { 157 formHintField = (String ) lcinfo.getAttributeValue( 158 LocaleCharsetInfo.PARAMETER_ENCODING, 159 LocaleCharsetInfo.FORM_HINT_FIELD); 160 defaultCharset = (String ) lcinfo.getAttributeValue( 161 LocaleCharsetInfo.PARAMETER_ENCODING, 162 LocaleCharsetInfo.DEFAULT_CHARSET); 163 } 164 _lcMap = lcinfo.getLocaleCharsetMap(); 165 } 166 } 167 168 169 172 public LocaleCharsetMap[] getLocaleCharsetMap() { 173 return _lcMap; 174 } 175 176 177 184 public boolean hasLocaleToCharsetMapping() { 185 LocaleCharsetMap[] locCharsetMap = getLocaleCharsetMap(); 186 return (locCharsetMap != null && locCharsetMap.length > 0); 187 } 188 189 190 201 public String mapLocalesToCharset(Enumeration locales) { 202 203 String encoding = null; 204 205 LocaleCharsetMap[] locCharsetMap = getLocaleCharsetMap(); 206 if (locCharsetMap != null && locCharsetMap.length > 0) { 207 212 boolean matchFound = false; 213 while (locales.hasMoreElements() && !matchFound) { 214 Locale reqLoc = (Locale ) locales.nextElement(); 215 for (int i=0; i<locCharsetMap.length && !matchFound; i++) { 216 String language = locCharsetMap[i].getAttributeValue( 217 LocaleCharsetMap.LOCALE); 218 if (language == null || language.equals("")) { 219 continue; 220 } 221 String country = null; 222 int index = language.indexOf('_'); 223 if (index != -1) { 224 country = language.substring(index+1); 225 language = language.substring(0, index); 226 } 227 Locale mapLoc = null; 228 if (country != null) { 229 mapLoc = new Locale (language, country); 230 } else { 231 mapLoc = new Locale (language); 232 } 233 if (mapLoc.equals(reqLoc)) { 234 238 encoding = locCharsetMap[i].getAttributeValue( 239 LocaleCharsetMap.CHARSET); 240 matchFound = true; 241 } 242 } 243 } 244 } 245 246 return encoding; 247 } 248 249 250 254 public void setXmlConfigured(boolean hasBeenXmlConfigured){ 255 this.hasBeenXmlConfigured = hasBeenXmlConfigured; 256 } 257 258 259 263 public boolean hasBeenXmlConfigured(){ 264 return hasBeenXmlConfigured; 265 } 266 267 268 273 public void setCachedFindOperation(Object [] cachedFinds){ 274 this.cachedFinds = cachedFinds; 275 } 276 277 278 283 public Object [] getCachedFindOperation(){ 284 return cachedFinds; 285 } 286 287 288 291 public synchronized void start() throws LifecycleException { 292 super.start(); 293 webContainer.enableMonitoring(this, 294 ((VirtualServer) getParent()).getID()); 295 } 296 297 298 310 public boolean hasAdHocPaths() { 311 return this.hasAdHocPaths; 312 } 313 314 315 321 public boolean hasAdHocSubtrees() { 322 return this.hasAdHocSubtrees; 323 } 324 325 326 336 void addAdHocPathAndSubtree(String path, 337 String subtree, 338 AdHocServletInfo servletInfo) { 339 340 if (path == null && subtree == null) { 341 return; 342 } 343 344 Wrapper adHocWrapper = (Wrapper) 345 findChild(servletInfo.getServletName()); 346 if (adHocWrapper == null) { 347 adHocWrapper = createAdHocWrapper(servletInfo); 348 addChild(adHocWrapper); 349 } 350 351 if (path != null) { 352 adHocPaths.put(path, servletInfo); 353 hasAdHocPaths = true; 354 } 355 356 if (subtree != null) { 357 adHocSubtrees.put(subtree, servletInfo); 358 hasAdHocSubtrees = true; 359 } 360 } 361 362 363 369 void addAdHocPaths(HashMap newPaths) { 370 371 if (newPaths == null || newPaths.isEmpty()) { 372 return; 373 } 374 375 Iterator <String > iter = newPaths.keySet().iterator(); 376 while (iter.hasNext()) { 377 String adHocPath = iter.next(); 378 AdHocServletInfo servletInfo = (AdHocServletInfo) 379 newPaths.get(adHocPath); 380 Wrapper adHocWrapper = (Wrapper) 381 findChild(servletInfo.getServletName()); 382 if (adHocWrapper == null) { 383 adHocWrapper = createAdHocWrapper(servletInfo); 384 addChild(adHocWrapper); 385 } 386 adHocPaths.put(adHocPath, servletInfo); 387 } 388 389 hasAdHocPaths = true; 390 } 391 392 393 400 void addAdHocSubtrees(HashMap newSubtrees) { 401 402 if (newSubtrees == null || newSubtrees.isEmpty()) { 403 return; 404 } 405 406 Iterator <String > iter = newSubtrees.keySet().iterator(); 407 while (iter.hasNext()) { 408 String adHocSubtree = iter.next(); 409 AdHocServletInfo servletInfo = (AdHocServletInfo) 410 newSubtrees.get(adHocSubtree); 411 Wrapper adHocWrapper = (Wrapper) 412 findChild(servletInfo.getServletName()); 413 if (adHocWrapper == null) { 414 adHocWrapper = createAdHocWrapper(servletInfo); 415 addChild(adHocWrapper); 416 } 417 adHocSubtrees.put(adHocSubtree, servletInfo); 418 } 419 420 hasAdHocSubtrees = true; 421 } 422 423 424 430 HashMap getAdHocPaths() { 431 return adHocPaths; 432 } 433 434 435 442 HashMap getAdHocSubtrees() { 443 return adHocSubtrees; 444 } 445 446 447 457 public String getAdHocServletName(String path) { 458 459 if (!hasAdHocPaths() && !hasAdHocSubtrees()) { 460 return null; 461 } 462 463 AdHocServletInfo servletInfo = null; 464 465 if (path == null) { 467 servletInfo = adHocPaths.get(""); 468 } else { 469 servletInfo = adHocPaths.get(path); 470 } 471 472 if (servletInfo == null && path != null && hasAdHocSubtrees()) { 474 Iterator <String > iter = adHocSubtrees.keySet().iterator(); 475 while (iter.hasNext()) { 476 String adHocSubtree = iter.next(); 477 if (path.startsWith(adHocSubtree)) { 478 servletInfo = adHocSubtrees.get(adHocSubtree); 479 break; 480 } 481 } 482 } 483 484 if (servletInfo != null) { 485 return servletInfo.getServletName(); 486 } else { 487 return null; 488 } 489 } 490 491 492 497 void removeAdHocPath(String path) { 498 499 if (path == null) { 500 return; 501 } 502 503 adHocPaths.remove(path); 504 if (adHocPaths.isEmpty()) { 505 this.hasAdHocPaths = false; 506 } 507 } 508 509 510 515 void removeAdHocSubtree(String subtree) { 516 517 if (subtree == null) { 518 return; 519 } 520 521 adHocSubtrees.remove(subtree); 522 if (adHocSubtrees.isEmpty()) { 523 this.hasAdHocSubtrees = false; 524 } 525 } 526 527 528 533 public void addAdHocValve(Valve valve) { 534 adHocPipeline.addValve(valve); 535 } 536 537 538 543 public void removeAdHocValve(Valve valve) { 544 adHocPipeline.removeValve(valve); 545 } 546 547 548 553 public Pipeline getAdHocPipeline() { 554 return adHocPipeline; 555 } 556 557 558 563 public void setFileEncoding(String enc) { 564 this.fileEncoding = enc; 565 } 566 567 568 573 public String getFileEncoding() { 574 return fileEncoding; 575 } 576 577 578 585 void addFilterMap(ServletFilterMapping sfm) { 586 587 FilterMap filterMap = null; 588 Iterator <String > i1 = null; 589 Iterator <String > i2 = null; 590 591 List servletNames = sfm.getServletNames(); 593 if (servletNames != null) { 594 i1 = servletNames.iterator(); 595 while (i1.hasNext()) { 596 filterMap = new FilterMap(); 597 filterMap.setFilterName(sfm.getName()); 598 filterMap.setServletName(i1.next()); 599 Set dispatchers = sfm.getDispatchers(); 600 if (dispatchers != null) { 601 i2 = dispatchers.iterator(); 602 while (i2.hasNext()){ 603 filterMap.setDispatcher(i2.next()); 604 } 605 } 606 addFilterMap(filterMap); 607 } 608 } 609 610 List urlPatterns = sfm.getURLPatterns(); 612 if (urlPatterns != null) { 613 i1 = urlPatterns.iterator(); 614 while (i1.hasNext()) { 615 filterMap = new FilterMap(); 616 filterMap.setFilterName(sfm.getName()); 617 filterMap.setURLPattern(i1.next()); 618 Set dispatchers = sfm.getDispatchers(); 619 if (dispatchers != null) { 620 i2 = dispatchers.iterator(); 621 while (i2.hasNext()){ 622 filterMap.setDispatcher(i2.next()); 623 } 624 } 625 addFilterMap(filterMap); 626 } 627 } 628 } 629 630 631 639 private Wrapper createAdHocWrapper(AdHocServletInfo servletInfo) { 640 641 Wrapper adHocWrapper = new StandardWrapper(); 642 adHocWrapper.setServletClass(servletInfo.getServletClass().getName()); 643 adHocWrapper.setName(servletInfo.getServletName()); 644 Map <String ,String > initParams = servletInfo.getServletInitParams(); 645 if (initParams != null && !initParams.isEmpty()) { 646 Iterator <String > iter = initParams.keySet().iterator(); 647 while (iter.hasNext()) { 648 String paramName = iter.next(); 649 adHocWrapper.addInitParameter( 650 paramName, 651 initParams.get(paramName)); 652 } 653 } 654 655 return adHocWrapper; 656 } 657 } 658 659 660 | Popular Tags |