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;
26 import java.lang.reflect.Method;
27 import java.util.HashMap;
28 import java.util.List;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.core.MultivaluedMap;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.UriInfo;
38 import org.collectionspace.services.common.CSWebApplicationException;
39 import org.collectionspace.services.common.api.Tools;
40 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
41 import org.collectionspace.services.common.context.ServiceContext;
42 import org.collectionspace.services.common.context.ServiceContextProperties;
43 import org.collectionspace.services.common.document.BadRequestException;
44 import org.collectionspace.services.common.document.DocumentException;
45 import org.collectionspace.services.common.document.DocumentHandler;
46 import org.collectionspace.services.common.document.DocumentNotFoundException;
47 import org.collectionspace.services.common.document.TransactionException;
48 import org.collectionspace.services.common.repository.RepositoryClient;
49 import org.collectionspace.services.common.repository.RepositoryClientFactory;
50 import org.collectionspace.services.common.security.UnauthorizedException;
51 import org.collectionspace.services.common.storage.StorageClient;
52 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
53 import org.collectionspace.services.config.service.ServiceBindingType;
54 import org.jboss.resteasy.core.ResourceMethodInvoker;
55 //import org.jboss.resteasy.core.ResourceMethod;
56 import org.jboss.resteasy.spi.HttpRequest;
57 import org.jboss.resteasy.spi.metadata.ResourceMethod;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
62 * The Class AbstractCollectionSpaceResourceImpl.
64 * @param <IT> the generic type
65 * @param <OT> the generic type
67 public abstract class AbstractCollectionSpaceResourceImpl<IT, OT>
68 implements CollectionSpaceResource<IT, OT> {
70 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
72 protected final ServiceContext<IT, OT> NULL_CONTEXT = null;
73 // Fields for default client factory and client
74 /** The repository client factory. */
75 private RepositoryClientFactory<IT, OT> repositoryClientFactory;
77 /** The repository client. */
78 private RepositoryClient<IT, OT> repositoryClient;
80 /** The storage client. */
81 private StorageClient storageClient;
89 protected static String extractId(Response res) {
90 MultivaluedMap<String, Object> mvm = res.getMetadata();
91 String uri = (String) ((List<Object>) mvm.get("Location")).get(0);
92 String[] segments = uri.split("/");
93 String id = segments[segments.length - 1];
98 * Instantiates a new abstract collection space resource.
100 public AbstractCollectionSpaceResourceImpl() {
101 repositoryClientFactory = (RepositoryClientFactory<IT, OT>) RepositoryClientFactory.getInstance();
105 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceName()
108 abstract public String getServiceName();
112 * @see org.collectionspace.services.common.CollectionSpaceResource#getRepositoryClient(org.collectionspace.services.common.context.ServiceContext)
115 synchronized public RepositoryClient<IT, OT> getRepositoryClient(ServiceContext<IT, OT> ctx) {
116 if(repositoryClient != null){
117 return repositoryClient;
119 repositoryClient = repositoryClientFactory.getClient(ctx.getRepositoryClientName());
120 return repositoryClient;
124 * @see org.collectionspace.services.common.CollectionSpaceResource#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
127 synchronized public StorageClient getStorageClient(ServiceContext<IT, OT> ctx) {
128 if(storageClient != null) {
129 return storageClient;
131 storageClient = new JpaStorageClientImpl();
132 return storageClient;
136 * @see org.collectionspace.services.common.CollectionSpaceResource#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
139 public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx) throws Exception {
140 DocumentHandler docHandler = createDocumentHandler(ctx, ctx.getInput());
145 * Creates the document handler.
148 * @param commonPart the common part
150 * @return the document handler
152 * @throws Exception the exception
154 public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx,
155 Object commonPart) throws Exception {
156 DocumentHandler docHandler = ctx.getDocumentHandler();
157 docHandler.setCommonPart(commonPart);
162 * Creates the service context.
164 * @return the service context< i t, o t>
166 * @throws Exception the exception
168 protected ServiceContext<IT, OT> createServiceContext() throws Exception {
169 ServiceContext<IT, OT> ctx = createServiceContext(this.getServiceName(),
170 (IT)null, //inputType
171 null, // The resource map
172 (UriInfo)null, // The query params
173 this.getCommonPartClass());
178 * Creates the service context.
180 * @param serviceName the service name
182 * @return the service context< i t, o t>
184 * @throws Exception the exception
186 protected ServiceContext<IT, OT> createServiceContext(String serviceName) throws Exception {
187 ServiceContext<IT, OT> ctx = createServiceContext(
189 (IT)null, // The input part
190 null, // The resource map
191 (UriInfo)null, // The queryParams
192 (Class<?>)null /*input type's Class*/);
196 protected ServiceContext<IT, OT> createServiceContext(String serviceName, UriInfo ui) throws Exception {
197 ServiceContext<IT, OT> ctx = createServiceContext(
199 (IT)null, // The input part
200 null, // The resource map
201 (UriInfo)null, // The queryParams
202 (Class<?>)null /*input type's Class*/);
208 * Creates the service context.
210 * @param serviceName the service name
211 * @param input the input
213 * @return the service context< i t, o t>
215 * @throws Exception the exception
217 protected ServiceContext<IT, OT> createServiceContext(String serviceName,
218 IT input) throws Exception {
219 ServiceContext<IT, OT> ctx = createServiceContext(serviceName,
221 null, // The resource map
222 (UriInfo)null, /*queryParams*/
223 (Class<?>)null /*input type's Class*/);
227 protected ServiceContext<IT, OT> createServiceContext(String serviceName,
229 UriInfo uriInfo) throws Exception {
230 ServiceContext<IT, OT> ctx = createServiceContext(serviceName,
232 null, // The resource map
233 uriInfo, /*queryParams*/
234 (Class<?>)null /*input type's Class*/);
238 protected ServiceContext<IT, OT> createServiceContext(UriInfo uriInfo) throws Exception {
239 ServiceContext<IT, OT> ctx = createServiceContext(
242 (Class<?>)null /*input type's Class*/);
247 * Creates the service context.
249 * @param input the input
251 * @return the service context< i t, o t>
253 * @throws Exception the exception
255 protected ServiceContext<IT, OT> createServiceContext(IT input) throws Exception {
256 ServiceContext<IT, OT> ctx = createServiceContext(
258 (Class<?>)null /*input type's Class*/);
262 protected ServiceContext<IT, OT> createServiceContext(IT input, UriInfo uriInfo) throws Exception {
263 ServiceContext<IT, OT> ctx = createServiceContext(
266 null ); // The class param/argument
271 * Creates the service context.
273 * @param input the input
274 * @param theClass the the class
276 * @return the service context
278 * @throws Exception the exception
280 protected ServiceContext<IT, OT> createServiceContext(IT input, Class<?> theClass) throws Exception {
281 ServiceContext<IT, OT> ctx = createServiceContext(
283 (UriInfo)null, //queryParams,
288 protected ServiceContext<IT, OT> createServiceContext(IT input, Class<?> theClass, UriInfo uriInfo) throws Exception {
289 ServiceContext<IT, OT> ctx = createServiceContext(
296 protected ServiceContext<IT, OT> createServiceContext(
298 ResourceMap resourceMap,
299 UriInfo uriInfo) throws Exception {
300 ServiceContext<IT, OT> ctx = createServiceContext(
302 null, // The input object
305 null /* the class of the input type */);
309 protected ServiceContext<IT, OT> createServiceContext(
311 ResourceMap resourceMap,
312 UriInfo uriInfo) throws Exception {
313 ServiceContext<IT, OT> ctx = createServiceContext(
314 this.getServiceName(),
318 null /* the class of the input type */);
322 protected ServiceContext<IT, OT> createServiceContext(
325 ResourceMap resourceMap,
326 UriInfo uriInfo) throws Exception {
327 ServiceContext<IT, OT> ctx = createServiceContext(
332 null /* the class of the input type */);
337 * Creates the service context.
339 * @param input the input
340 * @param queryParams the query params
341 * @param theClass the the class
343 * @return the service context< i t, o t>
345 * @throws Exception the exception
347 private ServiceContext<IT, OT> createServiceContext(
350 Class<?> theClass) throws Exception {
351 return createServiceContext(this.getServiceName(),
353 null, // The resource map
359 * Creates the service context.
361 * @param serviceName the service name
362 * @param input the input
363 * @param queryParams the query params
364 * @param theClass the the class
366 * @return the service context< i t, o t>
368 * @throws Exception the exception
370 private ServiceContext<IT, OT> createServiceContext(
373 ResourceMap resourceMap,
375 Class<?> theClass) throws Exception {
376 ServiceContext<IT, OT> ctx = getServiceContextFactory().createServiceContext(
381 theClass != null ? theClass.getPackage().getName() : null,
382 theClass != null ? theClass.getName() : null);
383 if (theClass != null) {
384 ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, theClass);
391 * Gets the version string.
393 * @return the version string
395 abstract protected String getVersionString();
400 * @return the version
404 @Produces("application/xml")
405 public Version getVersion() {
406 Version result = new Version();
408 result.setVersionString(getVersionString());
413 public void checkResult(Object resultToCheck, String csid, String serviceMessage) throws CSWebApplicationException {
414 if (resultToCheck == null) {
415 Response response = Response.status(Response.Status.NOT_FOUND).entity(
416 serviceMessage + "csid=" + csid
417 + ": was not found.").type(
418 "text/plain").build();
419 throw new CSWebApplicationException(response);
423 protected void ensureCSID(String csid, String crudType) throws CSWebApplicationException {
424 ensureCSID(csid, crudType, "csid");
427 protected void ensureCSID(String csid, String crudType, String whichCsid) throws CSWebApplicationException {
428 if (logger.isDebugEnabled()) {
429 logger.debug(crudType + " for " + getClass().getName() + " with csid=" + csid);
431 if (csid == null || "".equals(csid)) {
432 logger.error(crudType + " for " + getClass().getName() + " missing csid!");
433 Response response = Response.status(Response.Status.BAD_REQUEST).entity(crudType + " failed on " + getClass().getName() + ' '+whichCsid+'=' + csid).type("text/plain").build();
434 throw new CSWebApplicationException(response);
438 protected CSWebApplicationException bigReThrow(Exception e, String serviceMsg) throws CSWebApplicationException {
439 return bigReThrow(e, serviceMsg, "");
442 protected CSWebApplicationException bigReThrow(Exception e, String serviceMsg, String csid) throws CSWebApplicationException {
443 boolean logException = true;
444 CSWebApplicationException result = null;
446 String detail = Tools.errorToString(e, true);
447 String detailNoTrace = Tools.errorToString(e, true, 3);
449 if (e instanceof UnauthorizedException) {
450 response = Response.status(Response.Status.UNAUTHORIZED).entity(serviceMsg + e.getMessage()).type("text/plain").build();
451 result = new CSWebApplicationException(e, response);
453 } else if (e instanceof DocumentNotFoundException) {
455 // Don't log this error unless we're in 'trace' mode
457 logException = false;
458 response = Response.status(Response.Status.NOT_FOUND).entity(serviceMsg + " on " + getClass().getName() + " csid=" + csid).type("text/plain").build();
459 result = new CSWebApplicationException(e, response);
461 } else if (e instanceof TransactionException) {
462 int code = ((TransactionException) e).getErrorCode();
463 response = Response.status(code).entity(e.getMessage()).type("text/plain").build();
464 result = new CSWebApplicationException(e, response);
466 } else if (e instanceof BadRequestException) {
467 int code = ((BadRequestException) e).getErrorCode();
469 code = Response.Status.BAD_REQUEST.getStatusCode();
472 response = Response.status(code).entity(serviceMsg + e.getMessage()).type("text/plain").build();
473 // return new WebApplicationException(e, code);
474 result = new CSWebApplicationException(e, response);
476 } else if (e instanceof DocumentException) {
477 int code = ((DocumentException) e).getErrorCode();
479 code = Response.Status.BAD_REQUEST.getStatusCode();
482 response = Response.status(code).entity(serviceMsg + e.getMessage()).type("text/plain").build();
483 // return new WebApplicationException(e, code);
484 result = new CSWebApplicationException(e, response);
486 } else if (e instanceof CSWebApplicationException) {
487 // subresource may have already thrown this exception
488 // so just pass it on
489 result = (CSWebApplicationException) e;
491 } else { // e is now instanceof Exception
492 response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(serviceMsg + " detail: " + detailNoTrace).type("text/plain").build();
493 result = new CSWebApplicationException(e, response);
496 // Some exceptions like DocumentNotFoundException won't be logged unless we're in 'trace' mode
498 boolean traceEnabled = logger.isTraceEnabled();
499 if (logException == true || traceEnabled == true) {
500 if (traceEnabled == true) {
501 logger.error(getClass().getName() + " detail: " + detail, e);
503 logger.error(getClass().getName() + " detail: " + detailNoTrace);
511 public boolean allowAnonymousAccess(HttpRequest request,
512 Class<?> resourceClass) {
517 * Returns a UriRegistry entry: a map of tenant-qualified URI templates
518 * for the current resource, for all tenants
520 * @return a map of URI templates for the current resource, for all tenants
522 public Map<UriTemplateRegistryKey,StoredValuesUriTemplate> getUriRegistryEntries() {
523 Map<UriTemplateRegistryKey,StoredValuesUriTemplate> uriRegistryEntriesMap =
524 new HashMap<UriTemplateRegistryKey,StoredValuesUriTemplate>();
525 List<String> tenantIds = getTenantBindingsReader().getTenantIds();
526 for (String tenantId : tenantIds) {
527 uriRegistryEntriesMap.putAll(getUriRegistryEntries(tenantId, getDocType(tenantId), UriTemplateFactory.RESOURCE));
529 return uriRegistryEntriesMap;
533 * Returns a resource's document type.
539 public String getDocType(String tenantId) {
540 return getDocType(tenantId, getServiceName());
544 * Returns the document type associated with a specified service, within a specified tenant.
546 * @param tenantId a tenant ID
547 * @param serviceName a service name
548 * @return the Nuxeo document type associated with that service and tenant.
550 // FIXME: This method may properly belong in a different services package or class.
551 // Also, we need to check for any existing methods that may duplicate this one.
552 protected String getDocType(String tenantId, String serviceName) {
554 if (Tools.isBlank(tenantId)) {
557 ServiceBindingType sb = getTenantBindingsReader().getServiceBinding(tenantId, serviceName);
561 docType = sb.getObject().getName(); // Reads the Document Type from tenant bindings configuration
566 * Returns a UriRegistry entry: a map of tenant-qualified URI templates
567 * for the current resource, for a specified tenants
569 * @return a map of URI templates for the current resource, for a specified tenant
572 public Map<UriTemplateRegistryKey,StoredValuesUriTemplate> getUriRegistryEntries(String tenantId,
573 String docType, UriTemplateFactory.UriTemplateType type) {
574 Map<UriTemplateRegistryKey,StoredValuesUriTemplate> uriRegistryEntriesMap =
575 new HashMap<UriTemplateRegistryKey,StoredValuesUriTemplate>();
576 UriTemplateRegistryKey key;
577 if (Tools.isBlank(tenantId) || Tools.isBlank(docType)) {
578 return uriRegistryEntriesMap;
580 key = new UriTemplateRegistryKey();
581 key.setTenantId(tenantId);
582 key.setDocType(docType);
583 uriRegistryEntriesMap.put(key, getUriTemplate(type));
584 return uriRegistryEntriesMap;
588 * Returns a URI template of the appropriate type, populated with the
589 * current service name as one of its stored values.
591 * @param type a URI template type
592 * @return a URI template of the appropriate type.
595 public StoredValuesUriTemplate getUriTemplate(UriTemplateFactory.UriTemplateType type) {
596 Map<String,String> storedValuesMap = new HashMap<String,String>();
597 storedValuesMap.put(UriTemplateFactory.SERVICENAME_VAR, getServiceName());
598 StoredValuesUriTemplate template =
599 UriTemplateFactory.getURITemplate(type, storedValuesMap);
604 * Returns a reader for reading values from tenant bindings configuration
606 * @return a tenant bindings configuration reader
609 public TenantBindingConfigReaderImpl getTenantBindingsReader() {
610 return ServiceMain.getInstance().getTenantBindingConfigReader();