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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.blob.nuxeo;
26 import java.io.InputStream;
27 import java.util.Iterator;
28 import java.util.List;
30 import org.collectionspace.services.jaxb.AbstractCommonList;
31 import org.collectionspace.services.jaxb.BlobJAXBSchema;
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;
40 import org.collectionspace.services.blob.BlobsCommonList;
41 import org.collectionspace.services.blob.BlobsCommon;
42 import org.collectionspace.services.blob.BlobsCommonList.BlobListItem;
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;
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;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 * The Class BlobDocumentModelHandler.
59 public class BlobDocumentModelHandler
60 extends DocHandlerBase<BlobsCommon, AbstractCommonList> {
63 private final Logger logger = LoggerFactory.getLogger(BlobDocumentModelHandler.class);
65 public final String getNuxeoSchemaName(){
69 public String getSummaryFields(AbstractCommonList commonList){
70 return "name|mimeType|encoding|length|uri|csid";
73 public AbstractCommonList createAbstractCommonListImpl(){
74 return new BlobsCommonList();
77 public List createItemsList(AbstractCommonList commonList){
78 List list = ((BlobsCommonList)commonList).getBlobListItem();
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);
91 docModel.getProperty(label, BlobJAXBSchema.name));
92 item.setLength((String)
93 docModel.getProperty(label, BlobJAXBSchema.length));
94 item.setUri(getServiceContextPath() + id);
99 private BlobsCommon getCommonPartProperties(DocumentModel docModel) throws Exception {
100 String label = getServiceContext().getCommonPartLabel();
101 BlobsCommon result = new BlobsCommon();
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");
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());
136 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractAllParts(org.collectionspace.services.common.document.DocumentWrapper)
139 public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
141 ServiceContext ctx = this.getServiceContext();
142 DocumentModel docModel = wrapDoc.getWrappedObject();
144 // Setup of the content URL's
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");
152 blobsCommon.setRepositoryId(null); //hide the repository id from the GET results since it is private
153 this.setCommonPartProperties(docModel, blobsCommon);
155 super.extractAllParts(wrapDoc);
157 // If the derivativeTerm is set then we need to get the blob stream
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);
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);
174 // If blobInput is set then we just received a multipart/form-data file post
176 DocumentModel documentModel = wrapDoc.getWrappedObject();
177 RepositoryInstance repoSession = this.getRepositorySession();
178 BlobsCommon blobsCommon = NuxeoImageUtils.createPicture(ctx, repoSession, blobInput);
179 this.setCommonPartProperties(documentModel, blobsCommon);