]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5564: Added support for generic Nuxeo file importer to avoid unnecessary image...
authorRichard Millet <remillet@berkeley.edu>
Sat, 26 Jan 2013 01:05:20 +0000 (17:05 -0800)
committerRichard Millet <remillet@berkeley.edu>
Sat, 26 Jan 2013 01:05:20 +0000 (17:05 -0800)
services/article/client/src/test/java/org/collectionspace/services/client/test/ArticleServiceTest.java
services/blob/service/src/main/java/org/collectionspace/services/blob/BlobResource.java
services/blob/service/src/main/java/org/collectionspace/services/blob/nuxeo/BlobDocumentModelHandler.java
services/common/src/main/java/org/collectionspace/services/common/article/ArticleUtil.java
services/common/src/main/java/org/collectionspace/services/common/imaging/nuxeo/NuxeoBlobUtils.java

index 5d4cc35ff90d39956dae62e0c6c532799f2b5454..6bdbfb363daa4d8f1084ad52bbe185f372937595 100644 (file)
 package org.collectionspace.services.client.test;
 
 //import java.util.ArrayList;
+import java.math.BigInteger;
+import java.util.Date;
+
 import javax.ws.rs.core.Response;
+import javax.xml.datatype.DatatypeConfigurationException;
+import javax.xml.datatype.DatatypeFactory;
+import javax.xml.datatype.XMLGregorianCalendar;
 
 import org.collectionspace.services.client.AbstractCommonListUtils;
 import org.collectionspace.services.client.CollectionSpaceClient;
@@ -602,9 +608,21 @@ public class ArticleServiceTest extends
 
                ArticlesCommon articlesCommon = new ArticlesCommon();
                articlesCommon.setArticleNumber(articleNumber);
+               articlesCommon.setArticleContentName("contentname-" + articleNumber);
+               articlesCommon.setArticleContentRepositoryId("42640780-82eb-4650-8a70");
+               articlesCommon.setArticleContentUrl("https://github.com/collectionspace/services/blob/CSPACE-5564-REM-A/services/article/jaxb/src/main/resources/articles-common.xsd");
                articlesCommon.setArticleJobId(articleJobId);
                articlesCommon.setArticleSource(getUTF8DataFragment());
-
+               try {
+                       XMLGregorianCalendar expirationDate = 
+                                       DatatypeFactory.newInstance().newXMLGregorianCalendarDate(2013, 12, 31, 0);
+                       articlesCommon.setAccessExpirationDate(expirationDate);
+               } catch (DatatypeConfigurationException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
+               articlesCommon.setAccessedCount(new BigInteger("3"));
+               articlesCommon.setAccessedCountLimit(new BigInteger("5"));
 
                PoxPayloadOut multipart = new PoxPayloadOut(
                                this.getServicePathComponent());
index 7343a04cc463e16f82a84d48c3b143bdac073241..441baff01b575db6ec9d8f054a6ae80c7d55ffaa 100644 (file)
 package org.collectionspace.services.blob;
 
 import org.collectionspace.services.article.ArticlesCommon;
-import org.collectionspace.services.client.ArticleClient;
 import org.collectionspace.services.client.BlobClient;
+import org.collectionspace.services.client.PayloadOutputPart;
+import org.collectionspace.services.client.PayloadPart;
 import org.collectionspace.services.client.PoxPayloadIn;
 import org.collectionspace.services.client.PoxPayloadOut;
-import org.collectionspace.services.client.workflow.WorkflowClient;
 import org.collectionspace.services.common.FileUtils;
 import org.collectionspace.services.common.ResourceBase;
 import org.collectionspace.services.common.ResourceMap;
-import org.collectionspace.services.common.ServiceMain;
 import org.collectionspace.services.common.ServiceMessages;
-import org.collectionspace.services.common.article.ArticleResource;
 import org.collectionspace.services.common.article.ArticleUtil;
 import org.collectionspace.services.common.blob.BlobInput;
 import org.collectionspace.services.common.blob.BlobUtil;
 import org.collectionspace.services.common.context.ServiceContext;
-import org.collectionspace.services.common.imaging.nuxeo.NuxeoBlobUtils;
 import org.collectionspace.services.nuxeo.client.java.CommonList;
-import org.collectionspace.services.workflow.WorkflowCommon;
 import org.jboss.resteasy.plugins.providers.multipart.InputPart;
 import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
-import org.nuxeo.ecm.core.api.DocumentModel;
-import org.nuxeo.ecm.core.api.DocumentRef;
-import org.nuxeo.ecm.core.api.IdRef;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.Consumes;
@@ -271,6 +264,17 @@ public class BlobResource extends ResourceBase {
        return result;
     }
     
+    private BlobsCommon getBlobsCommon(String csid) throws Exception {
+       BlobsCommon result = null;
+       
+       ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
+               PoxPayloadOut ppo = this.get(csid, ctx);
+               PayloadPart blobsCommonPart = ppo.getPart(BlobClient.SERVICE_COMMON_PART_NAME);
+               result = (BlobsCommon)blobsCommonPart.getBody();
+               
+       return result;
+    }
+    
     /*
      * Publish the blob content.
      */
@@ -285,17 +289,43 @@ public class BlobResource extends ResourceBase {
        
        try {
                        ctx = createServiceContext();
+                       
+                       BlobsCommon blobsCommon = getBlobsCommon(csid);
                StringBuffer mimeType = new StringBuffer();
                InputStream contentStream = getBlobContent(ctx, csid, null /*derivative term*/, mimeType /*will get set*/);             
                result = ArticleUtil.publishToRepository((ArticlesCommon)null, resourceMap, uriInfo, 
-                               getRepositoryClient(ctx), ctx, contentStream, csid);
+                               getRepositoryClient(ctx), ctx, contentStream, blobsCommon.getName());
        } catch (Exception e) {
                throw bigReThrow(e, ServiceMessages.PUT_FAILED);
        }
        
        return result;
-    }    
+    }
+    
+    @GET
+    @Path("{csid}/derivatives/{derivativeTerm}/content/publish")
+    public Response publishDerivativeContent(
+               @Context ResourceMap resourceMap,
+               @Context UriInfo uriInfo,               
+               @PathParam("csid") String csid,
+               @PathParam("derivativeTerm") String derivativeTerm) {
+       Response result = null;
+       ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
+       
+               try {
+                       ctx = createServiceContext();
 
+                       StringBuffer mimeType = new StringBuffer();
+                       InputStream contentStream = getBlobContent(ctx, csid, derivativeTerm, mimeType);
+                       result = ArticleUtil.publishToRepository((ArticlesCommon)null, resourceMap, uriInfo, 
+                                       getRepositoryClient(ctx), ctx, contentStream, csid);
+               } catch (Exception e) {
+                       throw bigReThrow(e, ServiceMessages.CREATE_FAILED);
+               }
+               
+           return result;
+    }
+    
     @GET
     @Path("{csid}/derivatives/{derivativeTerm}/content")
     public Response getDerivativeContent(
index a9940189511d0d88ab4020ed8f1c8036fd4ddc49..9345ab91e2a852679e6ac2866ac4b1074aab5209 100644 (file)
@@ -222,7 +222,7 @@ extends DocHandlerBase<BlobsCommon> {
                        DocumentModel documentModel = wrapDoc.getWrappedObject();
                        RepositoryInstance repoSession = this.getRepositorySession();
                
-                       BlobsCommon blobsCommon = NuxeoBlobUtils.createBlobInRepository(ctx, repoSession, blobInput, purgeOriginal);
+                       BlobsCommon blobsCommon = NuxeoBlobUtils.createBlobInRepository(ctx, repoSession, blobInput, purgeOriginal, true);
                        blobInput.setBlobCsid(documentModel.getName()); //Assumption here is that the documentModel "name" field is storing a CSID
 
                PoxPayloadIn input = ctx.getInput();
index 2f3a84f0e6e566b5de3c3c5107c3b209c665b7ed..b03c5a6f214bf11f3ae01666913373a17c6a9b39 100644 (file)
@@ -82,7 +82,8 @@ public class ArticleUtil {
                        String streamName) throws TransactionException {
                Response result = null;
                
-       BlobsCommon blobsCommon = NuxeoBlobUtils.createBlobInRepository(parentCtx, repositoryClient, inputStream, streamName);
+       BlobsCommon blobsCommon = NuxeoBlobUtils.createBlobInRepository(parentCtx, repositoryClient,
+                       inputStream, streamName, false);
                
        articlesCommon = setArticlesCommonMetadata(articlesCommon, uriInfo, parentCtx);
        articlesCommon.setArticleContentRepositoryId(blobsCommon.getRepositoryId());
index cef902c6a06cce2522274ba050440e26e3c1a932..12acd2d8eb95667174addd9de5c344bc79ecebf1 100644 (file)
@@ -54,6 +54,9 @@ import org.nuxeo.ecm.platform.mimetype.MimetypeDetectionException;
 import org.nuxeo.ecm.platform.mimetype.interfaces.MimetypeRegistry;\r
 import org.nuxeo.ecm.platform.picture.api.adapters.PictureBlobHolder;\r
 import org.nuxeo.ecm.platform.filemanager.api.FileManager;\r
+import org.nuxeo.ecm.platform.filemanager.service.FileManagerService;\r
+import org.nuxeo.ecm.platform.filemanager.service.extension.FileImporter;\r
+import org.nuxeo.ecm.platform.filemanager.utils.FileManagerUtils;\r
 import org.nuxeo.ecm.platform.types.TypeManager;\r
 \r
 import org.nuxeo.ecm.core.repository.RepositoryDescriptor;\r
@@ -87,8 +90,11 @@ import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
 import org.nuxeo.ecm.core.api.repository.Repository;\r
 import org.nuxeo.ecm.core.api.Blob;\r
 import org.nuxeo.ecm.core.api.ClientException;\r
+import org.nuxeo.ecm.core.api.CoreSession;\r
 import org.nuxeo.ecm.core.api.DocumentModel;\r
 import org.nuxeo.ecm.core.api.DocumentRef;\r
+import org.nuxeo.ecm.core.event.EventServiceAdmin;\r
+import org.nuxeo.ecm.core.event.impl.EventListenerList;\r
 \r
 import org.nuxeo.ecm.core.schema.DocumentType;\r
 import org.nuxeo.ecm.core.schema.SchemaManager;\r
@@ -98,6 +104,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;\r
 //import org.nuxeo.ecm.core.repository.jcr.testing.RepositoryOSGITestCase;\r
 \r
+import org.collectionspace.services.client.PoxPayloadIn;\r
+import org.collectionspace.services.client.PoxPayloadOut;\r
 import org.collectionspace.services.common.ServiceMain;\r
 import org.collectionspace.services.common.blob.BlobInput;\r
 import org.collectionspace.services.common.context.ServiceContext;\r
@@ -538,6 +546,22 @@ public class NuxeoBlobUtils {
                return result;\r
        }\r
 \r
+    static private Blob checkMimeType(Blob blob, String fullname)\r
+            throws ClientException {\r
+        final String mimeType = blob.getMimeType();\r
+        if (mimeType != null && !mimeType.equals("application/octet-stream")\r
+                && !mimeType.equals("application/octetstream")) {\r
+            return blob;\r
+        }\r
+        String filename = FileManagerUtils.fetchFileName(fullname);\r
+        try {\r
+            blob = getMimeService().updateMimetype(blob, filename);\r
+        } catch (MimetypeDetectionException e) {\r
+            throw new ClientException(e);\r
+        }\r
+        return blob;\r
+    }\r
+       \r
        /**\r
         * Gets the type service.  Not in use, but please keep for future reference\r
         * \r
@@ -547,11 +571,13 @@ public class NuxeoBlobUtils {
         */\r
        private static TypeManager getTypeService() throws ClientException {\r
                TypeManager typeService = null;\r
+               \r
                try {\r
                        typeService = Framework.getService(TypeManager.class);\r
                } catch (Exception e) {\r
                        throw new ClientException(e);\r
                }\r
+               \r
                return typeService;\r
        }\r
 \r
@@ -728,7 +754,49 @@ public class NuxeoBlobUtils {
         * @throws ClientException\r
         *             the client exception\r
         */\r
-       private static FileManager getFileManagerService() throws ClientException {\r
+       private static FileManager getFileManager() throws ClientException {\r
+               FileManager result = null;\r
+               \r
+               try {\r
+                       result = Framework.getService(FileManager.class);\r
+               } catch (Exception e) {\r
+                       String msg = "Unable to get Nuxeo's FileManager service.";\r
+                       logger.error(msg, e);\r
+                       throw new ClientException("msg", e);\r
+               }\r
+               \r
+               return result;\r
+       }\r
+               \r
+       /**\r
+        * Gets Nuxeo's file manager service.\r
+        * \r
+        * @return the file manager service\r
+        * @throws ClientException\r
+        *             the client exception\r
+        */\r
+       private static FileManagerService getFileManagerService() throws ClientException {\r
+               FileManagerService result = null;\r
+               \r
+               try {\r
+                       result = (FileManagerService)getFileManager();\r
+               } catch (Exception e) {\r
+                       String msg = "Unable to get Nuxeo's FileManager service.";\r
+                       logger.error(msg, e);\r
+                       throw new ClientException("msg", e);\r
+               }\r
+               \r
+               return result;\r
+       }       \r
+       \r
+       /**\r
+        * Gets Nuxeo's file manager service.\r
+        * \r
+        * @return the file manager service\r
+        * @throws ClientException\r
+        *             the client exception\r
+        */\r
+       private static FileManager getFileManagerServicex() throws ClientException {\r
                FileManager result = null;\r
                try {\r
                        result = Framework.getService(FileManager.class);\r
@@ -740,6 +808,18 @@ public class NuxeoBlobUtils {
                return result;\r
        }\r
        \r
+       private static EventServiceAdmin getEventServiceAdmin() throws ClientException {\r
+               EventServiceAdmin result = null;\r
+               try {\r
+                       result = Framework.getService(EventServiceAdmin.class);\r
+               } catch (Exception e) {\r
+                       String msg = "Unable to get Nuxeo's EventServiceAdmin service.";\r
+                       logger.error(msg, e);\r
+                       throw new ClientException("msg", e);\r
+               }\r
+               return result;\r
+       }       \r
+       \r
        private static BinaryManager getBinaryManagerService() throws ClientException {\r
                BinaryManager result = null;\r
                try {\r
@@ -770,11 +850,60 @@ public class NuxeoBlobUtils {
                nuxeoClient.releaseRepositorySession(ctx, repoSession);\r
        }\r
        \r
+    static private MimetypeRegistry getMimeService() throws ClientException {\r
+       MimetypeRegistry result = null;\r
+       \r
+        try {\r
+               result = Framework.getService(MimetypeRegistry.class);\r
+        } catch (Exception e) {\r
+            throw new ClientException(e);\r
+        }\r
+               \r
+        return result;\r
+    }\r
+       \r
+       private static DocumentModel createDocumentFromBlob(\r
+                       RepositoryInstance repoSession,\r
+            Blob inputStreamBlob, \r
+            String blobLocation, \r
+            boolean overwrite, \r
+            String blobName, \r
+            boolean useNuxeoAdaptors) throws Exception {\r
+               DocumentModel result = null;\r
+               \r
+               if (useNuxeoAdaptors == true) {\r
+                       //\r
+                       // Use Nuxeo's high-level create method which looks for plugin adapters that match the MIME type.  For example,\r
+                       // for image blobs, Nuxeo's file manager will pick a special image plugin that will automatically generate\r
+                       // image derivatives.\r
+                       //\r
+                       result = getFileManager().createDocumentFromBlob(\r
+                                       repoSession, inputStreamBlob, blobLocation, true, blobName);\r
+               } else {\r
+                       //\r
+                       // User Nuxeo's default file importer/adapter explicitly.  This avoids specialized functionality from happening like\r
+                       // image derivative creation.\r
+                       //\r
+                       String digestAlgorithm = getFileManager()\r
+                       .getDigestAlgorithm(); // Only call this because we seem to need some way of initializing Nuxeo's FileManager with a call.\r
+                       \r
+                       FileManagerService fileManagerService = getFileManagerService();\r
+                       inputStreamBlob = checkMimeType(inputStreamBlob, blobName);\r
+\r
+                       FileImporter defaultFileImporter = fileManagerService.getPluginByName("DefaultFileImporter");\r
+                       result = defaultFileImporter.create(\r
+                                       repoSession, inputStreamBlob, blobLocation, true, blobName, getTypeService());                  \r
+               }\r
+               \r
+               return result;\r
+       }\r
+       \r
        static public BlobsCommon createBlobInRepository(\r
-                       ServiceContext ctx,\r
+                       ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,\r
                        RepositoryClient repositoryClient,\r
                        InputStream inputStream,\r
-                       String blobName) throws TransactionException {\r
+                       String blobName,\r
+                       boolean useNuxeoAdaptors) throws TransactionException {\r
                BlobsCommon result = null;\r
 \r
                boolean repoSessionCleanup = false;\r
@@ -783,25 +912,22 @@ public class NuxeoBlobUtils {
                        repoSession = getRepositorySession(ctx, repositoryClient);\r
                        repoSessionCleanup = true;\r
                }\r
-               \r
-               String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();\r
-               DocumentRef nuxeoWspace = new IdRef(nuxeoWspaceId);\r
-               \r
+                               \r
                try {\r
+                       // We'll store the blob inside the workspace directory of the calling service\r
+                       String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();\r
+                       DocumentRef nuxeoWspace = new IdRef(nuxeoWspaceId);\r
                        DocumentModel blobLocation = repoSession.getDocument(nuxeoWspace);\r
-                       Blob inputStreamBlob = new InputStreamBlob(inputStream);\r
-                       String digestAlgorithm = getFileManagerService()\r
-                                       .getDigestAlgorithm(); // Only call this because we seem to need some way of initializing Nuxeo's FileManager with a call.\r
                        \r
-                       logger.debug("Start --> Starting call to Nuxeo to create the blob document."); // For example, see Nuxeo's DefaultPictureAdapter class for details\r
-                       DocumentModel documentModel = getFileManagerService()\r
-                                       .createDocumentFromBlob(repoSession, inputStreamBlob,\r
-                                                       blobLocation.getPathAsString(), true,\r
-                                                       blobName);\r
-                       logger.debug("Stop --> Finished calling Nuxeo to create the blob document.");\r
-\r
-                       result = createBlobsCommon(documentModel, inputStreamBlob); // Now create our metadata resource document\r
-\r
+                       Blob inputStreamBlob = new InputStreamBlob(inputStream);\r
+                       DocumentModel documentModel = createDocumentFromBlob(\r
+                                       repoSession,\r
+                           inputStreamBlob, \r
+                           blobLocation.getPathAsString(), \r
+                           true, \r
+                           blobName,\r
+                           useNuxeoAdaptors);\r
+                       result = createBlobsCommon(documentModel, inputStreamBlob); // Now create the metadata about the Nuxeo blob document\r
                } catch (Exception e) {\r
                        result = null;\r
                        logger.error("Could not create new Nuxeo blob document.", e); //FIXME: REM - This should probably be re-throwing the exception?\r
@@ -826,28 +952,27 @@ public class NuxeoBlobUtils {
         * @return the string\r
         * @throws Exception \r
         */\r
-       public static BlobsCommon createBlobInRepository(ServiceContext ctx,\r
+       public static BlobsCommon createBlobInRepository(\r
+                       ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,\r
                        RepositoryInstance repoSession,\r
                        BlobInput blobInput,\r
-                       boolean purgeOriginal) throws Exception {\r
+                       boolean purgeOriginal,\r
+                       boolean useNuxeoAdaptors) throws Exception {\r
                BlobsCommon result = null;\r
 \r
                try {\r
                        File blobFile = blobInput.getBlobFile();\r
+                       // We'll store the blob inside the workspace directory of the calling service\r
                        String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();\r
                        DocumentRef nuxeoWspace = new IdRef(nuxeoWspaceId);\r
                        DocumentModel wspaceDoc = repoSession.getDocument(nuxeoWspace);\r
                        \r
-            if (logger.isTraceEnabled()) {\r
-                   for (String facet : wspaceDoc.getFacets()) {\r
-                       logger.trace("Facet: " + facet);\r
-                   }\r
-                   for (String docType : wspaceDoc.getDocumentType().getChildrenTypes()) {\r
-                       logger.trace("Child type: " + docType);\r
-                   }\r
-            }                  \r
-\r
-                       result = createBlobInRepository(repoSession, wspaceDoc, purgeOriginal, blobFile, null /*mime type*/);\r
+                       result = createBlobInRepository(repoSession,\r
+                                       wspaceDoc,\r
+                                       purgeOriginal,\r
+                                       blobFile, \r
+                                       null, // MIME type\r
+                                       useNuxeoAdaptors);\r
                } catch (Exception e) {\r
                        logger.error("Could not create image blob", e);\r
                        throw e;\r
@@ -885,26 +1010,25 @@ public class NuxeoBlobUtils {
         *            the mime type\r
         * @return the string\r
         */\r
-       static public BlobsCommon createBlobInRepository(RepositoryInstance nuxeoSession,\r
+       static private BlobsCommon createBlobInRepository(RepositoryInstance nuxeoSession,\r
                        DocumentModel blobLocation,\r
                        boolean purgeOriginal,\r
                        File file,\r
-                       String mimeType) {\r
+                       String mimeType,\r
+                       boolean useNuxeoAdaptors) {\r
                BlobsCommon result = null;\r
 \r
                try {\r
                        // Blob fileBlob = createStreamingBlob(blobFile, blobFile.getName(),\r
                        // mimeType);\r
                        Blob fileBlob = createNuxeoFileBasedBlob(file);\r
-                       String digestAlgorithm = getFileManagerService()\r
-                                       .getDigestAlgorithm(); // Only call this because we seem to need some way of initializing Nuxeo's FileManager with a call.\r
                        \r
-                       logger.debug("Start --> Starting call to Nuxeo to create the blob document."); // For example, see Nuxeo's DefaultPictureAdapter class for details\r
-                       DocumentModel documentModel = getFileManagerService()\r
-                                       .createDocumentFromBlob(nuxeoSession, fileBlob,\r
-                                                       blobLocation.getPathAsString(), true,\r
-                                                       file.getName());\r
-                       logger.debug("Stop --> Finished calling Nuxeo to create the blob document.");\r
+                       DocumentModel documentModel = createDocumentFromBlob(\r
+                                       nuxeoSession, fileBlob,\r
+                                       blobLocation.getPathAsString(),\r
+                                       true,\r
+                                       file.getName(),\r
+                                       useNuxeoAdaptors);\r
 \r
                        result = createBlobsCommon(documentModel, fileBlob); // Now create our metadata resource document\r
 \r