1 31 32 package org.opencms.flex; 33 34 import org.opencms.loader.I_CmsResourceLoader; 35 import org.opencms.main.CmsLog; 36 import org.opencms.util.CmsStringUtil; 37 38 import java.util.Arrays ; 39 import java.util.Collections ; 40 import java.util.HashSet ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 import java.util.Map ; 44 import java.util.Set ; 45 46 import javax.servlet.http.HttpSession ; 47 48 import org.apache.commons.logging.Log; 49 50 64 public class CmsFlexCacheKey { 65 66 67 private static final String CACHE_00_ALWAYS = "always"; 68 69 70 private static final String CACHE_01_NEVER = "never"; 71 72 73 private static final String CACHE_02_URI = "uri"; 74 75 76 private static final String CACHE_03_USER = "user"; 77 78 79 private static final String CACHE_04_PARAMS = "params"; 80 81 82 private static final String CACHE_05_NO_PARAMS = "no-params"; 83 84 85 private static final String CACHE_06_TIMEOUT = "timeout"; 86 87 88 private static final String CACHE_07_SESSION = "session"; 89 90 91 private static final String CACHE_08_SCHEMES = "schemes"; 92 93 94 private static final String CACHE_09_PORTS = "ports"; 95 96 97 private static final String CACHE_10_FALSE = CmsStringUtil.FALSE; 98 99 100 private static final String CACHE_11_PARSE_ERROR = "parse-error"; 101 102 103 private static final String CACHE_12_TRUE = CmsStringUtil.TRUE; 104 105 106 private static final String CACHE_13_IP = "ip"; 107 108 109 private static final String CACHE_14_ELEMENT = "element"; 110 111 112 private static final String CACHE_15_LOCALE = "locale"; 113 114 115 private static final String CACHE_16_ENCODING = "encoding"; 116 117 118 private static final String CACHE_17_SITE = "site"; 119 120 121 private static final List CACHE_COMMANDS = Arrays.asList(new String [] { 122 CACHE_00_ALWAYS, 123 CACHE_01_NEVER, 124 CACHE_02_URI, 125 CACHE_03_USER, 126 CACHE_04_PARAMS, 127 CACHE_05_NO_PARAMS, 128 CACHE_06_TIMEOUT, 129 CACHE_07_SESSION, 130 CACHE_08_SCHEMES, 131 CACHE_09_PORTS, 132 CACHE_10_FALSE, 133 CACHE_11_PARSE_ERROR, 134 CACHE_12_TRUE, 135 CACHE_13_IP, 136 CACHE_14_ELEMENT, 137 CACHE_15_LOCALE, 138 CACHE_16_ENCODING, 139 CACHE_17_SITE}); 140 141 142 private static final String IS_USED = "/ /"; 143 144 145 private static final Log LOG = CmsLog.getLog(CmsFlexCacheKey.class); 146 147 148 private int m_always; 149 150 151 private String m_element; 152 153 154 private String m_encoding; 155 156 157 private String m_ip; 158 159 160 private String m_locale; 161 162 163 private Set m_noparams; 164 165 166 private Set m_params; 167 168 169 private boolean m_parseError; 170 171 172 private Set m_ports; 173 174 175 private String m_resource; 176 177 178 private Set m_schemes; 179 180 181 private Set m_session; 182 183 184 private String m_site; 185 186 187 private long m_timeout; 188 189 190 private String m_uri; 191 192 193 private String m_user; 194 195 196 private String m_variation; 197 198 214 public CmsFlexCacheKey(String resourcename, String cacheDirectives, boolean online) { 215 216 m_resource = getKeyName(resourcename, online); 217 m_variation = "never"; 218 m_always = -1; 219 m_timeout = -1; 220 if (cacheDirectives != null) { 221 parseFlexKey(cacheDirectives); 222 } 223 if (LOG.isDebugEnabled()) { 224 LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLEXCACHEKEY_GENERATED_1, toString())); 225 } 226 } 227 228 237 public static String getKeyName(String resourcename, boolean online) { 238 239 return resourcename.concat(online ? CmsFlexCache.CACHE_ONLINESUFFIX : CmsFlexCache.CACHE_OFFLINESUFFIX); 240 } 241 242 249 private static void appendKeyValue(StringBuffer str, String key, String value) { 250 251 str.append(key); 252 if (value == IS_USED) { 253 str.append(";"); 254 } else { 255 str.append("=("); 256 str.append(value); 257 str.append(");"); 258 } 259 } 260 261 269 public boolean hadParseError() { 270 271 return m_parseError; 272 } 273 274 290 public String matchRequestKey(CmsFlexRequestKey key) { 291 292 StringBuffer str = new StringBuffer (100); 293 if (m_always < 0) { 294 if (LOG.isDebugEnabled()) { 295 LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLEXCACHEKEY_KEYMATCH_CACHE_NEVER_0)); 296 } 297 return null; 298 } 299 300 if (LOG.isDebugEnabled()) { 301 LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLEXCACHEKEY_KEYMATCH_CHECK_NO_PARAMS_0)); 302 } 303 if ((m_noparams != null) && (key.getParams() != null)) { 304 if ((m_noparams.size() == 0) && (key.getParams().size() > 0)) { 305 return null; 306 } 307 Iterator i = key.getParams().keySet().iterator(); 308 while (i.hasNext()) { 309 if (m_noparams.contains(i.next())) { 310 return null; 311 } 312 } 313 } 314 315 if (m_always > 0) { 316 if (LOG.isDebugEnabled()) { 317 LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLEXCACHEKEY_KEYMATCH_CACHE_ALWAYS_0)); 318 } 319 str.append(CACHE_00_ALWAYS); 320 return str.toString(); 321 } 322 323 if (m_uri != null) { 324 appendKeyValue(str, CACHE_02_URI, key.getUri()); 325 } 326 327 if (m_site != null) { 328 appendKeyValue(str, CACHE_17_SITE, key.getSite()); 329 } 330 331 if (m_element != null) { 332 appendKeyValue(str, CACHE_14_ELEMENT, key.getElement()); 333 } 334 335 if (m_locale != null) { 336 appendKeyValue(str, CACHE_15_LOCALE, key.getLocale()); 337 } 338 339 if (m_encoding != null) { 340 appendKeyValue(str, CACHE_16_ENCODING, key.getEncoding()); 341 } 342 343 if (m_ip != null) { 344 appendKeyValue(str, CACHE_13_IP, key.getIp()); 345 } 346 347 if (m_user != null) { 348 appendKeyValue(str, CACHE_03_USER, key.getUser()); 349 } 350 351 if (m_params != null) { 352 str.append(CACHE_04_PARAMS); 353 str.append("=("); 354 Map keyParams = key.getParams(); 355 if (keyParams != null) { 356 if (m_params.size() > 0) { 357 Iterator i = m_params.iterator(); 359 while (i.hasNext()) { 360 Object o = i.next(); 361 if (keyParams.containsKey(o)) { 362 str.append(o); 363 str.append("="); 364 String [] values = (String [])keyParams.get(o); 366 str.append(values[0]); 367 if (i.hasNext()) { 368 str.append(","); 369 } 370 } 371 } 372 } else { 373 Iterator i = keyParams.keySet().iterator(); 375 while (i.hasNext()) { 376 Object o = i.next(); 377 str.append(o); 378 str.append("="); 379 String [] values = (String [])keyParams.get(o); 381 str.append(values[0]); 382 if (i.hasNext()) { 383 str.append(","); 384 } 385 } 386 } 387 } 388 str.append(");"); 389 } 390 391 if (m_session != null) { 392 StringBuffer buf = new StringBuffer (32); 393 boolean found = false; 394 buf.append(CACHE_07_SESSION); 395 buf.append("=("); 396 HttpSession keySession = key.getSession(); 397 if (keySession != null) { 398 Iterator i = m_session.iterator(); 400 while (i.hasNext()) { 401 String name = (String )i.next(); 402 Object val = keySession.getAttribute(name); 403 if (val != null) { 404 found = true; 405 buf.append(name); 406 buf.append("="); 407 buf.append(val); 408 if (i.hasNext()) { 409 buf.append(","); 410 } 411 } 412 } 413 } 414 if (found) { 415 buf.append(");"); 416 str.append(buf); 417 } 418 } 419 420 if (m_schemes != null) { 421 String s = key.getScheme(); 422 if ((m_schemes.size() > 0) && (!m_schemes.contains(s))) { 423 return null; 424 } 425 appendKeyValue(str, CACHE_08_SCHEMES, s); 426 } 427 428 if (m_ports != null) { 429 Integer i = key.getPort(); 430 if ((m_ports.size() > 0) && (!m_ports.contains(i))) { 431 return null; 432 } 433 str.append(CACHE_09_PORTS); 434 str.append("=("); 435 str.append(i); 436 str.append(");"); 437 } 438 439 if (m_timeout > 0) { 440 str.append(CACHE_06_TIMEOUT); 441 str.append("=("); 442 str.append(m_timeout); 443 str.append(");"); 444 } 445 446 if (str.length() > 0) { 447 return str.toString(); 448 } else { 449 return null; 450 } 451 } 452 453 458 public String toString() { 459 460 StringBuffer str = new StringBuffer (100); 461 462 if (m_always < 0) { 463 str.append(CACHE_01_NEVER); 464 if (m_parseError) { 465 str.append(";"); 466 str.append(CACHE_11_PARSE_ERROR); 467 } 468 return str.toString(); 469 } 470 if (m_noparams != null) { 471 str.append(CACHE_05_NO_PARAMS); 473 if (m_noparams.size() == 0) { 474 str.append(";"); 475 } else { 476 str.append("=("); 477 Iterator i = m_noparams.iterator(); 478 while (i.hasNext()) { 479 Object o = i.next(); 480 str.append(o); 481 if (i.hasNext()) { 482 str.append(","); 483 } 484 } 485 str.append(");"); 486 } 487 } 488 if (m_always > 0) { 489 str.append(CACHE_00_ALWAYS); 490 if (m_parseError) { 491 str.append(";"); 492 str.append(CACHE_11_PARSE_ERROR); 493 } 494 return str.toString(); 495 } 496 if (m_uri != null) { 497 appendKeyValue(str, CACHE_02_URI, m_uri); 499 } 500 if (m_site != null) { 501 appendKeyValue(str, CACHE_17_SITE, m_site); 503 } 504 if (m_element != null) { 505 appendKeyValue(str, CACHE_14_ELEMENT, m_element); 507 } 508 if (m_locale != null) { 509 appendKeyValue(str, CACHE_15_LOCALE, m_locale); 511 } 512 if (m_encoding != null) { 513 appendKeyValue(str, CACHE_16_ENCODING, m_encoding); 515 } 516 if (m_ip != null) { 517 appendKeyValue(str, CACHE_13_IP, m_ip); 519 } 520 if (m_user != null) { 521 appendKeyValue(str, CACHE_03_USER, m_user); 523 } 524 if (m_params != null) { 525 str.append(CACHE_04_PARAMS); 527 if (m_params.size() == 0) { 528 str.append(";"); 529 } else { 530 str.append("=("); 531 Iterator i = m_params.iterator(); 532 while (i.hasNext()) { 533 Object o = i.next(); 534 if (I_CmsResourceLoader.PARAMETER_ELEMENT.equals(o)) { 535 continue; 536 } 537 str.append(o); 538 if (i.hasNext()) { 539 str.append(","); 540 } 541 } 542 str.append(");"); 543 } 544 } 545 if (m_session != null) { 546 str.append(CACHE_07_SESSION); 548 str.append("=("); 549 Iterator i = m_session.iterator(); 550 while (i.hasNext()) { 551 Object o = i.next(); 552 str.append(o); 553 if (i.hasNext()) { 554 str.append(","); 555 } 556 } 557 str.append(");"); 558 } 559 if (m_timeout >= 0) { 560 str.append(CACHE_06_TIMEOUT); 562 str.append("=("); 563 str.append(m_timeout); 564 str.append(");"); 565 } 566 if (m_schemes != null) { 567 str.append(CACHE_08_SCHEMES); 569 if (m_schemes.size() == 0) { 570 str.append(";"); 571 } else { 572 str.append("=("); 573 Iterator i = m_schemes.iterator(); 574 while (i.hasNext()) { 575 str.append(i.next()); 576 if (i.hasNext()) { 577 str.append(","); 578 } 579 } 580 str.append(");"); 581 } 582 } 583 if (m_ports != null) { 584 str.append(CACHE_09_PORTS); 586 if (m_ports.size() == 0) { 587 str.append(";"); 588 } else { 589 str.append("=("); 590 Iterator i = m_ports.iterator(); 591 while (i.hasNext()) { 592 str.append(i.next()); 593 if (i.hasNext()) { 594 str.append(","); 595 } 596 } 597 str.append(");"); 598 } 599 } 600 601 if (m_parseError) { 602 str.append(CACHE_11_PARSE_ERROR); 603 } 604 return str.toString(); 605 } 606 607 612 protected String getResource() { 613 614 return m_resource; 615 } 616 617 622 protected long getTimeout() { 623 624 return m_timeout; 625 } 626 627 632 protected String getVariation() { 633 634 return m_variation; 635 } 636 637 642 protected void setVariation(String variation) { 643 644 m_variation = variation; 645 } 646 647 653 private void parseFlexKey(String key) { 654 655 List tokens = CmsStringUtil.splitAsList(key, ';', false); 656 Iterator i = tokens.iterator(); 657 try { 658 while (i.hasNext()) { 659 String t = (String )i.next(); 660 String k = null; 661 String v = null; 662 int idx = t.indexOf('='); 663 if (idx >= 0) { 664 k = t.substring(0, idx).trim(); 665 if (t.length() > idx) { 666 v = t.substring(idx + 1).trim(); 667 } 668 } else { 669 k = t.trim(); 670 } 671 m_always = 0; 672 if (LOG.isDebugEnabled()) { 673 LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLEXCACHEKEY_PARSE_FLEXKEY_3, t, k, v)); 674 } 675 switch (CACHE_COMMANDS.indexOf(k)) { 676 case 0: case 12: m_always = 1; 679 break; 681 case 1: case 10: m_always = -1; 684 return; 686 case 2: m_uri = IS_USED; break; 689 case 3: m_user = IS_USED; break; 692 case 4: if (v!= null) { 694 m_params = parseValueList(v); 695 } else { 696 m_params = Collections.EMPTY_SET; 697 } 698 699 if (m_params.contains(I_CmsResourceLoader.PARAMETER_ELEMENT)) { 700 m_element = IS_USED; 702 m_params.remove(I_CmsResourceLoader.PARAMETER_ELEMENT); 703 if (m_params.size() == 0) { 704 m_params = null; 705 } 706 } 707 break; 708 case 5: if (v != null) { 710 m_noparams = parseValueList(v); 712 } else { 713 m_noparams = Collections.EMPTY_SET; 715 } 716 break; 717 case 6: m_timeout = Integer.parseInt(v); 719 break; 720 case 7: m_session = parseValueList(v); 722 if (m_session.size() <= 0) { 723 m_parseError = true; 725 } 726 break; 727 case 8: m_schemes = parseValueList(v); 729 break; 730 case 9: m_ports = parseValueList(v); 732 break; 733 case 11: break; 735 case 13: m_ip = IS_USED; break; 738 case 14: m_element = IS_USED; 740 break; 741 case 15: m_locale = IS_USED; 743 break; 744 case 16: m_encoding = IS_USED; 746 break; 747 case 17: m_site = IS_USED; 749 break; 750 default: m_parseError = true; 752 } 753 } 754 } catch (Exception e) { 755 if (LOG.isDebugEnabled()) { 757 LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLEXCACHEKEY_PARSE_ERROR_1, e.toString())); 758 } 759 m_parseError = true; 760 } 761 if (m_parseError) { 762 m_always = -1; 764 } 765 } 766 767 774 private Set parseValueList(String value) { 775 776 if (value.charAt(0) == '(') { 777 value = value.substring(1); 778 } 779 int len = value.length() - 1; 780 if (value.charAt(len) == ')') { 781 value = value.substring(0, len); 782 } 783 if (value.charAt(len - 1) == ',') { 784 value = value.substring(0, len - 1); 785 } 786 if (LOG.isDebugEnabled()) { 787 LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLEXCACHEKEY_PARSE_VALUES_1, value)); 788 } 789 List tokens = CmsStringUtil.splitAsList(value, ',', true); 790 Set result = new HashSet (); 791 result.addAll(tokens); 792 return result; 793 } 794 } | Popular Tags |