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.api.Tools;
\r
42 import org.collectionspace.services.common.context.MultipartServiceContext;
\r
43 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
\r
44 import org.collectionspace.services.common.context.ServiceContext;
\r
45 import org.collectionspace.services.common.context.ServiceContextFactory;
\r
46 import org.collectionspace.services.common.document.DocumentHandler;
\r
47 import org.collectionspace.services.common.document.DocumentNotFoundException;
\r
48 import org.collectionspace.services.common.security.UnauthorizedException;
\r
49 import org.collectionspace.services.common.workflow.service.nuxeo.WorkflowDocumentModelHandler;
\r
50 import org.collectionspace.services.workflow.WorkflowCommon;
\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 String detail = Tools.errorToString(e, true);
\r
98 response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
\r
99 .entity(serviceMsg+" detail: "+detail).type("text/plain").build();
\r
100 return new WebApplicationException(response);
\r
105 public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {
\r
106 return createDocumentHandler(ctx, ctx.getCommonPartLabel(), getCommonPartClass());
\r
110 * Creates the document handler.
\r
112 * @param serviceContext the service context
\r
113 * @param schemaName the schema name
\r
114 * @param commonClass the common class
\r
116 * @return the document handler
\r
118 * @throws Exception the exception
\r
120 public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
\r
121 String schemaName,
\r
122 Class<?> commonClass) throws Exception {
\r
123 DocumentHandler result = null;
\r
125 MultipartServiceContext ctx = (MultipartServiceContext)serviceContext;
\r
126 Object commonPart = null;
\r
127 if (ctx.getInput() != null) {
\r
128 commonPart = ctx.getInputPart(schemaName);
\r
130 result = super.createDocumentHandler(ctx, commonPart);
\r
136 * Creates the document handler.
\r
138 * @param ctx the ctx
\r
139 * @param commonClass the common class
\r
141 * @return the document handler
\r
143 * @throws Exception the exception
\r
145 public DocumentHandler createDocumentHandler(
\r
146 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
\r
147 Class<Object> commonClass) throws Exception {
\r
148 return createDocumentHandler(ctx, ctx.getCommonPartLabel(), commonClass);
\r
152 * Creates the contact document handler.
\r
154 * @param ctx the ctx
\r
155 * @param inAuthority the in authority
\r
156 * @param inItem the in item
\r
158 * @return the document handler
\r
160 * @throws Exception the exception
\r
162 private WorkflowDocumentModelHandler createWorkflowDocumentHandler(
\r
163 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {
\r
165 WorkflowDocumentModelHandler docHandler = (WorkflowDocumentModelHandler)createDocumentHandler(ctx,
\r
166 WorkflowClient.SERVICE_COMMONPART_NAME,
\r
167 WorkflowCommon.class);
\r
173 * JAX-RS Annotated methods
\r
177 @Path("{csid}" + WorkflowClient.SERVICE_PATH)
\r
178 public byte[] getWorkflow(
\r
179 @PathParam("csid") String csid) {
\r
180 PoxPayloadOut result = null;
\r
183 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext();
\r
184 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
\r
186 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME);
\r
187 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
\r
188 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
\r
189 getRepositoryClient(ctx).get(ctx, csid, handler);
\r
190 result = ctx.getOutput();
\r
191 } catch (Exception e) {
\r
192 throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
\r
195 return result.getBytes();
\r
199 @Path("{csid}" + WorkflowClient.SERVICE_PATH)
\r
200 public byte[] updateWorkflow(@PathParam("csid") String csid, String xmlPayload) {
\r
201 PoxPayloadOut result = null;
\r
203 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext();
\r
204 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
\r
206 PoxPayloadIn workflowUpdate = new PoxPayloadIn(xmlPayload);
\r
207 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, workflowUpdate);
\r
208 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
\r
209 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
\r
210 getRepositoryClient(ctx).update(ctx, csid, handler);
\r
211 result = ctx.getOutput();
\r
212 } catch (Exception e) {
\r
213 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
\r
215 return result.getBytes();
\r