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 {
//
// 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);
@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;
}
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);
// 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());
}
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
//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
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
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
}\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
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
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
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
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
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
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
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
//
// 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);
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);
@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);
@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);
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) {
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);