]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
76ec2781f32934a8c9dbe358e43ef3284bc5f1f2
[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.core.MultivaluedMap;\r
32 import javax.ws.rs.core.Response;\r
33 \r
34 import org.collectionspace.services.common.context.ServiceContext;\r
35 import org.collectionspace.services.common.context.ServiceContextProperties;\r
36 import org.collectionspace.services.common.document.DocumentHandler;\r
37 import org.collectionspace.services.common.repository.RepositoryClient;\r
38 import org.collectionspace.services.common.repository.RepositoryClientFactory;\r
39 import org.collectionspace.services.common.storage.StorageClient;\r
40 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;\r
41 import org.jboss.resteasy.client.ClientResponse;\r
42 \r
43 /**\r
44  * The Class AbstractCollectionSpaceResourceImpl.\r
45  *\r
46  * @param <IT> the generic type\r
47  * @param <OT> the generic type\r
48  */\r
49 public abstract class AbstractCollectionSpaceResourceImpl<IT, OT>\r
50         implements CollectionSpaceResource<IT, OT> {\r
51 \r
52     // Fields for default client factory and client\r
53     /** The repository client factory. */\r
54     private RepositoryClientFactory repositoryClientFactory;\r
55     \r
56     /** The repository client. */\r
57     private RepositoryClient repositoryClient;\r
58     \r
59     /** The storage client. */\r
60     private StorageClient storageClient;\r
61 \r
62     /**\r
63      * Extract id.\r
64      *\r
65      * @param res the res\r
66      * @return the string\r
67      */\r
68     protected static String extractId(Response res) {\r
69         MultivaluedMap<String, Object> mvm = res.getMetadata();\r
70         String uri = (String) ((List<Object>) mvm.get("Location")).get(0);\r
71         String[] segments = uri.split("/");\r
72         String id = segments[segments.length - 1];\r
73         return id;\r
74     }    \r
75     \r
76     /**\r
77      * Instantiates a new abstract collection space resource.\r
78      */\r
79     public AbstractCollectionSpaceResourceImpl() {\r
80         repositoryClientFactory = RepositoryClientFactory.getInstance();\r
81     }\r
82 \r
83     /* (non-Javadoc)\r
84      * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceName()\r
85      */\r
86     @Override\r
87     abstract public String getServiceName();\r
88 \r
89 \r
90     /* (non-Javadoc)\r
91      * @see org.collectionspace.services.common.CollectionSpaceResource#getRepositoryClient(org.collectionspace.services.common.context.ServiceContext)\r
92      */\r
93     @Override\r
94     synchronized public RepositoryClient getRepositoryClient(ServiceContext<IT, OT> ctx) {\r
95         if(repositoryClient != null){\r
96             return repositoryClient;\r
97         }\r
98         repositoryClient = repositoryClientFactory.getClient(ctx.getRepositoryClientName());\r
99         return repositoryClient;\r
100     }\r
101 \r
102     /* (non-Javadoc)\r
103      * @see org.collectionspace.services.common.CollectionSpaceResource#getStorageClient(org.collectionspace.services.common.context.ServiceContext)\r
104      */\r
105     @Override\r
106     synchronized public StorageClient getStorageClient(ServiceContext<IT, OT> ctx) {\r
107         if(storageClient != null) {\r
108             return storageClient;\r
109         }\r
110         storageClient = new JpaStorageClientImpl();\r
111         return storageClient;\r
112     }\r
113     \r
114     /* (non-Javadoc)\r
115      * @see org.collectionspace.services.common.CollectionSpaceResource#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)\r
116      */\r
117     @Override\r
118     public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx) throws Exception {\r
119         DocumentHandler docHandler = createDocumentHandler(ctx, ctx.getInput());\r
120         return docHandler;\r
121     }\r
122     \r
123     /**\r
124      * Creates the document handler.\r
125      * \r
126      * @param ctx the ctx\r
127      * @param commonPart the common part\r
128      * \r
129      * @return the document handler\r
130      * \r
131      * @throws Exception the exception\r
132      */\r
133     public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx,\r
134                 Object commonPart) throws Exception {\r
135         DocumentHandler docHandler = ctx.getDocumentHandler();\r
136         docHandler.setCommonPart(commonPart);\r
137         return docHandler;\r
138     }    \r
139     \r
140     /**\r
141      * Creates the service context.\r
142      * \r
143      * @return the service context< i t, o t>\r
144      * \r
145      * @throws Exception the exception\r
146      */\r
147     protected ServiceContext<IT, OT> createServiceContext() throws Exception {          \r
148         ServiceContext<IT, OT> ctx = createServiceContext(this.getServiceName(),\r
149                         (IT)null, //inputType\r
150                         (MultivaluedMap<String, String>)null, /*queryParams*/\r
151                         this.getCommonPartClass());\r
152         return ctx;\r
153     }    \r
154     \r
155     /**\r
156      * Creates the service context.\r
157      * \r
158      * @param serviceName the service name\r
159      * \r
160      * @return the service context< i t, o t>\r
161      * \r
162      * @throws Exception the exception\r
163      */\r
164     protected ServiceContext<IT, OT> createServiceContext(String serviceName) throws Exception {        \r
165         ServiceContext<IT, OT> ctx = createServiceContext(\r
166                         serviceName,\r
167                         (IT)null, /*input*/\r
168                         (MultivaluedMap<String, String>)null, /*queryParams*/\r
169                         (Class<?>)null  /*input type's Class*/);\r
170         return ctx;\r
171     }\r
172     \r
173     /**\r
174      * Creates the service context.\r
175      * \r
176      * @param serviceName the service name\r
177      * @param input the input\r
178      * \r
179      * @return the service context< i t, o t>\r
180      * \r
181      * @throws Exception the exception\r
182      */\r
183     protected ServiceContext<IT, OT> createServiceContext(String serviceName,\r
184                 IT input) throws Exception {            \r
185         ServiceContext<IT, OT> ctx = createServiceContext(serviceName, input,\r
186                         (MultivaluedMap<String, String>)null, /*queryParams*/\r
187                         (Class<?>)null  /*input type's Class*/);\r
188         return ctx;\r
189     }\r
190     \r
191     /**\r
192      * Creates the service context.\r
193      * \r
194      * @param serviceName the service name\r
195      * @param input the input\r
196      * \r
197      * @return the service context< i t, o t>\r
198      * \r
199      * @throws Exception the exception\r
200      */\r
201     protected ServiceContext<IT, OT> createServiceContext(String serviceName,\r
202                 MultivaluedMap<String, String> queryParams) throws Exception {          \r
203         ServiceContext<IT, OT> ctx = createServiceContext(serviceName,\r
204                         (IT)null,\r
205                         queryParams,\r
206                         (Class<?>)null  /*input type's Class*/);\r
207         return ctx;\r
208     }    \r
209 \r
210     /**\r
211      * Creates the service context.\r
212      * \r
213      * @param queryParams the query params\r
214      * \r
215      * @return the service context< i t, o t>\r
216      * \r
217      * @throws Exception the exception\r
218      */\r
219     protected ServiceContext<IT, OT> createServiceContext(MultivaluedMap<String, String> queryParams) throws Exception {        \r
220         ServiceContext<IT, OT> ctx = createServiceContext(\r
221                         (IT)null, /*input*/\r
222                         queryParams,\r
223                         (Class<?>)null  /*input type's Class*/);\r
224         return ctx;\r
225     }    \r
226         \r
227     /**\r
228      * Creates the service context.\r
229      * \r
230      * @param input the input\r
231      * \r
232      * @return the service context< i t, o t>\r
233      * \r
234      * @throws Exception the exception\r
235      */\r
236     protected ServiceContext<IT, OT> createServiceContext(IT input) throws Exception {          \r
237         ServiceContext<IT, OT> ctx = createServiceContext(\r
238                         input,\r
239                         (Class<?>)null /*input type's Class*/);\r
240         return ctx;\r
241     }\r
242     \r
243     /**\r
244      * Creates the service context.\r
245      * \r
246      * @param input the input\r
247      * @param theClass the the class\r
248      * \r
249      * @return the service context\r
250      * \r
251      * @throws Exception the exception\r
252      */\r
253     protected ServiceContext<IT, OT> createServiceContext(IT input, Class<?> theClass) throws Exception {       \r
254         ServiceContext<IT, OT> ctx = createServiceContext(\r
255                         input,\r
256                         (MultivaluedMap<String, String>)null, //queryParams,\r
257                         theClass);\r
258         return ctx;\r
259     }\r
260     \r
261     /**\r
262      * Creates the service context.\r
263      * \r
264      * @param input the input\r
265      * @param queryParams the query params\r
266      * @param theClass the the class\r
267      * \r
268      * @return the service context< i t, o t>\r
269      * \r
270      * @throws Exception the exception\r
271      */\r
272     protected ServiceContext<IT, OT> createServiceContext(\r
273                 IT input,\r
274                 MultivaluedMap<String, String> queryParams,\r
275                 Class<?> theClass) throws Exception {\r
276         return createServiceContext(this.getServiceName(),\r
277                         input,\r
278                         queryParams,\r
279                         theClass);\r
280     }\r
281 \r
282     /**\r
283      * Creates the service context.\r
284      * \r
285      * @param serviceName the service name\r
286      * @param input the input\r
287      * @param queryParams the query params\r
288      * @param theClass the the class\r
289      * \r
290      * @return the service context< i t, o t>\r
291      * \r
292      * @throws Exception the exception\r
293      */\r
294     private ServiceContext<IT, OT> createServiceContext(\r
295                 String serviceName,\r
296                 IT input,\r
297                 MultivaluedMap<String, String> queryParams,\r
298                 Class<?> theClass) throws Exception {\r
299         ServiceContext<IT, OT> ctx = getServiceContextFactory().createServiceContext(\r
300                         serviceName,\r
301                         input,\r
302                         queryParams,\r
303                         theClass != null ? theClass.getPackage().getName() : null,\r
304                         theClass != null ? theClass.getName() : null);\r
305         if(theClass != null) {\r
306             ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, theClass);\r
307         }\r
308         return ctx;\r
309     }\r
310         \r
311     /**\r
312      * Gets the version string.\r
313      * \r
314      * @return the version string\r
315      */\r
316     abstract protected String getVersionString();\r
317     \r
318     /**\r
319      * Gets the version.\r
320      * \r
321      * @return the version\r
322      */\r
323     @GET\r
324     @Path("/version")    \r
325     @Produces("application/xml")\r
326     public Version getVersion() {\r
327         Version result = new Version();\r
328         \r
329         result.setVersionString(getVersionString());\r
330         \r
331         return result;\r
332     }\r
333 }\r