View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.util;
20  
21  import org.apache.shiro.authz.Permission;
22  import org.apache.shiro.authz.permission.PermissionResolver;
23  
24  import java.util.Arrays;
25  import java.util.Collection;
26  import java.util.LinkedHashSet;
27  import java.util.Set;
28  
29  
30  /**
31   * Utility class to help with String-to-Permission object resolution.
32   *
33   * @since 0.1
34   */
35  public class PermissionUtils {
36  
37      public static Set<Permission> resolveDelimitedPermissions(String s, PermissionResolver permissionResolver) {
38          Set<String> permStrings = toPermissionStrings(s);
39          return resolvePermissions(permStrings, permissionResolver);
40      }
41  
42      public static Set<String> toPermissionStrings(String permissionsString) {
43          String[] tokens = StringUtils.split(permissionsString);
44          if (tokens != null && tokens.length > 0) {
45              return new LinkedHashSet<String>(Arrays.asList(tokens));
46          }
47          return null;
48      }
49  
50      public static Set<Permission> resolvePermissions(Collection<String> permissionStrings, PermissionResolver permissionResolver) {
51          Set<Permission> permissions = new LinkedHashSet<Permission>(permissionStrings.size());
52          for (String permissionString : permissionStrings) {
53              permissions.add(permissionResolver.resolvePermission(permissionString));
54          }
55          return permissions;
56      }
57  }