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.repository.RepositoryClient;
\r
44 import org.collectionspace.services.common.repository.RepositoryClientFactory;
\r
45 import org.collectionspace.services.common.security.UnauthorizedException;
\r
46 import org.collectionspace.services.common.storage.StorageClient;
\r
47 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
\r
48 import org.jboss.resteasy.client.ClientResponse;
\r
49 import org.slf4j.Logger;
\r
50 import org.slf4j.LoggerFactory;
\r
53 * The Class AbstractCollectionSpaceResourceImpl.
\r
55 * @param <IT> the generic type
\r
56 * @param <OT> the generic type
\r
58 public abstract class AbstractCollectionSpaceResourceImpl<IT, OT>
\r
59 implements CollectionSpaceResource<IT, OT> {
\r
61 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
\r
64 // Fields for default client factory and client
\r
65 /** The repository client factory. */
\r
66 private RepositoryClientFactory repositoryClientFactory;
\r
68 /** The repository client. */
\r
69 private RepositoryClient repositoryClient;
\r
71 /** The storage client. */
\r
72 private StorageClient storageClient;
\r
77 * @param res the res
\r
78 * @return the string
\r
80 protected static String extractId(Response res) {
\r
81 MultivaluedMap<String, Object> mvm = res.getMetadata();
\r
82 String uri = (String) ((List<Object>) mvm.get("Location")).get(0);
\r
83 String[] segments = uri.split("/");
\r
84 String id = segments[segments.length - 1];
\r
89 * Instantiates a new abstract collection space resource.
\r
91 public AbstractCollectionSpaceResourceImpl() {
\r
92 repositoryClientFactory = RepositoryClientFactory.getInstance();
\r
96 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceName()
\r
99 abstract public String getServiceName();
\r
103 * @see org.collectionspace.services.common.CollectionSpaceResource#getRepositoryClient(org.collectionspace.services.common.context.ServiceContext)
\r
106 synchronized public RepositoryClient getRepositoryClient(ServiceContext<IT, OT> ctx) {
\r
107 if(repositoryClient != null){
\r
108 return repositoryClient;
\r
110 repositoryClient = repositoryClientFactory.getClient(ctx.getRepositoryClientName());
\r
111 return repositoryClient;
\r
115 * @see org.collectionspace.services.common.CollectionSpaceResource#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
\r
118 synchronized public StorageClient getStorageClient(ServiceContext<IT, OT> ctx) {
\r
119 if(storageClient != null) {
\r
120 return storageClient;
\r
122 storageClient = new JpaStorageClientImpl();
\r
123 return storageClient;
\r
127 * @see org.collectionspace.services.common.CollectionSpaceResource#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
\r
130 public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx) throws Exception {
\r
131 DocumentHandler docHandler = createDocumentHandler(ctx, ctx.getInput());
\r
136 * Creates the document handler.
\r
138 * @param ctx the ctx
\r
139 * @param commonPart the common part
\r
141 * @return the document handler
\r
143 * @throws Exception the exception
\r
145 public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx,
\r
146 Object commonPart) throws Exception {
\r
147 DocumentHandler docHandler = ctx.getDocumentHandler();
\r
148 docHandler.setCommonPart(commonPart);
\r
153 * Creates the service context.
\r
155 * @return the service context< i t, o t>
\r
157 * @throws Exception the exception
\r
159 protected ServiceContext<IT, OT> createServiceContext() throws Exception {
\r
160 ServiceContext<IT, OT> ctx = createServiceContext(this.getServiceName(),
\r
161 (IT)null, //inputType
\r
162 null, // The resource map
\r
163 (MultivaluedMap<String, String>)null, // The query params
\r
164 this.getCommonPartClass());
\r
169 * Creates the service context.
\r
171 * @param serviceName the service name
\r
173 * @return the service context< i t, o t>
\r
175 * @throws Exception the exception
\r
177 protected ServiceContext<IT, OT> createServiceContext(String serviceName) throws Exception {
\r
178 ServiceContext<IT, OT> ctx = createServiceContext(
\r
180 (IT)null, // The input part
\r
181 null, // The resource map
\r
182 (MultivaluedMap<String, String>)null, // The queryParams
\r
183 (Class<?>)null /*input type's Class*/);
\r
187 protected ServiceContext<IT, OT> createServiceContext(String serviceName, UriInfo ui) throws Exception {
\r
188 ServiceContext<IT, OT> ctx = createServiceContext(
\r
190 (IT)null, // The input part
\r
191 null, // The resource map
\r
192 (MultivaluedMap<String, String>)null, // The queryParams
\r
193 (Class<?>)null /*input type's Class*/);
\r
194 ctx.setUriInfo(ui);
\r
199 * Creates the service context.
\r
201 * @param serviceName the service name
\r
202 * @param input the input
\r
204 * @return the service context< i t, o t>
\r
206 * @throws Exception the exception
\r
208 protected ServiceContext<IT, OT> createServiceContext(String serviceName,
\r
209 IT input) throws Exception {
\r
210 ServiceContext<IT, OT> ctx = createServiceContext(serviceName,
\r
212 null, // The resource map
\r
213 (MultivaluedMap<String, String>)null, /*queryParams*/
\r
214 (Class<?>)null /*input type's Class*/);
\r
219 * Creates the service context.
\r
221 * @param serviceName the service name
\r
222 * @return the service context< i t, o t>
\r
223 * @throws Exception the exception
\r
225 protected ServiceContext<IT, OT> createServiceContext(String serviceName,
\r
226 MultivaluedMap<String, String> queryParams) throws Exception {
\r
227 ServiceContext<IT, OT> ctx = createServiceContext(serviceName,
\r
229 null, // The resource map
\r
231 (Class<?>)null /*input type's Class*/);
\r
235 protected ServiceContext<IT, OT> createServiceContext(UriInfo ui) throws Exception {
\r
236 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
\r
237 ServiceContext<IT, OT> ctx = createServiceContext(
\r
238 (IT)null, /*input*/
\r
240 (Class<?>)null /*input type's Class*/);
\r
241 ctx.setUriInfo(ui);
\r
246 * Creates the service context.
\r
248 * @param input the input
\r
250 * @return the service context< i t, o t>
\r
252 * @throws Exception the exception
\r
254 protected ServiceContext<IT, OT> createServiceContext(IT input) throws Exception {
\r
255 ServiceContext<IT, OT> ctx = createServiceContext(
\r
257 (Class<?>)null /*input type's Class*/);
\r
262 * Creates the service context.
\r
264 * @param input the input
\r
265 * @param theClass the the class
\r
267 * @return the service context
\r
269 * @throws Exception the exception
\r
271 protected ServiceContext<IT, OT> createServiceContext(IT input, Class<?> theClass) throws Exception {
\r
272 ServiceContext<IT, OT> ctx = createServiceContext(
\r
274 (MultivaluedMap<String, String>)null, //queryParams,
\r
279 protected ServiceContext<IT, OT> createServiceContext(
\r
280 String serviceName,
\r
281 ResourceMap resourceMap,
\r
282 UriInfo ui) throws Exception {
\r
283 ServiceContext<IT, OT> ctx = createServiceContext(
\r
285 null, // The input object
\r
287 ui.getQueryParameters(),
\r
288 null /* the class of the input type */);
\r
289 ctx.setUriInfo(ui);
\r
293 protected ServiceContext<IT, OT> createServiceContext(
\r
295 ResourceMap resourceMap,
\r
296 UriInfo ui) throws Exception {
\r
297 ServiceContext<IT, OT> ctx = createServiceContext(
\r
298 this.getServiceName(),
\r
301 ui.getQueryParameters(),
\r
302 null /* the class of the input type */);
\r
303 ctx.setUriInfo(ui);
\r
307 protected ServiceContext<IT, OT> createServiceContext(
\r
309 MultivaluedMap<String, String> queryParams) throws Exception {
\r
310 return createServiceContext(this.getServiceName(),
\r
312 null, // The resource map
\r
314 null); // The class of the input type.
\r
318 * Creates the service context.
\r
320 * @param input the input
\r
321 * @param queryParams the query params
\r
322 * @param theClass the the class
\r
324 * @return the service context< i t, o t>
\r
326 * @throws Exception the exception
\r
328 protected ServiceContext<IT, OT> createServiceContext(
\r
330 MultivaluedMap<String, String> queryParams,
\r
331 Class<?> theClass) throws Exception {
\r
332 return createServiceContext(this.getServiceName(),
\r
334 null, // The resource map
\r
340 * Creates the service context.
\r
342 * @param serviceName the service name
\r
343 * @param input the input
\r
344 * @param queryParams the query params
\r
345 * @param theClass the the class
\r
347 * @return the service context< i t, o t>
\r
349 * @throws Exception the exception
\r
351 private ServiceContext<IT, OT> createServiceContext(
\r
352 String serviceName,
\r
354 ResourceMap resourceMap,
\r
355 MultivaluedMap<String, String> queryParams,
\r
356 Class<?> theClass) throws Exception {
\r
357 ServiceContext<IT, OT> ctx = getServiceContextFactory().createServiceContext(
\r
362 theClass != null ? theClass.getPackage().getName() : null,
\r
363 theClass != null ? theClass.getName() : null);
\r
364 if (theClass != null) {
\r
365 ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, theClass);
\r
372 * Gets the version string.
\r
374 * @return the version string
\r
376 abstract protected String getVersionString();
\r
379 * Gets the version.
\r
381 * @return the version
\r
385 @Produces("application/xml")
\r
386 public Version getVersion() {
\r
387 Version result = new Version();
\r
389 result.setVersionString(getVersionString());
\r
394 public void checkResult(Object resultToCheck, String csid, String serviceMessage) throws WebApplicationException {
\r
395 if (resultToCheck == null) {
\r
396 Response response = Response.status(Response.Status.NOT_FOUND).entity(
\r
397 serviceMessage + "csid=" + csid
\r
398 + ": was not found.").type(
\r
399 "text/plain").build();
\r
400 throw new WebApplicationException(response);
\r
404 protected void ensureCSID(String csid, String crudType) throws WebApplicationException {
\r
405 ensureCSID(csid, crudType, "csid");
\r
408 protected void ensureCSID(String csid, String crudType, String whichCsid) throws WebApplicationException {
\r
409 if (logger.isDebugEnabled()) {
\r
410 logger.debug(crudType + " for " + getClass().getName() + " with csid=" + csid);
\r
412 if (csid == null || "".equals(csid)) {
\r
413 logger.error(crudType + " for " + getClass().getName() + " missing csid!");
\r
414 Response response = Response.status(Response.Status.BAD_REQUEST).entity(crudType + " failed on " + getClass().getName() + ' '+whichCsid+'=' + csid).type("text/plain").build();
\r
415 throw new WebApplicationException(response);
\r
419 protected WebApplicationException bigReThrow(Exception e, String serviceMsg) throws WebApplicationException {
\r
420 return bigReThrow(e, serviceMsg, "");
\r
423 protected WebApplicationException bigReThrow(Exception e, String serviceMsg, String csid) throws WebApplicationException {
\r
424 boolean logException = true;
\r
425 WebApplicationException result = null;
\r
428 String detail = Tools.errorToString(e, true);
\r
429 String detailNoTrace = Tools.errorToString(e, true, 3);
\r
430 if (e instanceof UnauthorizedException) {
\r
431 response = Response.status(Response.Status.UNAUTHORIZED).entity(serviceMsg + e.getMessage()).type("text/plain").build();
\r
432 result = new WebApplicationException(response);
\r
434 } else if (e instanceof DocumentNotFoundException) {
\r
436 // Don't log this error unless we're in 'trace' mode
\r
438 logException = false;
\r
439 response = Response.status(Response.Status.NOT_FOUND).entity(serviceMsg + " on " + getClass().getName() + " csid=" + csid).type("text/plain").build();
\r
440 result = new WebApplicationException(response);
\r
442 } else if (e instanceof BadRequestException) {
\r
443 int code = ((BadRequestException) e).getErrorCode();
\r
445 code = Response.Status.BAD_REQUEST.getStatusCode();
\r
448 response = Response.status(code).entity(serviceMsg + e.getMessage()).type("text/plain").build();
\r
449 // return new WebApplicationException(e, code);
\r
450 result = new WebApplicationException(response);
\r
452 } else if (e instanceof DocumentException) {
\r
453 int code = ((DocumentException) e).getErrorCode();
\r
455 code = Response.Status.BAD_REQUEST.getStatusCode();
\r
458 response = Response.status(code).entity(serviceMsg + e.getMessage()).type("text/plain").build();
\r
459 // return new WebApplicationException(e, code);
\r
460 result = new WebApplicationException(response);
\r
462 } else if (e instanceof WebApplicationException) {
\r
463 // subresource may have already thrown this exception
\r
464 // so just pass it on
\r
465 result = (WebApplicationException) e;
\r
467 } else { // e is now instanceof Exception
\r
468 response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(serviceMsg + " detail: " + detailNoTrace).type("text/plain").build();
\r
469 result = new WebApplicationException(response);
\r
472 // Some exceptions like DocumentNotFoundException won't be logged unless we're in 'trace' mode
\r
474 boolean traceEnabled = logger.isTraceEnabled();
\r
475 if (logException == true || traceEnabled == true) {
\r
476 if (traceEnabled == true) {
\r
477 logger.error(getClass().getName() + " detail: " + detail, e);
\r
479 logger.error(getClass().getName() + " detail: " + detailNoTrace);
\r