View Javadoc

1   /*
2   Copyright 2011-2014 AGH-UST, http://www.agh.edu.pl
3   Faculty of Computer Science, Electronics and Telecommunications
4   Department of Computer Science 
5   
6   See the NOTICE file distributed with this work for additional
7   information regarding copyright ownership
8   
9   Licensed under the Apache License, Version 2.0 (the "License");
10  you may not use this file except in compliance with the License.
11  You may obtain a copy of the License at
12  
13    http://www.apache.org/licenses/LICENSE-2.0
14  
15  Unless required by applicable law or agreed to in writing, software
16  distributed under the License is distributed on an "AS IS" BASIS,
17  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  See the License for the specific language governing permissions and
19  limitations under the License.
20  */
21  package org.universAAL.ri.gateway.communicator.service.impl;
22  
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  
27  public enum SecurityManager {
28      Instance;
29      
30      static{
31      	allowImportSecurityEntries = new HashSet<SecurityEntry>();
32      	denyImportSecurityEntries = new HashSet<SecurityEntry>();
33      	
34      	allowExportSecurityEntries = new HashSet<SecurityEntry>();
35      	denyExportSecurityEntries = new HashSet<SecurityEntry>();
36      }
37      
38      private static Set<SecurityEntry> allowImportSecurityEntries;
39      private static Set<SecurityEntry> denyImportSecurityEntries;
40      
41      private static Set<SecurityEntry> allowExportSecurityEntries;
42      private static Set<SecurityEntry> denyExportSecurityEntries;
43     
44      public static boolean isOperationAllowed(String uri, Type type){
45      	switch (type) {
46  		case Import:
47  			if (matchesAnyValue(denyImportSecurityEntries, uri)){
48  	    		return false;
49  	    	}
50  	    	if (matchesAnyValue(allowImportSecurityEntries, uri)){
51  	    		return true;
52  	    	}
53  			break;
54  
55  		case Export:
56  			if (matchesAnyValue(denyExportSecurityEntries, uri)){
57  	    		return false;
58  	    	}
59  	    	if (matchesAnyValue(allowExportSecurityEntries, uri)){
60  	    		return true;
61  	    	}
62  			break;
63  		}
64      	return false;
65      }
66      
67      private static boolean matchesAnyValue(Set<SecurityEntry> entries, String value){
68      	for(SecurityEntry entry : entries){
69      		if (value.matches(entry.getEntryRegex())){
70      			return true;
71      		}
72      	}
73      	return false;
74      }
75  
76  	public Set<SecurityEntry> getAllowImportSecurityEntries() {
77  		return allowImportSecurityEntries;
78  	}
79  
80  	public void setAllowImportSecurityEntries(
81  			Set<SecurityEntry> allowImportSecurityEntries) {
82  		SecurityManager.allowImportSecurityEntries = allowImportSecurityEntries;
83  	}
84  
85  	public Set<SecurityEntry> getDenyImportSecurityEntries() {
86  		return denyImportSecurityEntries;
87  	}
88  
89  	public void setDenyImportSecurityEntries(
90  			Set<SecurityEntry> denyImportSecurityEntries) {
91  		SecurityManager.denyImportSecurityEntries = denyImportSecurityEntries;
92  	}
93  
94  	public Set<SecurityEntry> getAllowExportSecurityEntries() {
95  		return allowExportSecurityEntries;
96  	}
97  
98  	public void setAllowExportSecurityEntries(
99  			Set<SecurityEntry> allowExportSecurityEntries) {
100 		SecurityManager.allowExportSecurityEntries = allowExportSecurityEntries;
101 	}
102 
103 	public Set<SecurityEntry> getDenyExportSecurityEntries() {
104 		return denyExportSecurityEntries;
105 	}
106 
107 	public void setDenyExportSecurityEntries(
108 			Set<SecurityEntry> denyExportSecurityEntries) {
109 		SecurityManager.denyExportSecurityEntries = denyExportSecurityEntries;
110 	}
111 }
112 
113 enum Type {
114 	Import, Export;
115 }
116 
117 enum SecurityAction {
118 	Allow, Deny;
119 }
120