]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
91c56e4772f2e59f5f197cd6fc5dcd3b076c6dc9
[tmp/jakarta-migration.git] /
1 /**     \r
2  * NuxeoImageUtils.java\r
3  *\r
4  * {Purpose of This Class}\r
5  *\r
6  * {Other Notes Relating to This Class (Optional)}\r
7  *\r
8  * $LastChangedBy: $\r
9  * $LastChangedRevision: $\r
10  * $LastChangedDate: $\r
11  *\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
15  *\r
16  * http://www.collectionspace.org\r
17  * http://wiki.collectionspace.org\r
18  *\r
19  * Copyright © 2009 {Contributing Institution}\r
20  *\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
23  *\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
26  */\r
27 package org.collectionspace.services.common.imaging.nuxeo;\r
28 \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
35 \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
41 \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
46 \r
47 //import org.nuxeo.common.utils.FileUtils;\r
48 \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
59 \r
60 import org.nuxeo.ecm.core.repository.RepositoryDescriptor;\r
61 import org.nuxeo.ecm.core.repository.RepositoryManager;\r
62 \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
75 \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
93 \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
97 \r
98 import org.slf4j.Logger;\r
99 import org.slf4j.LoggerFactory;\r
100 //import org.nuxeo.ecm.core.repository.jcr.testing.RepositoryOSGITestCase;\r
101 \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
111 \r
112 // TODO: Auto-generated Javadoc\r
113 /**\r
114  * The Class NuxeoImageUtils.\r
115  */\r
116 public class NuxeoImageUtils {\r
117         /** The Constant logger. */\r
118         private static final Logger logger = LoggerFactory.getLogger(NuxeoImageUtils.class);\r
119         \r
120         /*\r
121          * FIXME: REM - These constants should be coming from configuration and NOT hard coded.\r
122          */\r
123         public static final String DERIVATIVE_ORIGINAL = "Original";\r
124         public static final String DERIVATIVE_ORIGINAL_TAG = DERIVATIVE_ORIGINAL + "_";\r
125         \r
126         public static final String DERIVATIVE_ORIGINAL_JPEG = "OriginalJpeg";\r
127         public static final String DERIVATIVE_ORIGINAL_JPEG_TAG = DERIVATIVE_ORIGINAL_JPEG + "_";\r
128         \r
129         public static final String DERIVATIVE_MEDIUM = "Medium";\r
130         public static final String DERIVATIVE_MEDIUM_TAG = DERIVATIVE_MEDIUM + "_";\r
131         \r
132         public static final String DERIVATIVE_THUMBNAIL = "Thumbnail";\r
133         public static final String DERIVATIVE_THUMBNAIL_TAG = DERIVATIVE_THUMBNAIL + "_";\r
134         \r
135         public static final String DERIVATIVE_UNKNOWN = "_UNKNOWN_DERIVATIVE_NAME_";\r
136         \r
137 //      static DefaultBinaryManager binaryManager = new DefaultBinaryManager(); //can we get this from Nuxeo? i.e., Framework.getService(BinaryManger.class)\r
138 \r
139 //      /** The temp file name. */\r
140 //static String tempFileName = "sunset.jpg";\r
141 //      \r
142 //      /** The file separator. */\r
143 //      static String fileSeparator = System.getProperty("file.separator");\r
144 //      \r
145 //      /** The cur dir. */\r
146 //      static String curDir = System.getProperty("user.dir");\r
147         \r
148         /**\r
149          * Instantiates a new nuxeo image utils.\r
150          */\r
151         NuxeoImageUtils() {\r
152                 //empty constructor\r
153         }\r
154         \r
155         /**\r
156          * Logger setup.\r
157          */\r
158         public static void loggerSetup() {\r
159                 //empty method\r
160         }       \r
161         \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
165                 \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
174                 }\r
175                 \r
176                 return uri + result + "/" + BlobInput.URI_CONTENT_PATH;\r
177         }\r
178         \r
179         static private BlobListItem createBlobListItem(Blob blob, String uri) {\r
180                 BlobListItem result = new BlobListItem();\r
181 \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
187                 \r
188                 return result;\r
189         }\r
190         \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
195                 \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
205                 }\r
206                 \r
207                 return result;\r
208         }\r
209         \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
217                 }\r
218                 return result;\r
219         }\r
220         \r
221         static private File getBlobFile(RepositoryInstance ri, DocumentModel documentModel, Blob blob) {\r
222                 DefaultBinaryManager binaryManager = null;\r
223                 RepositoryDescriptor descriptor = null;\r
224                                 \r
225                 try {\r
226                         ServiceManager sm = (ServiceManager) Framework.getService(ServiceManager.class);\r
227                         ServiceDescriptor[] sd = sm.getServiceDescriptors();\r
228                         \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
239                 \r
240                 \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
245 \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
250                 \r
251             binaryManager = new DefaultBinaryManager();\r
252 \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
257                 \r
258 //              binaryManager = new DefaultBinaryManager();\r
259                 } catch (Exception e) {\r
260                         e.printStackTrace();\r
261                 }\r
262         \r
263         try {\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
272                 }\r
273 \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
278         \r
279         return file;\r
280         }\r
281         \r
282         /**\r
283          * Returns a schema, given the name of a schema.\r
284          *\r
285          * @param schemaName  a schema name.\r
286          * @return  a schema.\r
287          */\r
288         private static Schema getSchemaFromName(String schemaName) {\r
289                 SchemaManager schemaManager = null;\r
290                 try {\r
291                         schemaManager = Framework.getService(SchemaManager.class);\r
292                 } catch (Exception e) {\r
293                         // TODO Auto-generated catch block\r
294                         e.printStackTrace();\r
295                 }\r
296                 return schemaManager != null ? schemaManager.getSchema(schemaName) : null;\r
297         }\r
298                 \r
299         /**\r
300          * Gets the blob.\r
301          *\r
302          * @param nuxeoSession the nuxeo session\r
303          * @param id the id\r
304          * @return the blob\r
305          */\r
306         static private Blob getBlob(RepositoryInstance nuxeoSession, String id) {\r
307                 Blob result = null;\r
308                 \r
309                 try {\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
318                 }\r
319                 \r
320                 return result;\r
321         }\r
322         \r
323     /**\r
324      * Gets the type service.\r
325      *\r
326      * @return the type service\r
327      * @throws ClientException the client exception\r
328      */\r
329     private static TypeManager getTypeService() throws ClientException {\r
330         TypeManager typeService = null;\r
331         try {\r
332             typeService = Framework.getService(TypeManager.class);\r
333         } catch (Exception e) {\r
334             throw new ClientException(e);\r
335         }\r
336         return typeService;\r
337     }\r
338         \r
339         /**\r
340          * Gets the bytes.\r
341          *\r
342          * @param fis the fis\r
343          * @return the bytes\r
344          */\r
345         private static byte[] getBytes(InputStream fis) {\r
346         ByteArrayOutputStream bos = new ByteArrayOutputStream();\r
347         byte[] buf = new byte[128 * 1024];\r
348         try {\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
355             }\r
356         } catch (IOException ex) {\r
357             logger.error(ex.getMessage(), ex);\r
358         }\r
359         byte[] bytes = bos.toByteArray();\r
360         //bytes is the ByteArray we need\r
361         return bytes;\r
362         }\r
363         \r
364     /**\r
365      * Creates the serializable blob.\r
366      *\r
367      * @param fileInputStream the file input stream\r
368      * @param filename the filename\r
369      * @param mimeType the mime type\r
370      * @return the blob\r
371      */\r
372     private static Blob createSerializableBlob(InputStream fileInputStream,\r
373             String filename, String mimeType) {\r
374         Blob blob = null;\r
375         try {\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
381             // filename\r
382             if (filename != null) {\r
383                 filename = getCleanFileName(filename);\r
384             }\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
393                 } else {\r
394                     // default\r
395                     detectedMimeType = "application/octet-stream";\r
396                 }\r
397             }\r
398             blob.setMimeType(detectedMimeType);\r
399         } catch (MimetypeDetectionException e) {\r
400             logger.error(String.format("could not fetch mimetype for file %s",\r
401                     filename), e);\r
402         } catch (Exception e) {\r
403                 logger.error("", e);\r
404         }\r
405         return blob;\r
406     }\r
407         \r
408     /**\r
409      * Creates a serializable blob from a stream, with filename and mimetype\r
410      * detection.\r
411      * \r
412      * <p>\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
415      * disk.\r
416      * </p>\r
417      *\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
423      * @return the blob\r
424      */\r
425     private static Blob createStreamingBlob(InputStream file,\r
426             String filename, String mimeType) {\r
427         Blob blob = null;\r
428         try {\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
433             // filename\r
434             if (filename != null) {\r
435                 filename = getCleanFileName(filename);\r
436             }\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
445                 } else {\r
446                     // default\r
447                     detectedMimeType = "application/octet-stream";\r
448                 }\r
449             }\r
450             blob.setMimeType(detectedMimeType);\r
451         } catch (MimetypeDetectionException e) {\r
452             logger.error(String.format("could not fetch mimetype for file %s",\r
453                     filename), e);\r
454         } catch (IOException e) {\r
455                 logger.error("", e);\r
456         } catch (Exception e) {\r
457                 logger.error("", e);\r
458         }\r
459         return blob;\r
460     }\r
461         \r
462     /**\r
463      * Returns a clean filename, stripping upload path on client side.\r
464      * <p>\r
465      * Fixes NXP-544\r
466      * </p>\r
467      *\r
468      * @param filename the filename\r
469      * @return the clean file name\r
470      */\r
471     private static String getCleanFileName(String filename) {\r
472         String res = null;\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
478         } else {\r
479             res = filename;\r
480         }\r
481         return res;\r
482     }\r
483                 \r
484     /**\r
485      * Gets Nuxeo's file manager service.\r
486      *\r
487      * @return the file manager service\r
488      * @throws ClientException the client exception\r
489      */\r
490     private static FileManager getFileManagerService() throws ClientException {\r
491         FileManager result = null;\r
492         try {\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
498         }\r
499         return result;\r
500     }\r
501     \r
502     /**\r
503      * Creates the picture.\r
504      *\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
509      */\r
510     public static BlobsCommon createPicture(ServiceContext ctx,\r
511                 RepositoryInstance repoSession,\r
512                 BlobInput blobInput) {\r
513                 BlobsCommon result = null;\r
514                 \r
515                 try {\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
520             \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
525                 }            \r
526                 } catch (Exception e) {\r
527                         logger.error("Could not create image blob", e);\r
528                 }               \r
529                 \r
530                 return result;\r
531     }\r
532             \r
533         /**\r
534          * Creates the image blob.\r
535          *\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
542          */\r
543         static public BlobsCommon createImage(RepositoryInstance nuxeoSession,\r
544                         DocumentModel blobLocation,\r
545                         InputStream file,\r
546             String fileName, \r
547             String mimeType) {\r
548                 BlobsCommon result = null;\r
549                                 \r
550                 try {\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
557                         result = null;\r
558                         logger.error("Could not create new image blob", e);\r
559                 }\r
560                 \r
561                 return result;\r
562         }\r
563                 \r
564         /**\r
565          * Gets the image.\r
566          *\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
571          */\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
578 \r
579                 try {\r
580                         IdRef documentRef = new IdRef(repositoryId);\r
581                         DocumentModel documentModel = repoSession.getDocument(documentRef);\r
582 \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
589                         \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
595                         } else {\r
596                                 pictureBlob = pictureBlobHolder.getBlob();\r
597                         }\r
598                         //\r
599                         // Create the result instance\r
600                         //\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
607                         }\r
608                         \r
609                 } catch (Exception e) {\r
610                         logger.error(e.getMessage(), e);\r
611                 }\r
612                 \r
613                 return result;\r
614         }\r
615 }\r
616 \r
617 /*\r
618 stream = new FileInputStream(fileUploadHolder.getTempFile());\r
619 \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
625                     null);\r
626 \r
627             DocumentModel createdDoc = getFileManagerService().createDocumentFromBlob(\r
628                     documentManager, blob, path, true, fileName);\r
629             eventManager.raiseEventsOnDocumentSelected(createdDoc);\r
630 \r
631     protected FileManager fileManager;\r
632 \r
633     protected FileManager getFileManagerService() throws ClientException {\r
634         if (fileManager == null) {\r
635             try {\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
640                         e);\r
641             }\r
642         }\r
643         return fileManager;\r
644     }\r
645 */\r
646 \r
647 /*\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
654 \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
659 */\r
660 \r
661 /*\r
662 RepositoryInstance.getStreamURI()\r
663 \r
664 String getStreamURI(String blobPropertyId)\r
665                     throws ClientException\r
666 \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
668 \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
670 \r
671     After the client has called this method, it will be able to download the stream using streaming server API.\r
672 \r
673     Returns:\r
674         an URI identifying the remote stream \r
675     Throws:\r
676         ClientException\r
677 \r
678  */\r
679 \r
680 /*\r
681 A blob contains usually large data.\r
682 \r
683 Document fields holding Blob data are by default fetched in a lazy manner.\r
684 \r
685 A Blob object hides the data source and it also describes data properties like the encoding or mime-type.\r
686 \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
688 \r
689 When retrieving the content from a document, it will be returned as source content instead of returning the content bytes.\r
690 \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
692 \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
698  \r
699 \r
700 Then you may want to retrieve the content as follow:\r
701 \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
704   */\r