]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3ed87f85e250283771c2f004c556a911cfcd76b8
[tmp/jakarta-migration.git] /
1 /**\r
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
5 \r
6  *  http://www.collectionspace.org\r
7  *  http://wiki.collectionspace.org\r
8 \r
9  *  Copyright 2009 University of California at Berkeley\r
10 \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
13 \r
14  *  You may obtain a copy of the ECL 2.0 License at\r
15 \r
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt\r
17 \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
23  */\r
24 package org.collectionspace.services.common;\r
25 \r
26 import java.util.List;\r
27 \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 \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
50 \r
51 /**\r
52  * The Class AbstractCollectionSpaceResourceImpl.\r
53  *\r
54  * @param <IT> the generic type\r
55  * @param <OT> the generic type\r
56  */\r
57 public abstract class AbstractCollectionSpaceResourceImpl<IT, OT>\r
58         implements CollectionSpaceResource<IT, OT> {\r
59 \r
60     protected final Logger logger = LoggerFactory.getLogger(this.getClass());\r
61 \r
62 \r
63     // Fields for default client factory and client\r
64     /** The repository client factory. */\r
65     private RepositoryClientFactory repositoryClientFactory;\r
66     \r
67     /** The repository client. */\r
68     private RepositoryClient repositoryClient;\r
69     \r
70     /** The storage client. */\r
71     private StorageClient storageClient;\r
72 \r
73     /**\r
74      * Extract id.\r
75      *\r
76      * @param res the res\r
77      * @return the string\r
78      */\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
84         return id;\r
85     }    \r
86     \r
87     /**\r
88      * Instantiates a new abstract collection space resource.\r
89      */\r
90     public AbstractCollectionSpaceResourceImpl() {\r
91         repositoryClientFactory = RepositoryClientFactory.getInstance();\r
92     }\r
93 \r
94     /* (non-Javadoc)\r
95      * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceName()\r
96      */\r
97     @Override\r
98     abstract public String getServiceName();\r
99 \r
100 \r
101     /* (non-Javadoc)\r
102      * @see org.collectionspace.services.common.CollectionSpaceResource#getRepositoryClient(org.collectionspace.services.common.context.ServiceContext)\r
103      */\r
104     @Override\r
105     synchronized public RepositoryClient getRepositoryClient(ServiceContext<IT, OT> ctx) {\r
106         if(repositoryClient != null){\r
107             return repositoryClient;\r
108         }\r
109         repositoryClient = repositoryClientFactory.getClient(ctx.getRepositoryClientName());\r
110         return repositoryClient;\r
111     }\r
112 \r
113     /* (non-Javadoc)\r
114      * @see org.collectionspace.services.common.CollectionSpaceResource#getStorageClient(org.collectionspace.services.common.context.ServiceContext)\r
115      */\r
116     @Override\r
117     synchronized public StorageClient getStorageClient(ServiceContext<IT, OT> ctx) {\r
118         if(storageClient != null) {\r
119             return storageClient;\r
120         }\r
121         storageClient = new JpaStorageClientImpl();\r
122         return storageClient;\r
123     }\r
124     \r
125     /* (non-Javadoc)\r
126      * @see org.collectionspace.services.common.CollectionSpaceResource#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)\r
127      */\r
128     @Override\r
129     public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx) throws Exception {\r
130         DocumentHandler docHandler = createDocumentHandler(ctx, ctx.getInput());\r
131         return docHandler;\r
132     }\r
133     \r
134     /**\r
135      * Creates the document handler.\r
136      * \r
137      * @param ctx the ctx\r
138      * @param commonPart the common part\r
139      * \r
140      * @return the document handler\r
141      * \r
142      * @throws Exception the exception\r
143      */\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
148         return docHandler;\r
149     }    \r
150     \r
151     /**\r
152      * Creates the service context.\r
153      * \r
154      * @return the service context< i t, o t>\r
155      * \r
156      * @throws Exception the exception\r
157      */\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
163         return ctx;\r
164     }    \r
165     \r
166     /**\r
167      * Creates the service context.\r
168      * \r
169      * @param serviceName the service name\r
170      * \r
171      * @return the service context< i t, o t>\r
172      * \r
173      * @throws Exception the exception\r
174      */\r
175     protected ServiceContext<IT, OT> createServiceContext(String serviceName) throws Exception {        \r
176         ServiceContext<IT, OT> ctx = createServiceContext(\r
177                         serviceName,\r
178                         (IT)null, /*input*/\r
179                         (MultivaluedMap<String, String>)null, /*queryParams*/\r
180                         (Class<?>)null  /*input type's Class*/);\r
181         return ctx;\r
182     }\r
183     \r
184     /**\r
185      * Creates the service context.\r
186      * \r
187      * @param serviceName the service name\r
188      * @param input the input\r
189      * \r
190      * @return the service context< i t, o t>\r
191      * \r
192      * @throws Exception the exception\r
193      */\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
199         return ctx;\r
200     }\r
201     \r
202     /**\r
203      * Creates the service context.\r
204      * \r
205      * @param serviceName the service name\r
206      * @return the service context< i t, o t>\r
207      * @throws Exception the exception\r
208      */\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
212                         (IT)null,\r
213                         queryParams,\r
214                         (Class<?>)null  /*input type's Class*/);\r
215         return ctx;\r
216     }    \r
217 \r
218     /**\r
219      * Creates the service context.\r
220      * \r
221      * @param queryParams the query params\r
222      * \r
223      * @return the service context< i t, o t>\r
224      * \r
225      * @throws Exception the exception\r
226      */\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
230                         queryParams,\r
231                         (Class<?>)null  /*input type's Class*/);\r
232         return ctx;\r
233     }    \r
234         \r
235     /**\r
236      * Creates the service context.\r
237      * \r
238      * @param input the input\r
239      * \r
240      * @return the service context< i t, o t>\r
241      * \r
242      * @throws Exception the exception\r
243      */\r
244     protected ServiceContext<IT, OT> createServiceContext(IT input) throws Exception {          \r
245         ServiceContext<IT, OT> ctx = createServiceContext(\r
246                         input,\r
247                         (Class<?>)null /*input type's Class*/);\r
248         return ctx;\r
249     }\r
250     \r
251     /**\r
252      * Creates the service context.\r
253      * \r
254      * @param input the input\r
255      * @param theClass the the class\r
256      * \r
257      * @return the service context\r
258      * \r
259      * @throws Exception the exception\r
260      */\r
261     protected ServiceContext<IT, OT> createServiceContext(IT input, Class<?> theClass) throws Exception {       \r
262         ServiceContext<IT, OT> ctx = createServiceContext(\r
263                         input,\r
264                         (MultivaluedMap<String, String>)null, //queryParams,\r
265                         theClass);\r
266         return ctx;\r
267     }\r
268     \r
269     /**\r
270      * Creates the service context.\r
271      * \r
272      * @param input the input\r
273      * @param queryParams the query params\r
274      * @param theClass the the class\r
275      * \r
276      * @return the service context< i t, o t>\r
277      * \r
278      * @throws Exception the exception\r
279      */\r
280     protected ServiceContext<IT, OT> createServiceContext(\r
281                 IT input,\r
282                 MultivaluedMap<String, String> queryParams,\r
283                 Class<?> theClass) throws Exception {\r
284         return createServiceContext(this.getServiceName(),\r
285                         input,\r
286                         queryParams,\r
287                         theClass);\r
288     }\r
289 \r
290     /**\r
291      * Creates the service context.\r
292      * \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
297      * \r
298      * @return the service context< i t, o t>\r
299      * \r
300      * @throws Exception the exception\r
301      */\r
302     private ServiceContext<IT, OT> createServiceContext(\r
303                 String serviceName,\r
304                 IT input,\r
305                 MultivaluedMap<String, String> queryParams,\r
306                 Class<?> theClass) throws Exception {\r
307         ServiceContext<IT, OT> ctx = getServiceContextFactory().createServiceContext(\r
308                         serviceName,\r
309                         input,\r
310                         queryParams,\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
315         }\r
316         return ctx;\r
317     }\r
318         \r
319     /**\r
320      * Gets the version string.\r
321      * \r
322      * @return the version string\r
323      */\r
324     abstract protected String getVersionString();\r
325     \r
326     /**\r
327      * Gets the version.\r
328      * \r
329      * @return the version\r
330      */\r
331     @GET\r
332     @Path("/version")    \r
333     @Produces("application/xml")\r
334     public Version getVersion() {\r
335         Version result = new Version();\r
336         \r
337         result.setVersionString(getVersionString());\r
338         \r
339         return result;\r
340     }\r
341 \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
349         }\r
350     }\r
351 \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
355            }\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
360            }\r
361        }\r
362 \r
363     protected WebApplicationException bigReThrow(Exception e, String serviceMsg) throws WebApplicationException {\r
364         return bigReThrow(e, serviceMsg, "");\r
365     }\r
366 \r
367     protected WebApplicationException bigReThrow(Exception e, String serviceMsg, String csid) throws WebApplicationException {\r
368         Response response;\r
369         if (logger.isDebugEnabled()) {\r
370             logger.debug(getClass().getName(), e);\r
371         }\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
388          */\r
389 \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
393 \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
397 \r
398         } else if (e instanceof BadRequestException) {\r
399             int code = ((BadRequestException) e).getErrorCode();\r
400             if (code == 0){\r
401                 code = Response.Status.BAD_REQUEST.getStatusCode();\r
402             }\r
403             return new WebApplicationException(e, code);\r
404 \r
405         } else if (e instanceof DocumentException){\r
406             int code = ((DocumentException) e).getErrorCode();\r
407             if (code == 0){\r
408                code = Response.Status.BAD_REQUEST.getStatusCode();\r
409             }\r
410             return new WebApplicationException(e, code);\r
411 \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
416 \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
421         }\r
422     }\r
423 }\r