Subversion Repositories XServices

Rev

Rev 177 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 177 Rev 185
1
/*
1
/*
2
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
2
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
3
 *
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
6
 *   You may obtain a copy of the License at
7
 *
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
14
 *   limitations under the License.
15
*/
15
*/
16
 
16
 
17
package net.brutex.xservices.security;
17
package net.brutex.xservices.security;
18
 
18
 
19
import net.brutex.xservices.ws.XmlService;
19
import net.brutex.xservices.ws.XmlService;
20
import net.brutex.xservices.ws.rs.FileInfo;
20
import net.brutex.xservices.ws.rs.FileInfo;
21
 
21
 
-
 
22
import org.apache.logging.log4j.Logger;
22
import org.apache.log4j.Logger;
23
import org.apache.logging.log4j.LogManager;
23
import org.apache.shiro.authz.Permission;
24
import org.apache.shiro.authz.Permission;
24
import org.apache.shiro.authz.permission.InvalidPermissionStringException;
25
import org.apache.shiro.authz.permission.InvalidPermissionStringException;
25
 
26
 
26
/**
27
/**
27
 * @author Brian Rosenberger, bru(at)brutex.de
28
 * @author Brian Rosenberger, bru(at)brutex.de
28
 *
29
 *
29
 */
30
 */
30
public class PermissionResolver implements org.apache.shiro.authz.permission.PermissionResolver {
31
public class PermissionResolver implements org.apache.shiro.authz.permission.PermissionResolver {
31
	
32
	
32
	final Logger logger = Logger.getLogger(PermissionResolver.class);
33
	private final Logger logger = LogManager.getLogger();
33
	private final String DELIMITER = "||";
34
	private final String DELIMITER = "||";
34
 
35
 
35
	@Override
36
	@Override
36
	public Permission resolvePermission(String permissionString) {
37
	public Permission resolvePermission(String permissionString) {
37
		logger.debug(String.format("Trying to examine new Permission '%s'", permissionString));
38
		logger.debug(String.format("Trying to examine new Permission '%s'", permissionString));
38
		if(! permissionString.contains(DELIMITER)) {
39
		if(! permissionString.contains(DELIMITER)) {
39
			logger.error(String.format("Permission '%s' is missing permission class.", permissionString));
40
			logger.error(String.format("Permission '%s' is missing permission class.", permissionString));
40
			throw new InvalidPermissionStringException(String.format("Permission '%s' is missing permission class.", permissionString), permissionString);
41
			throw new InvalidPermissionStringException(String.format("Permission '%s' is missing permission class.", permissionString), permissionString);
41
		}
42
		}
42
		int delimiterIndex = permissionString.indexOf(DELIMITER);
43
		int delimiterIndex = permissionString.indexOf(DELIMITER);
43
		String value = permissionString.substring(0, delimiterIndex);
44
		String value = permissionString.substring(0, delimiterIndex);
44
		delimiterIndex = delimiterIndex + DELIMITER.length();
45
		delimiterIndex = delimiterIndex + DELIMITER.length();
45
		switch (value) {
46
		switch (value) {
46
		case XmlService.SERVICE_NAME:
47
		case XmlService.SERVICE_NAME:
47
			logger.debug(String.format("Found '%s' as permission class.", value));
48
			logger.debug(String.format("Found '%s' as permission class.", value));
48
			return new XmlServicePermission(permissionString.substring( delimiterIndex ));
49
			return new XmlServicePermission(permissionString.substring( delimiterIndex ));
49
		
50
		
50
		case FileInfo.SERVICE_NAME:
51
		case FileInfo.SERVICE_NAME:
51
			logger.debug(String.format("Found '%s' as permission class.", value));
52
			logger.debug(String.format("Found '%s' as permission class.", value));
52
			return new DirectoryPermission(permissionString.substring( delimiterIndex ));
53
			return new DirectoryPermission(permissionString.substring( delimiterIndex ));
53
 
54
 
54
 
55
 
55
		default:
56
		default:
56
			logger.warn(String.format("Permission class '%s' is not defined.", value));
57
			logger.warn(String.format("Permission class '%s' is not defined.", value));
57
			throw new InvalidPermissionStringException(String.format("Permission class '%s' is not defined.", value), permissionString);
58
			throw new InvalidPermissionStringException(String.format("Permission class '%s' is not defined.", value), permissionString);
58
		}
59
		}
59
	}
60
	}
60
}
61
}