1 17 package org.jahia.data.applications; 18 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Vector ; 22 23 import org.jahia.services.applications.ServletIncludeRequestWrapper; 24 import org.jahia.utils.JahiaConsole; 25 import org.jahia.utils.InsertionSortedMap; 26 import java.util.Map ; 27 import java.io.Serializable ; 28 29 30 31 37 public class ApplicationContext implements Serializable 38 { 39 private static final String CLASS_NAME = ApplicationContext.class.getName(); 40 41 42 private String displayName = ""; 43 44 45 private String context = ""; 46 47 48 private String descr = ""; 49 50 51 private InsertionSortedMap servlets = new InsertionSortedMap(); 52 53 57 private HashMap servletMappings = new HashMap (); 58 59 60 private Vector roles = new Vector (); 61 62 63 private Vector welcomeFiles = new Vector (); 64 65 66 67 72 public ApplicationContext(String context){ 73 this.context = context; 74 } 75 76 87 public ApplicationContext( String context, 88 String displayName, 89 String descr, 90 Vector servlets, 91 HashMap servletMappings, 92 Vector roles, 93 Vector welcomeFiles ) 94 { 95 this.context = context; 96 97 if ( displayName != null ){ 98 this.displayName = displayName; 99 } 100 if ( descr != null ){ 101 this.descr = descr; 102 } 103 addServlets(servlets); 104 setServletMappings(servletMappings); 105 setRoles(roles); 106 setWelcomeFiles(welcomeFiles); 107 } 108 109 115 public void addServlets(Vector servlets) { 116 synchronized (this.servlets) { 117 if ( servlets!=null ){ 118 int size = servlets.size(); 119 ServletBean servlet = null; 120 for ( int i=0 ; i<size ; i++ ){ 121 servlet = (ServletBean)servlets.get(i); 122 if ( servlet!=null && servlet.getServletName() != null ){ 123 this.servlets.put(servlet.getServletName(), servlet); 124 } 125 } 126 } 127 } 128 } 129 130 136 public void addServlet(ServletBean servlet) { 137 synchronized (servlets) { 138 if ( servlet!=null && servlet.getServletName() != null ){ 139 servlets.put(servlet.getServletName(), servlet); 140 } 141 } 142 } 143 144 150 public ServletBean getServlet(String name) { 151 synchronized (servlets) { 152 if ( name!=null){ 153 return (ServletBean)servlets.get(name); 154 } 155 } 156 return null; 157 } 158 159 165 public void setServletMappings(HashMap servletMappings) { 166 synchronized (this.servletMappings) { 167 if ( servletMappings != null ){ 168 this.servletMappings = servletMappings; 169 } 170 } 171 } 172 173 180 public void addServletMapping(String pattern, String name) { 181 synchronized (servletMappings) { 182 servletMappings.put(pattern, name); 183 } 184 } 185 186 193 public String findServletMapping(String pattern) { 194 synchronized (servletMappings) { 195 return ((String ) servletMappings.get(pattern)); 196 } 197 } 198 199 205 public Map getServlets() { 206 return servlets; 207 } 208 209 216 public String findServletMappingFromRequestURI(String requestURI) { 217 218 JahiaConsole.println( CLASS_NAME+".findServletMappingFromRequestURI", 219 "Started for requestURI [" + requestURI + "]"); 220 if ( requestURI == null ){ 221 return null; 222 } 223 String decRequestURI = ServletIncludeRequestWrapper.URLDecode(requestURI); 224 225 if ( !decRequestURI.startsWith(context) ){ 226 return null; 228 } 229 int pos = decRequestURI.indexOf(context); 230 String str = decRequestURI.substring(context.length()); 231 pos = str.indexOf("?"); 232 if ( pos != -1 ){ 233 str = str.substring(0,pos); 234 } 235 pos = str.indexOf(";"); 236 if ( pos != -1 ){ 237 str = str.substring(0,pos); 238 } 239 240 JahiaConsole.println( CLASS_NAME+".findServletMappingFromRequestURI", 241 "Servlet Path + PathInfo [" + str + "]"); 242 243 String pattern = findServletExactMapping(str); 245 246 if ( pattern == null ){ 247 pattern = findServletPathMapping(str); 249 } 250 251 if ( pattern == null ){ 252 pattern = findServletExtensionMapping(str); 254 } 255 256 if ( pattern == null && findServletMapping("/")!=null ){ 257 pattern = "/"; 259 } 260 261 JahiaConsole.println( CLASS_NAME+".findServletMappingFromRequestURI", 262 "Result [" + pattern + "]"); 263 264 return pattern; 265 } 266 267 268 274 public void setRoles(Vector roles) { 275 synchronized (this.roles) { 276 if ( roles != null ){ 277 this.roles = roles; 278 } 279 } 280 } 281 282 288 public void addRole(String role) { 289 synchronized (roles) { 290 roles.add(role); 291 } 292 } 293 294 300 public boolean findRole(String role) { 301 if ( role == null ){ 302 return false; 303 } 304 synchronized (roles) { 305 return roles.contains(role); 306 } 307 } 308 309 313 public Vector getRoles() { 314 return roles; 315 } 316 317 323 public void setWelcomeFiles(Vector welcomeFiles) { 324 synchronized (this.welcomeFiles) { 325 if ( welcomeFiles != null ){ 326 this.welcomeFiles = welcomeFiles; 327 } 328 } 329 } 330 331 332 338 public void addWelcomeFile(String filename) { 339 synchronized (welcomeFiles) { 340 welcomeFiles.add(filename); 341 } 342 } 343 344 350 public Vector getWelcomeFiles() { 351 return welcomeFiles; 352 } 353 354 359 public String getContext(){ 360 return context; 361 } 362 363 369 public void setContext(String val){ 370 context = val; 371 } 372 373 378 public String getDisplayName(){ 379 return displayName; 380 } 381 382 388 public void setDisplayName(String val){ 389 displayName = val; 390 } 391 392 397 public String getDescr(){ 398 return descr; 399 } 400 401 407 public void setDescr(String val){ 408 descr = val; 409 } 410 411 417 private String findServletExactMapping(String path){ 418 if ( path == null ){ 419 return null; 420 } 421 422 Iterator iterator = servletMappings.keySet().iterator(); 423 String pattern = null; 424 String result = ""; 425 while ( iterator.hasNext() ) 426 { 427 pattern = (String )iterator.next(); 428 if ( !pattern.equals("/") 429 || !pattern.startsWith("*.") 430 || !(pattern.startsWith("/") && pattern.endsWith("/*")) ) 431 { 432 434 if ( path.startsWith(pattern) ){ 435 boolean match = false; 436 if ( pattern.length() == path.length() ){ 437 match = true; 438 } else if ( pattern.endsWith("/") ){ 439 match = true; 440 } else if ( path.charAt(pattern.length())=='/' ) { 441 match = true; 442 } 443 if ( match && (pattern.length() > result.length()) ){ 444 result = pattern; 445 } 446 } 447 } 448 } 449 450 JahiaConsole.println( CLASS_NAME+".findServletExactMapping", 451 "result [" + result + "]"); 452 453 if ( result.length() == 0 ){ 454 return null; 455 } 456 return result; 457 } 458 459 465 private String findServletPathMapping(String path){ 466 if ( path == null ){ 467 return null; 468 } 469 470 Iterator iterator = servletMappings.keySet().iterator(); 471 String pattern = null; 472 String result = ""; 473 while ( iterator.hasNext() ) 474 { 475 pattern = (String )iterator.next(); 476 if ( pattern.startsWith("/") && pattern.endsWith("/*") ) 477 { 478 479 if ( path.startsWith(pattern.substring(0,pattern.length()-2)) ){ 481 String str = pattern.substring(0,pattern.length()-2); 482 if ( (str.startsWith("/") || str.startsWith("?") || str.startsWith(";") 483 || str.startsWith("#") || str.startsWith("&") ) && pattern.length() > result.length() ){ 484 result = pattern; 485 } 486 } 487 } 488 } 489 490 JahiaConsole.println( CLASS_NAME+".findServletPathMapping", 491 "result [" + result + "]"); 492 493 if ( result.length() == 0 ){ 494 return null; 495 } 496 return result; 497 } 498 499 505 private String findServletExtensionMapping(String path){ 506 if ( path == null ){ 507 return null; 508 } 509 510 int pos = path.indexOf("?"); 511 String str = path; 512 513 if ( pos != -1 ){ 514 str = path.substring(0,pos); 515 } 516 517 pos = str.lastIndexOf("/"); 518 519 Iterator iterator = servletMappings.keySet().iterator(); 520 String pattern = null; 521 String result = ""; 522 int strPos = -1; 523 while ( iterator.hasNext() ) 524 { 525 pattern = (String )iterator.next(); 526 if ( pattern.startsWith("*.") ) 527 { 528 strPos = str.indexOf(pattern.substring(1)); 529 if ( strPos != -1 && pos < strPos 531 && pattern.length() > result.length() ) 532 { 533 result = pattern; 534 } 535 } 536 } 537 538 JahiaConsole.println( CLASS_NAME+".findServletExtensionMapping", 539 "result [" + result + "]"); 540 541 if ( result.length() == 0 ){ 542 return null; 543 } 544 545 return result; 546 } 547 548 549 } 550 | Popular Tags |