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 */ 019package org.apache.shiro.util; 020 021import org.apache.shiro.authz.Permission; 022import org.apache.shiro.authz.permission.PermissionResolver; 023import org.apache.shiro.lang.util.StringUtils; 024 025import java.util.Arrays; 026import java.util.Collection; 027import java.util.LinkedHashSet; 028import java.util.Set; 029 030 031/** 032 * Utility class to help with String-to-Permission object resolution. 033 * 034 * @since 0.1 035 */ 036public final class PermissionUtils { 037 038 private PermissionUtils() { 039 } 040 041 public static Set<Permission> resolveDelimitedPermissions(String s, PermissionResolver permissionResolver) { 042 Set<String> permStrings = toPermissionStrings(s); 043 return resolvePermissions(permStrings, permissionResolver); 044 } 045 046 public static Set<String> toPermissionStrings(String permissionsString) { 047 String[] tokens = StringUtils.split(permissionsString); 048 if (tokens != null && tokens.length > 0) { 049 return new LinkedHashSet<String>(Arrays.asList(tokens)); 050 } 051 return null; 052 } 053 054 public static Set<Permission> resolvePermissions(Collection<String> permissionStrings, 055 PermissionResolver permissionResolver) { 056 Set<Permission> permissions = new LinkedHashSet<>(permissionStrings.size()); 057 for (String permissionString : permissionStrings) { 058 permissions.add(permissionResolver.resolvePermission(permissionString)); 059 } 060 return permissions; 061 } 062}