]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f03505800dfea6e648ec1858e004ceb565516211
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.authorization;
25
26 import org.collectionspace.authentication.AuthN;
27
28 /**
29  * CSpaceResourceImpl abstract resource implementation
30  * @author 
31  */
32 public abstract class CSpaceResourceImpl implements CSpaceResource {
33
34     final protected static String SEPARATOR_HASH = "#";
35     final protected static String SEPARATOR_COLON = ":";
36     private String id;
37     private TYPE type;
38     private CSpaceAction action;
39     private String tenantId;
40
41     private CSpaceResourceImpl() {
42     }
43
44     /**
45      * constructor that uses logged in user's tenant context to associate resource with
46      * @param id
47      * @param action
48      * @param type
49      */
50     public CSpaceResourceImpl(String id, CSpaceAction action, TYPE type) {
51         setup(id, action, type);
52         tenantId = AuthN.get().getCurrentTenantId();
53     }
54
55     /**
56      * constructor that uses given tenant id to associate the resource with
57      * @param tenantId
58      * @param id
59      * @param action
60      * @param type
61      */
62     public CSpaceResourceImpl(String tenantId, String id, CSpaceAction action, TYPE type) {
63         setup(id, action, type);
64         if (tenantId == null) {
65             throw new IllegalArgumentException("tenantId cannot be null");
66         }
67         this.tenantId = tenantId;
68     }
69
70     private void setup(String id, CSpaceAction action, TYPE type) {
71         if (id == null || id.isEmpty()) {
72             throw new IllegalArgumentException("id cannot be null or empty");
73         }
74         this.id = id.toLowerCase();
75         if (type == null) {
76             throw new IllegalArgumentException("type cannot be null");
77         }
78         this.type = type;
79         if (action == null) {
80             throw new IllegalArgumentException("action cannot be null");
81         }
82         this.action = action;
83     }
84
85     @Override
86     public String getId() {
87         //tenant-qualified id
88         return tenantId + SEPARATOR_COLON + id;
89     }
90     
91     @Override
92     public Long getHashedId() {
93         return Long.valueOf(getId().hashCode());
94     }
95
96     @Override
97     public TYPE getType() {
98         return type;
99     }
100
101     @Override
102     public String getTenantId() {
103         return tenantId;
104     }
105
106     /**
107      * getAction a convenience method to get action invoked on the resource
108      */
109     @Override
110     public CSpaceAction getAction() {
111         return action;
112     }
113
114     @Override
115     public String toString() {
116         StringBuilder builder = new StringBuilder();
117         builder.append("CSpaceResourceImpl [");
118         builder.append("id=");
119         builder.append(id);
120         builder.append(", type=");
121         builder.append(type);
122         builder.append(", tenantId=");
123         builder.append(tenantId);
124         builder.append(", action=");
125         builder.append(action.toString());
126         builder.append("]");
127         return builder.toString();
128     }
129 }