]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e79ae7dd299090f389dcd21ffaac421de04e0846
[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.blob.BlobsCommonList;
28 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
29 import org.collectionspace.services.common.blob.BlobInput;
30 import org.collectionspace.services.common.blob.BlobOutput;
31 import org.collectionspace.services.common.blob.BlobUtil;
32 import org.collectionspace.services.common.context.ServiceContext;
33 import org.collectionspace.services.common.document.DocumentWrapper;
34 import org.collectionspace.services.common.imaging.nuxeo.NuxeoImageUtils;
35 import org.collectionspace.services.jaxb.AbstractCommonList;
36 import org.collectionspace.services.jaxb.BlobJAXBSchema;
37 import org.nuxeo.ecm.core.api.ClientException;
38 import org.nuxeo.ecm.core.api.DocumentModel;
39 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import java.util.ArrayList;
44 import java.util.List;
45
46 /**
47  * The Class BlobDocumentModelHandler.
48  */
49 public class BlobDocumentModelHandler
50 extends DocHandlerBase<BlobsCommon, AbstractCommonList> {
51
52         /** The logger. */
53         private final Logger logger = LoggerFactory.getLogger(BlobDocumentModelHandler.class);
54
55     //==============================================================================
56
57         private String getDerivativePathBase(DocumentModel docModel) {
58                 return getServiceContextPath() + docModel.getName() + "/" +
59                         BlobInput.URI_DERIVATIVES_PATH + "/";
60         }
61
62         private BlobsCommon getCommonPartProperties(DocumentModel docModel) throws Exception {
63                 String label = getServiceContext().getCommonPartLabel();
64                 BlobsCommon result = new BlobsCommon();
65                 
66                 result.setData((String) 
67                                 docModel.getProperty(label, BlobJAXBSchema.data));
68                 result.setDigest((String)
69                                 docModel.getProperty(label, BlobJAXBSchema.digest));
70                 result.setEncoding((String)
71                                 docModel.getProperty(label, BlobJAXBSchema.encoding));
72                 result.setLength((String)
73                                 docModel.getProperty(label, BlobJAXBSchema.length));
74                 result.setMimeType((String)
75                                 docModel.getProperty(label, BlobJAXBSchema.mimeType));
76                 result.setName((String)
77                                 docModel.getProperty(label, BlobJAXBSchema.name));
78                 result.setRepositoryId((String)
79                                 docModel.getProperty(label, BlobJAXBSchema.repositoryId));
80                 result.setUri(getServiceContextPath() + docModel.getName() + "/" +
81                                 BlobInput.URI_CONTENT_PATH);
82                 
83                 return result;
84         }
85         
86         private void setCommonPartProperties(DocumentModel documentModel,
87                         BlobsCommon blobsCommon) throws ClientException {
88                 String label = getServiceContext().getCommonPartLabel();
89                 documentModel.setProperty(label, BlobJAXBSchema.data, blobsCommon.getData());
90                 documentModel.setProperty(label, BlobJAXBSchema.digest, blobsCommon.getDigest());
91                 documentModel.setProperty(label, BlobJAXBSchema.encoding, blobsCommon.getEncoding());
92                 documentModel.setProperty(label, BlobJAXBSchema.length, blobsCommon.getLength());
93                 documentModel.setProperty(label, BlobJAXBSchema.mimeType, blobsCommon.getMimeType());
94                 documentModel.setProperty(label, BlobJAXBSchema.name, blobsCommon.getName());
95                 documentModel.setProperty(label, BlobJAXBSchema.uri, blobsCommon.getUri());
96                 documentModel.setProperty(label, BlobJAXBSchema.repositoryId, blobsCommon.getRepositoryId());
97         }
98
99         /* (non-Javadoc)
100          * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractAllParts(org.collectionspace.services.common.document.DocumentWrapper)
101          */
102         @Override
103         public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
104                         throws Exception {
105                 ServiceContext ctx = this.getServiceContext();
106                 BlobInput blobInput = BlobUtil.getBlobInput(ctx);
107                 RepositoryInstance repoSession = this.getRepositorySession();
108                 DocumentModel docModel = wrapDoc.getWrappedObject();
109                 BlobsCommon blobsCommon = this.getCommonPartProperties(docModel);               
110                 String blobRepositoryId = blobsCommon.getRepositoryId(); //cache the value to pass to the blob retriever
111                 
112                 if (blobInput.isDerivativeListRequested() == true) {
113                         BlobsCommonList blobsCommonList = NuxeoImageUtils.getBlobDerivatives(
114                                         repoSession, blobRepositoryId, getDerivativePathBase(docModel));
115 //                      ctx.setProperty(BlobInput.BLOB_DERIVATIVE_LIST_KEY, blobsCommonList);
116                         blobInput.setDerivativeList(blobsCommonList);
117                         return;  //FIXME: Don't like this exit point.  Perhaps derivatives should be a sub-resource?
118                 }               
119
120                 String derivativeTerm = blobInput.getDerivativeTerm();
121                 Boolean getContentFlag = blobInput.isContentRequested();
122                 BlobOutput blobOutput = NuxeoImageUtils.getBlobOutput(ctx, repoSession,
123                                 blobRepositoryId, derivativeTerm, getContentFlag);
124                 if (getContentFlag == true) {
125                         blobInput.setContentStream(blobOutput.getBlobInputStream());
126 //                      ctx.setProperty(BlobInput.BLOB_CONTENT_KEY, blobOutput.getBlobInputStream());
127                 }
128
129                 if (derivativeTerm != null) {
130                         // reset 'blobsCommon' if we have a derivative request
131                         blobsCommon = blobOutput.getBlobsCommon();
132                         blobsCommon.setUri(getDerivativePathBase(docModel) +
133                                         derivativeTerm + "/" + BlobInput.URI_CONTENT_PATH);
134                 }
135                 
136                 blobsCommon.setRepositoryId(null); //hide the repository id from the GET results payload since it is private
137                 this.setCommonPartProperties(docModel, blobsCommon);
138                 // finish extracting the other parts by calling the parent
139                 super.extractAllParts(wrapDoc);
140         }
141
142         @Override
143         public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
144                 ServiceContext ctx = this.getServiceContext();
145                 BlobInput blobInput = BlobUtil.getBlobInput(ctx);
146                 if (blobInput.getBlobFile() != null) {                  
147                         //
148                         // If blobInput has a file then we just received a multipart/form-data file post
149                         //
150                         DocumentModel documentModel = wrapDoc.getWrappedObject();
151                         RepositoryInstance repoSession = this.getRepositorySession();           
152                         BlobsCommon blobsCommon = NuxeoImageUtils.createPicture(ctx, repoSession, blobInput);
153                         this.setCommonPartProperties(documentModel, blobsCommon);
154                         blobInput.setBlobCsid(documentModel.getName());
155                 } else {
156                         super.fillAllParts(wrapDoc, action);
157                 }
158         }    
159 }
160