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.BadRequestException;
\r
47 import org.collectionspace.services.common.document.DocumentHandler;
\r
48 import org.collectionspace.services.common.document.DocumentNotFoundException;
\r
49 import org.collectionspace.services.common.security.UnauthorizedException;
\r
50 import org.collectionspace.services.common.workflow.service.nuxeo.WorkflowDocumentModelHandler;
\r
51 import org.collectionspace.services.workflow.WorkflowCommon;
\r
52 import org.jboss.resteasy.client.ClientResponse;
\r
54 import org.slf4j.Logger;
\r
55 import org.slf4j.LoggerFactory;
\r
58 * The Class AbstractMultiPartCollectionSpaceResourceImpl.
\r
60 public abstract class AbstractMultiPartCollectionSpaceResourceImpl extends 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).entity(serviceMsg + e.getMessage()).type("text/plain").build();
\r
82 return new WebApplicationException(response);
\r
83 } else if (e instanceof DocumentNotFoundException) {
\r
84 response = Response.status(Response.Status.NOT_FOUND).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 BadRequestException) {
\r
88 return new WebApplicationException(e, ((BadRequestException) e).getErrorCode());
\r
89 } else if (e instanceof WebApplicationException) {
\r
91 // subresource may have already thrown this exception
\r
92 // so just pass it on
\r
93 return (WebApplicationException) e;
\r
94 } else { // e is now instanceof Exception
\r
95 String detail = Tools.errorToString(e, true);
\r
96 response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(serviceMsg + " detail: " + detail).type("text/plain").build();
\r
97 return new WebApplicationException(response);
\r
102 public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {
\r
103 return createDocumentHandler(ctx, ctx.getCommonPartLabel(), getCommonPartClass());
\r
107 * Creates the document handler.
\r
109 * @param serviceContext the service context
\r
110 * @param schemaName the schema name
\r
111 * @param commonClass the common class
\r
113 * @return the document handler
\r
115 * @throws Exception the exception
\r
117 public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
\r
119 Class<?> commonClass) throws Exception {
\r
120 DocumentHandler result = null;
\r
122 MultipartServiceContext ctx = (MultipartServiceContext) serviceContext;
\r
123 Object commonPart = null;
\r
124 if (ctx.getInput() != null) {
\r
125 commonPart = ctx.getInputPart(schemaName);
\r
127 result = super.createDocumentHandler(ctx, commonPart);
\r
133 * Creates the document handler.
\r
135 * @param ctx the ctx
\r
136 * @param commonClass the common class
\r
138 * @return the document handler
\r
140 * @throws Exception the exception
\r
142 public DocumentHandler createDocumentHandler(
\r
143 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
\r
144 Class<Object> commonClass) throws Exception {
\r
145 return createDocumentHandler(ctx, ctx.getCommonPartLabel(), commonClass);
\r
149 * Creates the contact document handler.
\r
151 * @param ctx the ctx
\r
152 * @param inAuthority the in authority
\r
153 * @param inItem the in item
\r
155 * @return the document handler
\r
157 * @throws Exception the exception
\r
159 protected WorkflowDocumentModelHandler createWorkflowDocumentHandler(
\r
160 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {
\r
162 WorkflowDocumentModelHandler docHandler = (WorkflowDocumentModelHandler) createDocumentHandler(ctx,
\r
163 WorkflowClient.SERVICE_COMMONPART_NAME,
\r
164 WorkflowCommon.class);
\r
170 * JAX-RS Annotated methods
\r
173 @Path("{csid}" + WorkflowClient.SERVICE_PATH)
\r
174 public byte[] getWorkflow(
\r
175 @PathParam("csid") String csid) {
\r
176 PoxPayloadOut result = null;
\r
179 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext();
\r
180 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
\r
182 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME);
\r
183 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
\r
184 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
\r
185 getRepositoryClient(ctx).get(ctx, csid, handler);
\r
186 result = ctx.getOutput();
\r
187 } catch (Exception e) {
\r
188 throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
\r
191 return result.getBytes();
\r
195 @Path("{csid}" + WorkflowClient.SERVICE_PATH)
\r
196 public byte[] updateWorkflow(@PathParam("csid") String csid, String xmlPayload) {
\r
197 PoxPayloadOut result = null;
\r
199 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext();
\r
200 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
\r
202 PoxPayloadIn workflowUpdate = new PoxPayloadIn(xmlPayload);
\r
203 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, workflowUpdate);
\r
204 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
\r
205 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
\r
206 getRepositoryClient(ctx).update(ctx, csid, handler);
\r
207 result = ctx.getOutput();
\r
208 } catch (Exception e) {
\r
209 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
\r
211 return result.getBytes();
\r