2 * AbstractMultiPartCollectionSpaceResourceImpl.java
\r
4 * {Purpose of This Class}
\r
6 * {Other Notes Relating to This Class (Optional)}
\r
9 * $LastChangedRevision: $
\r
10 * $LastChangedDate: $
\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
16 * http://www.collectionspace.org
\r
17 * http://wiki.collectionspace.org
\r
19 * Copyright © 2009 {Contributing Institution}
\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
24 * You may obtain a copy of the ECL 2.0 License at
\r
25 * https://source.collectionspace.org/collection-space/LICENSE.txt
\r
27 package org.collectionspace.services.common;
\r
29 import javax.ws.rs.Consumes;
\r
30 import javax.ws.rs.GET;
\r
31 import javax.ws.rs.PUT;
\r
32 import javax.ws.rs.Path;
\r
33 import javax.ws.rs.PathParam;
\r
34 import javax.ws.rs.Produces;
\r
35 import javax.ws.rs.WebApplicationException;
\r
36 import javax.ws.rs.core.Response;
\r
38 import org.collectionspace.services.client.PayloadInputPart;
\r
39 import org.collectionspace.services.client.PoxPayloadIn;
\r
40 import org.collectionspace.services.client.PoxPayloadOut;
\r
41 import org.collectionspace.services.common.context.MultipartServiceContext;
\r
42 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
\r
43 import org.collectionspace.services.common.context.ServiceContext;
\r
44 import org.collectionspace.services.common.context.ServiceContextFactory;
\r
45 import org.collectionspace.services.common.document.DocumentHandler;
\r
46 import org.collectionspace.services.common.document.DocumentNotFoundException;
\r
47 import org.collectionspace.services.common.security.UnauthorizedException;
\r
48 import org.collectionspace.services.common.workflow.client.WorkflowClient;
\r
49 import org.collectionspace.services.common.workflow.service.nuxeo.WorkflowDocumentModelHandler;
\r
50 import org.collectionspace.services.workflow.WorkflowsCommon;
\r
51 import org.jboss.resteasy.client.ClientResponse;
\r
53 import org.slf4j.Logger;
\r
54 import org.slf4j.LoggerFactory;
\r
57 * The Class AbstractMultiPartCollectionSpaceResourceImpl.
\r
59 public abstract class AbstractMultiPartCollectionSpaceResourceImpl extends
\r
60 AbstractCollectionSpaceResourceImpl<PoxPayloadIn, PoxPayloadOut> {
\r
62 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
\r
65 public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {
\r
66 return MultipartServiceContextFactory.get();
\r
69 protected WebApplicationException bigReThrow(Exception e, String serviceMsg)
\r
70 throws WebApplicationException {
\r
71 return bigReThrow(e, serviceMsg, "");
\r
74 protected WebApplicationException bigReThrow(Exception e,
\r
75 String serviceMsg, String csid) throws WebApplicationException {
\r
77 if (logger.isDebugEnabled()) {
\r
78 logger.debug(getClass().getName(), e);
\r
80 if (e instanceof UnauthorizedException) {
\r
81 response = Response.status(Response.Status.UNAUTHORIZED)
\r
82 .entity(serviceMsg + e.getMessage()).type("text/plain")
\r
84 return new WebApplicationException(response);
\r
85 } else if (e instanceof DocumentNotFoundException) {
\r
87 .status(Response.Status.NOT_FOUND)
\r
88 .entity(serviceMsg + " on " + getClass().getName()
\r
89 + " csid=" + csid).type("text/plain").build();
\r
90 return new WebApplicationException(response);
\r
91 } else if (e instanceof WebApplicationException) {
\r
93 // subresource may have already thrown this exception
\r
94 // so just pass it on
\r
95 return (WebApplicationException)e;
\r
96 } else { // e is now instanceof Exception
\r
97 response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
\r
98 .entity(serviceMsg).type("text/plain").build();
\r
99 return new WebApplicationException(response);
\r
104 public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {
\r
105 return createDocumentHandler(ctx, ctx.getCommonPartLabel(), getCommonPartClass());
\r
109 * Creates the document handler.
\r
111 * @param serviceContext the service context
\r
112 * @param schemaName the schema name
\r
113 * @param commonClass the common class
\r
115 * @return the document handler
\r
117 * @throws Exception the exception
\r
119 public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
\r
120 String schemaName,
\r
121 Class<?> commonClass) throws Exception {
\r
122 DocumentHandler result = null;
\r
124 MultipartServiceContext ctx = (MultipartServiceContext)serviceContext;
\r
125 Object commonPart = null;
\r
126 if (ctx.getInput() != null) {
\r
127 commonPart = ctx.getInputPart(schemaName);
\r
129 result = super.createDocumentHandler(ctx, commonPart);
\r
135 * Creates the document handler.
\r
137 * @param ctx the ctx
\r
138 * @param commonClass the common class
\r
140 * @return the document handler
\r
142 * @throws Exception the exception
\r
144 public DocumentHandler createDocumentHandler(
\r
145 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
\r
146 Class<Object> commonClass) throws Exception {
\r
147 return createDocumentHandler(ctx, ctx.getCommonPartLabel(), commonClass);
\r
151 * Creates the contact document handler.
\r
153 * @param ctx the ctx
\r
154 * @param inAuthority the in authority
\r
155 * @param inItem the in item
\r
157 * @return the document handler
\r
159 * @throws Exception the exception
\r
161 private WorkflowDocumentModelHandler createWorkflowDocumentHandler(
\r
162 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {
\r
164 WorkflowDocumentModelHandler docHandler = (WorkflowDocumentModelHandler)createDocumentHandler(ctx,
\r
165 WorkflowClient.SERVICE_COMMONPART_NAME,
\r
166 WorkflowsCommon.class);
\r
172 * JAX-RS Annotated methods
\r
176 @Path("{csid}/workflow")
\r
177 public byte[] getWorkflow(
\r
178 @PathParam("csid") String csid) {
\r
179 PoxPayloadOut result = null;
\r
182 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext();
\r
183 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
\r
185 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME);
\r
186 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
\r
187 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
\r
188 getRepositoryClient(ctx).get(ctx, csid, handler);
\r
189 result = ctx.getOutput();
\r
190 } catch (Exception e) {
\r
191 throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
\r
194 return result.getBytes();
\r
198 @Path("{csid}/workflow")
\r
199 public byte[] updateWorkflow(@PathParam("csid") String csid, String xmlPayload) {
\r
200 PoxPayloadOut result = null;
\r
202 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext();
\r
203 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
\r
205 PoxPayloadIn workflowUpdate = new PoxPayloadIn(xmlPayload);
\r
206 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, workflowUpdate);
\r
207 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
\r
208 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
\r
209 getRepositoryClient(ctx).update(ctx, csid, handler);
\r
210 result = ctx.getOutput();
\r
211 } catch (Exception e) {
\r
212 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
\r
214 return result.getBytes();
\r