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 (MultivaluedMap<String, String>)null, /*queryParams*/
\r
163 this.getCommonPartClass());
\r
168 * Creates the service context.
\r
170 * @param serviceName the service name
\r
172 * @return the service context< i t, o t>
\r
174 * @throws Exception the exception
\r
176 protected ServiceContext<IT, OT> createServiceContext(String serviceName) throws Exception {
\r
177 ServiceContext<IT, OT> ctx = createServiceContext(
\r
179 (IT)null, /*input*/
\r
180 (MultivaluedMap<String, String>)null, /*queryParams*/
\r
181 (Class<?>)null /*input type's Class*/);
\r
186 * Creates the service context.
\r
188 * @param serviceName the service name
\r
189 * @param input the input
\r
191 * @return the service context< i t, o t>
\r
193 * @throws Exception the exception
\r
195 protected ServiceContext<IT, OT> createServiceContext(String serviceName,
\r
196 IT input) throws Exception {
\r
197 ServiceContext<IT, OT> ctx = createServiceContext(serviceName, input,
\r
198 (MultivaluedMap<String, String>)null, /*queryParams*/
\r
199 (Class<?>)null /*input type's Class*/);
\r
204 * Creates the service context.
\r
206 * @param serviceName the service name
\r
207 * @return the service context< i t, o t>
\r
208 * @throws Exception the exception
\r
210 protected ServiceContext<IT, OT> createServiceContext(String serviceName,
\r
211 MultivaluedMap<String, String> queryParams) throws Exception {
\r
212 ServiceContext<IT, OT> ctx = createServiceContext(serviceName,
\r
215 (Class<?>)null /*input type's Class*/);
\r
220 * Creates the service context.
\r
222 * @param queryParams the query params
\r
224 * @return the service context< i t, o t>
\r
226 * @throws Exception the exception
\r
228 protected ServiceContext<IT, OT> createServiceContext(MultivaluedMap<String, String> queryParams) throws Exception {
\r
229 ServiceContext<IT, OT> ctx = createServiceContext(
\r
230 (IT)null, /*input*/
\r
232 (Class<?>)null /*input type's Class*/);
\r
236 protected ServiceContext<IT, OT> createServiceContext(UriInfo ui) throws Exception {
\r
237 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
\r
238 ServiceContext<IT, OT> ctx = createServiceContext(
\r
239 (IT)null, /*input*/
\r
241 (Class<?>)null /*input type's Class*/);
\r
249 * Creates the service context.
\r
251 * @param input the input
\r
253 * @return the service context< i t, o t>
\r
255 * @throws Exception the exception
\r
257 protected ServiceContext<IT, OT> createServiceContext(IT input) throws Exception {
\r
258 ServiceContext<IT, OT> ctx = createServiceContext(
\r
260 (Class<?>)null /*input type's Class*/);
\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 (MultivaluedMap<String, String>)null, //queryParams,
\r
283 * Creates the service context.
\r
285 * @param input the input
\r
286 * @param queryParams the query params
\r
287 * @param theClass the the class
\r
289 * @return the service context< i t, o t>
\r
291 * @throws Exception the exception
\r
293 protected ServiceContext<IT, OT> createServiceContext(
\r
295 MultivaluedMap<String, String> queryParams,
\r
296 Class<?> theClass) throws Exception {
\r
297 return createServiceContext(this.getServiceName(),
\r
304 * Creates the service context.
\r
306 * @param serviceName the service name
\r
307 * @param input the input
\r
308 * @param queryParams the query params
\r
309 * @param theClass the the class
\r
311 * @return the service context< i t, o t>
\r
313 * @throws Exception the exception
\r
315 private ServiceContext<IT, OT> createServiceContext(
\r
316 String serviceName,
\r
318 MultivaluedMap<String, String> queryParams,
\r
319 Class<?> theClass) throws Exception {
\r
320 ServiceContext<IT, OT> ctx = getServiceContextFactory().createServiceContext(
\r
324 theClass != null ? theClass.getPackage().getName() : null,
\r
325 theClass != null ? theClass.getName() : null);
\r
326 if(theClass != null) {
\r
327 ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, theClass);
\r
333 * Gets the version string.
\r
335 * @return the version string
\r
337 abstract protected String getVersionString();
\r
340 * Gets the version.
\r
342 * @return the version
\r
346 @Produces("application/xml")
\r
347 public Version getVersion() {
\r
348 Version result = new Version();
\r
350 result.setVersionString(getVersionString());
\r
355 public void checkResult(Object resultToCheck, String csid, String serviceMessage) throws WebApplicationException {
\r
356 if (resultToCheck == null) {
\r
357 Response response = Response.status(Response.Status.NOT_FOUND).entity(
\r
358 serviceMessage + "csid=" + csid
\r
359 + ": was not found.").type(
\r
360 "text/plain").build();
\r
361 throw new WebApplicationException(response);
\r
365 protected void ensureCSID(String csid, String crudType) throws WebApplicationException {
\r
366 ensureCSID(csid, crudType, "csid");
\r
369 protected void ensureCSID(String csid, String crudType, String whichCsid) throws WebApplicationException {
\r
370 if (logger.isDebugEnabled()) {
\r
371 logger.debug(crudType + " for " + getClass().getName() + " with csid=" + csid);
\r
373 if (csid == null || "".equals(csid)) {
\r
374 logger.error(crudType + " for " + getClass().getName() + " missing csid!");
\r
375 Response response = Response.status(Response.Status.BAD_REQUEST).entity(crudType + " failed on " + getClass().getName() + ' '+whichCsid+'=' + csid).type("text/plain").build();
\r
376 throw new WebApplicationException(response);
\r
380 protected WebApplicationException bigReThrow(Exception e, String serviceMsg) throws WebApplicationException {
\r
381 return bigReThrow(e, serviceMsg, "");
\r
384 protected WebApplicationException bigReThrow(Exception e, String serviceMsg, String csid) throws WebApplicationException {
\r
385 boolean logException = true;
\r
386 WebApplicationException result = null;
\r
389 String detail = Tools.errorToString(e, true);
\r
390 String detailNoTrace = Tools.errorToString(e, true, 3);
\r
391 if (e instanceof UnauthorizedException) {
\r
392 response = Response.status(Response.Status.UNAUTHORIZED).entity(serviceMsg + e.getMessage()).type("text/plain").build();
\r
393 result = new WebApplicationException(response);
\r
395 } else if (e instanceof DocumentNotFoundException) {
\r
397 // Don't log this error unless we're in 'trace' mode
\r
399 logException = false;
\r
400 response = Response.status(Response.Status.NOT_FOUND).entity(serviceMsg + " on " + getClass().getName() + " csid=" + csid).type("text/plain").build();
\r
401 result = new WebApplicationException(response);
\r
403 } else if (e instanceof BadRequestException) {
\r
404 int code = ((BadRequestException) e).getErrorCode();
\r
406 code = Response.Status.BAD_REQUEST.getStatusCode();
\r
409 response = Response.status(code).entity(serviceMsg + e.getMessage()).type("text/plain").build();
\r
410 // return new WebApplicationException(e, code);
\r
411 result = new WebApplicationException(response);
\r
413 } else if (e instanceof DocumentException) {
\r
414 int code = ((DocumentException) e).getErrorCode();
\r
416 code = Response.Status.BAD_REQUEST.getStatusCode();
\r
419 response = Response.status(code).entity(serviceMsg + e.getMessage()).type("text/plain").build();
\r
420 // return new WebApplicationException(e, code);
\r
421 result = new WebApplicationException(response);
\r
423 } else if (e instanceof WebApplicationException) {
\r
424 // subresource may have already thrown this exception
\r
425 // so just pass it on
\r
426 result = (WebApplicationException) e;
\r
428 } else { // e is now instanceof Exception
\r
429 response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(serviceMsg + " detail: " + detailNoTrace).type("text/plain").build();
\r
430 result = new WebApplicationException(response);
\r
433 // Some exceptions like DocumentNotFoundException won't be logged unless we're in 'trace' mode
\r
435 boolean traceEnabled = logger.isTraceEnabled();
\r
436 if (logException == true || traceEnabled == true) {
\r
437 if (traceEnabled == true) {
\r
438 logger.error(getClass().getName() + " detail: " + detail, e);
\r
440 logger.error(getClass().getName() + " detail: " + detailNoTrace);
\r