001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.shiro.util;
020    
021    import org.apache.shiro.authz.Permission;
022    import org.apache.shiro.authz.permission.PermissionResolver;
023    
024    import java.util.Arrays;
025    import java.util.Collection;
026    import java.util.LinkedHashSet;
027    import java.util.Set;
028    
029    
030    /**
031     * Utility class to help with String-to-Permission object resolution.
032     *
033     * @author Les Hazlewood
034     * @author Jeremy Haile
035     * @since 0.1
036     */
037    public class PermissionUtils {
038    
039        public static Set<Permission> resolveDelimitedPermissions(String s, PermissionResolver permissionResolver) {
040            Set<String> permStrings = toPermissionStrings(s);
041            return resolvePermissions(permStrings, permissionResolver);
042        }
043    
044        public static Set<String> toPermissionStrings(String permissionsString) {
045            String[] tokens = StringUtils.split(permissionsString);
046            if (tokens != null && tokens.length > 0) {
047                return new LinkedHashSet<String>(Arrays.asList(tokens));
048            }
049            return null;
050        }
051    
052        public static Set<Permission> resolvePermissions(Collection<String> permissionStrings, PermissionResolver permissionResolver) {
053            Set<Permission> permissions = new LinkedHashSet<Permission>(permissionStrings.size());
054            for (String permissionString : permissionStrings) {
055                permissions.add(permissionResolver.resolvePermission(permissionString));
056            }
057            return permissions;
058        }
059    }