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