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.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;
41 import org.collectionspace.services.blob.BlobsCommonList;
42 import org.collectionspace.services.blob.BlobsCommon;
43 import org.collectionspace.services.blob.BlobsCommonList.BlobListItem;
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;
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;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 * The Class BlobDocumentModelHandler.
60 public class BlobDocumentModelHandler
61 extends DocHandlerBase<BlobsCommon, AbstractCommonList> {
64 private final Logger logger = LoggerFactory.getLogger(BlobDocumentModelHandler.class);
66 public final String getNuxeoSchemaName(){
70 public String getSummaryFields(AbstractCommonList commonList){
71 return "name|mimeType|encoding|length|uri|csid";
74 public AbstractCommonList createAbstractCommonListImpl(){
75 return new BlobsCommonList();
78 public List createItemsList(AbstractCommonList commonList){
79 List list = ((BlobsCommonList)commonList).getBlobListItem();
83 private String getDerivativePathBase(DocumentModel docModel) {
84 return getServiceContextPath() + docModel.getName() + "/" +
85 BlobInput.URI_DERIVATIVES_PATH + "/";
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);
97 docModel.getProperty(label, BlobJAXBSchema.name));
98 item.setLength((String)
99 docModel.getProperty(label, BlobJAXBSchema.length));
100 item.setUri(getServiceContextPath() + id);
105 private BlobsCommon getCommonPartProperties(DocumentModel docModel) throws Exception {
106 String label = getServiceContext().getCommonPartLabel();
107 BlobsCommon result = new BlobsCommon();
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);
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());
143 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractAllParts(org.collectionspace.services.common.document.DocumentWrapper)
146 public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
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
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?
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());
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);
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);
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);
190 // If blobInput is set then we just received a multipart/form-data file post
192 DocumentModel documentModel = wrapDoc.getWrappedObject();
193 RepositoryInstance repoSession = this.getRepositorySession();
194 BlobsCommon blobsCommon = NuxeoImageUtils.createPicture(ctx, repoSession, blobInput);
195 this.setCommonPartProperties(documentModel, blobsCommon);