*/
package org.collectionspace.services.collectionobject;
+import java.io.InputStream;
+import java.io.File;
import java.util.ArrayList;
import java.util.List;
+import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import org.collectionspace.services.common.imaging.nuxeo.NuxeoImageUtils;
import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
+import org.collectionspace.services.common.ServiceMain;
import org.collectionspace.services.common.authorityref.AuthorityRefList;
import org.collectionspace.services.common.context.ServiceContextFactory;
//import org.collectionspace.services.common.context.MultipartServiceContext;
import org.collectionspace.services.relation.RelationsCommonList;
import org.collectionspace.services.relation.RelationshipType;
import org.collectionspace.services.common.profile.Profiler;
+import org.collectionspace.services.common.FileUtils;
import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
import org.jboss.resteasy.util.HttpResponseCodes;
+//FIXME: There should be no direct dependency on Nuxeo in our resource classes.
import org.nuxeo.ecm.core.api.DocumentModel;
+import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
return result;
}
+
+ @GET
+ @Path("{csid}/getpicture/{blobId}")
+ @Produces("image/jpeg")
+ public InputStream getPicture(
+ @PathParam("csid") String csid,
+ @PathParam("blobId") String blobId) {
+ InputStream result = null;
+ RepositoryInstance repoSession = null;
+ try {
+ repoSession = ServiceMain.getInstance().getNuxeoConnector().getRepositorySession();
+ ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
+ result = NuxeoImageUtils.getPicture(ctx, repoSession, blobId, null);
+ } catch (Exception e) {
+ logger.error("Could not get image blob: " + blobId, e);
+ } finally {
+ try {
+ ServiceMain.getInstance().getNuxeoConnector().releaseRepositorySession(repoSession);
+ } catch (Exception e) {
+ logger.error("Could not release Nuxeo repository session", e);
+ }
+ }
+
+ if (result == null) {
+ Response response = Response.status(
+ Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed.").type("text/plain").build();
+ throw new WebApplicationException(response);
+ }
+
+ return result;
+ }
@GET
- @Path("/picture")
+ @Path("{csid}/getpicture/{blobId}/{derivativeTerm}")
+ @Produces("image/jpeg")
+ public InputStream getPicture(
+ @PathParam("csid") String csid,
+ @PathParam("blobId") String blobId,
+ @PathParam("derivativeTerm") String derivativeTerm) {
+ InputStream result = null;
+ RepositoryInstance repoSession = null;
+ try {
+ repoSession = ServiceMain.getInstance().getNuxeoConnector().getRepositorySession();
+ ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
+ result = NuxeoImageUtils.getPicture(ctx, repoSession, blobId, derivativeTerm);
+ } catch (Exception e) {
+ logger.error("Could not get image blob: " + blobId, e);
+ } finally {
+ try {
+ ServiceMain.getInstance().getNuxeoConnector().releaseRepositorySession(repoSession);
+ } catch (Exception e) {
+ logger.error("Could not release Nuxeo repository session", e);
+ }
+ }
+
+ if (result == null) {
+ Response response = Response.status(
+ Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed.").type("text/plain").build();
+ throw new WebApplicationException(response);
+ }
+
+ return result;
+ }
+
+ @POST
+ @Path("{csid}/postpicture")
+ @Consumes("multipart/form-data")
@Produces("application/xml")
- public Response createPictureDocument() {
- Response result = null;
+ public String createPictureDocument(@Context HttpServletRequest req,
+ @PathParam("csid") String csid,
+ @QueryParam("blobUri") String blobUri) {
+ String result = null;
- if (logger.isDebugEnabled()) {
- logger.debug("------------------------------------------------------------------------------");
- logger.debug("Prototype to create a Picture document in Nuxeo");
- logger.debug("------------------------------------------------------------------------------");
- logger.debug("");
- }
-
- NuxeoImageUtils.createPicture();
- result = Response.status(HttpResponseCodes.SC_OK).build();
+ RepositoryInstance repoSession = null;
+ try {
+ repoSession = ServiceMain.getInstance().getNuxeoConnector().getRepositorySession();
+ ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
+ File tmpFile = FileUtils.createTmpFile(req);
+ result = NuxeoImageUtils.createPicture(ctx, repoSession, tmpFile);
+ } catch (Exception e) {
+ logger.error("Could not create the new image file", e);
+ } finally {
+ try {
+ ServiceMain.getInstance().getNuxeoConnector().releaseRepositorySession(repoSession);
+ } catch (Exception e) {
+ logger.error("Could not release Nuxeo repository session", e);
+ }
+ }
+
+ if (result == null) {
+ Response response = Response.status(
+ Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed.").type("text/plain").build();
+ throw new WebApplicationException(response);
+ }
return result;
}
package org.collectionspace.services.common.imaging.nuxeo;\r
\r
import java.io.File;\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.FileInputStream;\r
import java.io.Serializable;\r
import java.util.ArrayList;\r
import java.util.HashMap;\r
import java.util.List;\r
import java.util.Map;\r
\r
-import org.nuxeo.ecm.core.api.repository.RepositoryInstance;\r
+import org.nuxeo.runtime.api.Framework;\r
+import org.nuxeo.runtime.api.ServiceManager;\r
+import org.nuxeo.runtime.api.ServiceDescriptor;\r
+\r
import org.nuxeo.common.utils.FileUtils;\r
+\r
+import org.nuxeo.ecm.platform.picture.api.adapters.PictureResourceAdapter;\r
+import org.nuxeo.ecm.platform.mimetype.MimetypeDetectionException;\r
+import org.nuxeo.ecm.platform.mimetype.interfaces.MimetypeRegistry;\r
+import org.nuxeo.ecm.platform.picture.api.adapters.PictureBlobHolder;\r
+import org.nuxeo.ecm.platform.picture.extension.ImagePlugin;\r
+import org.nuxeo.ecm.platform.filemanager.api.FileManager;\r
+import org.nuxeo.ecm.platform.filemanager.service.FileManagerService;\r
+import org.nuxeo.ecm.platform.types.TypeManager;\r
+import org.nuxeo.ecm.platform.picture.api.adapters.PictureBlobHolderFactory;\r
+import org.nuxeo.ecm.platform.picture.api.adapters.PictureBlobHolder;\r
+\r
+import org.nuxeo.ecm.core.repository.RepositoryDescriptor;\r
+import org.nuxeo.ecm.core.repository.RepositoryManager;\r
+\r
+//import org.nuxeo.ecm.core.api.repository.RepositoryManager;\r
+//import org.nuxeo.ecm.core.api.repository.Repository;\r
+\r
+\r
+import org.nuxeo.ecm.core.repository.RepositoryService;\r
+import org.nuxeo.runtime.model.ComponentManager;\r
+import org.nuxeo.runtime.model.ComponentInstance;\r
+import org.nuxeo.runtime.model.impl.ComponentManagerImpl;\r
+//import org.nuxeo.ecm.core.api.ejb.DocumentManagerBean;\r
+//import org.nuxeo.ecm.core.storage.sql.RepositoryImpl;\r
+//import org.nuxeo.ecm.core.storage.sql.Repository;\r
+import org.nuxeo.ecm.core.storage.sql.BinaryManager;\r
+import org.nuxeo.ecm.core.storage.sql.DefaultBinaryManager;\r
+import org.nuxeo.ecm.core.storage.sql.coremodel.SQLRepository;\r
+import org.nuxeo.ecm.core.storage.sql.coremodel.SQLBlob;\r
+//import org.nuxeo.ecm.core.storage.sql.RepositoryDescriptor;\r
+\r
+//import org.nuxeo.ecm.core.api.DocumentResolver;\r
+import org.nuxeo.ecm.core.api.IdRef;\r
+import org.nuxeo.ecm.core.api.blobholder.BlobHolder;\r
+import org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder;\r
+import org.nuxeo.ecm.core.api.impl.blob.FileBlob;\r
+import org.nuxeo.ecm.core.api.impl.blob.StreamingBlob;\r
+import org.nuxeo.ecm.core.api.impl.blob.ByteArrayBlob;\r
+import org.nuxeo.ecm.core.api.repository.RepositoryInstance;\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.DocumentModel;\r
+import org.nuxeo.ecm.core.api.DocumentRef;\r
import org.nuxeo.ecm.core.api.blobholder.BlobHolder;\r
import org.nuxeo.ecm.core.api.blobholder.BlobHolderAdapterService;\r
import org.nuxeo.ecm.core.api.impl.DocumentModelImpl;\r
import org.nuxeo.ecm.core.api.impl.blob.FileBlob;\r
+\r
+import org.nuxeo.ecm.core.model.Document;\r
+import org.nuxeo.ecm.core.schema.SchemaManager;\r
+import org.nuxeo.ecm.core.schema.types.Schema;\r
+\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
//import org.nuxeo.ecm.core.repository.jcr.testing.RepositoryOSGITestCase;\r
-import org.nuxeo.ecm.platform.picture.api.adapters.PictureBlobHolder;\r
-import org.nuxeo.runtime.api.Framework;\r
\r
import org.collectionspace.services.common.ServiceMain;\r
+import org.collectionspace.services.common.context.ServiceContext;\r
+import org.collectionspace.services.common.document.DocumentUtils;\r
\r
+// TODO: Auto-generated Javadoc\r
+/**\r
+ * The Class NuxeoImageUtils.\r
+ */\r
public class NuxeoImageUtils {\r
+ /** The Constant logger. */\r
+ private static final Logger logger = LoggerFactory.getLogger(NuxeoImageUtils.class);\r
+ \r
+// static DefaultBinaryManager binaryManager = new DefaultBinaryManager(); //can we get this from Nuxeo? i.e., Framework.getService(BinaryManger.class)\r
+\r
+// /** The temp file name. */\r
+//static String tempFileName = "sunset.jpg";\r
+// \r
+// /** The file separator. */\r
+// static String fileSeparator = System.getProperty("file.separator");\r
+// \r
+// /** The cur dir. */\r
+// static String curDir = System.getProperty("user.dir");\r
+ \r
+ /**\r
+ * Instantiates a new nuxeo image utils.\r
+ */\r
NuxeoImageUtils() {\r
//empty constructor\r
}\r
\r
/**\r
- * Creates the picture.\r
+ * Logger setup.\r
*/\r
- static public void createPicture() {\r
- RepositoryInstance nuxeoSession = null;\r
+ public static void loggerSetup() {\r
+ //empty method\r
+ } \r
+ \r
+ static private File getBlobFile(RepositoryInstance ri, DocumentModel documentModel, Blob blob) {\r
+ DefaultBinaryManager binaryManager = null;\r
+ RepositoryDescriptor descriptor = null;\r
+ \r
\r
try {\r
- nuxeoSession = ServiceMain.getInstance().getNuxeoConnector().getRepositorySession();\r
- //nuxeoSession.\r
+ ServiceManager sm = (ServiceManager) Framework.getService(ServiceManager.class);\r
+ ServiceDescriptor[] sd = sm.getServiceDescriptors();\r
+ \r
+ RepositoryService repositoryService1 = (RepositoryService) Framework.getRuntime().getComponent(\r
+ RepositoryService.NAME);\r
+ RepositoryService repositoryService2 = (RepositoryService) Framework.getRuntime().getService(\r
+ RepositoryService.class);\r
+ RepositoryService repositoryService3 = (RepositoryService) Framework.getService(\r
+ RepositoryService.class);\r
+ RepositoryService repositoryService4 = (RepositoryService) Framework.getLocalService(\r
+ RepositoryService.class);\r
+ ComponentManager componentManager1 = (ComponentManager) Framework.getService(ComponentManager.class);\r
+ ComponentManager componentManager2 = (ComponentManager) Framework.getService(ComponentManagerImpl.class);\r
+ \r
+ \r
+// RepositoryManager repositoryManager2 = (RepositoryManager) Framework.getService(RepositoryManager.class);\r
+// Repository repository = repositoryManager2.getDefaultRepository();\r
+// Map<String, String> repositoryMap = repository.getProperties();\r
+// String streamURI = ri.getStreamURI(arg0)\r
+\r
+ String repositoryName = documentModel.getRepositoryName();\r
+// RepositoryManager repositoryManager2 = (RepositoryManager) Framework.getService(RepositoryManager.class); \r
+ RepositoryManager repositoryManager = repositoryService1.getRepositoryManager();\r
+ descriptor = repositoryManager.getDescriptor(repositoryName);\r
+ \r
+ binaryManager = new DefaultBinaryManager();\r
+\r
+ File storageDir = binaryManager.getStorageDir();\r
+// SQLBlob blob = (SQLBlob) doc.getPropertyValue("schema:blobField");\r
+ File file = binaryManager.getFileForDigest(\r
+ blob.getDigest(), false); \r
+ \r
+// binaryManager = new DefaultBinaryManager();\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ try {\r
+ binaryManager.initialize(\r
+ SQLRepository.getDescriptor(descriptor));\r
+ } catch (IOException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ } catch (Exception e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ }\r
+\r
+ File storageDir = binaryManager.getStorageDir();\r
+// SQLBlob blob = (SQLBlob) documentModel.getPropertyValue("schema:blobField");\r
+ File file = binaryManager.getFileForDigest(\r
+ blob.getDigest(), false);\r
+ \r
+ return file;\r
+ }\r
+ \r
+ /**\r
+ * Returns a schema, given the name of a schema.\r
+ *\r
+ * @param schemaName a schema name.\r
+ * @return a schema.\r
+ */\r
+ private static Schema getSchemaFromName(String schemaName) {\r
+ SchemaManager schemaManager = null;\r
+ try {\r
+ schemaManager = Framework.getService(SchemaManager.class);\r
} catch (Exception e) {\r
+ // TODO Auto-generated catch block\r
e.printStackTrace();\r
}\r
+ return schemaManager != null ? schemaManager.getSchema(schemaName) : null;\r
+ }\r
+ \r
+ /**\r
+ * Gets the blob.\r
+ *\r
+ * @param nuxeoSession the nuxeo session\r
+ * @param id the id\r
+ * @return the blob\r
+ */\r
+ static private Blob getBlob(RepositoryInstance nuxeoSession, String id) {\r
+ Blob result = null;\r
+ \r
+ try {\r
+ Repository repository = nuxeoSession.getRepository();\r
+// binaryManager.initialize(new RepositoryDescriptor());\r
+// binaryManager.getBinary("a4cac052ae0281979f2dcf5ab2e61a6c");\r
+ // DocumentResolver.resolveReference(nuxeoSession, documentRef);\r
+ //binaryManager = repository.getBinaryManager();\r
+ // documentModel.getr\r
+ } catch (Exception x) {\r
+ x.printStackTrace();\r
+ }\r
+ \r
+ return result;\r
+ }\r
+ \r
+ /**\r
+ * Gets the type service.\r
+ *\r
+ * @return the type service\r
+ * @throws ClientException the client exception\r
+ */\r
+ private static TypeManager getTypeService() throws ClientException {\r
+ TypeManager typeService = null;\r
+ try {\r
+ typeService = Framework.getService(TypeManager.class);\r
+ } catch (Exception e) {\r
+ throw new ClientException(e);\r
+ }\r
+ return typeService;\r
+ }\r
+ \r
+ /**\r
+ * Gets the bytes.\r
+ *\r
+ * @param fis the fis\r
+ * @return the bytes\r
+ */\r
+ private static byte[] getBytes(InputStream fis) {\r
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();\r
+ byte[] buf = new byte[128 * 1024];\r
+ try {\r
+ for (int readNum; (readNum = fis.read(buf)) != -1;) {\r
+ bos.write(buf, 0, readNum); \r
+ //no doubt here is 0\r
+ /*Writes len bytes from the specified byte array starting at offset \r
+ off to this byte array output stream.*/\r
+ System.out.println("read " + readNum + " bytes,");\r
+ }\r
+ } catch (IOException ex) {\r
+ logger.error(ex.getMessage(), ex);\r
+ }\r
+ byte[] bytes = bos.toByteArray();\r
+ //bytes is the ByteArray we need\r
+ return bytes;\r
+ }\r
+ \r
+ /**\r
+ * Creates the serializable blob.\r
+ *\r
+ * @param fileInputStream the file input stream\r
+ * @param filename the filename\r
+ * @param mimeType the mime type\r
+ * @return the blob\r
+ */\r
+ private static Blob createSerializableBlob(InputStream fileInputStream,\r
+ String filename, String mimeType) {\r
+ Blob blob = null;\r
+ try {\r
+ // persisting the blob makes it possible to read the binary content\r
+ // of the request stream several times (mimetype sniffing, digest\r
+ // computation, core binary storage)\r
+ byte[] bytes = getBytes(fileInputStream);\r
+ blob = new ByteArrayBlob(bytes);\r
+ // filename\r
+ if (filename != null) {\r
+ filename = getCleanFileName(filename);\r
+ }\r
+ blob.setFilename(filename);\r
+ // mimetype detection\r
+ MimetypeRegistry mimeService = Framework.getService(MimetypeRegistry.class);\r
+ String detectedMimeType = mimeService.getMimetypeFromFilenameAndBlobWithDefault(\r
+ filename, blob, null);\r
+ if (detectedMimeType == null) {\r
+ if (mimeType != null) {\r
+ detectedMimeType = mimeType;\r
+ } else {\r
+ // default\r
+ detectedMimeType = "application/octet-stream";\r
+ }\r
+ }\r
+ blob.setMimeType(detectedMimeType);\r
+ } catch (MimetypeDetectionException e) {\r
+ logger.error(String.format("could not fetch mimetype for file %s",\r
+ filename), e);\r
+ } catch (Exception e) {\r
+ logger.error("", e);\r
+ }\r
+ return blob;\r
+ }\r
+ \r
+ /**\r
+ * Creates a serializable blob from a stream, with filename and mimetype\r
+ * detection.\r
+ * \r
+ * <p>\r
+ * Creates an in-memory blob if data is under 64K, otherwise constructs a\r
+ * serializable FileBlob which stores data in a temporary file on the hard\r
+ * disk.\r
+ * </p>\r
+ *\r
+ * @param file the input stream holding data\r
+ * @param filename the file name. Will be set on the blob and will used for\r
+ * mimetype detection.\r
+ * @param mimeType the detected mimetype at upload. Can be null. Will be\r
+ * verified by the mimetype service.\r
+ * @return the blob\r
+ */\r
+ private static Blob createStreamingBlob(InputStream file,\r
+ String filename, String mimeType) {\r
+ Blob blob = null;\r
+ try {\r
+ // persisting the blob makes it possible to read the binary content\r
+ // of the request stream several times (mimetype sniffing, digest\r
+ // computation, core binary storage)\r
+ blob = StreamingBlob.createFromStream(file, mimeType).persist();\r
+ // filename\r
+ if (filename != null) {\r
+ filename = getCleanFileName(filename);\r
+ }\r
+ blob.setFilename(filename);\r
+ // mimetype detection\r
+ MimetypeRegistry mimeService = Framework.getService(MimetypeRegistry.class);\r
+ String detectedMimeType = mimeService.getMimetypeFromFilenameAndBlobWithDefault(\r
+ filename, blob, null);\r
+ if (detectedMimeType == null) {\r
+ if (mimeType != null) {\r
+ detectedMimeType = mimeType;\r
+ } else {\r
+ // default\r
+ detectedMimeType = "application/octet-stream";\r
+ }\r
+ }\r
+ blob.setMimeType(detectedMimeType);\r
+ } catch (MimetypeDetectionException e) {\r
+ logger.error(String.format("could not fetch mimetype for file %s",\r
+ filename), e);\r
+ } catch (IOException e) {\r
+ logger.error("", e);\r
+ } catch (Exception e) {\r
+ logger.error("", e);\r
+ }\r
+ return blob;\r
+ }\r
+ \r
+ /**\r
+ * Returns a clean filename, stripping upload path on client side.\r
+ * <p>\r
+ * Fixes NXP-544\r
+ * </p>\r
+ *\r
+ * @param filename the filename\r
+ * @return the clean file name\r
+ */\r
+ private static String getCleanFileName(String filename) {\r
+ String res = null;\r
+ int lastWinSeparator = filename.lastIndexOf('\\');\r
+ int lastUnixSeparator = filename.lastIndexOf('/');\r
+ int lastSeparator = Math.max(lastWinSeparator, lastUnixSeparator);\r
+ if (lastSeparator != -1) {\r
+ res = filename.substring(lastSeparator + 1, filename.length());\r
+ } else {\r
+ res = filename;\r
+ }\r
+ return res;\r
+ }\r
+ \r
+ /**\r
+ * Gets Nuxeo's file manager service.\r
+ *\r
+ * @return the file manager service\r
+ * @throws ClientException the client exception\r
+ */\r
+ private static FileManager getFileManagerService() throws ClientException {\r
+ FileManager result = null;\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
+ return result;\r
+ }\r
+ \r
+ /**\r
+ * Creates the picture.\r
+ *\r
+ * @param ctx the ctx\r
+ * @param repoSession the repo session\r
+ * @param filePath the file path\r
+ * @return the string\r
+ */\r
+ public static String createPicture(ServiceContext ctx,\r
+ RepositoryInstance repoSession,\r
+ File blobFile) {\r
+ String result = null;\r
+ \r
+ try {\r
+ String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();\r
+ DocumentRef nuxeoWspace = new IdRef(nuxeoWspaceId);\r
+ DocumentModel wspaceDoc = repoSession.getDocument(nuxeoWspace);\r
+ \r
+ FileInputStream inputStream = new FileInputStream(blobFile); \r
+ if (inputStream != null) {\r
+ result = createImage(repoSession, wspaceDoc,\r
+ inputStream, blobFile.getName(), null);\r
+ } \r
+ } catch (Exception e) {\r
+ logger.error("Could not create image blob", e);\r
+ } \r
+ \r
+ return result;\r
+ }\r
+ \r
+ /**\r
+ * Creates the image blob.\r
+ *\r
+ * @param nuxeoSession the nuxeo session\r
+ * @param blobLocation the blob location\r
+ * @param file the file\r
+ * @param fileName the file name\r
+ * @param mimeType the mime type\r
+ * @return the string\r
+ */\r
+ static public String createImage(RepositoryInstance nuxeoSession,\r
+ DocumentModel blobLocation,\r
+ InputStream file,\r
+ String fileName, \r
+ String mimeType) {\r
+ String result = null;\r
+ \r
+ try {\r
+ Blob fileBlob = createStreamingBlob(file, fileName, mimeType);\r
+ String digestAlgorithm = getFileManagerService().getDigestAlgorithm(); //Need some way on initializing the FileManager with a call.\r
+// List<String> permissions = nuxeoSession.getAvailableSecurityPermissions();\r
+ DocumentModel documentModel = getFileManagerService().createDocumentFromBlob(nuxeoSession,\r
+ fileBlob, blobLocation.getPathAsString(), true, fileName);\r
+ result = documentModel.getId();\r
+ } catch (Exception e) {\r
+ logger.error("Could not create new image blob", e);\r
+ }\r
+ \r
+ return result;\r
+ }\r
+ \r
+ \r
+ /**\r
+ * Gets the picture.\r
+ *\r
+ * @param ctx the ctx\r
+ * @param repoSession the repo session\r
+ * @param blobId the blob id\r
+ * @param derivativeTerm the derivative term\r
+ * @return the picture\r
+ */\r
+ public static InputStream getPicture(ServiceContext ctx, RepositoryInstance repoSession,\r
+ String blobId, String derivativeTerm) {\r
+ return getImage(repoSession, blobId, derivativeTerm);\r
+ }\r
+ \r
+ /**\r
+ * Gets the image.\r
+ *\r
+ * @param repoSession the repo session\r
+ * @param repositoryId the repository id\r
+ * @param derivativeTerm the derivative term\r
+ * @return the image\r
+ */\r
+ static public InputStream getImage(RepositoryInstance repoSession,\r
+ String repositoryId, String derivativeTerm) {\r
+ InputStream result = null;\r
+\r
+ try {\r
+ IdRef documentRef = new IdRef(repositoryId);\r
+ DocumentModel documentModel = repoSession.getDocument(documentRef);\r
+\r
+// documentModel.getAdapter(PictureResourceAdapter.class);\r
+// DocumentBlobHolder docBlobHolder = (DocumentBlobHolder)documentModel.getAdapter(BlobHolder.class);\r
+// Blob docBlob = docBlobHolder.getBlob();\r
+// Map<String,Serializable> blobHolderProps = docBlobHolder.getProperties();\r
+// String filePath = docBlobHolder.getFilePath();\r
+// List<Blob> docBlobs = docBlobHolder.getBlobs();\r
+ \r
+ PictureBlobHolderFactory blobHolderFactory = new PictureBlobHolderFactory();\r
+ PictureBlobHolder pictureBlobHolder = (PictureBlobHolder) blobHolderFactory.getBlobHolder(documentModel);\r
+ Blob pictureBlob = null;\r
+ if (derivativeTerm != null) {\r
+ pictureBlob = pictureBlobHolder.getBlob(derivativeTerm);\r
+ } else {\r
+ pictureBlob = pictureBlobHolder.getBlob();\r
+ }\r
+ \r
+ result = pictureBlob.getStream(); \r
+ } catch (Exception e) {\r
+ logger.error(e.getMessage(), e);\r
+ }\r
+ \r
+ return result;\r
}\r
}\r
+\r
+/*\r
+stream = new FileInputStream(fileUploadHolder.getTempFile());\r
+\r
+ public String addFile(InputStream fileUpload, String fileName)\r
+ fileName = FileUtils.getCleanFileName(fileName);\r
+ DocumentModel currentDocument = navigationContext.getCurrentDocument();\r
+ String path = currentDocument.getPathAsString();\r
+ Blob blob = FileUtils.createSerializableBlob(fileUpload, fileName,\r
+ null);\r
+\r
+ DocumentModel createdDoc = getFileManagerService().createDocumentFromBlob(\r
+ documentManager, blob, path, true, fileName);\r
+ eventManager.raiseEventsOnDocumentSelected(createdDoc);\r
+\r
+ protected FileManager fileManager;\r
+\r
+ protected FileManager getFileManagerService() throws ClientException {\r
+ if (fileManager == null) {\r
+ try {\r
+ fileManager = Framework.getService(FileManager.class);\r
+ } catch (Exception e) {\r
+ log.error("Unable to get FileManager service ", e);\r
+ throw new ClientException("Unable to get FileManager service ",\r
+ e);\r
+ }\r
+ }\r
+ return fileManager;\r
+ }\r
+*/\r
+\r
+/*\r
+ RepositoryService repositoryService = (RepositoryService) Framework.getRuntime().getComponent(\r
+ RepositoryService.NAME);\r
+ RepositoryManager repositoryManager = repositoryService.getRepositoryManager();\r
+ RepositoryDescriptor descriptor = repositoryManager.getDescriptor(repositoryName);\r
+ DefaultBinaryManager binaryManager = new DefaultBinaryManager(\r
+ SQLRepository.getDescriptor(descriptor)));\r
+\r
+ File storageDir = binaryManager.getStorageDir();\r
+ SQLBlob blob = (SQLBlob) doc.getPropertyValue("schema:blobField");\r
+ File file = binaryManager.getFileForDigest(\r
+ blob.getBinary().getDigest(), false); \r
+*/\r
+\r
+/*\r
+RepositoryInstance.getStreamURI()\r
+\r
+String getStreamURI(String blobPropertyId)\r
+ throws ClientException\r
+\r
+ Returns an URI identifying the stream given the blob property id. This method should be used by a client to download the data of a blob property.\r
+\r
+ The blob is fetched from the repository and the blob stream is registered against the streaming service so the stream will be available remotely through stream service API.\r
+\r
+ After the client has called this method, it will be able to download the stream using streaming server API.\r
+\r
+ Returns:\r
+ an URI identifying the remote stream \r
+ Throws:\r
+ ClientException\r
+\r
+ */\r
+\r
+/*\r
+A blob contains usually large data.\r
+\r
+Document fields holding Blob data are by default fetched in a lazy manner.\r
+\r
+A Blob object hides the data source and it also describes data properties like the encoding or mime-type.\r
+\r
+The encoding is used to decode Unicode text content that was stored in an encoded form. If not encoding is specified, the default java encoding is used. The encoding is ignored for binary content.\r
+\r
+When retrieving the content from a document, it will be returned as source content instead of returning the content bytes.\r
+\r
+The same is true when setting the content for a document: you set a content source and not directly the content bytes. Ex:\r
+\r
+ File file = new File("/tmp/index.html");\r
+ FileBlob fb = new FileBlob(file);\r
+ fb.setMimeType("text/html");\r
+ fb.setEncoding("UTF-8"); // this specifies that content bytes will be stored as UTF-8\r
+ document.setProperty("file", "content", fb);\r
+ \r
+\r
+Then you may want to retrieve the content as follow:\r
+\r
+ Blob blob = document.getProperty("file:content");\r
+ htmlDoc = blob.getString(); // the content is decoded from UTF-8 into a java string\r
+ */\r