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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.common.context;
26 import java.security.acl.Group;
27 import java.util.Enumeration;
28 import java.util.HashMap;
29 import java.util.List;
32 import javax.security.auth.Subject;
33 import javax.security.jacc.PolicyContext;
34 import javax.security.jacc.PolicyContextException;
35 import org.collectionspace.authentication.CSpaceTenant;
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;
49 * AbstractServiceContext
51 * $LastChangedRevision: $
54 public abstract class AbstractServiceContext<IT, OT>
55 implements ServiceContext<IT, OT> {
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;
64 public AbstractServiceContext(String serviceName) throws UnauthorizedException {
65 TenantBindingConfigReader tReader =
66 ServiceMain.getInstance().getTenantBindingConfigReader();
67 //FIXME retrieveTenantId is not working consistently in non-auth mode
68 //TODO: get tenant binding from security context
69 String tenantId = retrieveTenantId();
70 if (tenantId == null) {
71 //for testing purposes
72 tenantId = "1"; //hardcoded for movingimages.us
74 tenantBinding = tReader.getTenantBinding(tenantId);
75 if (tenantBinding == null) {
76 String msg = "No tenant binding found for tenantId=" + tenantId
77 + " while processing request for service= " + serviceName;
79 throw new IllegalStateException(msg);
81 serviceBinding = tReader.getServiceBinding(tenantId, serviceName);
82 if (serviceBinding == null) {
83 String msg = "No service binding found while processing request for "
84 + serviceName + " for tenant id=" + getTenantId()
85 + " name=" + getTenantName();
87 throw new IllegalStateException(msg);
89 if (logger.isDebugEnabled()) {
90 logger.debug("tenantId=" + tenantId
91 + " service binding=" + serviceBinding.getName());
96 * getCommonPartLabel get common part label
100 public String getCommonPartLabel() {
101 return getCommonPartLabel(getServiceName());
105 * getCommonPartLabel get common part label
108 public String getCommonPartLabel(String schemaName) {
109 return schemaName.toLowerCase() + PART_LABEL_SEPERATOR + PART_COMMON_LABEL;
113 public Map<String, ObjectPartType> getPartsMetadata() {
114 if (objectPartMap.size() != 0) {
115 return objectPartMap;
117 ServiceBindingType serviceBinding = getServiceBinding();
118 ServiceObjectType objectType = serviceBinding.getObject();
119 List<ObjectPartType> objectPartTypes = objectType.getPart();
120 for (ObjectPartType objectPartType : objectPartTypes) {
121 objectPartMap.put(objectPartType.getLabel(), objectPartType);
123 return objectPartMap;
127 public String getQualifiedServiceName() {
128 return TenantBindingConfigReader.getTenantQualifiedServiceName(getTenantId(), getServiceName());
132 public String getRepositoryClientName() {
133 return serviceBinding.getRepositoryClient();
137 public ClientType getRepositoryClientType() {
138 //assumption: there is only one repository client configured
139 return ServiceMain.getInstance().getClientType();
143 public String getRepositoryDomainName() {
144 return tenantBinding.getRepositoryDomain();
148 public String getRepositoryWorkspaceId() {
149 TenantBindingConfigReader tbConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
150 return tbConfigReader.getWorkspaceId(getTenantId(), getServiceName());
154 public String getRepositoryWorkspaceName() {
155 //service name is workspace name by convention
156 return serviceBinding.getName();
160 public ServiceBindingType getServiceBinding() {
161 return serviceBinding;
165 public String getServiceName() {
166 return serviceBinding.getName();
170 public String getDocumentType() {
171 // If they have not overridden the setting, use the type of the service
173 return (overrideDocumentType != null) ? overrideDocumentType : serviceBinding.getObject().getName();
177 public void setDocumentType(String docType) {
178 overrideDocumentType = docType;
182 public String getTenantId() {
183 return tenantBinding.getId();
187 public String getTenantName() {
188 return tenantBinding.getName();
192 public abstract IT getInput();
195 public abstract void setInput(IT input);
198 public abstract OT getOutput();
201 public abstract void setOutput(OT output);
204 public Map<String, Object> getProperties() {
209 public void setProperties(Map<String, Object> props) {
210 properties.putAll(props);
213 public Object getProperty(String name) {
214 return properties.get(name);
217 public void setProperty(String name, Object o) {
218 properties.put(name, o);
220 private static final String SUBJECT_CONTEXT_KEY = "javax.security.auth.Subject.container";
222 private String retrieveTenantId() throws UnauthorizedException {
224 String tenantId = null;
225 Subject caller = null;
226 Set<Group> groups = null;
228 caller = (Subject) PolicyContext.getContext(SUBJECT_CONTEXT_KEY);
229 if (caller == null) {
230 //logger.warn("security not enabled...");
233 groups = caller.getPrincipals(Group.class);
234 if (groups != null && groups.size() == 0) {
235 //TODO: find out why subject is not null
236 if (logger.isDebugEnabled()) {
237 logger.debug("no tenant(s) found!");
241 } catch (PolicyContextException pce) {
242 String msg = "Could not retrieve principal information";
243 logger.error(msg, pce);
244 throw new UnauthorizedException(msg);
246 for (Group g : groups) {
247 if ("Tenants".equals(g.getName())) {
248 Enumeration members = g.members();
249 while (members.hasMoreElements()) {
250 CSpaceTenant tenant = (CSpaceTenant) members.nextElement();
251 tenantId = tenant.getId();
252 if (logger.isDebugEnabled()) {
253 logger.debug("found tenant id=" + tenant.getId()
254 + " name=" + tenant.getName());
259 //TODO: if a user is associated with more than one tenants, the tenant
260 //id should be matched with sent over the wire
261 if (tenantId == null) {
262 String msg = "Could not find tenant context";
264 throw new UnauthorizedException(msg);
270 public String toString() {
271 StringBuilder msg = new StringBuilder();
272 msg.append("AbstractServiceContext [");
273 msg.append("service name=" + serviceBinding.getName() + " ");
274 msg.append("service version=" + serviceBinding.getVersion() + " ");
275 msg.append("tenant id=" + tenantBinding.getId() + " ");
276 msg.append("tenant name=" + tenantBinding.getName() + " ");
277 msg.append(tenantBinding.getDisplayName() + " ");
278 msg.append("tenant repository domain=" + tenantBinding.getRepositoryDomain());
279 for (Map.Entry<String, Object> entry : properties.entrySet()) {
280 msg.append("property name=" + entry.getKey() + " value=" + entry.getValue().toString());
283 return msg.toString();