1 31 32 package org.opencms.file; 33 34 52 public final class CmsResourceFilter { 53 54 64 public static final CmsResourceFilter ALL = new CmsResourceFilter(); 65 66 69 public static final CmsResourceFilter ALL_MODIFIED = ALL.addExcludeState(CmsResource.STATE_UNCHANGED); 70 71 81 public static final CmsResourceFilter DEFAULT = ALL.addExcludeState(CmsResource.STATE_DELETED).addRequireTimerange(); 82 83 86 public static final CmsResourceFilter DEFAULT_FILES = DEFAULT.addRequireFile(); 87 88 91 public static final CmsResourceFilter DEFAULT_FOLDERS = DEFAULT.addRequireFolder(); 92 93 103 public static final CmsResourceFilter IGNORE_EXPIRATION = ALL.addExcludeState(CmsResource.STATE_DELETED); 104 105 115 public static final CmsResourceFilter ONLY_VISIBLE = ALL.addRequireVisible(); 116 117 127 public static final CmsResourceFilter ONLY_VISIBLE_NO_DELETED = ONLY_VISIBLE.addExcludeState(CmsResource.STATE_DELETED); 128 129 private static final int EXCLUDED = 2; 130 131 private static final int IGNORED = 0; 132 133 private static final int REQUIRED = 1; 134 135 136 private String m_cacheId; 137 138 139 private long m_expireAfter; 140 141 142 private long m_expireBefore; 143 144 145 private boolean m_filterExpire; 146 147 148 private int m_filterFlags; 149 150 151 private boolean m_filterLastModified; 152 153 154 private boolean m_filterRelease; 155 156 157 private int m_filterState; 158 159 160 private boolean m_filterTimerange; 161 162 163 private int m_filterType; 164 165 166 private boolean m_filterVisible; 167 168 169 private int m_flags; 170 171 172 private long m_modifiedAfter; 173 174 175 private long m_modifiedBefore; 176 177 178 private Boolean m_onlyFolders; 179 180 181 private long m_releaseAfter; 182 183 184 private long m_releaseBefore; 185 186 187 private int m_state; 188 189 190 private int m_type; 191 192 195 private CmsResourceFilter() { 196 197 m_filterState = IGNORED; 198 m_state = -1; 199 200 m_filterType = IGNORED; 201 m_type = -1; 202 203 m_filterFlags = IGNORED; 204 m_flags = -1; 205 206 m_filterVisible = false; 207 208 m_filterTimerange = false; 209 m_filterLastModified = false; 210 m_filterRelease = false; 211 m_filterExpire = false; 212 m_modifiedAfter = 0L; 213 m_modifiedBefore = 0L; 214 m_releaseAfter = 0L; 215 m_releaseBefore = 0L; 216 m_expireAfter = 0L; 217 m_expireBefore = 0L; 218 219 updateCacheId(); 220 } 221 222 228 public static CmsResourceFilter requireType(int type) { 229 230 return new CmsResourceFilter().addRequireType(type); 231 } 232 233 239 public CmsResourceFilter addExcludeFlags(int flags) { 240 241 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 242 243 extendedFilter.m_flags = flags; 244 extendedFilter.m_filterFlags = EXCLUDED; 245 extendedFilter.updateCacheId(); 246 247 return extendedFilter; 248 } 249 250 256 public CmsResourceFilter addExcludeState(int state) { 257 258 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 259 260 extendedFilter.m_state = state; 261 extendedFilter.m_filterState = EXCLUDED; 262 extendedFilter.updateCacheId(); 263 264 return extendedFilter; 265 } 266 267 274 public CmsResourceFilter addExcludeTimerange() { 275 276 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 277 278 extendedFilter.m_filterTimerange = false; 279 extendedFilter.updateCacheId(); 280 281 return extendedFilter; 282 } 283 284 290 public CmsResourceFilter addExcludeType(int type) { 291 292 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 293 294 extendedFilter.m_type = type; 295 extendedFilter.m_filterType = EXCLUDED; 296 extendedFilter.updateCacheId(); 297 298 return extendedFilter; 299 } 300 301 307 public CmsResourceFilter addRequireExpireAfter(long time) { 308 309 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 310 311 extendedFilter.m_filterExpire = true; 312 extendedFilter.m_expireAfter = time; 313 extendedFilter.updateCacheId(); 314 315 return extendedFilter; 316 } 317 318 324 public CmsResourceFilter addRequireExpireBefore(long time) { 325 326 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 327 328 extendedFilter.m_filterExpire = true; 329 extendedFilter.m_expireBefore = time; 330 extendedFilter.updateCacheId(); 331 332 return extendedFilter; 333 } 334 335 340 public CmsResourceFilter addRequireFile() { 341 342 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 343 344 extendedFilter.m_onlyFolders = Boolean.FALSE; 345 extendedFilter.updateCacheId(); 346 347 return extendedFilter; 348 } 349 350 356 public CmsResourceFilter addRequireFlags(int flags) { 357 358 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 359 360 extendedFilter.m_flags = flags; 361 extendedFilter.m_filterFlags = REQUIRED; 362 extendedFilter.updateCacheId(); 363 364 return extendedFilter; 365 } 366 367 372 public CmsResourceFilter addRequireFolder() { 373 374 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 375 376 extendedFilter.m_onlyFolders = Boolean.TRUE; 377 extendedFilter.updateCacheId(); 378 379 return extendedFilter; 380 } 381 382 388 public CmsResourceFilter addRequireLastModifiedAfter(long time) { 389 390 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 391 392 extendedFilter.m_filterLastModified = true; 393 extendedFilter.m_modifiedAfter = time; 394 extendedFilter.updateCacheId(); 395 396 return extendedFilter; 397 } 398 399 405 public CmsResourceFilter addRequireLastModifiedBefore(long time) { 406 407 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 408 409 extendedFilter.m_filterLastModified = true; 410 extendedFilter.m_modifiedBefore = time; 411 extendedFilter.updateCacheId(); 412 413 return extendedFilter; 414 } 415 416 422 public CmsResourceFilter addRequireReleaseAfter(long time) { 423 424 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 425 426 extendedFilter.m_filterRelease = true; 427 extendedFilter.m_releaseAfter = time; 428 extendedFilter.updateCacheId(); 429 430 return extendedFilter; 431 } 432 433 439 public CmsResourceFilter addRequireReleaseBefore(long time) { 440 441 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 442 443 extendedFilter.m_filterRelease = true; 444 extendedFilter.m_releaseBefore = time; 445 extendedFilter.updateCacheId(); 446 447 return extendedFilter; 448 } 449 450 456 public CmsResourceFilter addRequireState(int state) { 457 458 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 459 460 extendedFilter.m_state = state; 461 extendedFilter.m_filterState = REQUIRED; 462 extendedFilter.updateCacheId(); 463 464 return extendedFilter; 465 } 466 467 472 public CmsResourceFilter addRequireTimerange() { 473 474 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 475 476 extendedFilter.m_filterTimerange = true; 477 extendedFilter.updateCacheId(); 478 479 return extendedFilter; 480 } 481 482 488 public CmsResourceFilter addRequireType(int type) { 489 490 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 491 492 extendedFilter.m_type = type; 493 extendedFilter.m_filterType = REQUIRED; 494 extendedFilter.updateCacheId(); 495 496 return extendedFilter; 497 } 498 499 504 public CmsResourceFilter addRequireVisible() { 505 506 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 507 508 extendedFilter.m_filterVisible = true; 509 extendedFilter.updateCacheId(); 510 511 return extendedFilter; 512 } 513 514 517 public Object clone() { 518 519 CmsResourceFilter filter = new CmsResourceFilter(); 520 521 filter.m_cacheId = m_cacheId; 522 filter.m_expireAfter = m_expireAfter; 523 filter.m_expireBefore = m_expireBefore; 524 filter.m_filterExpire = m_filterExpire; 525 filter.m_filterFlags = m_filterFlags; 526 filter.m_filterLastModified = m_filterLastModified; 527 filter.m_filterRelease = m_filterRelease; 528 filter.m_filterState = m_filterState; 529 filter.m_filterTimerange = m_filterTimerange; 530 filter.m_filterType = m_filterType; 531 filter.m_filterVisible = m_filterVisible; 532 filter.m_flags = m_flags; 533 filter.m_modifiedAfter = m_modifiedAfter; 534 filter.m_modifiedBefore = m_modifiedBefore; 535 filter.m_onlyFolders = m_onlyFolders; 536 filter.m_releaseAfter = m_releaseAfter; 537 filter.m_releaseBefore = m_releaseBefore; 538 filter.m_state = m_state; 539 filter.m_type = m_type; 540 541 return filter; 542 } 543 544 549 public boolean excludeFlags() { 550 551 return m_filterFlags == EXCLUDED; 552 } 553 554 559 public boolean excludeState() { 560 561 return m_filterState == EXCLUDED; 562 } 563 564 569 public boolean excludeType() { 570 571 return m_filterType == EXCLUDED; 572 } 573 574 579 public String getCacheId() { 580 581 return m_cacheId; 582 } 583 584 589 public long getExpireAfter() { 590 591 return m_expireAfter; 592 } 593 594 599 public long getExpireBefore() { 600 601 return m_expireBefore; 602 } 603 604 609 public int getFlags() { 610 611 return m_flags; 612 } 613 614 619 public long getModifiedAfter() { 620 621 return m_modifiedAfter; 622 } 623 624 629 public long getModifiedBefore() { 630 631 return m_modifiedBefore; 632 } 633 634 641 public Boolean getOnlyFolders() { 642 643 return m_onlyFolders; 644 } 645 646 651 public long getReleaseAfter() { 652 653 return m_releaseAfter; 654 } 655 656 661 public long getReleaseBefore() { 662 663 return m_releaseBefore; 664 } 665 666 671 public int getState() { 672 673 return m_state; 674 } 675 676 681 public int getType() { 682 683 return m_type; 684 } 685 686 691 public boolean includeDeleted() { 692 693 return (m_filterState == IGNORED) 694 || ((m_filterState == REQUIRED) && (m_state == CmsResource.STATE_DELETED)) 695 || ((m_filterState == EXCLUDED) && (m_state != CmsResource.STATE_DELETED)); 696 } 697 698 710 public boolean isValid(CmsRequestContext context, CmsResource resource) { 711 712 if (this == ALL) { 713 return true; 715 } 716 717 switch (m_filterState) { 719 case EXCLUDED: 720 if (resource.getState() == m_state) { 721 return false; 722 } 723 break; 724 case REQUIRED: 725 if (resource.getState() != m_state) { 726 return false; 727 } 728 break; 729 default: 730 } 732 733 switch (m_filterFlags) { 735 case EXCLUDED: 736 if ((resource.getFlags() & m_flags) != 0) { 737 return false; 738 } 739 break; 740 case REQUIRED: 741 if ((resource.getFlags() & m_flags) != m_flags) { 742 return false; 743 } 744 break; 745 default: 746 } 748 749 switch (m_filterType) { 751 case EXCLUDED: 752 if (resource.getTypeId() == m_type) { 753 return false; 754 } 755 break; 756 case REQUIRED: 757 if (resource.getTypeId() != m_type) { 758 return false; 759 } 760 break; 761 default: 762 } 764 765 if (m_onlyFolders != null) { 766 if (m_onlyFolders.booleanValue()) { 767 if (!resource.isFolder()) { 768 return false; 770 } 771 } else { 772 if (resource.isFolder()) { 773 return false; 775 } 776 } 777 } 778 779 if (m_filterLastModified) { 781 if (m_modifiedAfter > 0L && resource.getDateLastModified() < m_modifiedAfter) { 782 return false; 783 } 784 if (m_modifiedBefore > 0L && resource.getDateLastModified() > m_modifiedBefore) { 785 return false; 786 } 787 } 788 789 if (m_filterExpire) { 791 if (m_expireAfter > 0L && resource.getDateExpired() < m_expireAfter) { 792 return false; 793 } 794 if (m_expireBefore > 0L && resource.getDateExpired() > m_expireBefore) { 795 return false; 796 } 797 } 798 799 if (m_filterRelease) { 801 if (m_releaseAfter > 0L && resource.getDateReleased() < m_releaseAfter) { 802 return false; 803 } 804 if (m_releaseBefore > 0L && resource.getDateReleased() > m_releaseBefore) { 805 return false; 806 } 807 } 808 809 if (m_filterTimerange 811 && ((resource.getDateReleased() > context.getRequestTime()) || (resource.getDateExpired() < context.getRequestTime()))) { 812 return false; 813 } 814 815 return true; 817 } 818 819 824 public boolean requireFlags() { 825 826 return m_filterFlags == REQUIRED; 827 } 828 829 834 public boolean requireState() { 835 836 return m_filterState == REQUIRED; 837 } 838 839 844 public boolean requireTimerange() { 845 846 return m_filterTimerange; 847 } 848 849 854 public boolean requireType() { 855 856 return m_filterType == REQUIRED; 857 } 858 859 864 public boolean requireVisible() { 865 866 return m_filterVisible; 867 } 868 869 875 public String toString() { 876 877 if (this.equals(CmsResourceFilter.ALL)) { 878 return "ALL"; 879 } else if (this.equals(CmsResourceFilter.ALL_MODIFIED)) { 880 return "ALL_MODIFIED"; 881 } else if (this.equals(CmsResourceFilter.DEFAULT)) { 882 return "DEFAULT"; 883 } else if (this.equals(CmsResourceFilter.DEFAULT_FILES)) { 884 return "DEFAULT_FILES"; 885 } else if (this.equals(CmsResourceFilter.DEFAULT_FOLDERS)) { 886 return "DEFAULT_FOLDERS"; 887 } else if (this.equals(CmsResourceFilter.IGNORE_EXPIRATION)) { 888 return "IGNORE_EXPIRATION"; 889 } else if (this.equals(CmsResourceFilter.ONLY_VISIBLE)) { 890 return "ONLY_VISIBLE"; 891 } else if (this.equals(CmsResourceFilter.ONLY_VISIBLE_NO_DELETED)) { 892 return "ONLY_VISIBLE_NO_DELETED"; 893 } else { 894 return "Nonstandard Resource Filter"; 895 } 896 } 897 898 901 private void updateCacheId() { 902 903 StringBuffer result = new StringBuffer (32); 904 if (m_filterVisible) { 905 result.append(" Vi"); 906 } 907 if (m_filterTimerange) { 908 result.append(" Ti"); 909 } 910 switch (m_filterState) { 911 case REQUIRED: 912 result.append(" Sr"); 913 result.append(m_state); 914 break; 915 case EXCLUDED: 916 result.append(" Sx"); 917 result.append(m_state); 918 break; 919 default: 920 } 922 switch (m_filterFlags) { 923 case REQUIRED: 924 result.append(" Fr"); 925 result.append(m_flags); 926 break; 927 case EXCLUDED: 928 result.append(" Fx"); 929 result.append(m_flags); 930 break; 931 default: 932 } 934 switch (m_filterType) { 935 case REQUIRED: 936 result.append(" Tr"); 937 result.append(m_type); 938 break; 939 case EXCLUDED: 940 result.append(" Tx"); 941 result.append(m_type); 942 break; 943 default: 944 } 946 if (m_onlyFolders != null) { 947 if (m_onlyFolders.booleanValue()) { 948 result.append(" Fo"); 949 } else { 950 result.append(" Fi"); 951 } 952 } 953 if (m_filterLastModified) { 954 result.append(" Lt"); 955 result.append(m_modifiedAfter); 956 result.append("-"); 957 result.append(m_modifiedBefore); 958 } 959 if (m_filterExpire) { 960 result.append(" Ex"); 961 result.append(m_expireAfter); 962 result.append("-"); 963 result.append(m_expireBefore); 964 } 965 if (m_filterRelease) { 966 result.append(" Re"); 967 result.append(m_releaseAfter); 968 result.append("-"); 969 result.append(m_releaseBefore); 970 } 971 m_cacheId = result.toString(); 972 } 973 } | Popular Tags |