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.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
30 import javax.ws.rs.core.MultivaluedMap;
31 import javax.ws.rs.core.UriInfo;
33 import org.collectionspace.services.client.IQueryManager;
34 import org.collectionspace.services.client.workflow.WorkflowClient;
35 import org.collectionspace.services.common.ServiceMain;
36 import org.collectionspace.services.common.config.PropertyItemUtils;
37 import org.collectionspace.services.common.config.ServiceConfigUtils;
38 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
39 import org.collectionspace.services.common.document.DocumentHandler;
40 import org.collectionspace.services.common.document.DocumentFilter;
41 import org.collectionspace.services.common.document.ValidatorHandler;
42 import org.collectionspace.services.common.security.SecurityContext;
43 import org.collectionspace.services.common.security.SecurityContextImpl;
44 import org.collectionspace.services.common.security.UnauthorizedException;
45 import org.collectionspace.services.config.ClientType;
46 import org.collectionspace.services.config.service.ObjectPartType;
47 import org.collectionspace.services.config.service.ServiceBindingType;
48 import org.collectionspace.services.config.tenant.RepositoryDomainType;
49 import org.collectionspace.services.config.tenant.TenantBindingType;
50 import org.collectionspace.services.config.types.PropertyItemType;
51 import org.collectionspace.services.config.types.PropertyType;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * AbstractServiceContext
58 * $LastChangedRevision: $
73 public abstract class AbstractServiceContextImpl<IT, OT>
74 implements ServiceContext<IT, OT> {
77 final Logger logger = LoggerFactory.getLogger(AbstractServiceContextImpl.class);
78 /** The properties. */
79 Map<String, Object> properties = new HashMap<String, Object>();
80 /** The object part map. */
81 Map<String, ObjectPartType> objectPartMap = new HashMap<String, ObjectPartType>();
82 /** The service binding. */
83 protected ServiceBindingType serviceBinding;
84 /** The tenant binding. */
85 private TenantBindingType tenantBinding;
86 /** repository domain used by the service */
87 private RepositoryDomainType repositoryDomain;
88 /** The override document type. */
89 private String overrideDocumentType = null;
90 /** The val handlers. */
91 private List<ValidatorHandler<IT, OT>> valHandlers = null;
92 /** The doc handler. */
93 private DocumentHandler docHandler = null;
94 /** security context */
95 private SecurityContext securityContext;
96 /** The sessions JAX-RS URI information */
97 private UriInfo uriInfo;
98 /** The current repository session */
99 private Object currentRepositorySession;
100 /** A reference count for the current repository session */
101 private int currentRepoSesssionRefCount = 0;
104 * Instantiates a new abstract service context impl.
106 private AbstractServiceContextImpl() {
107 // private constructor for singleton pattern
109 // request query params
110 /** The query params. */
111 private MultivaluedMap<String, String> queryParams;
114 * Instantiates a new abstract service context impl.
116 * @param serviceName the service name
118 * @throws UnauthorizedException the unauthorized exception
120 protected AbstractServiceContextImpl(String serviceName) throws UnauthorizedException {
122 //establish security context
123 securityContext = new SecurityContextImpl();
124 //make sure tenant context exists
125 checkTenantContext();
127 //retrieve service bindings
128 TenantBindingConfigReaderImpl tReader =
129 ServiceMain.getInstance().getTenantBindingConfigReader();
130 String tenantId = securityContext.getCurrentTenantId();
131 tenantBinding = tReader.getTenantBinding(tenantId);
132 if (tenantBinding == null) {
133 String msg = "No tenant binding found for tenantId=" + tenantId
134 + " while processing request for service= " + serviceName;
136 throw new IllegalStateException(msg);
138 serviceBinding = tReader.getServiceBinding(tenantId, serviceName);
139 if (serviceBinding == null) {
140 String msg = "No service binding found while processing request for "
141 + serviceName + " for tenant id=" + getTenantId()
142 + " name=" + getTenantName();
144 throw new IllegalStateException(msg);
146 if (logger.isDebugEnabled()) {
147 logger.debug("tenantId=" + tenantId
148 + " service binding=" + serviceBinding.getName());
150 repositoryDomain = tReader.getRepositoryDomain(tenantId, serviceName);
151 if (repositoryDomain != null) {
152 if (logger.isDebugEnabled()) {
153 logger.debug("tenantId=" + tenantId
154 + " repository doamin=" + repositoryDomain.getName());
160 * @see org.collectionspace.services.common.context.ServiceContext#getCommonPartLabel()
163 public String getCommonPartLabel() {
164 return getCommonPartLabel(getServiceName());
168 * @see org.collectionspace.services.common.context.ServiceContext#getCommonPartLabel(java.lang.String)
170 public String getCommonPartLabel(String schemaName) {
171 return schemaName.toLowerCase() + PART_LABEL_SEPARATOR + PART_COMMON_LABEL;
175 * @see org.collectionspace.services.common.context.ServiceContext#getPartsMetadata()
178 public Map<String, ObjectPartType> getPartsMetadata() {
179 if (objectPartMap.size() != 0) {
180 return objectPartMap;
182 ServiceBindingUtils.getPartsMetadata(getServiceBinding(), objectPartMap);
183 return objectPartMap;
187 * Gets the properties for part.
189 * @param partLabel the part label
191 * @return the properties for part
193 public List<PropertyItemType> getPropertiesForPart(String partLabel) {
194 Map<String, ObjectPartType> partMap = getPartsMetadata();
195 ObjectPartType part = partMap.get(partLabel);
197 throw new RuntimeException("No such part found: " + partLabel);
199 List<PropertyType> propNodeList = part.getProperties();
200 return propNodeList.isEmpty() ? null : propNodeList.get(0).getItem();
204 * @param partLabel The name of the scehma part to search in
205 * @param propName The name of the property (or properties) to find
206 * @param qualified Whether the returned values should be qualified with the
207 * partLabel. This is when the property values are schema field references.
208 * @return List of property values for the matched property on the named schema part.
210 public List<String> getPropertyValuesForPart(String partLabel, String propName, boolean qualified) {
211 List<PropertyItemType> allProps = getPropertiesForPart(partLabel);
212 return PropertyItemUtils.getPropertyValuesByName(allProps, propName,
213 (qualified ? (partLabel + ":") : null));
217 * @param propName The name of the property (or properties) to find
218 * @param qualified Whether the returned values should be qualified with the
219 * partLabel. This is when the property values are schema field references.
220 * @return List of property values for the matched property on any schema part.
222 public List<String> getAllPartsPropertyValues(String propName, boolean qualified) {
223 return ServiceBindingUtils.getAllPartsPropertyValues(getServiceBinding(), propName, qualified);
227 * @see org.collectionspace.services.common.context.ServiceContext#getServiceBindingPropertyValue(java.lang.String)
229 public String getServiceBindingPropertyValue(String propName) {
230 return ServiceBindingUtils.getPropertyValue(getServiceBinding(), propName);
234 * Gets the common part properties.
236 * @return the common part properties
238 public List<PropertyItemType> getCommonPartProperties() {
239 return getPropertiesForPart(getCommonPartLabel());
243 * @param propName The name of the property (or properties) to find
244 * @param qualified Whether the returned values should be qualified with the
245 * partLabel. This is when the property values are schema field references.
246 * @return List of property values for the matched property on the common schema part.
248 public List<String> getCommonPartPropertyValues(String propName, boolean qualified) {
249 return getPropertyValuesForPart(getCommonPartLabel(), propName, qualified);
253 * @see org.collectionspace.services.common.context.ServiceContext#getQualifiedServiceName()
256 public String getQualifiedServiceName() {
257 return TenantBindingConfigReaderImpl.getTenantQualifiedServiceName(getTenantId(), getServiceName());
261 * @see org.collectionspace.services.common.context.ServiceContext#getRepositoryClientName()
264 public String getRepositoryClientName() {
265 if (repositoryDomain == null) {
268 return repositoryDomain.getRepositoryClient();
272 * @see org.collectionspace.services.common.context.ServiceContext#getRepositoryClientType()
275 public ClientType getRepositoryClientType() {
276 //assumption: there is only one repository client configured
277 return ServiceMain.getInstance().getClientType();
281 * @see org.collectionspace.services.common.context.ServiceContext#getRepositoryDomainName()
284 public String getRepositoryDomainName() {
285 if (repositoryDomain == null) {
288 return repositoryDomain.getName();
292 * @see org.collectionspace.services.common.context.ServiceContext#getRepositoryDomainName()
295 public String getRepositoryDomainStorageName() {
296 if (repositoryDomain == null) {
299 return repositoryDomain.getStorageName();
303 * @see org.collectionspace.services.common.context.ServiceContext#getRepositoryWorkspaceId()
306 public String getRepositoryWorkspaceId() {
307 return ServiceMain.getInstance().getWorkspaceId(getTenantId(), getServiceName());
311 * @see org.collectionspace.services.common.context.ServiceContext#getRepositoryWorkspaceName()
314 public String getRepositoryWorkspaceName() {
315 //service name is workspace name by convention
316 return serviceBinding.getName();
320 * @see org.collectionspace.services.common.context.ServiceContext#getServiceBinding()
323 public ServiceBindingType getServiceBinding() {
324 return serviceBinding;
328 * @see org.collectionspace.services.common.context.ServiceContext#getServiceName()
331 public String getServiceName() {
332 return serviceBinding.getName();
336 * @see org.collectionspace.services.common.context.ServiceContext#getDocumentType()
339 public String getDocumentType() {
340 // If they have not overridden the setting, use the type of the service
342 return (overrideDocumentType != null) ? overrideDocumentType : serviceBinding.getObject().getName();
346 public String getTenantQualifiedDoctype(String docType) {
347 // If they have not overridden the setting, use the type of the service
349 String result = ServiceBindingUtils.getTenantQualifiedDocType(this.getTenantId(), docType);
355 public String getTenantQualifiedDoctype() {
356 String docType = (overrideDocumentType != null) ? overrideDocumentType : serviceBinding.getObject().getName();
357 return getTenantQualifiedDoctype(docType);
361 * @see org.collectionspace.services.common.context.ServiceContext#setDocumentType(java.lang.String)
364 public void setDocumentType(String docType) {
365 overrideDocumentType = docType;
369 * @see org.collectionspace.services.common.context.ServiceContext#getSecurityContext()
372 public SecurityContext getSecurityContext() {
373 return securityContext;
377 * @see org.collectionspace.services.common.context.ServiceContext#getUserId()
380 public String getUserId() {
381 return securityContext.getUserId();
385 * @see org.collectionspace.services.common.context.ServiceContext#getTenantId()
388 public String getTenantId() {
389 return securityContext.getCurrentTenantId();
393 * @see org.collectionspace.services.common.context.ServiceContext#getTenantName()
396 public String getTenantName() {
397 return securityContext.getCurrentTenantName();
401 * @see org.collectionspace.services.common.context.ServiceContext#getInput()
404 public abstract IT getInput();
407 * @see org.collectionspace.services.common.context.ServiceContext#setInput(java.lang.Object)
410 public abstract void setInput(IT input);
413 * @see org.collectionspace.services.common.context.ServiceContext#getOutput()
416 public abstract OT getOutput();
419 * @see org.collectionspace.services.common.context.ServiceContext#setOutput(java.lang.Object)
422 public abstract void setOutput(OT output);
425 * @see org.collectionspace.services.common.context.ServiceContext#getProperties()
428 public Map<String, Object> getProperties() {
433 * @see org.collectionspace.services.common.context.ServiceContext#setProperties(java.util.Map)
436 public void setProperties(Map<String, Object> props) {
437 properties.putAll(props);
441 * @see org.collectionspace.services.common.context.ServiceContext#getProperty(java.lang.String)
443 public Object getProperty(String name) {
444 return properties.get(name);
448 * @see org.collectionspace.services.common.context.ServiceContext#setProperty(java.lang.String, java.lang.Object)
450 public void setProperty(String name, Object o) {
451 properties.put(name, o);
455 * checkTenantContext makss sure tenant context exists
459 * @throws UnauthorizedException the unauthorized exception
461 private void checkTenantContext() throws UnauthorizedException {
463 String tenantId = securityContext.getCurrentTenantId();
464 if (tenantId == null) {
465 String msg = "Could not find tenant context";
467 throw new UnauthorizedException(msg);
471 private static String buildWorkflowWhereClause(MultivaluedMap<String, String> queryParams) {
472 String result = null;
474 String includeDeleted = queryParams.getFirst(WorkflowClient.WORKFLOW_QUERY_NONDELETED);
475 if (includeDeleted != null && includeDeleted.equalsIgnoreCase(Boolean.FALSE.toString())) {
476 result = "ecm:currentLifeCycleState <> 'deleted'";
483 * Creates the document handler instance.
485 * @return the document handler
487 * @throws Exception the exception
489 private DocumentHandler createDocumentHandlerInstance() throws Exception {
490 docHandler = ServiceConfigUtils.createDocumentHandlerInstance(tenantBinding, serviceBinding);
492 // Create a default document filter
494 docHandler.setServiceContext(this);
495 DocumentFilter docFilter = docHandler.createDocumentFilter();
497 // If the context was created with query parameters,
498 // reflect the values of those parameters in the document filter
499 // to specify sort ordering, pagination, etc.
501 if (this.getQueryParams() != null) {
502 docFilter.setSortOrder(this.getQueryParams());
503 docFilter.setPagination(this.getQueryParams());
504 String workflowWhereClause = buildWorkflowWhereClause(queryParams);
505 if (workflowWhereClause != null) {
506 docFilter.appendWhereClause(workflowWhereClause, IQueryManager.SEARCH_QUALIFIER_AND);
510 docHandler.setDocumentFilter(docFilter);
516 * @see org.collectionspace.services.common.context.ServiceContext#getDocumentHandler()
519 public DocumentHandler getDocumentHandler() throws Exception {
520 DocumentHandler result = docHandler;
521 // create a new instance if one does not yet exist
522 if (result == null) {
523 result = createDocumentHandlerInstance();
529 * @see org.collectionspace.services.common.context.ServiceContext#getDocumentHanlder(javax.ws.rs.core.MultivaluedMap)
532 public DocumentHandler getDocumentHandler(MultivaluedMap<String, String> queryParams) throws Exception {
533 DocumentHandler result = getDocumentHandler();
534 DocumentFilter documentFilter = result.getDocumentFilter(); //to see results in debugger variables view
535 documentFilter.setPagination(queryParams);
540 * If this element is set in the service binding then use it otherwise
541 * assume that asserts are NOT disabled.
543 private boolean disableValidationAsserts() {
545 Boolean disableAsserts = getServiceBinding().isDisableAsserts();
546 result = (disableAsserts != null) ? disableAsserts : false;
551 * @see org.collectionspace.services.common.context.ServiceContext#getValidatorHandlers()
554 public List<ValidatorHandler<IT, OT>> getValidatorHandlers() throws Exception {
555 if (valHandlers != null) {
558 List<String> handlerClazzes = getServiceBinding().getValidatorHandler();
559 List<ValidatorHandler<IT, OT>> handlers = new ArrayList<ValidatorHandler<IT, OT>>(handlerClazzes.size());
560 ClassLoader tccl = Thread.currentThread().getContextClassLoader();
561 for (String clazz : handlerClazzes) {
562 clazz = clazz.trim();
563 Class<?> c = tccl.loadClass(clazz);
564 if (disableValidationAsserts() == false) {
565 // enable validation assertions
566 tccl.setClassAssertionStatus(clazz, true);
568 if (ValidatorHandler.class.isAssignableFrom(c)) {
569 handlers.add((ValidatorHandler) c.newInstance());
572 valHandlers = handlers;
577 * @see java.lang.Object#toString()
580 public String toString() {
581 StringBuilder msg = new StringBuilder();
582 msg.append("AbstractServiceContext [");
583 msg.append("service name=" + serviceBinding.getName() + " ");
584 msg.append("service version=" + serviceBinding.getVersion() + " ");
585 msg.append("tenant id=" + tenantBinding.getId() + " ");
586 msg.append("tenant name=" + tenantBinding.getName() + " ");
587 msg.append(tenantBinding.getDisplayName() + " ");
588 if (repositoryDomain != null) {
589 msg.append("tenant repository domain=" + repositoryDomain.getName());
591 for (Map.Entry<String, Object> entry : properties.entrySet()) {
592 msg.append("property name=" + entry.getKey() + " value=" + entry.getValue().toString());
595 return msg.toString();
599 * @see org.collectionspace.services.common.context.ServiceContext#getQueryParams()
602 public MultivaluedMap<String, String> getQueryParams() {
604 if (queryParams == null){
605 if (this.uriInfo != null){
606 queryParams = this.uriInfo.getQueryParameters();
609 if (queryParams == null){
610 queryParams = new org.jboss.resteasy.specimpl.MultivaluedMapImpl<String,String>();
612 return this.queryParams;
616 public MultivaluedMap<String, String> getQueryParamsPtr() {
617 return this.queryParams;
621 * @see org.collectionspace.services.common.context.ServiceContext#setQueryParams(javax.ws.rs.core.MultivaluedMap)
624 public void setQueryParams(MultivaluedMap<String, String> theQueryParams) {
625 this.queryParams = theQueryParams;
629 public void setUriInfo(UriInfo ui){
634 public UriInfo getUriInfo() {
639 * We expect the 'currentRepositorySession' member to be set only once per instance. Also, we expect only one open repository session
640 * per HTTP request. We'll log an error if we see more than one attempt to set a service context's current repo session.
642 * @see org.collectionspace.services.common.context.ServiceContext#setCurrentRepositorySession(java.lang.Object)
645 public void setCurrentRepositorySession(Object repoSession) throws Exception {
646 if (repoSession == null) {
647 String errMsg = "Setting a service context's repository session to null is not allowed.";
648 logger.error(errMsg);
649 throw new Exception(errMsg);
650 } else if (currentRepositorySession != null && currentRepositorySession != repoSession) {
651 String errMsg = "The current service context's repository session was replaced. This may cause unexpected behavior and/or data loss.";
652 logger.error(errMsg);
653 throw new Exception(errMsg);
656 currentRepositorySession = repoSession;
657 this.currentRepoSesssionRefCount++;
661 public void clearCurrentRepositorySession() {
662 if (this.currentRepoSesssionRefCount > 0) {
663 currentRepoSesssionRefCount--;
666 if (currentRepoSesssionRefCount == 0) {
667 this.currentRepositorySession = null;
672 public Object getCurrentRepositorySession() {
673 // TODO Auto-generated method stub
674 return currentRepositorySession;