]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c4686b097a9eb9ccf75d6573d8751d58a78ef514
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.blob.nuxeo;
25
26 import org.collectionspace.services.blob.BlobsCommon;
27 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
28 import org.collectionspace.services.common.blob.BlobInput;
29 import org.collectionspace.services.common.blob.BlobOutput;
30 import org.collectionspace.services.common.blob.BlobUtil;
31 import org.collectionspace.services.common.context.ServiceContext;
32 import org.collectionspace.services.common.document.DocumentWrapper;
33 import org.collectionspace.services.common.imaging.nuxeo.NuxeoImageUtils;
34 import org.collectionspace.services.common.service.ListResultField;
35 import org.collectionspace.services.jaxb.AbstractCommonList;
36 import org.collectionspace.services.jaxb.BlobJAXBSchema;
37 import org.collectionspace.services.jaxb.AbstractCommonList;
38 import org.collectionspace.services.nuxeo.client.java.CommonList;
39 import org.nuxeo.ecm.core.api.ClientException;
40 import org.nuxeo.ecm.core.api.DocumentModel;
41 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 import java.util.ArrayList;
46 import java.util.List;
47
48 /**
49  * The Class BlobDocumentModelHandler.
50  */
51 public class BlobDocumentModelHandler
52 extends DocHandlerBase<BlobsCommon> {
53
54         /** The logger. */
55         private final Logger logger = LoggerFactory.getLogger(BlobDocumentModelHandler.class);
56
57     //==============================================================================
58
59         private String getDerivativePathBase(DocumentModel docModel) {
60                 return getServiceContextPath() + docModel.getName() + "/" +
61                         BlobInput.URI_DERIVATIVES_PATH + "/";
62         }
63
64         private BlobsCommon getCommonPartProperties(DocumentModel docModel) throws Exception {
65                 String label = getServiceContext().getCommonPartLabel();
66                 BlobsCommon result = new BlobsCommon();
67                 
68                 result.setData((String) 
69                                 docModel.getProperty(label, BlobJAXBSchema.data));
70                 result.setDigest((String)
71                                 docModel.getProperty(label, BlobJAXBSchema.digest));
72                 result.setEncoding((String)
73                                 docModel.getProperty(label, BlobJAXBSchema.encoding));
74                 result.setLength((String)
75                                 docModel.getProperty(label, BlobJAXBSchema.length));
76                 result.setMimeType((String)
77                                 docModel.getProperty(label, BlobJAXBSchema.mimeType));
78                 result.setName((String)
79                                 docModel.getProperty(label, BlobJAXBSchema.name));
80                 result.setRepositoryId((String)
81                                 docModel.getProperty(label, BlobJAXBSchema.repositoryId));
82                 result.setUri(getServiceContextPath() + docModel.getName() + "/" +
83                                 BlobInput.URI_CONTENT_PATH);
84                 
85                 return result;
86         }
87         
88         private void setCommonPartProperties(DocumentModel documentModel,
89                         BlobsCommon blobsCommon) throws ClientException {
90                 String label = getServiceContext().getCommonPartLabel();
91                 documentModel.setProperty(label, BlobJAXBSchema.data, blobsCommon.getData());
92                 documentModel.setProperty(label, BlobJAXBSchema.digest, blobsCommon.getDigest());
93                 documentModel.setProperty(label, BlobJAXBSchema.encoding, blobsCommon.getEncoding());
94                 documentModel.setProperty(label, BlobJAXBSchema.length, blobsCommon.getLength());
95                 documentModel.setProperty(label, BlobJAXBSchema.mimeType, blobsCommon.getMimeType());
96                 documentModel.setProperty(label, BlobJAXBSchema.name, blobsCommon.getName());
97                 documentModel.setProperty(label, BlobJAXBSchema.uri, blobsCommon.getUri());
98                 documentModel.setProperty(label, BlobJAXBSchema.repositoryId, blobsCommon.getRepositoryId());
99         }
100
101         /* (non-Javadoc)
102          * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractAllParts(org.collectionspace.services.common.document.DocumentWrapper)
103          */
104         @Override
105         public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
106                         throws Exception {
107                 ServiceContext ctx = this.getServiceContext();
108                 BlobInput blobInput = BlobUtil.getBlobInput(ctx);
109                 RepositoryInstance repoSession = this.getRepositorySession();
110                 DocumentModel docModel = wrapDoc.getWrappedObject();
111                 BlobsCommon blobsCommon = this.getCommonPartProperties(docModel);               
112                 String blobRepositoryId = blobsCommon.getRepositoryId(); //cache the value to pass to the blob retriever
113                 
114                 if (blobInput.isDerivativeListRequested() == true) {
115                 List<ListResultField> resultsFields = getListItemsArray();
116                         CommonList blobsCommonList = NuxeoImageUtils.getBlobDerivatives(
117                                         repoSession, blobRepositoryId, resultsFields, getDerivativePathBase(docModel));
118 //                      ctx.setProperty(BlobInput.BLOB_DERIVATIVE_LIST_KEY, blobsCommonList);
119                         blobInput.setDerivativeList(blobsCommonList);
120                         return;  //FIXME: Don't like this exit point.  Perhaps derivatives should be a sub-resource?
121                 }               
122
123                 String derivativeTerm = blobInput.getDerivativeTerm();
124                 Boolean getContentFlag = blobInput.isContentRequested();
125                 BlobOutput blobOutput = NuxeoImageUtils.getBlobOutput(ctx, repoSession,
126                                 blobRepositoryId, derivativeTerm, getContentFlag);
127                 if (getContentFlag == true) {
128                         blobInput.setContentStream(blobOutput.getBlobInputStream());
129 //                      ctx.setProperty(BlobInput.BLOB_CONTENT_KEY, blobOutput.getBlobInputStream());
130                 }
131
132                 if (derivativeTerm != null) {
133                         // reset 'blobsCommon' if we have a derivative request
134                         blobsCommon = blobOutput.getBlobsCommon();
135                         blobsCommon.setUri(getDerivativePathBase(docModel) +
136                                         derivativeTerm + "/" + BlobInput.URI_CONTENT_PATH);
137                 }
138                 
139                 blobsCommon.setRepositoryId(null); //hide the repository id from the GET results payload since it is private
140                 this.setCommonPartProperties(docModel, blobsCommon);
141                 // finish extracting the other parts by calling the parent
142                 super.extractAllParts(wrapDoc);
143         }
144
145         @Override
146         public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
147                 ServiceContext ctx = this.getServiceContext();
148                 BlobInput blobInput = BlobUtil.getBlobInput(ctx);
149                 if (blobInput.getBlobFile() != null) {                  
150                         //
151                         // If blobInput has a file then we just received a multipart/form-data file post or a URI query parameter
152                         //
153                         DocumentModel documentModel = wrapDoc.getWrappedObject();
154                         RepositoryInstance repoSession = this.getRepositorySession();           
155                         BlobsCommon blobsCommon = NuxeoImageUtils.createPicture(ctx, repoSession, blobInput);
156                         this.setCommonPartProperties(documentModel, blobsCommon);
157                         blobInput.setBlobCsid(documentModel.getName());
158                 } else {
159                         super.fillAllParts(wrapDoc, action);
160                 }
161         }    
162 }
163