From: Richard Millet Date: Tue, 14 Aug 2012 09:19:20 +0000 (-0700) Subject: CSPACE-3635: Modified Media resource to create a blob record from a URI at the same... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=3568017c77beade82f8c00ae069d22f0bb22001a;p=tmp%2Fjakarta-migration.git CSPACE-3635: Modified Media resource to create a blob record from a URI at the same time a new Media resource gets created. --- diff --git a/services/blob/client/src/main/java/org/collectionspace/services/client/BlobClient.java b/services/blob/client/src/main/java/org/collectionspace/services/client/BlobClient.java index 5ae0e2662..4033f33de 100644 --- a/services/blob/client/src/main/java/org/collectionspace/services/client/BlobClient.java +++ b/services/blob/client/src/main/java/org/collectionspace/services/client/BlobClient.java @@ -37,6 +37,7 @@ public class BlobClient extends AbstractCommonListPoxServiceClientImpl return ctx; } + protected ServiceContext createServiceContext( + IT input, + MultivaluedMap queryParams) throws Exception { + return createServiceContext(this.getServiceName(), + input, + queryParams, + null /* the class of the input type */); + } + /** * Creates the service context. * diff --git a/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java b/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java index 7d8a55683..8c21ffb1a 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java +++ b/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java @@ -88,10 +88,10 @@ public abstract class ResourceBase public Response create(@Context ResourceMap resourceMap, @Context UriInfo ui, String xmlPayload) { - return this.create(null, resourceMap, ui, xmlPayload); + return this.create(null, resourceMap, ui, xmlPayload); } - public Response create(ServiceContext parentCtx, + public Response create(ServiceContext parentCtx, // REM: 8/13/2012 - Some sub-classes will override this method -e.g., MediaResource does. @Context ResourceMap resourceMap, @Context UriInfo ui, String xmlPayload) { @@ -99,7 +99,7 @@ public abstract class ResourceBase try { PoxPayloadIn input = new PoxPayloadIn(xmlPayload); - ServiceContext ctx = createServiceContext(input); + ServiceContext ctx = createServiceContext(input, ui.getQueryParameters()); ctx.setResourceMap(resourceMap); if (parentCtx != null && parentCtx.getCurrentRepositorySession() != null) { ctx.setCurrentRepositorySession(parentCtx.getCurrentRepositorySession()); // Reuse the current repo session if one exists diff --git a/services/media/client/src/main/java/org/collectionspace/services/client/MediaClient.java b/services/media/client/src/main/java/org/collectionspace/services/client/MediaClient.java index 67ac258d3..7943c13df 100644 --- a/services/media/client/src/main/java/org/collectionspace/services/client/MediaClient.java +++ b/services/media/client/src/main/java/org/collectionspace/services/client/MediaClient.java @@ -55,9 +55,11 @@ public class MediaClient extends AbstractCommonListPoxServiceClientImpl createBlobFromFormData(String csid, + public ClientResponse createBlobFromFormData(String csid, // this is the Media resource CSID MultipartFormDataOutput formDataOutput) { return getProxy().createBlobFromFormData(csid, formDataOutput); } @@ -65,11 +67,20 @@ public class MediaClient extends AbstractCommonListPoxServiceClientImpl createBlobFromUri(String csid, String blobUri) { return getProxy().createBlobFromUri(csid, blobUri, blobUri); //send the URI as both a query param and as content - } + } + + /* + * Create both a new media record + */ + public ClientResponse createMediaAndBlobWithUri(PoxPayloadOut xmlPayload, String blobUri) { + return getProxy().createMediaAndBlobWithUri(xmlPayload.getBytes(), blobUri); + } /** * @param csid diff --git a/services/media/client/src/main/java/org/collectionspace/services/client/MediaProxy.java b/services/media/client/src/main/java/org/collectionspace/services/client/MediaProxy.java index 841cc57ab..d91b08ecb 100644 --- a/services/media/client/src/main/java/org/collectionspace/services/client/MediaProxy.java +++ b/services/media/client/src/main/java/org/collectionspace/services/client/MediaProxy.java @@ -34,4 +34,10 @@ public interface MediaProxy extends CollectionSpaceCommonListPoxProxy { ClientResponsecreateBlobFromUri(@PathParam("csid") String csid, @QueryParam(BlobClient.BLOB_URI_PARAM) String blobUri, String emptyXML); //this "emptyXML" param is needed to force RESTEasy to produce a Content-Type header for this POST + + @POST + @Produces("application/xml") + @Consumes("application/xml") + ClientResponsecreateMediaAndBlobWithUri(byte[] xmlPayload, + @QueryParam(BlobClient.BLOB_URI_PARAM) String blobUri); } diff --git a/services/media/client/src/test/java/org/collectionspace/services/client/test/MediaServiceTest.java b/services/media/client/src/test/java/org/collectionspace/services/client/test/MediaServiceTest.java index 3d8a2cf5c..b6c3318d2 100644 --- a/services/media/client/src/test/java/org/collectionspace/services/client/test/MediaServiceTest.java +++ b/services/media/client/src/test/java/org/collectionspace/services/client/test/MediaServiceTest.java @@ -56,6 +56,7 @@ public class MediaServiceTest extends AbstractPoxServiceTestImpl mediaRes = client.createMediaAndBlobWithUri(multipart, PUBLIC_URL_DECK); + String mediaCsid = null; + try { + assertStatusCode(mediaRes, testName); + mediaCsid = extractId(mediaRes); + } finally { + if (mediaRes != null) { + mediaRes.releaseConnection(); + } + } + } + + @Test(dataProvider = "testName", dependsOnMethods = {"createWithBlobUri"}) public void createWithBlobPost(String testName) throws Exception { diff --git a/services/media/service/src/main/java/org/collectionspace/services/media/MediaResource.java b/services/media/service/src/main/java/org/collectionspace/services/media/MediaResource.java index 9d1cd1865..0afbf1717 100644 --- a/services/media/service/src/main/java/org/collectionspace/services/media/MediaResource.java +++ b/services/media/service/src/main/java/org/collectionspace/services/media/MediaResource.java @@ -25,12 +25,14 @@ package org.collectionspace.services.media; import org.collectionspace.services.blob.BlobResource; import org.collectionspace.services.client.BlobClient; +import org.collectionspace.services.client.CollectionSpaceClientUtils; +import org.collectionspace.services.client.IQueryManager; import org.collectionspace.services.client.MediaClient; import org.collectionspace.services.client.PayloadOutputPart; import org.collectionspace.services.client.PoxPayloadIn; import org.collectionspace.services.client.PoxPayloadOut; import org.collectionspace.services.common.ResourceBase; -import org.collectionspace.services.common.ServiceMain; +import org.collectionspace.services.common.ResourceMap; import org.collectionspace.services.common.ServiceMessages; import org.collectionspace.services.common.blob.BlobInput; import org.collectionspace.services.common.blob.BlobUtil; @@ -50,8 +52,9 @@ import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; +import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; -import java.io.InputStream; +import javax.ws.rs.core.UriInfo; @Path(MediaClient.SERVICE_PATH) @Consumes("application/xml") @@ -102,9 +105,6 @@ public class MediaResource extends ResourceBase { return result; } - //FIXME retrieve client type from configuration -// final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType(); - @Override protected String getVersionString() { final String lastChangeRevision = "$LastChangedRevision: 2108 $"; @@ -116,11 +116,62 @@ public class MediaResource extends ResourceBase { return MediaCommon.class; } + /* + * Creates a new media record/resource AND creates a new blob (using a URL pointing to a media file/resource) and associates + * it with the new media record/resource. + */ + protected Response createBlobWithUri(ResourceMap resourceMap, UriInfo ui, String xmlPayload, String blobUri) { + Response response = null; + + try { + ServiceContext ctx = createServiceContext(BlobClient.SERVICE_NAME, (PoxPayloadIn)null); // The blobUri argument is our payload + BlobInput blobInput = BlobUtil.getBlobInput(ctx); // the blob doc handler will look for this in the context + blobInput.createBlobFile(blobUri); + response = this.create((PoxPayloadIn)null, ctx); // By now the binary bits have been created and we just need to create the metadata blob record -this info is in the blobInput var + } catch (Exception e) { + throw bigReThrow(e, ServiceMessages.CREATE_FAILED); + } + + return response; + } + + /* + * Looks for a blobUri query param from a POST. If it finds one then it creates a blob and a media resource and associates them. + * (non-Javadoc) + * @see org.collectionspace.services.common.ResourceBase#create(org.collectionspace.services.common.context.ServiceContext, org.collectionspace.services.common.ResourceMap, javax.ws.rs.core.UriInfo, java.lang.String) + */ + @Override + public Response create(ServiceContext parentCtx, + @Context ResourceMap resourceMap, + @Context UriInfo ui, + String xmlPayload) { + Response result = null; + + // + // If we find a "blobUri" query param, then we need to create a blob resource/record first and then the media resource/record + // + MultivaluedMap queryParams = ui.getQueryParameters(); + String blobUri = queryParams.getFirst(BlobClient.BLOB_URI_PARAM); + if (blobUri != null && blobUri.isEmpty() == false) { + result = createBlobWithUri(resourceMap, ui, xmlPayload, blobUri); + String blobCsid = CollectionSpaceClientUtils.extractId(result); + queryParams.add(BlobClient.BLOB_CSID_PARAM, blobCsid); // Add the new blob's csid as an artificial query param -the media doc handler will look for this + } + + result = super.create(parentCtx, resourceMap, ui, xmlPayload); // Now call the parent to finish the media resource POST request + + return result; + } + + + /* + * Creates a new blob (using a URL pointing to a media file/resource) and associates it with an existing media record/resource. + */ @POST @Path("{csid}") @Consumes("application/xml") @Produces("application/xml") - public Response createBlobWithUri(@PathParam("csid") String csid, + public Response createBlobWithUriAndUpdateMedia(@PathParam("csid") String csid, @QueryParam(BlobClient.BLOB_URI_PARAM) String blobUri) { Response response = null; PoxPayloadIn input = null; @@ -143,6 +194,10 @@ public class MediaResource extends ResourceBase { return response; } + /* + * Creates a new blob (using the incoming multipart form data) and associates it with an existing media record/resource. + * If a URL query param is passed in as well, we use the URL to create the new blob instead of the multipart form data. + */ @POST @Path("{csid}") @Consumes("multipart/form-data") @@ -169,7 +224,7 @@ public class MediaResource extends ResourceBase { this.update(csid, input, mediaContext); } else { //A URI query param overrides the incoming multipart/form-data payload in the request - response = createBlobWithUri(csid, blobUri); + response = createBlobWithUriAndUpdateMedia(csid, blobUri); } } catch (Exception e) { throw bigReThrow(e, ServiceMessages.CREATE_FAILED); diff --git a/services/media/service/src/main/java/org/collectionspace/services/media/nuxeo/MediaDocumentModelHandler.java b/services/media/service/src/main/java/org/collectionspace/services/media/nuxeo/MediaDocumentModelHandler.java index c90614aab..9389ba30d 100644 --- a/services/media/service/src/main/java/org/collectionspace/services/media/nuxeo/MediaDocumentModelHandler.java +++ b/services/media/service/src/main/java/org/collectionspace/services/media/nuxeo/MediaDocumentModelHandler.java @@ -25,6 +25,7 @@ package org.collectionspace.services.media.nuxeo; import org.collectionspace.services.MediaJAXBSchema; import org.collectionspace.services.nuxeo.client.java.DocHandlerBase; +import org.collectionspace.services.client.BlobClient; import org.collectionspace.services.common.blob.BlobInput; import org.collectionspace.services.common.blob.BlobUtil; import org.collectionspace.services.common.context.ServiceContext; @@ -36,6 +37,8 @@ import org.nuxeo.ecm.core.api.DocumentModel; import java.util.ArrayList; import java.util.List; +import javax.ws.rs.core.MultivaluedMap; + /** * The Class MediaDocumentModelHandler. */ @@ -73,17 +76,23 @@ public class MediaDocumentModelHandler @Override public void fillAllParts(DocumentWrapper wrapDoc, Action action) throws Exception { ServiceContext ctx = this.getServiceContext(); + String blobCsid = null; + BlobInput blobInput = BlobUtil.getBlobInput(ctx); if (blobInput != null && blobInput.getBlobCsid() != null) { - String blobCsid = blobInput.getBlobCsid(); - // - // If getBlobCsid has a value then we just received a multipart/form-data file post - // - DocumentModel documentModel = wrapDoc.getWrappedObject(); - documentModel.setProperty(ctx.getCommonPartLabel(), MediaJAXBSchema.blobCsid, blobCsid); + blobCsid = blobInput.getBlobCsid(); // If getBlobCsid has a value then we just finishing a multipart/form-data file post, created a blob, and now associating it with an existing media resource } else { + MultivaluedMap queryParams = this.getServiceContext().getQueryParams(); + blobCsid = queryParams.getFirst(BlobClient.BLOB_CSID_PARAM); // if the blobUri query param is set, it's probably set from the MediaResource.create method -we're creating a blob from a URI and creating a new media resource as well + // extract all the other fields from the input payload super.fillAllParts(wrapDoc, action); } + + // + if (blobCsid != null) { + DocumentModel documentModel = wrapDoc.getWrappedObject(); + documentModel.setProperty(ctx.getCommonPartLabel(), MediaJAXBSchema.blobCsid, blobCsid); + } } }