001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.scripting;
016    
017    import com.liferay.portal.util.PropsValues;
018    
019    import java.util.Arrays;
020    import java.util.HashSet;
021    import java.util.Set;
022    import java.util.regex.Matcher;
023    import java.util.regex.Pattern;
024    
025    /**
026     * @author Alberto Montero
027     * @author Brian Wing Shun Chan
028     */
029    public class ClassVisibilityChecker {
030    
031            public static final String ALL_CLASSES = "all_classes";
032    
033            public ClassVisibilityChecker(Set<String> allowedClasses) {
034                    if ((allowedClasses != null) && allowedClasses.contains(ALL_CLASSES)) {
035                            _allowAll = true;
036                    }
037                    else {
038                            _allowAll = false;
039                    }
040    
041                    if (_forbiddenClasses.contains(ALL_CLASSES)) {
042                            _denyAll = true;
043                    }
044                    else {
045                            _denyAll = false;
046                    }
047    
048                    if (!_allowAll && !_denyAll) {
049                            _allowedPatterns = new HashSet<Pattern>();
050    
051                            for (String allowedClass : allowedClasses) {
052                                    Pattern allowedPattern = Pattern.compile(allowedClass);
053    
054                                    _allowedPatterns.add(allowedPattern);
055                            }
056                    }
057                    else {
058                            _allowedPatterns = null;
059                    }
060            }
061    
062            public boolean isVisible(String className) {
063                    if (_denyAll || _forbiddenClasses.contains(className)) {
064                            return false;
065                    }
066    
067                    if (_allowAll) {
068                            return true;
069                    }
070    
071                    for (Pattern allowedPattern : _allowedPatterns) {
072                            Matcher matcher = allowedPattern.matcher(className);
073    
074                            if (matcher.find()) {
075                                    return true;
076                            }
077                    }
078    
079                    return false;
080            }
081    
082            private static final Set<String> _forbiddenClasses = new HashSet<String>(
083                    Arrays.asList(PropsValues.SCRIPTING_FORBIDDEN_CLASSES));
084    
085            private final boolean _allowAll;
086            private final Set<Pattern> _allowedPatterns;
087            private final boolean _denyAll;
088    
089    }