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.PoxPayloadIn;
\r
39 import org.collectionspace.services.client.PoxPayloadOut;
\r
40 import org.collectionspace.services.client.workflow.WorkflowClient;
\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.service.nuxeo.WorkflowDocumentModelHandler;
\r
49 import org.collectionspace.services.workflow.WorkflowsCommon;
\r
50 import org.jboss.resteasy.client.ClientResponse;
\r
52 import org.slf4j.Logger;
\r
53 import org.slf4j.LoggerFactory;
\r
56 * The Class AbstractMultiPartCollectionSpaceResourceImpl.
\r
58 public abstract class AbstractMultiPartCollectionSpaceResourceImpl extends
\r
59 AbstractCollectionSpaceResourceImpl<PoxPayloadIn, PoxPayloadOut> {
\r
61 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
\r
64 public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {
\r
65 return MultipartServiceContextFactory.get();
\r
68 protected WebApplicationException bigReThrow(Exception e, String serviceMsg)
\r
69 throws WebApplicationException {
\r
70 return bigReThrow(e, serviceMsg, "");
\r
73 protected WebApplicationException bigReThrow(Exception e,
\r
74 String serviceMsg, String csid) throws WebApplicationException {
\r
76 if (logger.isDebugEnabled()) {
\r
77 logger.debug(getClass().getName(), e);
\r
79 if (e instanceof UnauthorizedException) {
\r
80 response = Response.status(Response.Status.UNAUTHORIZED)
\r
81 .entity(serviceMsg + e.getMessage()).type("text/plain")
\r
83 return new WebApplicationException(response);
\r
84 } else if (e instanceof DocumentNotFoundException) {
\r
86 .status(Response.Status.NOT_FOUND)
\r
87 .entity(serviceMsg + " on " + getClass().getName()
\r
88 + " csid=" + csid).type("text/plain").build();
\r
89 return new WebApplicationException(response);
\r
90 } else if (e instanceof WebApplicationException) {
\r
92 // subresource may have already thrown this exception
\r
93 // so just pass it on
\r
94 return (WebApplicationException)e;
\r
95 } else { // e is now instanceof Exception
\r
96 response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
\r
97 .entity(serviceMsg).type("text/plain").build();
\r
98 return new WebApplicationException(response);
\r
103 public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {
\r
104 return createDocumentHandler(ctx, ctx.getCommonPartLabel(), getCommonPartClass());
\r
108 * Creates the document handler.
\r
110 * @param serviceContext the service context
\r
111 * @param schemaName the schema name
\r
112 * @param commonClass the common class
\r
114 * @return the document handler
\r
116 * @throws Exception the exception
\r
118 public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
\r
119 String schemaName,
\r
120 Class<?> commonClass) throws Exception {
\r
121 DocumentHandler result = null;
\r
123 MultipartServiceContext ctx = (MultipartServiceContext)serviceContext;
\r
124 Object commonPart = null;
\r
125 if (ctx.getInput() != null) {
\r
126 commonPart = ctx.getInputPart(schemaName);
\r
128 result = super.createDocumentHandler(ctx, commonPart);
\r
134 * Creates the document handler.
\r
136 * @param ctx the ctx
\r
137 * @param commonClass the common class
\r
139 * @return the document handler
\r
141 * @throws Exception the exception
\r
143 public DocumentHandler createDocumentHandler(
\r
144 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
\r
145 Class<Object> commonClass) throws Exception {
\r
146 return createDocumentHandler(ctx, ctx.getCommonPartLabel(), commonClass);
\r
150 * Creates the contact document handler.
\r
152 * @param ctx the ctx
\r
153 * @param inAuthority the in authority
\r
154 * @param inItem the in item
\r
156 * @return the document handler
\r
158 * @throws Exception the exception
\r
160 private WorkflowDocumentModelHandler createWorkflowDocumentHandler(
\r
161 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {
\r
163 WorkflowDocumentModelHandler docHandler = (WorkflowDocumentModelHandler)createDocumentHandler(ctx,
\r
164 WorkflowClient.SERVICE_COMMONPART_NAME,
\r
165 WorkflowsCommon.class);
\r
171 * JAX-RS Annotated methods
\r
175 @Path("{csid}/workflow")
\r
176 public byte[] getWorkflow(
\r
177 @PathParam("csid") String csid) {
\r
178 PoxPayloadOut result = null;
\r
181 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext();
\r
182 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
\r
184 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME);
\r
185 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
\r
186 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
\r
187 getRepositoryClient(ctx).get(ctx, csid, handler);
\r
188 result = ctx.getOutput();
\r
189 } catch (Exception e) {
\r
190 throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
\r
193 return result.getBytes();
\r
197 @Path("{csid}/workflow")
\r
198 public byte[] updateWorkflow(@PathParam("csid") String csid, String xmlPayload) {
\r
199 PoxPayloadOut result = null;
\r
201 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext();
\r
202 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
\r
204 PoxPayloadIn workflowUpdate = new PoxPayloadIn(xmlPayload);
\r
205 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, workflowUpdate);
\r
206 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
\r
207 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
\r
208 getRepositoryClient(ctx).update(ctx, csid, handler);
\r
209 result = ctx.getOutput();
\r
210 } catch (Exception e) {
\r
211 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
\r
213 return result.getBytes();
\r