2 * This document is a part of the source code and related artifacts
\r
3 * for CollectionSpace, an open source collections management system
\r
4 * for museums and related institutions:
\r
6 * http://www.collectionspace.org
\r
7 * http://wiki.collectionspace.org
\r
9 * Copyright 2009 University of California at Berkeley
\r
11 * Licensed under the Educational Community License (ECL), Version 2.0.
\r
12 * You may not use this file except in compliance with this License.
\r
14 * You may obtain a copy of the ECL 2.0 License at
\r
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
\r
18 * Unless required by applicable law or agreed to in writing, software
\r
19 * distributed under the License is distributed on an "AS IS" BASIS,
\r
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
21 * See the License for the specific language governing permissions and
\r
22 * limitations under the License.
\r
24 package org.collectionspace.services.common;
\r
26 import java.util.List;
\r
28 import javax.ws.rs.GET;
\r
29 import javax.ws.rs.Path;
\r
30 import javax.ws.rs.Produces;
\r
31 import javax.ws.rs.WebApplicationException;
\r
32 import javax.ws.rs.core.MultivaluedMap;
\r
33 import javax.ws.rs.core.Response;
\r
34 import javax.ws.rs.core.UriInfo;
\r
36 import org.collectionspace.services.common.api.Tools;
\r
37 import org.collectionspace.services.common.context.ServiceContext;
\r
38 import org.collectionspace.services.common.context.ServiceContextProperties;
\r
39 import org.collectionspace.services.common.document.BadRequestException;
\r
40 import org.collectionspace.services.common.document.DocumentException;
\r
41 import org.collectionspace.services.common.document.DocumentHandler;
\r
42 import org.collectionspace.services.common.document.DocumentNotFoundException;
\r
43 import org.collectionspace.services.common.document.TransactionException;
\r
44 import org.collectionspace.services.common.repository.RepositoryClient;
\r
45 import org.collectionspace.services.common.repository.RepositoryClientFactory;
\r
46 import org.collectionspace.services.common.security.UnauthorizedException;
\r
47 import org.collectionspace.services.common.storage.StorageClient;
\r
48 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
\r
49 import org.jboss.resteasy.core.ResourceMethod;
\r
50 import org.jboss.resteasy.spi.HttpRequest;
\r
52 import org.slf4j.Logger;
\r
53 import org.slf4j.LoggerFactory;
\r
56 * The Class AbstractCollectionSpaceResourceImpl.
\r
58 * @param <IT> the generic type
\r
59 * @param <OT> the generic type
\r
61 public abstract class AbstractCollectionSpaceResourceImpl<IT, OT>
\r
62 implements CollectionSpaceResource<IT, OT> {
\r
64 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
\r
67 // Fields for default client factory and client
\r
68 /** The repository client factory. */
\r
69 private RepositoryClientFactory repositoryClientFactory;
\r
71 /** The repository client. */
\r
72 private RepositoryClient repositoryClient;
\r
74 /** The storage client. */
\r
75 private StorageClient storageClient;
\r
80 * @param res the res
\r
81 * @return the string
\r
83 protected static String extractId(Response res) {
\r
84 MultivaluedMap<String, Object> mvm = res.getMetadata();
\r
85 String uri = (String) ((List<Object>) mvm.get("Location")).get(0);
\r
86 String[] segments = uri.split("/");
\r
87 String id = segments[segments.length - 1];
\r
92 * Instantiates a new abstract collection space resource.
\r
94 public AbstractCollectionSpaceResourceImpl() {
\r
95 repositoryClientFactory = RepositoryClientFactory.getInstance();
\r
99 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceName()
\r
102 abstract public String getServiceName();
\r
106 * @see org.collectionspace.services.common.CollectionSpaceResource#getRepositoryClient(org.collectionspace.services.common.context.ServiceContext)
\r
109 synchronized public RepositoryClient getRepositoryClient(ServiceContext<IT, OT> ctx) {
\r
110 if(repositoryClient != null){
\r
111 return repositoryClient;
\r
113 repositoryClient = repositoryClientFactory.getClient(ctx.getRepositoryClientName());
\r
114 return repositoryClient;
\r
118 * @see org.collectionspace.services.common.CollectionSpaceResource#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
\r
121 synchronized public StorageClient getStorageClient(ServiceContext<IT, OT> ctx) {
\r
122 if(storageClient != null) {
\r
123 return storageClient;
\r
125 storageClient = new JpaStorageClientImpl();
\r
126 return storageClient;
\r
130 * @see org.collectionspace.services.common.CollectionSpaceResource#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
\r
133 public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx) throws Exception {
\r
134 DocumentHandler docHandler = createDocumentHandler(ctx, ctx.getInput());
\r
139 * Creates the document handler.
\r
141 * @param ctx the ctx
\r
142 * @param commonPart the common part
\r
144 * @return the document handler
\r
146 * @throws Exception the exception
\r
148 public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx,
\r
149 Object commonPart) throws Exception {
\r
150 DocumentHandler docHandler = ctx.getDocumentHandler();
\r
151 docHandler.setCommonPart(commonPart);
\r
156 * Creates the service context.
\r
158 * @return the service context< i t, o t>
\r
160 * @throws Exception the exception
\r
162 protected ServiceContext<IT, OT> createServiceContext() throws Exception {
\r
163 ServiceContext<IT, OT> ctx = createServiceContext(this.getServiceName(),
\r
164 (IT)null, //inputType
\r
165 null, // The resource map
\r
166 (UriInfo)null, // The query params
\r
167 this.getCommonPartClass());
\r
172 * Creates the service context.
\r
174 * @param serviceName the service name
\r
176 * @return the service context< i t, o t>
\r
178 * @throws Exception the exception
\r
180 protected ServiceContext<IT, OT> createServiceContext(String serviceName) throws Exception {
\r
181 ServiceContext<IT, OT> ctx = createServiceContext(
\r
183 (IT)null, // The input part
\r
184 null, // The resource map
\r
185 (UriInfo)null, // The queryParams
\r
186 (Class<?>)null /*input type's Class*/);
\r
190 protected ServiceContext<IT, OT> createServiceContext(String serviceName, UriInfo ui) throws Exception {
\r
191 ServiceContext<IT, OT> ctx = createServiceContext(
\r
193 (IT)null, // The input part
\r
194 null, // The resource map
\r
195 (UriInfo)null, // The queryParams
\r
196 (Class<?>)null /*input type's Class*/);
\r
197 ctx.setUriInfo(ui);
\r
202 * Creates the service context.
\r
204 * @param serviceName the service name
\r
205 * @param input the input
\r
207 * @return the service context< i t, o t>
\r
209 * @throws Exception the exception
\r
211 protected ServiceContext<IT, OT> createServiceContext(String serviceName,
\r
212 IT input) throws Exception {
\r
213 ServiceContext<IT, OT> ctx = createServiceContext(serviceName,
\r
215 null, // The resource map
\r
216 (UriInfo)null, /*queryParams*/
\r
217 (Class<?>)null /*input type's Class*/);
\r
221 protected ServiceContext<IT, OT> createServiceContext(String serviceName,
\r
223 UriInfo uriInfo) throws Exception {
\r
224 ServiceContext<IT, OT> ctx = createServiceContext(serviceName,
\r
226 null, // The resource map
\r
227 uriInfo, /*queryParams*/
\r
228 (Class<?>)null /*input type's Class*/);
\r
232 protected ServiceContext<IT, OT> createServiceContext(UriInfo uriInfo) throws Exception {
\r
233 ServiceContext<IT, OT> ctx = createServiceContext(
\r
234 (IT)null, /*input*/
\r
236 (Class<?>)null /*input type's Class*/);
\r
241 * Creates the service context.
\r
243 * @param input the input
\r
245 * @return the service context< i t, o t>
\r
247 * @throws Exception the exception
\r
249 protected ServiceContext<IT, OT> createServiceContext(IT input) throws Exception {
\r
250 ServiceContext<IT, OT> ctx = createServiceContext(
\r
252 (Class<?>)null /*input type's Class*/);
\r
256 protected ServiceContext<IT, OT> createServiceContext(IT input, UriInfo uriInfo) throws Exception {
\r
257 ServiceContext<IT, OT> ctx = createServiceContext(
\r
260 null ); // The class param/argument
\r
265 * Creates the service context.
\r
267 * @param input the input
\r
268 * @param theClass the the class
\r
270 * @return the service context
\r
272 * @throws Exception the exception
\r
274 protected ServiceContext<IT, OT> createServiceContext(IT input, Class<?> theClass) throws Exception {
\r
275 ServiceContext<IT, OT> ctx = createServiceContext(
\r
277 (UriInfo)null, //queryParams,
\r
282 protected ServiceContext<IT, OT> createServiceContext(IT input, Class<?> theClass, UriInfo uriInfo) throws Exception {
\r
283 ServiceContext<IT, OT> ctx = createServiceContext(
\r
290 protected ServiceContext<IT, OT> createServiceContext(
\r
291 String serviceName,
\r
292 ResourceMap resourceMap,
\r
293 UriInfo uriInfo) throws Exception {
\r
294 ServiceContext<IT, OT> ctx = createServiceContext(
\r
296 null, // The input object
\r
299 null /* the class of the input type */);
\r
303 protected ServiceContext<IT, OT> createServiceContext(
\r
305 ResourceMap resourceMap,
\r
306 UriInfo uriInfo) throws Exception {
\r
307 ServiceContext<IT, OT> ctx = createServiceContext(
\r
308 this.getServiceName(),
\r
312 null /* the class of the input type */);
\r
316 protected ServiceContext<IT, OT> createServiceContext(
\r
317 String serviceName,
\r
319 ResourceMap resourceMap,
\r
320 UriInfo uriInfo) throws Exception {
\r
321 ServiceContext<IT, OT> ctx = createServiceContext(
\r
326 null /* the class of the input type */);
\r
331 * Creates the service context.
\r
333 * @param input the input
\r
334 * @param queryParams the query params
\r
335 * @param theClass the the class
\r
337 * @return the service context< i t, o t>
\r
339 * @throws Exception the exception
\r
341 private ServiceContext<IT, OT> createServiceContext(
\r
344 Class<?> theClass) throws Exception {
\r
345 return createServiceContext(this.getServiceName(),
\r
347 null, // The resource map
\r
353 * Creates the service context.
\r
355 * @param serviceName the service name
\r
356 * @param input the input
\r
357 * @param queryParams the query params
\r
358 * @param theClass the the class
\r
360 * @return the service context< i t, o t>
\r
362 * @throws Exception the exception
\r
364 private ServiceContext<IT, OT> createServiceContext(
\r
365 String serviceName,
\r
367 ResourceMap resourceMap,
\r
369 Class<?> theClass) throws Exception {
\r
370 ServiceContext<IT, OT> ctx = getServiceContextFactory().createServiceContext(
\r
375 theClass != null ? theClass.getPackage().getName() : null,
\r
376 theClass != null ? theClass.getName() : null);
\r
377 if (theClass != null) {
\r
378 ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, theClass);
\r
385 * Gets the version string.
\r
387 * @return the version string
\r
389 abstract protected String getVersionString();
\r
392 * Gets the version.
\r
394 * @return the version
\r
398 @Produces("application/xml")
\r
399 public Version getVersion() {
\r
400 Version result = new Version();
\r
402 result.setVersionString(getVersionString());
\r
407 public void checkResult(Object resultToCheck, String csid, String serviceMessage) throws WebApplicationException {
\r
408 if (resultToCheck == null) {
\r
409 Response response = Response.status(Response.Status.NOT_FOUND).entity(
\r
410 serviceMessage + "csid=" + csid
\r
411 + ": was not found.").type(
\r
412 "text/plain").build();
\r
413 throw new WebApplicationException(response);
\r
417 protected void ensureCSID(String csid, String crudType) throws WebApplicationException {
\r
418 ensureCSID(csid, crudType, "csid");
\r
421 protected void ensureCSID(String csid, String crudType, String whichCsid) throws WebApplicationException {
\r
422 if (logger.isDebugEnabled()) {
\r
423 logger.debug(crudType + " for " + getClass().getName() + " with csid=" + csid);
\r
425 if (csid == null || "".equals(csid)) {
\r
426 logger.error(crudType + " for " + getClass().getName() + " missing csid!");
\r
427 Response response = Response.status(Response.Status.BAD_REQUEST).entity(crudType + " failed on " + getClass().getName() + ' '+whichCsid+'=' + csid).type("text/plain").build();
\r
428 throw new WebApplicationException(response);
\r
432 protected WebApplicationException bigReThrow(Exception e, String serviceMsg) throws WebApplicationException {
\r
433 return bigReThrow(e, serviceMsg, "");
\r
436 protected WebApplicationException bigReThrow(Exception e, String serviceMsg, String csid) throws WebApplicationException {
\r
437 boolean logException = true;
\r
438 WebApplicationException result = null;
\r
441 String detail = Tools.errorToString(e, true);
\r
442 String detailNoTrace = Tools.errorToString(e, true, 3);
\r
443 if (e instanceof UnauthorizedException) {
\r
444 response = Response.status(Response.Status.UNAUTHORIZED).entity(serviceMsg + e.getMessage()).type("text/plain").build();
\r
445 result = new WebApplicationException(response);
\r
447 } else if (e instanceof DocumentNotFoundException) {
\r
449 // Don't log this error unless we're in 'trace' mode
\r
451 logException = false;
\r
452 response = Response.status(Response.Status.NOT_FOUND).entity(serviceMsg + " on " + getClass().getName() + " csid=" + csid).type("text/plain").build();
\r
453 result = new WebApplicationException(response);
\r
455 } else if (e instanceof TransactionException) {
\r
456 int code = ((TransactionException) e).getErrorCode();
\r
457 response = Response.status(code).entity(e.getMessage()).type("text/plain").build();
\r
458 result = new WebApplicationException(response);
\r
460 } else if (e instanceof BadRequestException) {
\r
461 int code = ((BadRequestException) e).getErrorCode();
\r
463 code = Response.Status.BAD_REQUEST.getStatusCode();
\r
466 response = Response.status(code).entity(serviceMsg + e.getMessage()).type("text/plain").build();
\r
467 // return new WebApplicationException(e, code);
\r
468 result = new WebApplicationException(response);
\r
470 } else if (e instanceof DocumentException) {
\r
471 int code = ((DocumentException) e).getErrorCode();
\r
473 code = Response.Status.BAD_REQUEST.getStatusCode();
\r
476 response = Response.status(code).entity(serviceMsg + e.getMessage()).type("text/plain").build();
\r
477 // return new WebApplicationException(e, code);
\r
478 result = new WebApplicationException(response);
\r
480 } else if (e instanceof WebApplicationException) {
\r
481 // subresource may have already thrown this exception
\r
482 // so just pass it on
\r
483 result = (WebApplicationException) e;
\r
485 } else { // e is now instanceof Exception
\r
486 response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(serviceMsg + " detail: " + detailNoTrace).type("text/plain").build();
\r
487 result = new WebApplicationException(response);
\r
490 // Some exceptions like DocumentNotFoundException won't be logged unless we're in 'trace' mode
\r
492 boolean traceEnabled = logger.isTraceEnabled();
\r
493 if (logException == true || traceEnabled == true) {
\r
494 if (traceEnabled == true) {
\r
495 logger.error(getClass().getName() + " detail: " + detail, e);
\r
497 logger.error(getClass().getName() + " detail: " + detailNoTrace);
\r
505 public boolean allowAnonymousAccess(HttpRequest request,
\r
506 ResourceMethod method) {
\r