001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.file; 029 030import org.opencms.db.CmsResourceState; 031import org.opencms.file.types.I_CmsResourceType; 032 033/** 034 * Provides filters for resource result sets obtained from requests to the OpenCms VFS.<p> 035 * 036 * Using the constant filters provided by this class 037 * you can control "special" behaviour 038 * of access to the VFS. For example, in the "Offline" project 039 * there can be deleted files, by using this filter you can control 040 * if deleted files should be included in a result set or not.<p> 041 * 042 * @since 6.0.0 043 */ 044public final class CmsResourceFilter { 045 046 /** 047 * Filter to display all resources.<p> 048 * 049 * This filter uses the following rules: 050 * <ul> 051 * <li>Includes: Resources marked as deleted.</li> 052 * <li>Includes: Resources outside the 'time window' set with release and expiration date.</li> 053 * <li>Includes: Resources marked as 'invisible' using permissions.</li> 054 * </ul> 055 */ 056 public static final CmsResourceFilter ALL = new CmsResourceFilter(); 057 058 /** 059 * Filter to display all modified (new/changed/deleted) resources.<p> 060 */ 061 public static final CmsResourceFilter ALL_MODIFIED = ALL.addExcludeState(CmsResource.STATE_UNCHANGED); 062 063 /** 064 * Default filter to display resources for the online project.<p> 065 * 066 * This filter uses the following rules: 067 * <ul> 068 * <li>Excludes: Resources marked as deleted.</li> 069 * <li>Excludes: Resources outside the 'time window' set with release and expiration date.</li> 070 * <li>Includes: Resources marked as 'invisible' using permissions.</li> 071 * </ul> 072 */ 073 public static final CmsResourceFilter DEFAULT = ALL.addExcludeState( 074 CmsResource.STATE_DELETED).addRequireTimerange(); 075 076 /** 077 * Default filter to display files for the online project.<p> 078 */ 079 public static final CmsResourceFilter DEFAULT_FILES = DEFAULT.addRequireFile(); 080 081 /** 082 * Default filter to display folders for the online project.<p> 083 */ 084 public static final CmsResourceFilter DEFAULT_FOLDERS = DEFAULT.addRequireFolder(); 085 086 /** 087 * Default filter to display resources for the online project.<p> 088 * 089 * This filter uses the following rules: 090 * <ul> 091 * <li>Excludes: Resources marked as deleted.</li> 092 * <li>Excludes: Resources outside the 'time window' set with release and expiration date.</li> 093 * <li>Excludes: Resources marked as 'invisible' using permissions.</li> 094 * </ul> 095 */ 096 public static final CmsResourceFilter DEFAULT_ONLY_VISIBLE = DEFAULT.addRequireVisible(); 097 098 /** 099 * Filter to display resources ignoring the release and expiration dates.<p> 100 * 101 * This filter uses the following rules: 102 * <ul> 103 * <li>Excludes: Resources marked as deleted.</li> 104 * <li>Includes: Resources outside the 'time window' set with release and expiration date.</li> 105 * <li>Includes: Resources marked as 'invisible' using permissions.</li> 106 * </ul> 107 */ 108 public static final CmsResourceFilter IGNORE_EXPIRATION = ALL.addExcludeState(CmsResource.STATE_DELETED); 109 110 /** 111 * Filter to display only visible resources.<p> 112 * 113 * This filter used the following rules: 114 * <ul> 115 * <li>Includes: Resources marked as deleted.</li> 116 * <li>Includes: Resources outside the 'time window' set with release and expiration date.</li> 117 * <li>Excludes: Resources marked as 'invisible' using permissions.</li> 118 * </ul> 119 */ 120 public static final CmsResourceFilter ONLY_VISIBLE = ALL.addRequireVisible(); 121 122 /** 123 * Filter to display only visible and not deleted resources.<p> 124 * 125 * This filter used the following rules: 126 * <ul> 127 * <li>Excludes: Resources marked as deleted.</li> 128 * <li>Includes: Resources outside the 'time window' set with release and expiration date.</li> 129 * <li>Excludes: Resources marked as 'invisible' using permissions.</li> 130 * </ul> 131 */ 132 public static final CmsResourceFilter ONLY_VISIBLE_NO_DELETED = ONLY_VISIBLE.addExcludeState( 133 CmsResource.STATE_DELETED); 134 135 /** The excluded flag value. */ 136 private static final int EXCLUDED = 2; 137 138 /** The ignored flag value. */ 139 private static final int IGNORED = 0; 140 141 /** The required flag value. */ 142 private static final int REQUIRED = 1; 143 144 /** The cache id for this filter. */ 145 private String m_cacheId; 146 147 /** The required start date for the timerange of the expire date. */ 148 private long m_expireAfter; 149 150 /** The required end data for the timerange of the expire date. */ 151 private long m_expireBefore; 152 153 /** Indicates if the expire date is used (true) or ignored (false). */ 154 private boolean m_filterExpire; 155 156 /** Indicates if the resource flag is filtered (true) or not (false). */ 157 private int m_filterFlags; 158 159 /** Indicates if the date of the last modification is used (true) or ignored (false). */ 160 private boolean m_filterLastModified; 161 162 /** Indicates if the release date is used (true) or ignored (false). */ 163 private boolean m_filterRelease; 164 165 /** Indicates if the resource state (unchanged/new/deleted/modified) is filtered (true) or not (false). */ 166 private int m_filterState; 167 168 /** Indicates if the resource valid timerage is used (true) or ignored (false). */ 169 private boolean m_filterTimerange; 170 171 /** Indicates if the resource type is filtered (true) or not (false). */ 172 private int m_filterType; 173 174 /** Indicates if the visible permission is used (true) or ignored (false). */ 175 private boolean m_filterVisible; 176 177 /** The required/excluded flags for filtering resources. */ 178 private int m_flags; 179 180 /** The required start date for the timerange of the last modification date. */ 181 private long m_modifiedAfter; 182 183 /** The required end data for the timerange of the last modification date. */ 184 private long m_modifiedBefore; 185 186 /** Indicates if the filter should return only folders. */ 187 private Boolean m_onlyFolders; 188 189 /** The required start date for the timerange of the release date. */ 190 private long m_releaseAfter; 191 192 /** The required end data for the timerange of the release date. */ 193 private long m_releaseBefore; 194 195 /** The required/excluded state for filtering resources. */ 196 private CmsResourceState m_state; 197 198 /** The required/excluded type for filtering resources. */ 199 private int m_type; 200 201 /** 202 * Hides the public contructor.<p> 203 */ 204 private CmsResourceFilter() { 205 206 m_filterState = IGNORED; 207 m_state = null; 208 209 m_filterType = IGNORED; 210 m_type = -1; 211 212 m_filterFlags = IGNORED; 213 m_flags = -1; 214 215 m_filterVisible = false; 216 217 m_filterTimerange = false; 218 m_filterLastModified = false; 219 m_filterRelease = false; 220 m_filterExpire = false; 221 m_modifiedAfter = 0L; 222 m_modifiedBefore = 0L; 223 m_releaseAfter = 0L; 224 m_releaseBefore = 0L; 225 m_expireAfter = 0L; 226 m_expireBefore = 0L; 227 228 updateCacheId(); 229 } 230 231 /** 232 * Returns the DEFAULT filter in the Online project, and IGNORE_EXPIRATION otherwise.<p> 233 * 234 * @param cms the CMS context whose project to check 235 * @return the resource filter based on the given CmsObject 236 */ 237 public static CmsResourceFilter ignoreExpirationOffline(CmsObject cms) { 238 239 return cms.getRequestContext().getCurrentProject().isOnlineProject() ? DEFAULT : IGNORE_EXPIRATION; 240 } 241 242 /** 243 * Returns a new CmsResourceFilter requiring the given type.<p> 244 * 245 * @param type the required resource type 246 * @return a filter requiring the given type 247 */ 248 public static CmsResourceFilter requireType(I_CmsResourceType type) { 249 250 return new CmsResourceFilter().addRequireType(type); 251 } 252 253 /** 254 * Returns a new CmsResourceFilter requiring the given type.<p> 255 * 256 * @param type the required resource type 257 * @return a filter requiring the given type 258 * 259 * @deprecated 260 * Use {@link #requireType(I_CmsResourceType)} instead. 261 * Resource types should always be referenced either by its type class (preferred) or by type name. 262 * Use of int based resource type references will be discontinued in a future OpenCms release. 263 */ 264 @Deprecated 265 public static CmsResourceFilter requireType(int type) { 266 267 return new CmsResourceFilter().addRequireType(type); 268 } 269 270 /** 271 * Returns an extended filter in order to avoid the given flags in the filtered resources.<p> 272 * 273 * @param flags the resource flags to exclude 274 * @return a filter excluding the given resource flags 275 */ 276 public CmsResourceFilter addExcludeFlags(int flags) { 277 278 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 279 280 extendedFilter.m_flags = flags; 281 extendedFilter.m_filterFlags = EXCLUDED; 282 extendedFilter.updateCacheId(); 283 284 return extendedFilter; 285 } 286 287 /** 288 * Returns an extended filter in order to avoid the given type in the filtered resources.<p> 289 * 290 * @param state the resource state to exclude 291 * @return a filter excluding the given resource state 292 */ 293 public CmsResourceFilter addExcludeState(CmsResourceState state) { 294 295 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 296 297 extendedFilter.m_state = state; 298 extendedFilter.m_filterState = EXCLUDED; 299 extendedFilter.updateCacheId(); 300 301 return extendedFilter; 302 } 303 304 /** 305 * Returns an extended filter to guarantee all filtered resources ignoring the time range (released and not expired) window.<p> 306 * 307 * This is the reverse of {@link #addRequireTimerange()}.<p> 308 * 309 * @return a filter excluding invalid resources 310 */ 311 public CmsResourceFilter addExcludeTimerange() { 312 313 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 314 315 extendedFilter.m_filterTimerange = false; 316 extendedFilter.updateCacheId(); 317 318 return extendedFilter; 319 } 320 321 /** 322 * Returns an extended filter in order to avoid the given type in the filtered resources.<p> 323 * 324 * @param type the resource type to exclude 325 * @return a filter excluding the given resource type 326 */ 327 public CmsResourceFilter addExcludeType(I_CmsResourceType type) { 328 329 return addExcludeFlags(type.getTypeId()); 330 } 331 332 /** 333 * Returns an extended filter in order to avoid the given type in the filtered resources.<p> 334 * 335 * @param type the resource type to exclude 336 * @return a filter excluding the given resource type 337 * 338 * @deprecated 339 * Use {@link #addExcludeType(I_CmsResourceType)} instead. 340 * Resource types should always be referenced either by its type class (preferred) or by type name. 341 * Use of int based resource type references will be discontinued in a future OpenCms release. 342 */ 343 @Deprecated 344 public CmsResourceFilter addExcludeType(int type) { 345 346 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 347 348 extendedFilter.m_type = type; 349 extendedFilter.m_filterType = EXCLUDED; 350 extendedFilter.updateCacheId(); 351 352 return extendedFilter; 353 } 354 355 /** 356 * Returns an extended filter to restrict the results to resources that expire in the given timerange.<p> 357 * 358 * @param time the required time 359 * @return a filter to restrict the results to resources that expire in the given timerange 360 */ 361 public CmsResourceFilter addRequireExpireAfter(long time) { 362 363 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 364 365 extendedFilter.m_filterExpire = true; 366 extendedFilter.m_expireAfter = time; 367 extendedFilter.updateCacheId(); 368 369 return extendedFilter; 370 } 371 372 /** 373 * Returns an extended filter to restrict the results to resources that expire in the given timerange.<p> 374 * 375 * @param time the required time 376 * @return a filter to restrict the results to resources that expire in the given timerange 377 */ 378 public CmsResourceFilter addRequireExpireBefore(long time) { 379 380 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 381 382 extendedFilter.m_filterExpire = true; 383 extendedFilter.m_expireBefore = time; 384 extendedFilter.updateCacheId(); 385 386 return extendedFilter; 387 } 388 389 /** 390 * Returns an extended filter that requires all returned resources to be files.<p> 391 * 392 * @return an extended filter that requires all returned resources to be files 393 */ 394 public CmsResourceFilter addRequireFile() { 395 396 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 397 398 extendedFilter.m_onlyFolders = Boolean.FALSE; 399 extendedFilter.updateCacheId(); 400 401 return extendedFilter; 402 } 403 404 /** 405 * Returns an extended filter to guarantee a distinct resource flags of the filtered resources.<p> 406 * 407 * @param flags the required resource flags 408 * @return a filter requiring the given resource flags 409 */ 410 public CmsResourceFilter addRequireFlags(int flags) { 411 412 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 413 414 extendedFilter.m_flags = flags; 415 extendedFilter.m_filterFlags = REQUIRED; 416 extendedFilter.updateCacheId(); 417 418 return extendedFilter; 419 } 420 421 /** 422 * Returns an extended filter that requires all returned resources to be folders.<p> 423 * 424 * @return an extended filter that requires all returned resources to be folders 425 */ 426 public CmsResourceFilter addRequireFolder() { 427 428 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 429 430 extendedFilter.m_onlyFolders = Boolean.TRUE; 431 extendedFilter.updateCacheId(); 432 433 return extendedFilter; 434 } 435 436 /** 437 * Returns an extended filter to restrict the results to resources modified in the given timerange.<p> 438 * 439 * @param time the required time 440 * @return a filter to restrict the results to resources modified in the given timerange 441 */ 442 public CmsResourceFilter addRequireLastModifiedAfter(long time) { 443 444 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 445 446 extendedFilter.m_filterLastModified = true; 447 extendedFilter.m_modifiedAfter = time; 448 extendedFilter.updateCacheId(); 449 450 return extendedFilter; 451 } 452 453 /** 454 * Returns an extended filter to restrict the results to resources modified in the given timerange.<p> 455 * 456 * @param time the required time 457 * @return a filter to restrict the results to resources modified in the given timerange 458 */ 459 public CmsResourceFilter addRequireLastModifiedBefore(long time) { 460 461 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 462 463 extendedFilter.m_filterLastModified = true; 464 extendedFilter.m_modifiedBefore = time; 465 extendedFilter.updateCacheId(); 466 467 return extendedFilter; 468 } 469 470 /** 471 * Returns an extended filter to restrict the results to resources that are released in the given timerange.<p> 472 * 473 * @param time the required time 474 * @return a filter to restrict the results to resources that are released in the given timerange 475 */ 476 public CmsResourceFilter addRequireReleaseAfter(long time) { 477 478 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 479 480 extendedFilter.m_filterRelease = true; 481 extendedFilter.m_releaseAfter = time; 482 extendedFilter.updateCacheId(); 483 484 return extendedFilter; 485 } 486 487 /** 488 * Returns an extended filter to restrict the results to resources that are released in the given timerange.<p> 489 * 490 * @param time the required time 491 * @return a filter to restrict the results to resources that are released in the given timerange 492 */ 493 public CmsResourceFilter addRequireReleaseBefore(long time) { 494 495 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 496 497 extendedFilter.m_filterRelease = true; 498 extendedFilter.m_releaseBefore = time; 499 extendedFilter.updateCacheId(); 500 501 return extendedFilter; 502 } 503 504 /** 505 * Returns an extended filter to guarantee a distinct resource state of the filtered resources.<p> 506 * 507 * @param state the required resource state 508 * @return a filter requiring the given resource state 509 */ 510 public CmsResourceFilter addRequireState(CmsResourceState state) { 511 512 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 513 514 extendedFilter.m_state = state; 515 extendedFilter.m_filterState = REQUIRED; 516 extendedFilter.updateCacheId(); 517 518 return extendedFilter; 519 } 520 521 /** 522 * Returns an extended filter to guarantee all filtered resources are valid (released and not expired).<p> 523 * 524 * @return a filter excluding invalid resources 525 */ 526 public CmsResourceFilter addRequireTimerange() { 527 528 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 529 530 extendedFilter.m_filterTimerange = true; 531 extendedFilter.updateCacheId(); 532 533 return extendedFilter; 534 } 535 536 /** 537 * Returns an extended filter to guarantee a distinct resource type of the filtered resources.<p> 538 * 539 * If <code>-1</code> is given as type, the filter will not be extended to require a resource type 540 * 541 * @param type the required resource type 542 * @return a filter requiring the given resource type 543 */ 544 public CmsResourceFilter addRequireType(I_CmsResourceType type) { 545 546 // TODO: Refactor driver layer to use resource type id classes (or names) instead of int 547 return addRequireType(type.getTypeId()); 548 } 549 550 /** 551 * Returns an extended filter to guarantee a distinct resource type of the filtered resources.<p> 552 * 553 * If <code>-1</code> is given as type, the filter will not be extended to require a resource type 554 * 555 * @param type the required resource type 556 * @return a filter requiring the given resource type 557 * 558 * @deprecated 559 * Use {@link #addRequireType(I_CmsResourceType)} instead. 560 * Resource types should always be referenced either by its type class (preferred) or by type name. 561 * Use of int based resource type references will be discontinued in a future OpenCms release. 562 */ 563 @Deprecated 564 public CmsResourceFilter addRequireType(int type) { 565 566 if (type != -1) { 567 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 568 569 extendedFilter.m_type = type; 570 extendedFilter.m_filterType = REQUIRED; 571 extendedFilter.updateCacheId(); 572 return extendedFilter; 573 } 574 575 return this; 576 } 577 578 /** 579 * Returns an extended filter to guarantee all filtered resources are visible.<p> 580 * 581 * @return a filter excluding invisible resources 582 */ 583 public CmsResourceFilter addRequireVisible() { 584 585 CmsResourceFilter extendedFilter = (CmsResourceFilter)clone(); 586 587 extendedFilter.m_filterVisible = true; 588 extendedFilter.updateCacheId(); 589 590 return extendedFilter; 591 } 592 593 /** 594 * @see java.lang.Object#clone() 595 */ 596 @Override 597 public Object clone() { 598 599 CmsResourceFilter filter = new CmsResourceFilter(); 600 601 filter.m_cacheId = m_cacheId; 602 filter.m_expireAfter = m_expireAfter; 603 filter.m_expireBefore = m_expireBefore; 604 filter.m_filterExpire = m_filterExpire; 605 filter.m_filterFlags = m_filterFlags; 606 filter.m_filterLastModified = m_filterLastModified; 607 filter.m_filterRelease = m_filterRelease; 608 filter.m_filterState = m_filterState; 609 filter.m_filterTimerange = m_filterTimerange; 610 filter.m_filterType = m_filterType; 611 filter.m_filterVisible = m_filterVisible; 612 filter.m_flags = m_flags; 613 filter.m_modifiedAfter = m_modifiedAfter; 614 filter.m_modifiedBefore = m_modifiedBefore; 615 filter.m_onlyFolders = m_onlyFolders; 616 filter.m_releaseAfter = m_releaseAfter; 617 filter.m_releaseBefore = m_releaseBefore; 618 filter.m_state = m_state; 619 filter.m_type = m_type; 620 621 return filter; 622 } 623 624 /** 625 * return if the stored flags should be excluded while filtering resources.<p> 626 * 627 * @return if the flags should be excluded 628 */ 629 public boolean excludeFlags() { 630 631 return m_filterFlags == EXCLUDED; 632 } 633 634 /** 635 * return if the stored state should be excluded while filtering resources.<p> 636 * 637 * @return if the state should be excluded 638 */ 639 public boolean excludeState() { 640 641 return m_filterState == EXCLUDED; 642 } 643 644 /** 645 * Returns if the stored type should be excluded while filtering resources.<p> 646 * 647 * @return if the type should be excluded 648 */ 649 public boolean excludeType() { 650 651 return m_filterType == EXCLUDED; 652 } 653 654 /** 655 * Returns the unique cache id for this filter.<p> 656 * 657 * @return the unique cache id for this filter 658 */ 659 public String getCacheId() { 660 661 return m_cacheId; 662 } 663 664 /** 665 * Returns the start of the expire time range for this filter.<p> 666 * 667 * @return start of the expire time range for this filter 668 */ 669 public long getExpireAfter() { 670 671 return m_expireAfter; 672 } 673 674 /** 675 * Returns the end of the expire time range for this filter.<p> 676 * 677 * @return the end of the expire time range for this filter 678 */ 679 public long getExpireBefore() { 680 681 return m_expireBefore; 682 } 683 684 /** 685 * Returns the flags for this filter.<p> 686 * 687 * @return the flags for this filter 688 */ 689 public int getFlags() { 690 691 return m_flags; 692 } 693 694 /** 695 * Returns the start of the modification time range for this filter.<p> 696 * 697 * @return start of the modification time range for this filter 698 */ 699 public long getModifiedAfter() { 700 701 return m_modifiedAfter; 702 } 703 704 /** 705 * Returns the end of the modification time range for this filter.<p> 706 * 707 * @return the end of the modification time range for this filter 708 */ 709 public long getModifiedBefore() { 710 711 return m_modifiedBefore; 712 } 713 714 /** 715 * Returns the state of the "only folders" flag.<p> 716 * 717 * If the result is <code>null</code>, then this flag is not set.<p> 718 * 719 * @return the state of the "only folders" flag 720 */ 721 public Boolean getOnlyFolders() { 722 723 return m_onlyFolders; 724 } 725 726 /** 727 * Returns the start of the release time range for this filter.<p> 728 * 729 * @return start of the release time range for this filter 730 */ 731 public long getReleaseAfter() { 732 733 return m_releaseAfter; 734 } 735 736 /** 737 * Returns the end of the release time range for this filter.<p> 738 * 739 * @return the end of the release time range for this filter 740 */ 741 public long getReleaseBefore() { 742 743 return m_releaseBefore; 744 } 745 746 /** 747 * Returns the state for this filter.<p> 748 * 749 * @return the state for this filter 750 */ 751 public CmsResourceState getState() { 752 753 return m_state; 754 } 755 756 /** 757 * Returns the type for this filter.<p> 758 * 759 * @return the type for this filter 760 */ 761 public int getType() { 762 763 return m_type; 764 } 765 766 /** 767 * Check if deleted resources should be filtered.<p> 768 * 769 * @return true if deleted resources should be included, false otherwise 770 */ 771 public boolean includeDeleted() { 772 773 return (m_filterState == IGNORED) 774 || ((m_filterState == REQUIRED) && m_state.isDeleted()) 775 || ((m_filterState == EXCLUDED) && !m_state.isDeleted()); 776 } 777 778 /** 779 * Validates if a CmsResource fits to all filer settings.<p> 780 * 781 * Please note that the "visible permission" setting of the filter is NOT checked 782 * in this method since the permission information is not part of the resource. 783 * The visible permission information in the filter will be used in the permission 784 * checks 785 * 786 * @param context the current request context 787 * @param resource the resource to be validated 788 * @return true if the resource passes all validations, false otherwise 789 */ 790 public boolean isValid(CmsRequestContext context, CmsResource resource) { 791 792 if (this == ALL) { 793 // shortcut for "ALL" filter where nothing is filtered 794 return true; 795 } 796 797 // check for required resource state 798 switch (m_filterState) { 799 case EXCLUDED: 800 if (resource.getState().equals(m_state)) { 801 return false; 802 } 803 break; 804 case REQUIRED: 805 if (!resource.getState().equals(m_state)) { 806 return false; 807 } 808 break; 809 default: 810 // ignored 811 } 812 813 // check for required resource state 814 switch (m_filterFlags) { 815 case EXCLUDED: 816 if ((resource.getFlags() & m_flags) != 0) { 817 return false; 818 } 819 break; 820 case REQUIRED: 821 if ((resource.getFlags() & m_flags) != m_flags) { 822 return false; 823 } 824 break; 825 default: 826 // ignored 827 } 828 829 // check for required resource type 830 switch (m_filterType) { 831 case EXCLUDED: 832 if (resource.getTypeId() == m_type) { 833 return false; 834 } 835 break; 836 case REQUIRED: 837 if (resource.getTypeId() != m_type) { 838 return false; 839 } 840 break; 841 default: 842 // ignored 843 } 844 845 if (m_onlyFolders != null) { 846 if (m_onlyFolders.booleanValue()) { 847 if (!resource.isFolder()) { 848 // only folder resource are allowed 849 return false; 850 } 851 } else { 852 if (resource.isFolder()) { 853 // no folder resources are allowed 854 return false; 855 } 856 } 857 } 858 859 // check if the resource was last modified within the given time range 860 if (m_filterLastModified) { 861 if ((m_modifiedAfter > 0L) && (resource.getDateLastModified() < m_modifiedAfter)) { 862 return false; 863 } 864 if ((m_modifiedBefore > 0L) && (resource.getDateLastModified() > m_modifiedBefore)) { 865 return false; 866 } 867 } 868 869 // check if the resource expires within the given time range 870 if (m_filterExpire) { 871 if ((m_expireAfter > 0L) && (resource.getDateExpired() < m_expireAfter)) { 872 return false; 873 } 874 if ((m_expireBefore > 0L) && (resource.getDateExpired() > m_expireBefore)) { 875 return false; 876 } 877 } 878 879 // check if the resource is released within the given time range 880 if (m_filterRelease) { 881 if ((m_releaseAfter > 0L) && (resource.getDateReleased() < m_releaseAfter)) { 882 return false; 883 } 884 if ((m_releaseBefore > 0L) && (resource.getDateReleased() > m_releaseBefore)) { 885 return false; 886 } 887 } 888 889 // check if the resource is currently released and not expired 890 if (m_filterTimerange && !resource.isReleasedAndNotExpired(context.getRequestTime())) { 891 return false; 892 } 893 894 // everything is ok, so return true 895 return true; 896 } 897 898 /** 899 * Returns if the stored flags is required while filtering resources.<p> 900 * 901 * @return if the flags is required 902 */ 903 public boolean requireFlags() { 904 905 return m_filterFlags == REQUIRED; 906 } 907 908 /** 909 * Returns if the stored state is required while filtering resources.<p> 910 * 911 * @return if the state is required 912 */ 913 public boolean requireState() { 914 915 return m_filterState == REQUIRED; 916 } 917 918 /** 919 * Returns if the release timerange of the resource should be required.<p> 920 * 921 * @return true if the release timerange of the resource should be required 922 */ 923 public boolean requireTimerange() { 924 925 return m_filterTimerange; 926 } 927 928 /** 929 * Returns if the stored type is required while filtering resources.<p> 930 * 931 * @return true if the type is required 932 */ 933 public boolean requireType() { 934 935 return m_filterType == REQUIRED; 936 } 937 938 /** 939 * Returns if the visible permission should be required for resources.<p> 940 * 941 * @return true if the visible permission is required, false if the visible permission is ignored 942 */ 943 public boolean requireVisible() { 944 945 return m_filterVisible; 946 } 947 948 /** 949 * Returns the name of the filter, if it is one of the filters used as a constant of this class and 950 * a default message otherwise.<p> 951 * 952 * @return the name of the filter 953 */ 954 @Override 955 public String toString() { 956 957 if (equals(CmsResourceFilter.ALL)) { 958 return "ALL"; 959 } else if (equals(CmsResourceFilter.ALL_MODIFIED)) { 960 return "ALL_MODIFIED"; 961 } else if (equals(CmsResourceFilter.DEFAULT)) { 962 return "DEFAULT"; 963 } else if (equals(CmsResourceFilter.DEFAULT_FILES)) { 964 return "DEFAULT_FILES"; 965 } else if (equals(CmsResourceFilter.DEFAULT_FOLDERS)) { 966 return "DEFAULT_FOLDERS"; 967 } else if (equals(CmsResourceFilter.IGNORE_EXPIRATION)) { 968 return "IGNORE_EXPIRATION"; 969 } else if (equals(CmsResourceFilter.ONLY_VISIBLE)) { 970 return "ONLY_VISIBLE"; 971 } else if (equals(CmsResourceFilter.ONLY_VISIBLE_NO_DELETED)) { 972 return "ONLY_VISIBLE_NO_DELETED"; 973 } else { 974 return "Nonstandard Resource Filter"; 975 } 976 } 977 978 /** 979 * Recalculates the cache id.<p> 980 */ 981 private void updateCacheId() { 982 983 StringBuffer result = new StringBuffer(32); 984 if (m_filterVisible) { 985 result.append(" Vi"); 986 } 987 if (m_filterTimerange) { 988 result.append(" Ti"); 989 } 990 switch (m_filterState) { 991 case REQUIRED: 992 result.append(" Sr"); 993 result.append(m_state); 994 break; 995 case EXCLUDED: 996 result.append(" Sx"); 997 result.append(m_state); 998 break; 999 default: 1000 // ignored 1001 } 1002 switch (m_filterFlags) { 1003 case REQUIRED: 1004 result.append(" Fr"); 1005 result.append(m_flags); 1006 break; 1007 case EXCLUDED: 1008 result.append(" Fx"); 1009 result.append(m_flags); 1010 break; 1011 default: 1012 // ignored 1013 } 1014 switch (m_filterType) { 1015 case REQUIRED: 1016 result.append(" Tr"); 1017 result.append(m_type); 1018 break; 1019 case EXCLUDED: 1020 result.append(" Tx"); 1021 result.append(m_type); 1022 break; 1023 default: 1024 // ignored 1025 } 1026 if (m_onlyFolders != null) { 1027 if (m_onlyFolders.booleanValue()) { 1028 result.append(" Fo"); 1029 } else { 1030 result.append(" Fi"); 1031 } 1032 } 1033 if (m_filterLastModified) { 1034 result.append(" Lt"); 1035 result.append(m_modifiedAfter); 1036 result.append("-"); 1037 result.append(m_modifiedBefore); 1038 } 1039 if (m_filterExpire) { 1040 result.append(" Ex"); 1041 result.append(m_expireAfter); 1042 result.append("-"); 1043 result.append(m_expireBefore); 1044 } 1045 if (m_filterRelease) { 1046 result.append(" Re"); 1047 result.append(m_releaseAfter); 1048 result.append("-"); 1049 result.append(m_releaseBefore); 1050 } 1051 m_cacheId = result.toString(); 1052 } 1053}