2 * NuxeoImageUtils.java
\r
4 * {Purpose of This Class}
\r
6 * {Other Notes Relating to This Class (Optional)}
\r
9 * $LastChangedRevision: $
\r
10 * $LastChangedDate: $
\r
12 * This document is a part of the source code and related artifacts
\r
13 * for CollectionSpace, an open source collections management system
\r
14 * for museums and related institutions:
\r
16 * http://www.collectionspace.org
\r
17 * http://wiki.collectionspace.org
\r
19 * Copyright © 2009 {Contributing Institution}
\r
21 * Licensed under the Educational Community License (ECL), Version 2.0.
\r
22 * You may not use this file except in compliance with this License.
\r
24 * You may obtain a copy of the ECL 2.0 License at
\r
25 * https://source.collectionspace.org/collection-space/LICENSE.txt
\r
27 package org.collectionspace.services.common.imaging.nuxeo;
\r
29 import java.io.File;
\r
30 import java.io.ByteArrayOutputStream;
\r
31 import java.io.InputStream;
\r
32 import java.io.FileInputStream;
\r
33 import java.io.BufferedInputStream;
\r
34 import java.io.IOException;
\r
36 import java.io.Serializable;
\r
37 import java.util.ArrayList;
\r
38 import java.util.HashMap;
\r
39 import java.util.List;
\r
40 import java.util.Map;
\r
42 import org.nuxeo.runtime.api.Framework;
\r
43 import org.nuxeo.runtime.api.ServiceManager;
\r
44 import org.nuxeo.runtime.api.ServiceDescriptor;
\r
45 import org.nuxeo.runtime.services.streaming.RemoteInputStream;
\r
47 //import org.nuxeo.common.utils.FileUtils;
\r
49 import org.nuxeo.ecm.platform.picture.api.adapters.PictureResourceAdapter;
\r
50 import org.nuxeo.ecm.platform.mimetype.MimetypeDetectionException;
\r
51 import org.nuxeo.ecm.platform.mimetype.interfaces.MimetypeRegistry;
\r
52 import org.nuxeo.ecm.platform.picture.api.adapters.PictureBlobHolder;
\r
53 import org.nuxeo.ecm.platform.picture.extension.ImagePlugin;
\r
54 import org.nuxeo.ecm.platform.filemanager.api.FileManager;
\r
55 import org.nuxeo.ecm.platform.filemanager.service.FileManagerService;
\r
56 import org.nuxeo.ecm.platform.types.TypeManager;
\r
57 import org.nuxeo.ecm.platform.picture.api.adapters.PictureBlobHolderFactory;
\r
58 import org.nuxeo.ecm.platform.picture.api.adapters.PictureBlobHolder;
\r
60 import org.nuxeo.ecm.core.repository.RepositoryDescriptor;
\r
61 import org.nuxeo.ecm.core.repository.RepositoryManager;
\r
63 import org.nuxeo.ecm.core.repository.RepositoryService;
\r
64 import org.nuxeo.runtime.model.ComponentManager;
\r
65 import org.nuxeo.runtime.model.ComponentInstance;
\r
66 import org.nuxeo.runtime.model.impl.ComponentManagerImpl;
\r
67 //import org.nuxeo.ecm.core.api.ejb.DocumentManagerBean;
\r
68 //import org.nuxeo.ecm.core.storage.sql.RepositoryImpl;
\r
69 //import org.nuxeo.ecm.core.storage.sql.Repository;
\r
70 import org.nuxeo.ecm.core.storage.sql.BinaryManager;
\r
71 import org.nuxeo.ecm.core.storage.sql.DefaultBinaryManager;
\r
72 import org.nuxeo.ecm.core.storage.sql.coremodel.SQLRepository;
\r
73 import org.nuxeo.ecm.core.storage.sql.coremodel.SQLBlob;
\r
74 //import org.nuxeo.ecm.core.storage.sql.RepositoryDescriptor;
\r
76 //import org.nuxeo.ecm.core.api.DocumentResolver;
\r
77 import org.nuxeo.ecm.core.api.IdRef;
\r
78 import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
\r
79 import org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder;
\r
80 import org.nuxeo.ecm.core.api.impl.blob.FileBlob;
\r
81 import org.nuxeo.ecm.core.api.impl.blob.StreamingBlob;
\r
82 import org.nuxeo.ecm.core.api.impl.blob.ByteArrayBlob;
\r
83 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
\r
84 import org.nuxeo.ecm.core.api.repository.Repository;
\r
85 import org.nuxeo.ecm.core.api.Blob;
\r
86 import org.nuxeo.ecm.core.api.ClientException;
\r
87 import org.nuxeo.ecm.core.api.DocumentModel;
\r
88 import org.nuxeo.ecm.core.api.DocumentRef;
\r
89 import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
\r
90 import org.nuxeo.ecm.core.api.blobholder.BlobHolderAdapterService;
\r
91 import org.nuxeo.ecm.core.api.impl.DocumentModelImpl;
\r
92 import org.nuxeo.ecm.core.api.impl.blob.FileBlob;
\r
94 import org.nuxeo.ecm.core.model.Document;
\r
95 import org.nuxeo.ecm.core.schema.SchemaManager;
\r
96 import org.nuxeo.ecm.core.schema.types.Schema;
\r
98 import org.slf4j.Logger;
\r
99 import org.slf4j.LoggerFactory;
\r
100 //import org.nuxeo.ecm.core.repository.jcr.testing.RepositoryOSGITestCase;
\r
102 import org.collectionspace.services.common.ServiceMain;
\r
103 import org.collectionspace.services.common.blob.BlobInput;
\r
104 import org.collectionspace.services.common.context.ServiceContext;
\r
105 import org.collectionspace.services.common.document.DocumentUtils;
\r
106 import org.collectionspace.services.common.FileUtils;
\r
107 import org.collectionspace.services.blob.BlobsCommon;
\r
108 import org.collectionspace.services.blob.BlobsCommonList;
\r
109 import org.collectionspace.services.blob.BlobsCommonList.BlobListItem;
\r
110 import org.collectionspace.services.common.blob.BlobOutput;
\r
112 // TODO: Auto-generated Javadoc
\r
114 * The Class NuxeoImageUtils.
\r
116 public class NuxeoImageUtils {
\r
117 /** The Constant logger. */
\r
118 private static final Logger logger = LoggerFactory.getLogger(NuxeoImageUtils.class);
\r
121 * FIXME: REM - These constants should be coming from configuration and NOT hard coded.
\r
123 public static final String DERIVATIVE_ORIGINAL = "Original";
\r
124 public static final String DERIVATIVE_ORIGINAL_TAG = DERIVATIVE_ORIGINAL + "_";
\r
126 public static final String DERIVATIVE_ORIGINAL_JPEG = "OriginalJpeg";
\r
127 public static final String DERIVATIVE_ORIGINAL_JPEG_TAG = DERIVATIVE_ORIGINAL_JPEG + "_";
\r
129 public static final String DERIVATIVE_MEDIUM = "Medium";
\r
130 public static final String DERIVATIVE_MEDIUM_TAG = DERIVATIVE_MEDIUM + "_";
\r
132 public static final String DERIVATIVE_THUMBNAIL = "Thumbnail";
\r
133 public static final String DERIVATIVE_THUMBNAIL_TAG = DERIVATIVE_THUMBNAIL + "_";
\r
135 public static final String DERIVATIVE_UNKNOWN = "_UNKNOWN_DERIVATIVE_NAME_";
\r
137 // static DefaultBinaryManager binaryManager = new DefaultBinaryManager(); //can we get this from Nuxeo? i.e., Framework.getService(BinaryManger.class)
\r
139 // /** The temp file name. */
\r
140 //static String tempFileName = "sunset.jpg";
\r
142 // /** The file separator. */
\r
143 // static String fileSeparator = System.getProperty("file.separator");
\r
145 // /** The cur dir. */
\r
146 // static String curDir = System.getProperty("user.dir");
\r
149 * Instantiates a new nuxeo image utils.
\r
151 NuxeoImageUtils() {
\r
152 //empty constructor
\r
158 public static void loggerSetup() {
\r
162 //FIXME: This needs to be configuration-bases and NOT hard coded!
\r
163 static private String getDerivativeUri(String uri, String derivativeName) {
\r
164 String result = DERIVATIVE_UNKNOWN;
\r
166 if (derivativeName.startsWith(DERIVATIVE_ORIGINAL_TAG) == true) {
\r
167 result = DERIVATIVE_ORIGINAL;
\r
168 } else if (derivativeName.startsWith(DERIVATIVE_ORIGINAL_JPEG_TAG) == true) {
\r
169 result = DERIVATIVE_ORIGINAL_JPEG;
\r
170 } else if (derivativeName.startsWith(DERIVATIVE_MEDIUM_TAG) == true) {
\r
171 result = DERIVATIVE_MEDIUM;
\r
172 } else if (derivativeName.startsWith(DERIVATIVE_THUMBNAIL_TAG) == true) {
\r
173 result = DERIVATIVE_THUMBNAIL;
\r
176 return uri + result + "/" + BlobInput.URI_CONTENT_PATH;
\r
179 static private BlobListItem createBlobListItem(Blob blob, String uri) {
\r
180 BlobListItem result = new BlobListItem();
\r
182 result.setEncoding(blob.getEncoding());
\r
183 result.setLength(Long.toString(blob.getLength()));
\r
184 result.setMimeType(blob.getMimeType());
\r
185 result.setName(blob.getFilename());
\r
186 result.setUri(getDerivativeUri(uri, blob.getFilename()));
\r
191 static public BlobsCommonList getBlobDerivatives(RepositoryInstance repoSession,
\r
192 String repositoryId,
\r
193 String uri) throws Exception {
\r
194 BlobsCommonList result = new BlobsCommonList();
\r
196 IdRef documentRef = new IdRef(repositoryId);
\r
197 DocumentModel documentModel = repoSession.getDocument(documentRef);
\r
198 DocumentBlobHolder docBlobHolder = (DocumentBlobHolder)documentModel.getAdapter(BlobHolder.class);
\r
199 List<Blob> docBlobs = docBlobHolder.getBlobs();
\r
200 List<BlobListItem> blobListItems = result.getBlobListItem();
\r
201 BlobListItem blobListItem = null;
\r
202 for (Blob blob : docBlobs) {
\r
203 blobListItem = createBlobListItem(blob, uri);
\r
204 blobListItems.add(blobListItem);
\r
210 static private BlobsCommon createBlobsCommon(DocumentModel documentModel, Blob nuxeoBlob) {
\r
211 BlobsCommon result = new BlobsCommon();
\r
212 if (documentModel != null) {
\r
213 result.setMimeType(nuxeoBlob.getMimeType());
\r
214 result.setName(nuxeoBlob.getFilename());
\r
215 result.setLength(Long.toString(nuxeoBlob.getLength()));
\r
216 result.setRepositoryId(documentModel.getId());
\r
221 static private File getBlobFile(RepositoryInstance ri, DocumentModel documentModel, Blob blob) {
\r
222 DefaultBinaryManager binaryManager = null;
\r
223 RepositoryDescriptor descriptor = null;
\r
226 ServiceManager sm = (ServiceManager) Framework.getService(ServiceManager.class);
\r
227 ServiceDescriptor[] sd = sm.getServiceDescriptors();
\r
229 RepositoryService repositoryService1 = (RepositoryService) Framework.getRuntime().getComponent(
\r
230 RepositoryService.NAME);
\r
231 RepositoryService repositoryService2 = (RepositoryService) Framework.getRuntime().getService(
\r
232 RepositoryService.class);
\r
233 RepositoryService repositoryService3 = (RepositoryService) Framework.getService(
\r
234 RepositoryService.class);
\r
235 RepositoryService repositoryService4 = (RepositoryService) Framework.getLocalService(
\r
236 RepositoryService.class);
\r
237 ComponentManager componentManager1 = (ComponentManager) Framework.getService(ComponentManager.class);
\r
238 ComponentManager componentManager2 = (ComponentManager) Framework.getService(ComponentManagerImpl.class);
\r
241 // RepositoryManager repositoryManager2 = (RepositoryManager) Framework.getService(RepositoryManager.class);
\r
242 // Repository repository = repositoryManager2.getDefaultRepository();
\r
243 // Map<String, String> repositoryMap = repository.getProperties();
\r
244 // String streamURI = ri.getStreamURI(arg0)
\r
246 String repositoryName = documentModel.getRepositoryName();
\r
247 // RepositoryManager repositoryManager2 = (RepositoryManager) Framework.getService(RepositoryManager.class);
\r
248 RepositoryManager repositoryManager = repositoryService1.getRepositoryManager();
\r
249 descriptor = repositoryManager.getDescriptor(repositoryName);
\r
251 binaryManager = new DefaultBinaryManager();
\r
253 File storageDir = binaryManager.getStorageDir();
\r
254 // SQLBlob blob = (SQLBlob) doc.getPropertyValue("schema:blobField");
\r
255 File file = binaryManager.getFileForDigest(
\r
256 blob.getDigest(), false);
\r
258 // binaryManager = new DefaultBinaryManager();
\r
259 } catch (Exception e) {
\r
260 e.printStackTrace();
\r
264 binaryManager.initialize(
\r
265 SQLRepository.getDescriptor(descriptor));
\r
266 } catch (IOException e) {
\r
267 // TODO Auto-generated catch block
\r
268 e.printStackTrace();
\r
269 } catch (Exception e) {
\r
270 // TODO Auto-generated catch block
\r
271 e.printStackTrace();
\r
274 File storageDir = binaryManager.getStorageDir();
\r
275 // SQLBlob blob = (SQLBlob) documentModel.getPropertyValue("schema:blobField");
\r
276 File file = binaryManager.getFileForDigest(
\r
277 blob.getDigest(), false);
\r
283 * Returns a schema, given the name of a schema.
\r
285 * @param schemaName a schema name.
\r
286 * @return a schema.
\r
288 private static Schema getSchemaFromName(String schemaName) {
\r
289 SchemaManager schemaManager = null;
\r
291 schemaManager = Framework.getService(SchemaManager.class);
\r
292 } catch (Exception e) {
\r
293 // TODO Auto-generated catch block
\r
294 e.printStackTrace();
\r
296 return schemaManager != null ? schemaManager.getSchema(schemaName) : null;
\r
302 * @param nuxeoSession the nuxeo session
\r
306 static private Blob getBlob(RepositoryInstance nuxeoSession, String id) {
\r
307 Blob result = null;
\r
310 Repository repository = nuxeoSession.getRepository();
\r
311 // binaryManager.initialize(new RepositoryDescriptor());
\r
312 // binaryManager.getBinary("a4cac052ae0281979f2dcf5ab2e61a6c");
\r
313 // DocumentResolver.resolveReference(nuxeoSession, documentRef);
\r
314 //binaryManager = repository.getBinaryManager();
\r
315 // documentModel.getr
\r
316 } catch (Exception x) {
\r
317 x.printStackTrace();
\r
324 * Gets the type service.
\r
326 * @return the type service
\r
327 * @throws ClientException the client exception
\r
329 private static TypeManager getTypeService() throws ClientException {
\r
330 TypeManager typeService = null;
\r
332 typeService = Framework.getService(TypeManager.class);
\r
333 } catch (Exception e) {
\r
334 throw new ClientException(e);
\r
336 return typeService;
\r
342 * @param fis the fis
\r
343 * @return the bytes
\r
345 private static byte[] getBytes(InputStream fis) {
\r
346 ByteArrayOutputStream bos = new ByteArrayOutputStream();
\r
347 byte[] buf = new byte[128 * 1024];
\r
349 for (int readNum; (readNum = fis.read(buf)) != -1;) {
\r
350 bos.write(buf, 0, readNum);
\r
351 //no doubt here is 0
\r
352 /*Writes len bytes from the specified byte array starting at offset
\r
353 off to this byte array output stream.*/
\r
354 System.out.println("read " + readNum + " bytes,");
\r
356 } catch (IOException ex) {
\r
357 logger.error(ex.getMessage(), ex);
\r
359 byte[] bytes = bos.toByteArray();
\r
360 //bytes is the ByteArray we need
\r
365 * Creates the serializable blob.
\r
367 * @param fileInputStream the file input stream
\r
368 * @param filename the filename
\r
369 * @param mimeType the mime type
\r
372 private static Blob createSerializableBlob(InputStream fileInputStream,
\r
373 String filename, String mimeType) {
\r
376 // persisting the blob makes it possible to read the binary content
\r
377 // of the request stream several times (mimetype sniffing, digest
\r
378 // computation, core binary storage)
\r
379 byte[] bytes = getBytes(fileInputStream);
\r
380 blob = new ByteArrayBlob(bytes);
\r
382 if (filename != null) {
\r
383 filename = getCleanFileName(filename);
\r
385 blob.setFilename(filename);
\r
386 // mimetype detection
\r
387 MimetypeRegistry mimeService = Framework.getService(MimetypeRegistry.class);
\r
388 String detectedMimeType = mimeService.getMimetypeFromFilenameAndBlobWithDefault(
\r
389 filename, blob, null);
\r
390 if (detectedMimeType == null) {
\r
391 if (mimeType != null) {
\r
392 detectedMimeType = mimeType;
\r
395 detectedMimeType = "application/octet-stream";
\r
398 blob.setMimeType(detectedMimeType);
\r
399 } catch (MimetypeDetectionException e) {
\r
400 logger.error(String.format("could not fetch mimetype for file %s",
\r
402 } catch (Exception e) {
\r
403 logger.error("", e);
\r
409 * Creates a serializable blob from a stream, with filename and mimetype
\r
413 * Creates an in-memory blob if data is under 64K, otherwise constructs a
\r
414 * serializable FileBlob which stores data in a temporary file on the hard
\r
418 * @param file the input stream holding data
\r
419 * @param filename the file name. Will be set on the blob and will used for
\r
420 * mimetype detection.
\r
421 * @param mimeType the detected mimetype at upload. Can be null. Will be
\r
422 * verified by the mimetype service.
\r
425 private static Blob createStreamingBlob(InputStream file,
\r
426 String filename, String mimeType) {
\r
429 // persisting the blob makes it possible to read the binary content
\r
430 // of the request stream several times (mimetype sniffing, digest
\r
431 // computation, core binary storage)
\r
432 blob = StreamingBlob.createFromStream(file, mimeType).persist();
\r
434 if (filename != null) {
\r
435 filename = getCleanFileName(filename);
\r
437 blob.setFilename(filename);
\r
438 // mimetype detection
\r
439 MimetypeRegistry mimeService = Framework.getService(MimetypeRegistry.class);
\r
440 String detectedMimeType = mimeService.getMimetypeFromFilenameAndBlobWithDefault(
\r
441 filename, blob, null);
\r
442 if (detectedMimeType == null) {
\r
443 if (mimeType != null) {
\r
444 detectedMimeType = mimeType;
\r
447 detectedMimeType = "application/octet-stream";
\r
450 blob.setMimeType(detectedMimeType);
\r
451 } catch (MimetypeDetectionException e) {
\r
452 logger.error(String.format("could not fetch mimetype for file %s",
\r
454 } catch (IOException e) {
\r
455 logger.error("", e);
\r
456 } catch (Exception e) {
\r
457 logger.error("", e);
\r
463 * Returns a clean filename, stripping upload path on client side.
\r
468 * @param filename the filename
\r
469 * @return the clean file name
\r
471 private static String getCleanFileName(String filename) {
\r
473 int lastWinSeparator = filename.lastIndexOf('\\');
\r
474 int lastUnixSeparator = filename.lastIndexOf('/');
\r
475 int lastSeparator = Math.max(lastWinSeparator, lastUnixSeparator);
\r
476 if (lastSeparator != -1) {
\r
477 res = filename.substring(lastSeparator + 1, filename.length());
\r
485 * Gets Nuxeo's file manager service.
\r
487 * @return the file manager service
\r
488 * @throws ClientException the client exception
\r
490 private static FileManager getFileManagerService() throws ClientException {
\r
491 FileManager result = null;
\r
493 result = Framework.getService(FileManager.class);
\r
494 } catch (Exception e) {
\r
495 String msg = "Unable to get Nuxeo's FileManager service.";
\r
496 logger.error(msg, e);
\r
497 throw new ClientException("msg", e);
\r
503 * Creates the picture.
\r
505 * @param ctx the ctx
\r
506 * @param repoSession the repo session
\r
507 * @param filePath the file path
\r
508 * @return the string
\r
510 public static BlobsCommon createPicture(ServiceContext ctx,
\r
511 RepositoryInstance repoSession,
\r
512 BlobInput blobInput) {
\r
513 BlobsCommon result = null;
\r
516 File blobFile = blobInput.getBlobFile();
\r
517 String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
\r
518 DocumentRef nuxeoWspace = new IdRef(nuxeoWspaceId);
\r
519 DocumentModel wspaceDoc = repoSession.getDocument(nuxeoWspace);
\r
521 FileInputStream inputStream = new FileInputStream(blobFile);
\r
522 if (inputStream != null) {
\r
523 result = createImage(repoSession, wspaceDoc,
\r
524 inputStream, blobFile.getName(), null);
\r
526 } catch (Exception e) {
\r
527 logger.error("Could not create image blob", e);
\r
534 * Creates the image blob.
\r
536 * @param nuxeoSession the nuxeo session
\r
537 * @param blobLocation the blob location
\r
538 * @param file the file
\r
539 * @param fileName the file name
\r
540 * @param mimeType the mime type
\r
541 * @return the string
\r
543 static public BlobsCommon createImage(RepositoryInstance nuxeoSession,
\r
544 DocumentModel blobLocation,
\r
548 BlobsCommon result = null;
\r
551 Blob fileBlob = createStreamingBlob(file, fileName, mimeType);
\r
552 String digestAlgorithm = getFileManagerService().getDigestAlgorithm(); //Need some way on initializing the FileManager with a call.
\r
553 DocumentModel documentModel = getFileManagerService().createDocumentFromBlob(nuxeoSession,
\r
554 fileBlob, blobLocation.getPathAsString(), true, fileName);
\r
555 result = createBlobsCommon(documentModel, fileBlob);
\r
556 } catch (Exception e) {
\r
558 logger.error("Could not create new image blob", e);
\r
567 * @param repoSession the repo session
\r
568 * @param repositoryId the repository id
\r
569 * @param derivativeTerm the derivative term
\r
570 * @return the image
\r
572 static public BlobOutput getBlobOutput(ServiceContext ctx,
\r
573 RepositoryInstance repoSession,
\r
574 String repositoryId,
\r
575 String derivativeTerm,
\r
576 Boolean getContentFlag) {
\r
577 BlobOutput result = new BlobOutput();
\r
580 IdRef documentRef = new IdRef(repositoryId);
\r
581 DocumentModel documentModel = repoSession.getDocument(documentRef);
\r
583 // documentModel.getAdapter(PictureResourceAdapter.class);
\r
584 // DocumentBlobHolder docBlobHolder = (DocumentBlobHolder)documentModel.getAdapter(BlobHolder.class);
\r
585 // Blob docBlob = docBlobHolder.getBlob();
\r
586 // Map<String,Serializable> blobHolderProps = docBlobHolder.getProperties();
\r
587 // String filePath = docBlobHolder.getFilePath();
\r
588 // List<Blob> docBlobs = docBlobHolder.getBlobs();
\r
590 PictureBlobHolderFactory blobHolderFactory = new PictureBlobHolderFactory();
\r
591 PictureBlobHolder pictureBlobHolder = (PictureBlobHolder) blobHolderFactory.getBlobHolder(documentModel);
\r
592 Blob pictureBlob = null;
\r
593 if (derivativeTerm != null) {
\r
594 pictureBlob = pictureBlobHolder.getBlob(derivativeTerm);
\r
596 pictureBlob = pictureBlobHolder.getBlob();
\r
599 // Create the result instance
\r
601 BlobsCommon blobsCommon = createBlobsCommon(documentModel, pictureBlob);
\r
602 result.setBlobsCommon(blobsCommon); // the blob metadata
\r
603 if (getContentFlag == true) {
\r
604 InputStream remoteStream = pictureBlob.getStream();
\r
605 BufferedInputStream bufferedInputStream = new BufferedInputStream(remoteStream);
\r
606 result.setBlobInputStream(bufferedInputStream); // the blob stream
\r
609 } catch (Exception e) {
\r
610 logger.error(e.getMessage(), e);
\r
618 stream = new FileInputStream(fileUploadHolder.getTempFile());
\r
620 public String addFile(InputStream fileUpload, String fileName)
\r
621 fileName = FileUtils.getCleanFileName(fileName);
\r
622 DocumentModel currentDocument = navigationContext.getCurrentDocument();
\r
623 String path = currentDocument.getPathAsString();
\r
624 Blob blob = FileUtils.createSerializableBlob(fileUpload, fileName,
\r
627 DocumentModel createdDoc = getFileManagerService().createDocumentFromBlob(
\r
628 documentManager, blob, path, true, fileName);
\r
629 eventManager.raiseEventsOnDocumentSelected(createdDoc);
\r
631 protected FileManager fileManager;
\r
633 protected FileManager getFileManagerService() throws ClientException {
\r
634 if (fileManager == null) {
\r
636 fileManager = Framework.getService(FileManager.class);
\r
637 } catch (Exception e) {
\r
638 log.error("Unable to get FileManager service ", e);
\r
639 throw new ClientException("Unable to get FileManager service ",
\r
643 return fileManager;
\r
648 RepositoryService repositoryService = (RepositoryService) Framework.getRuntime().getComponent(
\r
649 RepositoryService.NAME);
\r
650 RepositoryManager repositoryManager = repositoryService.getRepositoryManager();
\r
651 RepositoryDescriptor descriptor = repositoryManager.getDescriptor(repositoryName);
\r
652 DefaultBinaryManager binaryManager = new DefaultBinaryManager(
\r
653 SQLRepository.getDescriptor(descriptor)));
\r
655 File storageDir = binaryManager.getStorageDir();
\r
656 SQLBlob blob = (SQLBlob) doc.getPropertyValue("schema:blobField");
\r
657 File file = binaryManager.getFileForDigest(
\r
658 blob.getBinary().getDigest(), false);
\r
662 RepositoryInstance.getStreamURI()
\r
664 String getStreamURI(String blobPropertyId)
\r
665 throws ClientException
\r
667 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
669 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
671 After the client has called this method, it will be able to download the stream using streaming server API.
\r
674 an URI identifying the remote stream
\r
681 A blob contains usually large data.
\r
683 Document fields holding Blob data are by default fetched in a lazy manner.
\r
685 A Blob object hides the data source and it also describes data properties like the encoding or mime-type.
\r
687 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
689 When retrieving the content from a document, it will be returned as source content instead of returning the content bytes.
\r
691 The same is true when setting the content for a document: you set a content source and not directly the content bytes. Ex:
\r
693 File file = new File("/tmp/index.html");
\r
694 FileBlob fb = new FileBlob(file);
\r
695 fb.setMimeType("text/html");
\r
696 fb.setEncoding("UTF-8"); // this specifies that content bytes will be stored as UTF-8
\r
697 document.setProperty("file", "content", fb);
\r
700 Then you may want to retrieve the content as follow:
\r
702 Blob blob = document.getProperty("file:content");
\r
703 htmlDoc = blob.getString(); // the content is decoded from UTF-8 into a java string
\r