]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f3b250a66fa66537a5c98f6d88c8bc421044a2da
[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.context.ServiceContext;
35 import org.collectionspace.services.common.document.DocumentWrapper;
36 import org.collectionspace.services.common.document.DocumentHandler.Action;
37 import org.collectionspace.services.common.imaging.nuxeo.NuxeoImageUtils;
38 import org.collectionspace.services.common.DocHandlerBase;
39
40 import org.collectionspace.services.blob.BlobsCommonList;
41 import org.collectionspace.services.blob.BlobsCommon;
42 import org.collectionspace.services.blob.BlobsCommonList.BlobListItem;
43
44 import org.collectionspace.services.nuxeo.client.java.DocumentModelHandler;
45 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
46 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
47
48 import org.nuxeo.ecm.core.api.DocumentModel;
49 import org.nuxeo.ecm.core.api.DocumentModelList;
50 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
51 import org.nuxeo.ecm.core.api.ClientException;
52
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 /**
57  * The Class BlobDocumentModelHandler.
58  */
59 public class BlobDocumentModelHandler
60 extends DocHandlerBase<BlobsCommon, AbstractCommonList> {
61
62         /** The logger. */
63         private final Logger logger = LoggerFactory.getLogger(BlobDocumentModelHandler.class);
64
65         public final String getNuxeoSchemaName(){
66                 return "blobs";
67         }
68
69         public String getSummaryFields(AbstractCommonList commonList){
70                 return "name|mimeType|encoding|length|uri|csid";
71         }
72
73         public AbstractCommonList createAbstractCommonListImpl(){
74                 return new BlobsCommonList();
75         }
76
77         public List createItemsList(AbstractCommonList commonList){
78                 List list = ((BlobsCommonList)commonList).getBlobListItem();
79                 return list;
80         }
81
82         public Object createItemForCommonList(DocumentModel docModel, String label, String id) throws Exception {
83                 BlobListItem item = new BlobListItem();
84                 item.setEncoding((String)
85                                 docModel.getProperty(label, BlobJAXBSchema.encoding));
86                 item.setMimeType((String)
87                                 docModel.getProperty(label, BlobJAXBSchema.mimeType));
88                 //String theData = (String)
89                 docModel.getProperty(label, BlobJAXBSchema.data);
90                 item.setName((String)
91                                 docModel.getProperty(label, BlobJAXBSchema.name));
92                 item.setLength((String)
93                                 docModel.getProperty(label, BlobJAXBSchema.length));
94                 item.setUri(getServiceContextPath() + id);
95                 item.setCsid(id);
96                 return item;
97         }
98
99         private BlobsCommon getCommonPartProperties(DocumentModel docModel) throws Exception {
100                 String label = getServiceContext().getCommonPartLabel();
101                 BlobsCommon result = new BlobsCommon();
102                 
103                 result.setData((String) 
104                                 docModel.getProperty(label, BlobJAXBSchema.data));
105                 result.setDigest((String)
106                                 docModel.getProperty(label, BlobJAXBSchema.digest));
107                 result.setEncoding((String)
108                                 docModel.getProperty(label, BlobJAXBSchema.encoding));
109                 result.setLength((String)
110                                 docModel.getProperty(label, BlobJAXBSchema.length));
111                 result.setMimeType((String)
112                                 docModel.getProperty(label, BlobJAXBSchema.mimeType));
113                 result.setName((String)
114                                 docModel.getProperty(label, BlobJAXBSchema.name));
115                 result.setRepositoryId((String)
116                                 docModel.getProperty(label, BlobJAXBSchema.repositoryId));
117                 result.setUri(getServiceContextPath() + docModel.getName() + "/content");
118                 
119                 return result;
120         }
121         
122         private void setCommonPartProperties(DocumentModel documentModel,
123                         BlobsCommon blobsCommon) throws ClientException {
124                 String label = getServiceContext().getCommonPartLabel();
125                 documentModel.setProperty(label, BlobJAXBSchema.data, blobsCommon.getData());
126                 documentModel.setProperty(label, BlobJAXBSchema.digest, blobsCommon.getDigest());
127                 documentModel.setProperty(label, BlobJAXBSchema.encoding, blobsCommon.getEncoding());
128                 documentModel.setProperty(label, BlobJAXBSchema.length, blobsCommon.getLength());
129                 documentModel.setProperty(label, BlobJAXBSchema.mimeType, blobsCommon.getMimeType());
130                 documentModel.setProperty(label, BlobJAXBSchema.name, blobsCommon.getName());
131                 documentModel.setProperty(label, BlobJAXBSchema.uri, blobsCommon.getUri());
132                 documentModel.setProperty(label, BlobJAXBSchema.repositoryId, blobsCommon.getRepositoryId());
133         }
134
135         /* (non-Javadoc)
136          * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractAllParts(org.collectionspace.services.common.document.DocumentWrapper)
137          */
138         @Override
139         public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
140                         throws Exception {
141                 ServiceContext ctx = this.getServiceContext();
142                 DocumentModel docModel = wrapDoc.getWrappedObject();
143                 //
144                 // Setup of the content URL's
145                 //
146                 BlobsCommon blobsCommon = this.getCommonPartProperties(docModel);
147                 String derivativeTerm = (String)ctx.getProperty(BlobInput.DERIVATIVE_TERM_KEY);
148                 if (derivativeTerm != null  && !derivativeTerm.equalsIgnoreCase(BlobInput.DERIVATIVE_ORIGINAL_VALUE)) {
149                         blobsCommon.setUri(getServiceContextPath() + docModel.getName() + "/derivatives/" +
150                                         derivativeTerm + "/content");
151                 }
152                 blobsCommon.setRepositoryId(null); //hide the repository id from the GET results since it is private
153                 this.setCommonPartProperties(docModel, blobsCommon);
154                 
155                 super.extractAllParts(wrapDoc);
156                 //
157                 // If the derivativeTerm is set then we need to get the blob stream
158                 //
159                 if (derivativeTerm != null) {
160                         RepositoryInstance repoSession = this.getRepositorySession();
161                         InputStream blobStream = NuxeoImageUtils.getPicture(ctx, repoSession, blobsCommon.getRepositoryId(), derivativeTerm);
162                         ctx.setProperty(BlobInput.DERIVATIVE_CONTENT_KEY, blobStream);
163                 }
164         }
165
166         @Override
167         public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
168                 ServiceContext ctx = this.getServiceContext();
169                 BlobInput blobInput = (BlobInput)ctx.getProperty(BlobInput.class.getName());
170                 if (blobInput == null) {                
171                         super.fillAllParts(wrapDoc, action);
172                 } else {
173                         //
174                         // If blobInput is set then we just received a multipart/form-data file post
175                         //
176                         DocumentModel documentModel = wrapDoc.getWrappedObject();
177                         RepositoryInstance repoSession = this.getRepositorySession();           
178                         BlobsCommon blobsCommon = NuxeoImageUtils.createPicture(ctx, repoSession, blobInput);
179                         this.setCommonPartProperties(documentModel, blobsCommon);
180                 }
181         }    
182 }
183