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;
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.
*/
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(
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
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
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
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
*/\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
* @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
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
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
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
* @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
* 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