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
35 import org.collectionspace.services.common.api.Tools;
\r
36 import org.collectionspace.services.common.context.ServiceContext;
\r
37 import org.collectionspace.services.common.context.ServiceContextProperties;
\r
38 import org.collectionspace.services.common.document.BadRequestException;
\r
39 import org.collectionspace.services.common.document.DocumentException;
\r
40 import org.collectionspace.services.common.document.DocumentHandler;
\r
41 import org.collectionspace.services.common.document.DocumentNotFoundException;
\r
42 import org.collectionspace.services.common.repository.RepositoryClient;
\r
43 import org.collectionspace.services.common.repository.RepositoryClientFactory;
\r
44 import org.collectionspace.services.common.security.UnauthorizedException;
\r
45 import org.collectionspace.services.common.storage.StorageClient;
\r
46 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
\r
47 import org.jboss.resteasy.client.ClientResponse;
\r
48 import org.slf4j.Logger;
\r
49 import org.slf4j.LoggerFactory;
\r
52 * The Class AbstractCollectionSpaceResourceImpl.
\r
54 * @param <IT> the generic type
\r
55 * @param <OT> the generic type
\r
57 public abstract class AbstractCollectionSpaceResourceImpl<IT, OT>
\r
58 implements CollectionSpaceResource<IT, OT> {
\r
60 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
\r
63 // Fields for default client factory and client
\r
64 /** The repository client factory. */
\r
65 private RepositoryClientFactory repositoryClientFactory;
\r
67 /** The repository client. */
\r
68 private RepositoryClient repositoryClient;
\r
70 /** The storage client. */
\r
71 private StorageClient storageClient;
\r
76 * @param res the res
\r
77 * @return the string
\r
79 protected static String extractId(Response res) {
\r
80 MultivaluedMap<String, Object> mvm = res.getMetadata();
\r
81 String uri = (String) ((List<Object>) mvm.get("Location")).get(0);
\r
82 String[] segments = uri.split("/");
\r
83 String id = segments[segments.length - 1];
\r
88 * Instantiates a new abstract collection space resource.
\r
90 public AbstractCollectionSpaceResourceImpl() {
\r
91 repositoryClientFactory = RepositoryClientFactory.getInstance();
\r
95 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceName()
\r
98 abstract public String getServiceName();
\r
102 * @see org.collectionspace.services.common.CollectionSpaceResource#getRepositoryClient(org.collectionspace.services.common.context.ServiceContext)
\r
105 synchronized public RepositoryClient getRepositoryClient(ServiceContext<IT, OT> ctx) {
\r
106 if(repositoryClient != null){
\r
107 return repositoryClient;
\r
109 repositoryClient = repositoryClientFactory.getClient(ctx.getRepositoryClientName());
\r
110 return repositoryClient;
\r
114 * @see org.collectionspace.services.common.CollectionSpaceResource#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
\r
117 synchronized public StorageClient getStorageClient(ServiceContext<IT, OT> ctx) {
\r
118 if(storageClient != null) {
\r
119 return storageClient;
\r
121 storageClient = new JpaStorageClientImpl();
\r
122 return storageClient;
\r
126 * @see org.collectionspace.services.common.CollectionSpaceResource#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
\r
129 public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx) throws Exception {
\r
130 DocumentHandler docHandler = createDocumentHandler(ctx, ctx.getInput());
\r
135 * Creates the document handler.
\r
137 * @param ctx the ctx
\r
138 * @param commonPart the common part
\r
140 * @return the document handler
\r
142 * @throws Exception the exception
\r
144 public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx,
\r
145 Object commonPart) throws Exception {
\r
146 DocumentHandler docHandler = ctx.getDocumentHandler();
\r
147 docHandler.setCommonPart(commonPart);
\r
152 * Creates the service context.
\r
154 * @return the service context< i t, o t>
\r
156 * @throws Exception the exception
\r
158 protected ServiceContext<IT, OT> createServiceContext() throws Exception {
\r
159 ServiceContext<IT, OT> ctx = createServiceContext(this.getServiceName(),
\r
160 (IT)null, //inputType
\r
161 (MultivaluedMap<String, String>)null, /*queryParams*/
\r
162 this.getCommonPartClass());
\r
167 * Creates the service context.
\r
169 * @param serviceName the service name
\r
171 * @return the service context< i t, o t>
\r
173 * @throws Exception the exception
\r
175 protected ServiceContext<IT, OT> createServiceContext(String serviceName) throws Exception {
\r
176 ServiceContext<IT, OT> ctx = createServiceContext(
\r
178 (IT)null, /*input*/
\r
179 (MultivaluedMap<String, String>)null, /*queryParams*/
\r
180 (Class<?>)null /*input type's Class*/);
\r
185 * Creates the service context.
\r
187 * @param serviceName the service name
\r
188 * @param input the input
\r
190 * @return the service context< i t, o t>
\r
192 * @throws Exception the exception
\r
194 protected ServiceContext<IT, OT> createServiceContext(String serviceName,
\r
195 IT input) throws Exception {
\r
196 ServiceContext<IT, OT> ctx = createServiceContext(serviceName, input,
\r
197 (MultivaluedMap<String, String>)null, /*queryParams*/
\r
198 (Class<?>)null /*input type's Class*/);
\r
203 * Creates the service context.
\r
205 * @param serviceName the service name
\r
206 * @return the service context< i t, o t>
\r
207 * @throws Exception the exception
\r
209 protected ServiceContext<IT, OT> createServiceContext(String serviceName,
\r
210 MultivaluedMap<String, String> queryParams) throws Exception {
\r
211 ServiceContext<IT, OT> ctx = createServiceContext(serviceName,
\r
214 (Class<?>)null /*input type's Class*/);
\r
219 * Creates the service context.
\r
221 * @param queryParams the query params
\r
223 * @return the service context< i t, o t>
\r
225 * @throws Exception the exception
\r
227 protected ServiceContext<IT, OT> createServiceContext(MultivaluedMap<String, String> queryParams) throws Exception {
\r
228 ServiceContext<IT, OT> ctx = createServiceContext(
\r
229 (IT)null, /*input*/
\r
231 (Class<?>)null /*input type's Class*/);
\r
236 * Creates the service context.
\r
238 * @param input the input
\r
240 * @return the service context< i t, o t>
\r
242 * @throws Exception the exception
\r
244 protected ServiceContext<IT, OT> createServiceContext(IT input) throws Exception {
\r
245 ServiceContext<IT, OT> ctx = createServiceContext(
\r
247 (Class<?>)null /*input type's Class*/);
\r
252 * Creates the service context.
\r
254 * @param input the input
\r
255 * @param theClass the the class
\r
257 * @return the service context
\r
259 * @throws Exception the exception
\r
261 protected ServiceContext<IT, OT> createServiceContext(IT input, Class<?> theClass) throws Exception {
\r
262 ServiceContext<IT, OT> ctx = createServiceContext(
\r
264 (MultivaluedMap<String, String>)null, //queryParams,
\r
270 * Creates the service context.
\r
272 * @param input the input
\r
273 * @param queryParams the query params
\r
274 * @param theClass the the class
\r
276 * @return the service context< i t, o t>
\r
278 * @throws Exception the exception
\r
280 protected ServiceContext<IT, OT> createServiceContext(
\r
282 MultivaluedMap<String, String> queryParams,
\r
283 Class<?> theClass) throws Exception {
\r
284 return createServiceContext(this.getServiceName(),
\r
291 * Creates the service context.
\r
293 * @param serviceName the service name
\r
294 * @param input the input
\r
295 * @param queryParams the query params
\r
296 * @param theClass the the class
\r
298 * @return the service context< i t, o t>
\r
300 * @throws Exception the exception
\r
302 private ServiceContext<IT, OT> createServiceContext(
\r
303 String serviceName,
\r
305 MultivaluedMap<String, String> queryParams,
\r
306 Class<?> theClass) throws Exception {
\r
307 ServiceContext<IT, OT> ctx = getServiceContextFactory().createServiceContext(
\r
311 theClass != null ? theClass.getPackage().getName() : null,
\r
312 theClass != null ? theClass.getName() : null);
\r
313 if(theClass != null) {
\r
314 ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, theClass);
\r
320 * Gets the version string.
\r
322 * @return the version string
\r
324 abstract protected String getVersionString();
\r
327 * Gets the version.
\r
329 * @return the version
\r
333 @Produces("application/xml")
\r
334 public Version getVersion() {
\r
335 Version result = new Version();
\r
337 result.setVersionString(getVersionString());
\r
342 public void checkResult(Object resultToCheck, String csid, String serviceMessage) throws WebApplicationException {
\r
343 if (resultToCheck == null) {
\r
344 Response response = Response.status(Response.Status.NOT_FOUND).entity(
\r
345 serviceMessage + "csid=" + csid
\r
346 + ": was not found.").type(
\r
347 "text/plain").build();
\r
348 throw new WebApplicationException(response);
\r
352 protected void ensureCSID(String csid, String crudType) throws WebApplicationException {
\r
353 if (logger.isDebugEnabled()) {
\r
354 logger.debug(crudType + " for " + getClass().getName() + " with csid=" + csid);
\r
356 if (csid == null || "".equals(csid)) {
\r
357 logger.error(crudType + " for " + getClass().getName() + " missing csid!");
\r
358 Response response = Response.status(Response.Status.BAD_REQUEST).entity(crudType + " failed on " + getClass().getName() + " csid=" + csid).type("text/plain").build();
\r
359 throw new WebApplicationException(response);
\r
363 protected WebApplicationException bigReThrow(Exception e, String serviceMsg) throws WebApplicationException {
\r
364 return bigReThrow(e, serviceMsg, "");
\r
367 protected WebApplicationException bigReThrow(Exception e, String serviceMsg, String csid) throws WebApplicationException {
\r
369 if (logger.isDebugEnabled()) {
\r
370 logger.debug(getClass().getName(), e);
\r
372 /* ===== how RoleResource does it: =======
\r
373 } catch (BadRequestException bre) {
\r
374 response = Response.status(
\r
375 Response.Status.BAD_REQUEST).entity(ServiceMessages.POST_FAILED
\r
376 + bre.getErrorReason()).type("text/plain").build();
\r
377 throw new WebApplicationException(response);
\r
378 } catch (DocumentException bre) {
\r
379 response = Response.status(
\r
380 Response.Status.BAD_REQUEST).entity(ServiceMessages.POST_FAILED
\r
381 + bre.getErrorReason()).type("text/plain").build();
\r
382 throw new WebApplicationException(response);
\r
383 } catch (UnauthorizedException ue) {
\r
384 response = Response.status(
\r
385 Response.Status.UNAUTHORIZED).entity(ServiceMessages.POST_FAILED
\r
386 + ue.getErrorReason()).type("text/plain").build();
\r
387 throw new WebApplicationException(response);
\r
390 if (e instanceof UnauthorizedException) {
\r
391 response = Response.status(Response.Status.UNAUTHORIZED).entity(serviceMsg + e.getMessage()).type("text/plain").build();
\r
392 return new WebApplicationException(response);
\r
394 } else if (e instanceof DocumentNotFoundException) {
\r
395 response = Response.status(Response.Status.NOT_FOUND).entity(serviceMsg + " on " + getClass().getName() + " csid=" + csid).type("text/plain").build();
\r
396 return new WebApplicationException(response);
\r
398 } else if (e instanceof BadRequestException) {
\r
399 int code = ((BadRequestException) e).getErrorCode();
\r
401 code = Response.Status.BAD_REQUEST.getStatusCode();
\r
403 return new WebApplicationException(e, code);
\r
405 } else if (e instanceof DocumentException){
\r
406 int code = ((DocumentException) e).getErrorCode();
\r
408 code = Response.Status.BAD_REQUEST.getStatusCode();
\r
410 return new WebApplicationException(e, code);
\r
412 } else if (e instanceof WebApplicationException) {
\r
413 // subresource may have already thrown this exception
\r
414 // so just pass it on
\r
415 return (WebApplicationException) e;
\r
417 } else { // e is now instanceof Exception
\r
418 String detail = Tools.errorToString(e, true);
\r
419 response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(serviceMsg + " detail: " + detail).type("text/plain").build();
\r
420 return new WebApplicationException(response);
\r