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