]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1371cb62be714bd9a61f28ca82dafc9dd4260bea
[tmp/jakarta-migration.git] /
1 /**     \r
2  * AbstractMultiPartCollectionSpaceResourceImpl.java\r
3  *\r
4  * {Purpose of This Class}\r
5  *\r
6  * {Other Notes Relating to This Class (Optional)}\r
7  *\r
8  * $LastChangedBy: $\r
9  * $LastChangedRevision: $\r
10  * $LastChangedDate: $\r
11  *\r
12  * This document is a part of the source code and related artifacts\r
13  * for CollectionSpace, an open source collections management system\r
14  * for museums and related institutions:\r
15  *\r
16  * http://www.collectionspace.org\r
17  * http://wiki.collectionspace.org\r
18  *\r
19  * Copyright © 2009 {Contributing Institution}\r
20  *\r
21  * Licensed under the Educational Community License (ECL), Version 2.0.\r
22  * You may not use this file except in compliance with this License.\r
23  *\r
24  * You may obtain a copy of the ECL 2.0 License at\r
25  * https://source.collectionspace.org/collection-space/LICENSE.txt\r
26  */\r
27 package org.collectionspace.services.common;\r
28 \r
29 import javax.ws.rs.GET;\r
30 import javax.ws.rs.Path;\r
31 import javax.ws.rs.PathParam;\r
32 import javax.ws.rs.WebApplicationException;\r
33 import javax.ws.rs.core.Response;\r
34 \r
35 import org.collectionspace.services.client.PayloadInputPart;\r
36 import org.collectionspace.services.client.PoxPayloadIn;\r
37 import org.collectionspace.services.client.PoxPayloadOut;\r
38 import org.collectionspace.services.common.context.MultipartServiceContext;\r
39 import org.collectionspace.services.common.context.MultipartServiceContextFactory;\r
40 import org.collectionspace.services.common.context.ServiceContext;\r
41 import org.collectionspace.services.common.context.ServiceContextFactory;\r
42 import org.collectionspace.services.common.document.DocumentHandler;\r
43 import org.collectionspace.services.common.document.DocumentNotFoundException;\r
44 import org.collectionspace.services.common.security.UnauthorizedException;\r
45 import org.collectionspace.services.common.workflow.client.WorkflowClient;\r
46 import org.collectionspace.services.common.workflow.service.nuxeo.WorkflowDocumentModelHandler;\r
47 import org.collectionspace.services.workflow.WorkflowsCommon;\r
48 \r
49 import org.slf4j.Logger;\r
50 import org.slf4j.LoggerFactory;\r
51 \r
52 /**\r
53  * The Class AbstractMultiPartCollectionSpaceResourceImpl.\r
54  */\r
55 public abstract class AbstractMultiPartCollectionSpaceResourceImpl extends\r
56                 AbstractCollectionSpaceResourceImpl<PoxPayloadIn, PoxPayloadOut> {\r
57 \r
58     protected final Logger logger = LoggerFactory.getLogger(this.getClass());\r
59 \r
60     @Override\r
61     public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {\r
62         return MultipartServiceContextFactory.get();\r
63     }\r
64 \r
65     protected WebApplicationException bigReThrow(Exception e, String serviceMsg)\r
66     throws WebApplicationException {\r
67         return bigReThrow(e, serviceMsg, "");\r
68     }\r
69 \r
70         protected WebApplicationException bigReThrow(Exception e,\r
71                         String serviceMsg, String csid) throws WebApplicationException {\r
72                 Response response;\r
73                 if (logger.isDebugEnabled()) {\r
74                         logger.debug(getClass().getName(), e);\r
75                 }\r
76                 if (e instanceof UnauthorizedException) {\r
77                         response = Response.status(Response.Status.UNAUTHORIZED)\r
78                                         .entity(serviceMsg + e.getMessage()).type("text/plain")\r
79                                         .build();\r
80                         return new WebApplicationException(response);\r
81                 } else if (e instanceof DocumentNotFoundException) {\r
82                         response = Response\r
83                                         .status(Response.Status.NOT_FOUND)\r
84                                         .entity(serviceMsg + " on " + getClass().getName()\r
85                                                         + " csid=" + csid).type("text/plain").build();\r
86                         return new WebApplicationException(response);\r
87                 } else if (e instanceof WebApplicationException) {\r
88                         //\r
89                         // subresource may have already thrown this exception\r
90                         // so just pass it on\r
91                         return (WebApplicationException)e;\r
92                 } else { // e is now instanceof Exception\r
93                         response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)\r
94                                         .entity(serviceMsg).type("text/plain").build();\r
95                         return new WebApplicationException(response);\r
96                 }\r
97         }\r
98     \r
99     @Override\r
100     public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {\r
101         return createDocumentHandler(ctx, ctx.getCommonPartLabel(), getCommonPartClass());\r
102     }\r
103     \r
104     /**\r
105      * Creates the document handler.\r
106      * \r
107      * @param serviceContext the service context\r
108      * @param schemaName the schema name\r
109      * @param commonClass the common class\r
110      * \r
111      * @return the document handler\r
112      * \r
113      * @throws Exception the exception\r
114      */\r
115     public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,\r
116                 String schemaName, \r
117                 Class<?> commonClass) throws Exception {\r
118         DocumentHandler result = null;\r
119         \r
120         MultipartServiceContext ctx = (MultipartServiceContext)serviceContext;\r
121         Object commonPart = null;\r
122         if (ctx.getInput() != null) {\r
123                 commonPart = ctx.getInputPart(schemaName);\r
124         }\r
125         result = super.createDocumentHandler(ctx, commonPart);\r
126         \r
127         return result;\r
128     }\r
129     \r
130     /**\r
131      * Creates the document handler.\r
132      * \r
133      * @param ctx the ctx\r
134      * @param commonClass the common class\r
135      * \r
136      * @return the document handler\r
137      * \r
138      * @throws Exception the exception\r
139      */\r
140     public DocumentHandler createDocumentHandler(\r
141                 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,\r
142                 Class<Object> commonClass) throws Exception {\r
143         return createDocumentHandler(ctx, ctx.getCommonPartLabel(), commonClass);\r
144     }\r
145     \r
146     /**\r
147      * Creates the contact document handler.\r
148      * \r
149      * @param ctx the ctx\r
150      * @param inAuthority the in authority\r
151      * @param inItem the in item\r
152      * \r
153      * @return the document handler\r
154      * \r
155      * @throws Exception the exception\r
156      */\r
157     private WorkflowDocumentModelHandler createWorkflowDocumentHandler(\r
158                 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {\r
159         \r
160         WorkflowDocumentModelHandler docHandler = (WorkflowDocumentModelHandler)createDocumentHandler(ctx,\r
161                         WorkflowClient.SERVICE_COMMONPART_NAME,\r
162                         WorkflowsCommon.class);         \r
163         \r
164         return docHandler;\r
165     }\r
166     \r
167     /*\r
168      * JAX-RS Annotated methods\r
169      */\r
170         \r
171     @GET\r
172     @Path("{csid}/workflow")\r
173     public byte[] getWorkflow(\r
174             @PathParam("csid") String csid) {\r
175         PoxPayloadOut result = null;\r
176 \r
177         try {\r
178             ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext();\r
179             String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();\r
180                 \r
181                 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME);\r
182                 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);\r
183                 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace\r
184             getRepositoryClient(ctx).get(ctx, csid, handler);\r
185             result = ctx.getOutput();\r
186         } catch (Exception e) {\r
187             throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);\r
188         }\r
189                 \r
190         return result.getBytes();\r
191     }\r
192     \r
193 }\r