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.core.MultivaluedMap;
\r
32 import javax.ws.rs.core.Response;
\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
44 * The Class AbstractCollectionSpaceResourceImpl.
\r
46 * @param <IT> the generic type
\r
47 * @param <OT> the generic type
\r
49 public abstract class AbstractCollectionSpaceResourceImpl<IT, OT>
\r
50 implements CollectionSpaceResource<IT, OT> {
\r
52 // Fields for default client factory and client
\r
53 /** The repository client factory. */
\r
54 private RepositoryClientFactory repositoryClientFactory;
\r
56 /** The repository client. */
\r
57 private RepositoryClient repositoryClient;
\r
59 /** The storage client. */
\r
60 private StorageClient storageClient;
\r
65 * @param res the res
\r
66 * @return the string
\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
77 * Instantiates a new abstract collection space resource.
\r
79 public AbstractCollectionSpaceResourceImpl() {
\r
80 repositoryClientFactory = RepositoryClientFactory.getInstance();
\r
84 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceName()
\r
87 abstract public String getServiceName();
\r
91 * @see org.collectionspace.services.common.CollectionSpaceResource#getRepositoryClient(org.collectionspace.services.common.context.ServiceContext)
\r
94 synchronized public RepositoryClient getRepositoryClient(ServiceContext<IT, OT> ctx) {
\r
95 if(repositoryClient != null){
\r
96 return repositoryClient;
\r
98 repositoryClient = repositoryClientFactory.getClient(ctx.getRepositoryClientName());
\r
99 return repositoryClient;
\r
103 * @see org.collectionspace.services.common.CollectionSpaceResource#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
\r
106 synchronized public StorageClient getStorageClient(ServiceContext<IT, OT> ctx) {
\r
107 if(storageClient != null) {
\r
108 return storageClient;
\r
110 storageClient = new JpaStorageClientImpl();
\r
111 return storageClient;
\r
115 * @see org.collectionspace.services.common.CollectionSpaceResource#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
\r
118 public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx) throws Exception {
\r
119 DocumentHandler docHandler = createDocumentHandler(ctx, ctx.getInput());
\r
124 * Creates the document handler.
\r
126 * @param ctx the ctx
\r
127 * @param commonPart the common part
\r
129 * @return the document handler
\r
131 * @throws Exception the exception
\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
141 * Creates the service context.
\r
143 * @return the service context< i t, o t>
\r
145 * @throws Exception the exception
\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
156 * Creates the service context.
\r
158 * @param serviceName the service name
\r
160 * @return the service context< i t, o t>
\r
162 * @throws Exception the exception
\r
164 protected ServiceContext<IT, OT> createServiceContext(String serviceName) throws Exception {
\r
165 ServiceContext<IT, OT> ctx = createServiceContext(
\r
167 (IT)null, /*input*/
\r
168 (MultivaluedMap<String, String>)null, /*queryParams*/
\r
169 (Class<?>)null /*input type's Class*/);
\r
174 * Creates the service context.
\r
176 * @param serviceName the service name
\r
177 * @param input the input
\r
179 * @return the service context< i t, o t>
\r
181 * @throws Exception the exception
\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
192 * Creates the service context.
\r
194 * @param serviceName the service name
\r
195 * @param input the input
\r
197 * @return the service context< i t, o t>
\r
199 * @throws Exception the exception
\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
206 (Class<?>)null /*input type's Class*/);
\r
211 * Creates the service context.
\r
213 * @param queryParams the query params
\r
215 * @return the service context< i t, o t>
\r
217 * @throws Exception the exception
\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
223 (Class<?>)null /*input type's Class*/);
\r
228 * Creates the service context.
\r
230 * @param input the input
\r
232 * @return the service context< i t, o t>
\r
234 * @throws Exception the exception
\r
236 protected ServiceContext<IT, OT> createServiceContext(IT input) throws Exception {
\r
237 ServiceContext<IT, OT> ctx = createServiceContext(
\r
239 (Class<?>)null /*input type's Class*/);
\r
244 * Creates the service context.
\r
246 * @param input the input
\r
247 * @param theClass the the class
\r
249 * @return the service context
\r
251 * @throws Exception the exception
\r
253 protected ServiceContext<IT, OT> createServiceContext(IT input, Class<?> theClass) throws Exception {
\r
254 ServiceContext<IT, OT> ctx = createServiceContext(
\r
256 (MultivaluedMap<String, String>)null, //queryParams,
\r
262 * Creates the service context.
\r
264 * @param input the input
\r
265 * @param queryParams the query params
\r
266 * @param theClass the the class
\r
268 * @return the service context< i t, o t>
\r
270 * @throws Exception the exception
\r
272 protected ServiceContext<IT, OT> createServiceContext(
\r
274 MultivaluedMap<String, String> queryParams,
\r
275 Class<?> theClass) throws Exception {
\r
276 return createServiceContext(this.getServiceName(),
\r
283 * Creates the service context.
\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
290 * @return the service context< i t, o t>
\r
292 * @throws Exception the exception
\r
294 private ServiceContext<IT, OT> createServiceContext(
\r
295 String serviceName,
\r
297 MultivaluedMap<String, String> queryParams,
\r
298 Class<?> theClass) throws Exception {
\r
299 ServiceContext<IT, OT> ctx = getServiceContextFactory().createServiceContext(
\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
312 * Gets the version string.
\r
314 * @return the version string
\r
316 abstract protected String getVersionString();
\r
319 * Gets the version.
\r
321 * @return the version
\r
325 @Produces("application/xml")
\r
326 public Version getVersion() {
\r
327 Version result = new Version();
\r
329 result.setVersionString(getVersionString());
\r