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.CSWebApplicationException;
\r
37 import org.collectionspace.services.common.api.Tools;
\r
38 import org.collectionspace.services.common.context.ServiceContext;
\r
39 import org.collectionspace.services.common.context.ServiceContextProperties;
\r
40 import org.collectionspace.services.common.document.BadRequestException;
\r
41 import org.collectionspace.services.common.document.DocumentException;
\r
42 import org.collectionspace.services.common.document.DocumentHandler;
\r
43 import org.collectionspace.services.common.document.DocumentNotFoundException;
\r
44 import org.collectionspace.services.common.document.TransactionException;
\r
45 import org.collectionspace.services.common.repository.RepositoryClient;
\r
46 import org.collectionspace.services.common.repository.RepositoryClientFactory;
\r
47 import org.collectionspace.services.common.security.UnauthorizedException;
\r
48 import org.collectionspace.services.common.storage.StorageClient;
\r
49 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
\r
51 import org.jboss.resteasy.core.ResourceMethod;
\r
52 import org.jboss.resteasy.spi.HttpRequest;
\r
54 import org.slf4j.Logger;
\r
55 import org.slf4j.LoggerFactory;
\r
58 * The Class AbstractCollectionSpaceResourceImpl.
\r
60 * @param <IT> the generic type
\r
61 * @param <OT> the generic type
\r
63 public abstract class AbstractCollectionSpaceResourceImpl<IT, OT>
\r
64 implements CollectionSpaceResource<IT, OT> {
\r
66 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
\r
69 // Fields for default client factory and client
\r
70 /** The repository client factory. */
\r
71 private RepositoryClientFactory repositoryClientFactory;
\r
73 /** The repository client. */
\r
74 private RepositoryClient repositoryClient;
\r
76 /** The storage client. */
\r
77 private StorageClient storageClient;
\r
82 * @param res the res
\r
83 * @return the string
\r
85 protected static String extractId(Response res) {
\r
86 MultivaluedMap<String, Object> mvm = res.getMetadata();
\r
87 String uri = (String) ((List<Object>) mvm.get("Location")).get(0);
\r
88 String[] segments = uri.split("/");
\r
89 String id = segments[segments.length - 1];
\r
94 * Instantiates a new abstract collection space resource.
\r
96 public AbstractCollectionSpaceResourceImpl() {
\r
97 repositoryClientFactory = RepositoryClientFactory.getInstance();
\r
101 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceName()
\r
104 abstract public String getServiceName();
\r
108 * @see org.collectionspace.services.common.CollectionSpaceResource#getRepositoryClient(org.collectionspace.services.common.context.ServiceContext)
\r
111 synchronized public RepositoryClient getRepositoryClient(ServiceContext<IT, OT> ctx) {
\r
112 if(repositoryClient != null){
\r
113 return repositoryClient;
\r
115 repositoryClient = repositoryClientFactory.getClient(ctx.getRepositoryClientName());
\r
116 return repositoryClient;
\r
120 * @see org.collectionspace.services.common.CollectionSpaceResource#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
\r
123 synchronized public StorageClient getStorageClient(ServiceContext<IT, OT> ctx) {
\r
124 if(storageClient != null) {
\r
125 return storageClient;
\r
127 storageClient = new JpaStorageClientImpl();
\r
128 return storageClient;
\r
132 * @see org.collectionspace.services.common.CollectionSpaceResource#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
\r
135 public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx) throws Exception {
\r
136 DocumentHandler docHandler = createDocumentHandler(ctx, ctx.getInput());
\r
141 * Creates the document handler.
\r
143 * @param ctx the ctx
\r
144 * @param commonPart the common part
\r
146 * @return the document handler
\r
148 * @throws Exception the exception
\r
150 public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx,
\r
151 Object commonPart) throws Exception {
\r
152 DocumentHandler docHandler = ctx.getDocumentHandler();
\r
153 docHandler.setCommonPart(commonPart);
\r
158 * Creates the service context.
\r
160 * @return the service context< i t, o t>
\r
162 * @throws Exception the exception
\r
164 protected ServiceContext<IT, OT> createServiceContext() throws Exception {
\r
165 ServiceContext<IT, OT> ctx = createServiceContext(this.getServiceName(),
\r
166 (IT)null, //inputType
\r
167 null, // The resource map
\r
168 (UriInfo)null, // The query params
\r
169 this.getCommonPartClass());
\r
174 * Creates the service context.
\r
176 * @param serviceName the service name
\r
178 * @return the service context< i t, o t>
\r
180 * @throws Exception the exception
\r
182 protected ServiceContext<IT, OT> createServiceContext(String serviceName) throws Exception {
\r
183 ServiceContext<IT, OT> ctx = createServiceContext(
\r
185 (IT)null, // The input part
\r
186 null, // The resource map
\r
187 (UriInfo)null, // The queryParams
\r
188 (Class<?>)null /*input type's Class*/);
\r
192 protected ServiceContext<IT, OT> createServiceContext(String serviceName, UriInfo ui) throws Exception {
\r
193 ServiceContext<IT, OT> ctx = createServiceContext(
\r
195 (IT)null, // The input part
\r
196 null, // The resource map
\r
197 (UriInfo)null, // The queryParams
\r
198 (Class<?>)null /*input type's Class*/);
\r
199 ctx.setUriInfo(ui);
\r
204 * Creates the service context.
\r
206 * @param serviceName the service name
\r
207 * @param input the input
\r
209 * @return the service context< i t, o t>
\r
211 * @throws Exception the exception
\r
213 protected ServiceContext<IT, OT> createServiceContext(String serviceName,
\r
214 IT input) throws Exception {
\r
215 ServiceContext<IT, OT> ctx = createServiceContext(serviceName,
\r
217 null, // The resource map
\r
218 (UriInfo)null, /*queryParams*/
\r
219 (Class<?>)null /*input type's Class*/);
\r
223 protected ServiceContext<IT, OT> createServiceContext(String serviceName,
\r
225 UriInfo uriInfo) throws Exception {
\r
226 ServiceContext<IT, OT> ctx = createServiceContext(serviceName,
\r
228 null, // The resource map
\r
229 uriInfo, /*queryParams*/
\r
230 (Class<?>)null /*input type's Class*/);
\r
234 protected ServiceContext<IT, OT> createServiceContext(UriInfo uriInfo) throws Exception {
\r
235 ServiceContext<IT, OT> ctx = createServiceContext(
\r
236 (IT)null, /*input*/
\r
238 (Class<?>)null /*input type's Class*/);
\r
243 * Creates the service context.
\r
245 * @param input the input
\r
247 * @return the service context< i t, o t>
\r
249 * @throws Exception the exception
\r
251 protected ServiceContext<IT, OT> createServiceContext(IT input) throws Exception {
\r
252 ServiceContext<IT, OT> ctx = createServiceContext(
\r
254 (Class<?>)null /*input type's Class*/);
\r
258 protected ServiceContext<IT, OT> createServiceContext(IT input, UriInfo uriInfo) throws Exception {
\r
259 ServiceContext<IT, OT> ctx = createServiceContext(
\r
262 null ); // The class param/argument
\r
267 * Creates the service context.
\r
269 * @param input the input
\r
270 * @param theClass the the class
\r
272 * @return the service context
\r
274 * @throws Exception the exception
\r
276 protected ServiceContext<IT, OT> createServiceContext(IT input, Class<?> theClass) throws Exception {
\r
277 ServiceContext<IT, OT> ctx = createServiceContext(
\r
279 (UriInfo)null, //queryParams,
\r
284 protected ServiceContext<IT, OT> createServiceContext(IT input, Class<?> theClass, UriInfo uriInfo) throws Exception {
\r
285 ServiceContext<IT, OT> ctx = createServiceContext(
\r
292 protected ServiceContext<IT, OT> createServiceContext(
\r
293 String serviceName,
\r
294 ResourceMap resourceMap,
\r
295 UriInfo uriInfo) throws Exception {
\r
296 ServiceContext<IT, OT> ctx = createServiceContext(
\r
298 null, // The input object
\r
301 null /* the class of the input type */);
\r
305 protected ServiceContext<IT, OT> createServiceContext(
\r
307 ResourceMap resourceMap,
\r
308 UriInfo uriInfo) throws Exception {
\r
309 ServiceContext<IT, OT> ctx = createServiceContext(
\r
310 this.getServiceName(),
\r
314 null /* the class of the input type */);
\r
318 protected ServiceContext<IT, OT> createServiceContext(
\r
319 String serviceName,
\r
321 ResourceMap resourceMap,
\r
322 UriInfo uriInfo) throws Exception {
\r
323 ServiceContext<IT, OT> ctx = createServiceContext(
\r
328 null /* the class of the input type */);
\r
333 * Creates the service context.
\r
335 * @param input the input
\r
336 * @param queryParams the query params
\r
337 * @param theClass the the class
\r
339 * @return the service context< i t, o t>
\r
341 * @throws Exception the exception
\r
343 private ServiceContext<IT, OT> createServiceContext(
\r
346 Class<?> theClass) throws Exception {
\r
347 return createServiceContext(this.getServiceName(),
\r
349 null, // The resource map
\r
355 * Creates the service context.
\r
357 * @param serviceName the service name
\r
358 * @param input the input
\r
359 * @param queryParams the query params
\r
360 * @param theClass the the class
\r
362 * @return the service context< i t, o t>
\r
364 * @throws Exception the exception
\r
366 private ServiceContext<IT, OT> createServiceContext(
\r
367 String serviceName,
\r
369 ResourceMap resourceMap,
\r
371 Class<?> theClass) throws Exception {
\r
372 ServiceContext<IT, OT> ctx = getServiceContextFactory().createServiceContext(
\r
377 theClass != null ? theClass.getPackage().getName() : null,
\r
378 theClass != null ? theClass.getName() : null);
\r
379 if (theClass != null) {
\r
380 ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, theClass);
\r
387 * Gets the version string.
\r
389 * @return the version string
\r
391 abstract protected String getVersionString();
\r
394 * Gets the version.
\r
396 * @return the version
\r
400 @Produces("application/xml")
\r
401 public Version getVersion() {
\r
402 Version result = new Version();
\r
404 result.setVersionString(getVersionString());
\r
409 public void checkResult(Object resultToCheck, String csid, String serviceMessage) throws CSWebApplicationException {
\r
410 if (resultToCheck == null) {
\r
411 Response response = Response.status(Response.Status.NOT_FOUND).entity(
\r
412 serviceMessage + "csid=" + csid
\r
413 + ": was not found.").type(
\r
414 "text/plain").build();
\r
415 throw new CSWebApplicationException(response);
\r
419 protected void ensureCSID(String csid, String crudType) throws CSWebApplicationException {
\r
420 ensureCSID(csid, crudType, "csid");
\r
423 protected void ensureCSID(String csid, String crudType, String whichCsid) throws CSWebApplicationException {
\r
424 if (logger.isDebugEnabled()) {
\r
425 logger.debug(crudType + " for " + getClass().getName() + " with csid=" + csid);
\r
427 if (csid == null || "".equals(csid)) {
\r
428 logger.error(crudType + " for " + getClass().getName() + " missing csid!");
\r
429 Response response = Response.status(Response.Status.BAD_REQUEST).entity(crudType + " failed on " + getClass().getName() + ' '+whichCsid+'=' + csid).type("text/plain").build();
\r
430 throw new CSWebApplicationException(response);
\r
434 protected CSWebApplicationException bigReThrow(Exception e, String serviceMsg) throws CSWebApplicationException {
\r
435 return bigReThrow(e, serviceMsg, "");
\r
438 protected CSWebApplicationException bigReThrow(Exception e, String serviceMsg, String csid) throws CSWebApplicationException {
\r
439 boolean logException = true;
\r
440 CSWebApplicationException result = null;
\r
442 String detail = Tools.errorToString(e, true);
\r
443 String detailNoTrace = Tools.errorToString(e, true, 3);
\r
445 if (e instanceof UnauthorizedException) {
\r
446 response = Response.status(Response.Status.UNAUTHORIZED).entity(serviceMsg + e.getMessage()).type("text/plain").build();
\r
447 result = new CSWebApplicationException(e, response);
\r
449 } else if (e instanceof DocumentNotFoundException) {
\r
451 // Don't log this error unless we're in 'trace' mode
\r
453 logException = false;
\r
454 response = Response.status(Response.Status.NOT_FOUND).entity(serviceMsg + " on " + getClass().getName() + " csid=" + csid).type("text/plain").build();
\r
455 result = new CSWebApplicationException(e, response);
\r
457 } else if (e instanceof TransactionException) {
\r
458 int code = ((TransactionException) e).getErrorCode();
\r
459 response = Response.status(code).entity(e.getMessage()).type("text/plain").build();
\r
460 result = new CSWebApplicationException(e, response);
\r
462 } else if (e instanceof BadRequestException) {
\r
463 int code = ((BadRequestException) e).getErrorCode();
\r
465 code = Response.Status.BAD_REQUEST.getStatusCode();
\r
468 response = Response.status(code).entity(serviceMsg + e.getMessage()).type("text/plain").build();
\r
469 // return new WebApplicationException(e, code);
\r
470 result = new CSWebApplicationException(e, response);
\r
472 } else if (e instanceof DocumentException) {
\r
473 int code = ((DocumentException) e).getErrorCode();
\r
475 code = Response.Status.BAD_REQUEST.getStatusCode();
\r
478 response = Response.status(code).entity(serviceMsg + e.getMessage()).type("text/plain").build();
\r
479 // return new WebApplicationException(e, code);
\r
480 result = new CSWebApplicationException(e, response);
\r
482 } else if (e instanceof CSWebApplicationException) {
\r
483 // subresource may have already thrown this exception
\r
484 // so just pass it on
\r
485 result = (CSWebApplicationException) e;
\r
487 } else { // e is now instanceof Exception
\r
488 response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(serviceMsg + " detail: " + detailNoTrace).type("text/plain").build();
\r
489 result = new CSWebApplicationException(e, response);
\r
492 // Some exceptions like DocumentNotFoundException won't be logged unless we're in 'trace' mode
\r
494 boolean traceEnabled = logger.isTraceEnabled();
\r
495 if (logException == true || traceEnabled == true) {
\r
496 if (traceEnabled == true) {
\r
497 logger.error(getClass().getName() + " detail: " + detail, e);
\r
499 logger.error(getClass().getName() + " detail: " + detailNoTrace);
\r
507 public boolean allowAnonymousAccess(HttpRequest request,
\r
508 ResourceMethod method) {
\r