]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d6d0a4b779de4a59c6f81a82a21f193a41a5ce1f
[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.common.context;
25
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import java.security.Principal;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import javax.security.auth.Subject;
34 import javax.security.jacc.PolicyContext;
35 import javax.security.jacc.PolicyContextException;
36
37 import org.collectionspace.services.common.ClientType;
38 import org.collectionspace.services.common.ServiceMain;
39 import org.collectionspace.services.common.config.TenantBindingConfigReader;
40 import org.collectionspace.services.common.security.UnauthorizedException;
41 import org.collectionspace.services.common.service.ObjectPartType;
42 import org.collectionspace.services.common.service.ServiceBindingType;
43 import org.collectionspace.services.common.service.ServiceObjectType;
44 import org.collectionspace.services.common.tenant.TenantBindingType;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * AbstractServiceContext
50  *
51  * $LastChangedRevision: $
52  * $LastChangedDate: $
53  */
54 public abstract class AbstractServiceContext<IT, OT>
55         implements ServiceContext<IT, OT> {
56
57     final Logger logger = LoggerFactory.getLogger(AbstractServiceContext.class);
58     Map<String, Object> properties = new HashMap<String, Object>();
59     Map<String, ObjectPartType> objectPartMap = new HashMap<String, ObjectPartType>();
60     private ServiceBindingType serviceBinding;
61     private TenantBindingType tenantBinding;
62     private String overrideDocumentType = null;
63
64     public AbstractServiceContext(String serviceName) throws UnauthorizedException {
65         TenantBindingConfigReader tReader =
66                 ServiceMain.getInstance().getTenantBindingConfigReader();
67         //TODO: get tenant binding from security context
68         String tenantId = retrieveTenantId();
69         if (tenantId == null) {
70             //for testing purposes
71             tenantId = "1"; //hardcoded for movingimages.us
72         }
73         tenantBinding = tReader.getTenantBinding(tenantId);
74         if (tenantBinding == null) {
75             String msg = "No tenant binding found while processing request for " +
76                     serviceName;
77             logger.error(msg);
78             throw new IllegalStateException(msg);
79         }
80         serviceBinding = tReader.getServiceBinding(tenantId, serviceName);
81         if (serviceBinding == null) {
82             String msg = "No service binding found while processing request for " +
83                     serviceName + " for tenant id=" + getTenantId() +
84                     " name=" + getTenantName();
85             logger.error(msg);
86             throw new IllegalStateException(msg);
87         }
88         if (logger.isDebugEnabled()) {
89             logger.debug("tenantId=" + tenantId +
90                     " service binding=" + serviceBinding.getName());
91         }
92     }
93
94     /**
95      * getCommonPartLabel get common part label
96      * @return
97      */
98     @Override
99     public String getCommonPartLabel() {
100         return getCommonPartLabel(getServiceName());
101     }
102
103     /**
104      * getCommonPartLabel get common part label
105      * @return
106      */
107     public String getCommonPartLabel(String schemaName) {
108         return schemaName.toLowerCase() + PART_LABEL_SEPERATOR + PART_COMMON_LABEL;
109     }
110
111     @Override
112     public Map<String, ObjectPartType> getPartsMetadata() {
113         if (objectPartMap.size() != 0) {
114             return objectPartMap;
115         }
116         ServiceBindingType serviceBinding = getServiceBinding();
117         List<ServiceObjectType> objectTypes = serviceBinding.getObject();
118         for (ServiceObjectType objectType : objectTypes) {
119             List<ObjectPartType> objectPartTypes = objectType.getPart();
120             for (ObjectPartType objectPartType : objectPartTypes) {
121                 objectPartMap.put(objectPartType.getLabel(), objectPartType);
122             }
123         }
124         return objectPartMap;
125     }
126
127     @Override
128     public String getQualifiedServiceName() {
129         return TenantBindingConfigReader.getTenantQualifiedServiceName(getTenantId(), getServiceName());
130     }
131
132     @Override
133     public String getRepositoryClientName() {
134         return serviceBinding.getRepositoryClient();
135     }
136
137     @Override
138     public ClientType getRepositoryClientType() {
139         //assumption: there is only one repository client configured
140         return ServiceMain.getInstance().getClientType();
141     }
142
143     @Override
144     public String getRepositoryDomainName() {
145         return tenantBinding.getRepositoryDomain();
146     }
147
148     @Override
149     public String getRepositoryWorkspaceId() {
150         TenantBindingConfigReader tbConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
151         return tbConfigReader.getWorkspaceId(getTenantId(), getServiceName());
152     }
153
154     @Override
155     public String getRepositoryWorkspaceName() {
156         //service name is workspace name by convention
157         return serviceBinding.getName();
158     }
159
160     @Override
161     public ServiceBindingType getServiceBinding() {
162         return serviceBinding;
163     }
164
165     @Override
166     public String getServiceName() {
167         return serviceBinding.getName();
168     }
169
170     @Override
171     public String getDocumentType() {
172         // If they have not overridden the setting, use the type of the service
173         // object.
174         return (overrideDocumentType != null) ? overrideDocumentType : serviceBinding.getObject().get(0).getName();
175     }
176
177     @Override
178     public void setDocumentType(String docType) {
179         overrideDocumentType = docType;
180     }
181
182     @Override
183     public String getTenantId() {
184         return tenantBinding.getId();
185     }
186
187     @Override
188     public String getTenantName() {
189         return tenantBinding.getName();
190     }
191
192     @Override
193     public abstract IT getInput();
194
195     @Override
196     public abstract void setInput(IT input);
197
198     @Override
199     public abstract OT getOutput();
200
201     @Override
202     public abstract void setOutput(OT output);
203
204     @Override
205     public Map<String, Object> getProperties() {
206         return properties;
207     }
208
209     @Override
210     public void setProperties(Map<String, Object> props) {
211         properties.putAll(props);
212     }
213
214     public Object getProperty(String name) {
215         return properties.get(name);
216     }
217
218     public void setProperty(String name, Object o) {
219         properties.put(name, o);
220     }
221     private static final String SUBJECT_CONTEXT_KEY = "javax.security.auth.Subject.container";
222
223     private String retrieveTenantId() throws UnauthorizedException {
224
225         String tenantId = null;
226         Set<Principal> principals = null;
227         try {
228             Subject caller = (Subject) PolicyContext.getContext(SUBJECT_CONTEXT_KEY);
229             if(caller == null) {
230                 //logger.warn("security not enabled...");
231                 return tenantId;
232             }
233             principals = caller.getPrincipals(Principal.class);
234         } catch (PolicyContextException pce) {
235             String msg = "Could not retrieve principal information";
236             logger.error(msg, pce);
237             throw new UnauthorizedException(msg);
238         }
239         for (Principal p : principals) {
240             try {
241                 Method m = p.getClass().getMethod("getTenantId");
242                 Object r = m.invoke(p);
243                 if (logger.isDebugEnabled()) {
244                     logger.debug("retrieved tenantid=" + r +
245                             " for principal=" + p.getName());
246                 }
247                 tenantId = (String) r;
248                 break;
249             } catch (Exception e) {
250                 //continue with next principal
251             } 
252         }
253         if(tenantId == null) {
254             String msg = "Could not find tenant context";
255             logger.error(msg);
256             throw new UnauthorizedException(msg);
257         }
258         return tenantId;
259     }
260
261     @Override
262     public String toString() {
263         StringBuilder msg = new StringBuilder();
264         msg.append("AbstractServiceContext [");
265         msg.append("service name=" + serviceBinding.getName() + " ");
266         msg.append("service version=" + serviceBinding.getVersion() + " ");
267         msg.append("tenant id=" + tenantBinding.getId() + " ");
268         msg.append("tenant name=" + tenantBinding.getName() + " ");
269         msg.append(tenantBinding.getDisplayName() + " ");
270         msg.append("tenant repository domain=" + tenantBinding.getRepositoryDomain());
271         for (Map.Entry<String, Object> entry : properties.entrySet()) {
272             msg.append("property name=" + entry.getKey() + " value=" + entry.getValue().toString());
273         }
274         msg.append("]");
275         return msg.toString();
276     }
277 }