Moved over some of the security stuff from Photos.
[utils] / security / src / main / java / org / wamblee / security / authorization / DefaultOperationRegistry.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16
17 package org.wamblee.security.authorization;
18
19 import java.util.ArrayList;
20 import java.util.Map;
21 import java.util.TreeMap;
22
23 /**
24  * Operation registry implementation. 
25  * This implementation ignores the distinction between different types of resources
26  * and simply assumes that every operation is applicable to every type of resource. 
27  */
28 public class DefaultOperationRegistry implements OperationRegistry {
29
30     private Map<String,Operation> _operations; 
31     
32     public DefaultOperationRegistry(Operation[] aOperations) { 
33         _operations = new TreeMap<String, Operation>();
34         for (Operation operation: aOperations) { 
35             _operations.put(operation.getName(), operation);
36         }
37     }
38     
39     /* (non-Javadoc)
40      * @see org.wamblee.security.authorization.OperationRegistry#getOperations(java.lang.Class)
41      */
42     public Operation[] getOperations(Class aResourceClass) {
43         return _operations.values().toArray(new Operation[0]); 
44     }
45
46     /* (non-Javadoc)
47      * @see org.wamblee.security.authorization.OperationRegistry#encode(org.wamblee.security.authorization.Operation[])
48      */
49     public String encode(Operation[] aOperations) {
50         StringBuffer buffer = new StringBuffer(); 
51         for (Operation operation: aOperations) {
52             if ( buffer.length() > 0 ) { 
53                 buffer.append(',');
54             }
55             buffer.append(operation.getName());
56         }
57         return buffer.toString();
58     }
59
60     /* (non-Javadoc)
61      * @see org.wamblee.security.authorization.OperationRegistry#decode(java.lang.Class, java.lang.String)
62      */
63     public Operation[] decode(Class aResourceClass, String aOperationsString) {
64         return decode(aOperationsString); 
65     }
66     
67     /* (non-Javadoc)
68      * @see org.wamblee.security.authorization.OperationRegistry#decode(java.lang.String)
69      */
70     public Operation[] decode(String aOperationsString) {
71         if ( aOperationsString.length() == 0 ) { 
72             return new Operation[0]; 
73         }
74         String[] names = aOperationsString.split(",");
75         ArrayList<Operation> result = new ArrayList<Operation>(); 
76         for (String name: names) { 
77             Operation operation = _operations.get(name);
78             if ( operation == null ) { 
79                 throw new IllegalArgumentException("Unknown operation '" + name + "'");
80             }
81             result.add(operation);
82         }
83         return result.toArray(new Operation[0]); 
84     }
85
86 }