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.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import java.security.Principal;
29 import java.util.HashMap;
30 import java.util.List;
33 import javax.security.auth.Subject;
34 import javax.security.jacc.PolicyContext;
35 import javax.security.jacc.PolicyContextException;
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 //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
73 tenantBinding = tReader.getTenantBinding(tenantId);
74 if (tenantBinding == null) {
75 String msg = "No tenant binding found while processing request for " +
78 throw new IllegalStateException(msg);
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();
86 throw new IllegalStateException(msg);
88 if (logger.isDebugEnabled()) {
89 logger.debug("tenantId=" + tenantId +
90 " service binding=" + serviceBinding.getName());
95 * getCommonPartLabel get common part label
99 public String getCommonPartLabel() {
100 return getCommonPartLabel(getServiceName());
104 * getCommonPartLabel get common part label
107 public String getCommonPartLabel(String schemaName) {
108 return schemaName.toLowerCase() + PART_LABEL_SEPERATOR + PART_COMMON_LABEL;
112 public Map<String, ObjectPartType> getPartsMetadata() {
113 if (objectPartMap.size() != 0) {
114 return objectPartMap;
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);
124 return objectPartMap;
128 public String getQualifiedServiceName() {
129 return TenantBindingConfigReader.getTenantQualifiedServiceName(getTenantId(), getServiceName());
133 public String getRepositoryClientName() {
134 return serviceBinding.getRepositoryClient();
138 public ClientType getRepositoryClientType() {
139 //assumption: there is only one repository client configured
140 return ServiceMain.getInstance().getClientType();
144 public String getRepositoryDomainName() {
145 return tenantBinding.getRepositoryDomain();
149 public String getRepositoryWorkspaceId() {
150 TenantBindingConfigReader tbConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
151 return tbConfigReader.getWorkspaceId(getTenantId(), getServiceName());
155 public String getRepositoryWorkspaceName() {
156 //service name is workspace name by convention
157 return serviceBinding.getName();
161 public ServiceBindingType getServiceBinding() {
162 return serviceBinding;
166 public String getServiceName() {
167 return serviceBinding.getName();
171 public String getDocumentType() {
172 // If they have not overridden the setting, use the type of the service
174 return (overrideDocumentType != null) ? overrideDocumentType : serviceBinding.getObject().get(0).getName();
178 public void setDocumentType(String docType) {
179 overrideDocumentType = docType;
183 public String getTenantId() {
184 return tenantBinding.getId();
188 public String getTenantName() {
189 return tenantBinding.getName();
193 public abstract IT getInput();
196 public abstract void setInput(IT input);
199 public abstract OT getOutput();
202 public abstract void setOutput(OT output);
205 public Map<String, Object> getProperties() {
210 public void setProperties(Map<String, Object> props) {
211 properties.putAll(props);
214 public Object getProperty(String name) {
215 return properties.get(name);
218 public void setProperty(String name, Object o) {
219 properties.put(name, o);
221 private static final String SUBJECT_CONTEXT_KEY = "javax.security.auth.Subject.container";
223 private String retrieveTenantId() throws UnauthorizedException {
225 String tenantId = null;
226 Set<Principal> principals = null;
228 Subject caller = (Subject) PolicyContext.getContext(SUBJECT_CONTEXT_KEY);
230 //logger.warn("security not enabled...");
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);
239 for (Principal p : principals) {
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());
247 tenantId = (String) r;
249 } catch (Exception e) {
250 //continue with next principal
253 if(tenantId == null) {
254 String msg = "Could not find tenant context";
256 throw new UnauthorizedException(msg);
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());
275 return msg.toString();