]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5386: Dealing with MIME type issues related to JAX-RS as well as with Nuxeo...
authorRichard Millet <remillet@berkeley.edu>
Wed, 11 Jul 2012 00:27:30 +0000 (17:27 -0700)
committerRichard Millet <remillet@berkeley.edu>
Wed, 11 Jul 2012 00:27:30 +0000 (17:27 -0700)
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/FileUtils.java
services/common/src/main/java/org/collectionspace/services/common/blob/BlobInput.java
services/common/src/main/java/org/collectionspace/services/common/blob/BlobOutput.java
services/common/src/main/java/org/collectionspace/services/common/imaging/nuxeo/NuxeoImageUtils.java
services/media/service/src/main/java/org/collectionspace/services/media/MediaResource.java

index 21819006d6cf51063749c5c4d66456c0d669a0bc..7ed7b396acde29f19a2e153e152caf40a4be2bec 100644 (file)
@@ -116,7 +116,7 @@ public class BlobResource extends ResourceBase {
        return result;
     }
     
-    private InputStream getBlobContent(String csid, String derivativeTerm) throws WebApplicationException {
+    private InputStream getBlobContent(String csid, String derivativeTerm, StringBuffer outMimeType) throws WebApplicationException {
        InputStream result = null;
        
        try {
@@ -132,7 +132,12 @@ public class BlobResource extends ResourceBase {
                //
                // The result of a successful get should have put the results in the
                // blobInput instance
-               //              
+               //
+               
+               String mimeType = blobInput.getMimeType();
+               if (mimeType != null) {
+                       outMimeType.append(mimeType); // blobInput's mime type was set on call to "get" above by the doc handler
+               }
                result = BlobUtil.getBlobInput(ctx).getContentStream();
        } catch (Exception e) {
                throw bigReThrow(e, ServiceMessages.CREATE_FAILED);
@@ -235,23 +240,29 @@ public class BlobResource extends ResourceBase {
     
     @GET
     @Path("{csid}/content")
-    @Produces({"image/jpeg", "image/png", "image/tiff", "application/pdf"})
-    public InputStream getBlobContent(
-               @PathParam("csid") String csid) {
-       InputStream result = null;
-           result = getBlobContent(csid, null /*derivative term*/);            
+    public Response getBlobContent(    @PathParam("csid") String csid) {
+       Response result = null;
+
+       StringBuffer mimeType = new StringBuffer();
+       InputStream contentStream = getBlobContent(csid, null /*derivative term*/, mimeType /*will get set*/);
+           Response.ResponseBuilder responseBuilder = Response.ok(contentStream, mimeType.toString());
+           
+       result = responseBuilder.build();
        return result;
     }
 
     @GET
     @Path("{csid}/derivatives/{derivativeTerm}/content")
-    @Produces({"image/jpeg", "image/png", "image/tiff"})
-    public InputStream getDerivativeContent(
+    public Response getDerivativeContent(
                @PathParam("csid") String csid,
                @PathParam("derivativeTerm") String derivativeTerm) {
-       InputStream result = null;
-           result = getBlobContent(csid, derivativeTerm);
+       Response result = null;
+       
+       StringBuffer mimeType = new StringBuffer();
+       InputStream contentStream = getBlobContent(csid, derivativeTerm, mimeType);         
+           Response.ResponseBuilder responseBuilder = Response.ok(contentStream, mimeType.toString());
            
+       result = responseBuilder.build();
        return result;
     }
     
index 6e48cfdeb13edc986968cffd458ce6c663bfd9e2..f2156b0c8f2bc26cf4b8fdad681bdea6811a20c8 100644 (file)
@@ -136,7 +136,7 @@ extends DocHandlerBase<BlobsCommon> {
        public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
                        throws Exception {
                ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
-               BlobInput blobInput = BlobUtil.getBlobInput(ctx);
+               BlobInput blobInput = BlobUtil.getBlobInput(ctx); // the blobInput was set by the Blob JAX-RS resource code and put into the service context
                RepositoryInstance repoSession = this.getRepositorySession();
                DocumentModel docModel = wrapDoc.getWrappedObject();
                BlobsCommon blobsCommon = this.getCommonPartProperties(docModel);               
@@ -160,8 +160,9 @@ extends DocHandlerBase<BlobsCommon> {
                // fall into this block of code.  Otherwise, we'll just call our parent to deal with a plain-old-blob payload.
                //
                if (derivativeTerm != null || getContentFlag == true) {
+                       StringBuffer mimeTypeBuffer = new StringBuffer();
                        BlobOutput blobOutput = NuxeoImageUtils.getBlobOutput(ctx, repoSession, //FIXME: REM - If the blob's binary has been removed from the file system, then this call will return null.  We need to at least spit out a meaningful error/warning message
-                                       blobRepositoryId, derivativeTerm, getContentFlag);
+                                       blobRepositoryId, derivativeTerm, getContentFlag, mimeTypeBuffer);
                        if (getContentFlag == true) {
                                blobInput.setContentStream(blobOutput.getBlobInputStream());
                        }
@@ -173,6 +174,13 @@ extends DocHandlerBase<BlobsCommon> {
                                                derivativeTerm + "/" + BlobInput.URI_CONTENT_PATH);                             
                        }
                        
+                       String mimeType = mimeTypeBuffer.toString();
+                       if (mimeType != null && !mimeType.isEmpty()) { // MIME type for derivatives might be different from original
+                               blobInput.setMimeType(mimeType);
+                       } else {
+                               blobInput.setMimeType(blobsCommon.getMimeType());
+                       }
+                       
                        blobsCommon.setRepositoryId(null); //hide the repository id from the GET results payload since it is private
                        this.setCommonPartProperties(docModel, blobsCommon);
                        // finish extracting the other parts by calling the parent
index 8a89e2cd5c92ab01428a01563cd107282898e3e2..529308d52791a460f3b128a0e67b4290d8446107 100644 (file)
@@ -41,6 +41,12 @@ import java.util.UUID;
 //import javax.servlet.ServletException;\r
 //import javax.servlet.http.HttpServlet;\r
 \r
+import net.sf.jmimemagic.Magic;\r
+import net.sf.jmimemagic.MagicException;\r
+import net.sf.jmimemagic.MagicMatch;\r
+import net.sf.jmimemagic.MagicMatchNotFoundException;\r
+import net.sf.jmimemagic.MagicParseException;\r
+\r
 import org.apache.commons.fileupload.FileItemFactory;\r
 import org.apache.commons.fileupload.FileItem;\r
 import org.apache.commons.fileupload.servlet.ServletFileUpload;\r
@@ -96,6 +102,33 @@ public class FileUtils {
                return result;\r
        }\r
        \r
+       static public String getMimeType(File file) {\r
+               String result = null;\r
+               \r
+               Magic parser = new Magic() ;\r
+               // getMagicMatch accepts Files or byte[],\r
+               // which is nice if you want to test streams\r
+               MagicMatch match = null;\r
+               try {\r
+                       match = parser.getMagicMatch(file, true);\r
+               } catch (MagicParseException e) {\r
+                       logger.debug("MagicParseException encountered trying to get MIME type for "\r
+                                       + file.getAbsolutePath(), e);\r
+               } catch (MagicMatchNotFoundException e) {\r
+                       logger.debug("MagicMatchNotFoundException encountered trying to get MIME type for "\r
+                                       + file.getAbsolutePath(), e);\r
+               } catch (MagicException e) {\r
+                       logger.debug("MagicException encountered trying to get MIME type for "\r
+                                       + file.getAbsolutePath(), e);\r
+               }\r
+               \r
+               if (match != null) {\r
+                       result = match.getMimeType();\r
+               }\r
+               \r
+               return result;\r
+       }\r
+       \r
        /**\r
         * Look for an uploaded file from the HTTP request of type "multipart/form-data".\r
         *\r
index ba7613170e900cbbecf0af2b07d1edd6be6deda7..446db9ea08e005bef2e4c07953bc7e445e062cb4 100644 (file)
@@ -27,6 +27,7 @@ public class BlobInput {
        private String blobCsid = null;\r
        private File blobFile = null;\r
        private String blobUri = null;\r
+       private String blobMimeType = null;\r
        \r
        private String derivativeTerm;\r
        private boolean derivativeListRequested = false;\r
@@ -168,6 +169,14 @@ public class BlobInput {
                }\r
        this.setBlobFile(theBlobFile);\r
        this.setBlobUri(blobUri);\r
+       }\r
+\r
+       public String getMimeType() {\r
+               return blobMimeType;\r
+       }\r
+\r
+       public void setMimeType(String mimeType) {\r
+               this.blobMimeType = mimeType;\r
        }       \r
        \r
 }\r
index de71337ba3d5fdf700b44e390ca0d3d07b3efc1a..a9325d853c0a87566f2bfa10336823683447703e 100644 (file)
@@ -4,8 +4,9 @@ import java.io.InputStream;
 import org.collectionspace.services.blob.BlobsCommon;\r
 \r
 public class BlobOutput {\r
-       BlobsCommon blobsCommon;\r
-       InputStream blobInputStream;\r
+       private String mimeType;\r
+       private BlobsCommon blobsCommon;\r
+       private InputStream blobInputStream;\r
 \r
        public BlobsCommon getBlobsCommon() {\r
                return blobsCommon;\r
@@ -19,4 +20,10 @@ public class BlobOutput {
        public void setBlobInputStream(InputStream blobInputStream) {\r
                this.blobInputStream = blobInputStream;\r
        }\r
+       public String getMimeType() {\r
+               return mimeType;\r
+       }\r
+       public void setMimeType(String mimeType) {\r
+               this.mimeType = mimeType;\r
+       }\r
 }\r
index bc97cc837c8f055813aff9fabfd33fa7e4fd4dfc..641b500b4b32e4fdb566cd1cc8ccd6c71dff54c0 100644 (file)
@@ -115,6 +115,7 @@ public class NuxeoImageUtils {
        private static final Logger logger = LoggerFactory\r
                        .getLogger(NuxeoImageUtils.class);\r
 \r
+       private static final String MIME_JPEG = "image/jpeg";\r
        /*\r
         * FIXME: REM - These constants should be coming from configuration and NOT\r
         * hard coded.\r
@@ -271,10 +272,29 @@ public class NuxeoImageUtils {
                return metadataMap;\r
        }\r
 \r
+       private static String[] imageTypes = {"jpeg", "bmp", "gif", "png", "tiff", "octet-stream"};\r
+       static private boolean isImageMedia(Blob nuxeoBlob) {\r
+               boolean result = false;\r
+               \r
+               String mimeType = nuxeoBlob.getMimeType().toLowerCase().trim();\r
+               String[] parts = mimeType.split("/"); // split strings like "application/xml" into an array of two strings\r
+               if (parts.length == 2) {\r
+                       for (String type : imageTypes) {\r
+                               if (parts[1].equalsIgnoreCase(type)) {\r
+                                       result = true;\r
+                                       break;\r
+                               }\r
+                       }\r
+               }\r
+               \r
+               return result;\r
+       }\r
+       \r
        static private MeasuredPartGroupList getDimensions(\r
                        DocumentModel documentModel, Blob nuxeoBlob) {\r
                MeasuredPartGroupList result = null;\r
-               try {\r
+               \r
+               if (isImageMedia(nuxeoBlob) == true) try {\r
                        ImagingService service = Framework.getService(ImagingService.class);\r
                        ImageInfo imageInfo = service.getImageInfo(nuxeoBlob);\r
                        Map<String, Object> metadataMap = getMetadata(nuxeoBlob);\r
@@ -692,12 +712,12 @@ public class NuxeoImageUtils {
                        DocumentRef nuxeoWspace = new IdRef(nuxeoWspaceId);\r
                        DocumentModel wspaceDoc = repoSession.getDocument(nuxeoWspace);\r
                        \r
-            if (logger.isDebugEnabled()) {\r
+            if (logger.isTraceEnabled()) {\r
                    for (String facet : wspaceDoc.getFacets()) {\r
-                       logger.debug("Facet: " + facet);\r
+                       logger.trace("Facet: " + facet);\r
                    }\r
                    for (String docType : wspaceDoc.getDocumentType().getChildrenTypes()) {\r
-                       logger.debug("Child type: " + docType);\r
+                       logger.trace("Child type: " + docType);\r
                    }\r
             }                  \r
 \r
@@ -790,7 +810,8 @@ public class NuxeoImageUtils {
                        RepositoryInstance repoSession,\r
                        String repositoryId,\r
                        String derivativeTerm,\r
-                       Boolean getContentFlag) {\r
+                       Boolean getContentFlag,\r
+                       StringBuffer outMimeType) {\r
                BlobOutput result = new BlobOutput();\r
 \r
                if (repositoryId != null && repositoryId.isEmpty() == false)\r
@@ -811,6 +832,8 @@ public class NuxeoImageUtils {
                                        PictureBlobHolder pictureBlobHolder = (PictureBlobHolder) docBlobHolder;\r
                                        if (derivativeTerm != null) {\r
                                                docBlob = pictureBlobHolder.getBlob(derivativeTerm);\r
+                                               // Nuxeo derivatives are all JPEG\r
+                                               outMimeType.append(MIME_JPEG);\r
                                        } else {\r
                                                docBlob = pictureBlobHolder.getBlob();\r
                                        }\r
index 4f16a9f3d8bbed4611cb3df19a5299d2cdfbe235..9d1cd186519524d34fb6420983ef7b541f4bdd7e 100644 (file)
@@ -157,7 +157,7 @@ public class MediaResource extends ResourceBase {
                        //
                        // First, create the blob
                        //
-                       ServiceContext<PoxPayloadIn, PoxPayloadOut> blobContext = createServiceContext(BlobUtil.BLOB_RESOURCE_NAME, input);
+                       ServiceContext<PoxPayloadIn, PoxPayloadOut> blobContext = createServiceContext(BlobClient.SERVICE_NAME, input);
                        BlobInput blobInput = BlobUtil.getBlobInput(blobContext);
                        blobInput.createBlobFile(req, null);
                        response = this.create(input, blobContext);
@@ -185,7 +185,7 @@ public class MediaResource extends ResourceBase {
        
            try {
                String blobCsid = this.getBlobCsid(csid);
-               ServiceContext<PoxPayloadIn, PoxPayloadOut> blobContext = createServiceContext(BlobUtil.BLOB_RESOURCE_NAME);
+               ServiceContext<PoxPayloadIn, PoxPayloadOut> blobContext = createServiceContext(BlobClient.SERVICE_NAME);
                result = this.get(blobCsid, blobContext);               
            } catch (Exception e) {
                throw bigReThrow(e, ServiceMessages.READ_FAILED, csid);
@@ -196,10 +196,9 @@ public class MediaResource extends ResourceBase {
     
     @GET
     @Path("{csid}/blob/content")
-    @Produces({"image/jpeg", "image/png", "image/tiff"})
-    public InputStream getBlobContent(
+    public Response getBlobContent(
                @PathParam("csid") String csid) {
-       InputStream result = null;
+       Response result = null;
        
            try {
                ensureCSID(csid, READ);
@@ -214,11 +213,10 @@ public class MediaResource extends ResourceBase {
     
     @GET
     @Path("{csid}/blob/derivatives/{derivativeTerm}/content")
-    @Produces({"image/jpeg", "image/png", "image/tiff"})
-    public InputStream getDerivativeContent(
+    public Response getDerivativeContent(
                @PathParam("csid") String csid,
                @PathParam("derivativeTerm") String derivativeTerm) {
-       InputStream result = null;
+       Response result = null;
        
            try {
                ensureCSID(csid, READ);
@@ -240,7 +238,7 @@ public class MediaResource extends ResourceBase {
            try {
                ensureCSID(csid, READ);
                String blobCsid = this.getBlobCsid(csid);
-               ServiceContext<PoxPayloadIn, PoxPayloadOut> blobContext = createServiceContext(BlobUtil.BLOB_RESOURCE_NAME);
+               ServiceContext<PoxPayloadIn, PoxPayloadOut> blobContext = createServiceContext(BlobClient.SERVICE_NAME);
                String xmlPayload = getBlobResource().getDerivative(blobCsid, derivativeTerm);
                result = new PoxPayloadOut(xmlPayload.getBytes());
            } catch (Exception e) {
@@ -260,7 +258,7 @@ public class MediaResource extends ResourceBase {
            try {
                ensureCSID(csid, READ);
                String blobCsid = this.getBlobCsid(csid);
-               ServiceContext<PoxPayloadIn, PoxPayloadOut> blobContext = createServiceContext(BlobUtil.BLOB_RESOURCE_NAME);
+               ServiceContext<PoxPayloadIn, PoxPayloadOut> blobContext = createServiceContext(BlobClient.SERVICE_NAME);
                result = getBlobResource().getDerivatives(blobCsid);            
            } catch (Exception e) {
                throw bigReThrow(e, ServiceMessages.READ_FAILED, csid);