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     * @since 0.1
034     */
035    public class PermissionUtils {
036    
037        public static Set<Permission> resolveDelimitedPermissions(String s, PermissionResolver permissionResolver) {
038            Set<String> permStrings = toPermissionStrings(s);
039            return resolvePermissions(permStrings, permissionResolver);
040        }
041    
042        public static Set<String> toPermissionStrings(String permissionsString) {
043            String[] tokens = StringUtils.split(permissionsString);
044            if (tokens != null && tokens.length > 0) {
045                return new LinkedHashSet<String>(Arrays.asList(tokens));
046            }
047            return null;
048        }
049    
050        public static Set<Permission> resolvePermissions(Collection<String> permissionStrings, PermissionResolver permissionResolver) {
051            Set<Permission> permissions = new LinkedHashSet<Permission>(permissionStrings.size());
052            for (String permissionString : permissionStrings) {
053                permissions.add(permissionResolver.resolvePermission(permissionString));
054            }
055            return permissions;
056        }
057    }