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 org.collectionspace.services.blob.BlobsCommon;
27 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
28 import org.collectionspace.services.common.blob.BlobInput;
29 import org.collectionspace.services.common.blob.BlobOutput;
30 import org.collectionspace.services.common.blob.BlobUtil;
31 import org.collectionspace.services.common.context.ServiceContext;
32 import org.collectionspace.services.common.document.DocumentWrapper;
33 import org.collectionspace.services.common.imaging.nuxeo.NuxeoImageUtils;
34 import org.collectionspace.services.common.service.ListResultField;
35 import org.collectionspace.services.jaxb.AbstractCommonList;
36 import org.collectionspace.services.jaxb.BlobJAXBSchema;
37 import org.collectionspace.services.jaxb.AbstractCommonList;
38 import org.collectionspace.services.nuxeo.client.java.CommonList;
39 import org.nuxeo.ecm.core.api.ClientException;
40 import org.nuxeo.ecm.core.api.DocumentModel;
41 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import java.util.ArrayList;
46 import java.util.List;
49 * The Class BlobDocumentModelHandler.
51 public class BlobDocumentModelHandler
52 extends DocHandlerBase<BlobsCommon> {
55 private final Logger logger = LoggerFactory.getLogger(BlobDocumentModelHandler.class);
57 //==============================================================================
59 private String getDerivativePathBase(DocumentModel docModel) {
60 return getServiceContextPath() + docModel.getName() + "/" +
61 BlobInput.URI_DERIVATIVES_PATH + "/";
64 private BlobsCommon getCommonPartProperties(DocumentModel docModel) throws Exception {
65 String label = getServiceContext().getCommonPartLabel();
66 BlobsCommon result = new BlobsCommon();
68 result.setData((String)
69 docModel.getProperty(label, BlobJAXBSchema.data));
70 result.setDigest((String)
71 docModel.getProperty(label, BlobJAXBSchema.digest));
72 result.setEncoding((String)
73 docModel.getProperty(label, BlobJAXBSchema.encoding));
74 result.setLength((String)
75 docModel.getProperty(label, BlobJAXBSchema.length));
76 result.setMimeType((String)
77 docModel.getProperty(label, BlobJAXBSchema.mimeType));
78 result.setName((String)
79 docModel.getProperty(label, BlobJAXBSchema.name));
80 result.setRepositoryId((String)
81 docModel.getProperty(label, BlobJAXBSchema.repositoryId));
82 result.setUri(getServiceContextPath() + docModel.getName() + "/" +
83 BlobInput.URI_CONTENT_PATH);
88 private void setCommonPartProperties(DocumentModel documentModel,
89 BlobsCommon blobsCommon) throws ClientException {
90 String label = getServiceContext().getCommonPartLabel();
91 documentModel.setProperty(label, BlobJAXBSchema.data, blobsCommon.getData());
92 documentModel.setProperty(label, BlobJAXBSchema.digest, blobsCommon.getDigest());
93 documentModel.setProperty(label, BlobJAXBSchema.encoding, blobsCommon.getEncoding());
94 documentModel.setProperty(label, BlobJAXBSchema.length, blobsCommon.getLength());
95 documentModel.setProperty(label, BlobJAXBSchema.mimeType, blobsCommon.getMimeType());
96 documentModel.setProperty(label, BlobJAXBSchema.name, blobsCommon.getName());
97 documentModel.setProperty(label, BlobJAXBSchema.uri, blobsCommon.getUri());
98 documentModel.setProperty(label, BlobJAXBSchema.repositoryId, blobsCommon.getRepositoryId());
102 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractAllParts(org.collectionspace.services.common.document.DocumentWrapper)
105 public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
107 ServiceContext ctx = this.getServiceContext();
108 BlobInput blobInput = BlobUtil.getBlobInput(ctx);
109 RepositoryInstance repoSession = this.getRepositorySession();
110 DocumentModel docModel = wrapDoc.getWrappedObject();
111 BlobsCommon blobsCommon = this.getCommonPartProperties(docModel);
112 String blobRepositoryId = blobsCommon.getRepositoryId(); //cache the value to pass to the blob retriever
114 if (blobInput.isDerivativeListRequested() == true) {
115 List<ListResultField> resultsFields = getListItemsArray();
116 CommonList blobsCommonList = NuxeoImageUtils.getBlobDerivatives(
117 repoSession, blobRepositoryId, resultsFields, getDerivativePathBase(docModel));
118 // ctx.setProperty(BlobInput.BLOB_DERIVATIVE_LIST_KEY, blobsCommonList);
119 blobInput.setDerivativeList(blobsCommonList);
120 return; //FIXME: Don't like this exit point. Perhaps derivatives should be a sub-resource?
123 String derivativeTerm = blobInput.getDerivativeTerm();
124 Boolean getContentFlag = blobInput.isContentRequested();
125 BlobOutput blobOutput = NuxeoImageUtils.getBlobOutput(ctx, repoSession,
126 blobRepositoryId, derivativeTerm, getContentFlag);
127 if (getContentFlag == true) {
128 blobInput.setContentStream(blobOutput.getBlobInputStream());
129 // ctx.setProperty(BlobInput.BLOB_CONTENT_KEY, blobOutput.getBlobInputStream());
132 if (derivativeTerm != null) {
133 // reset 'blobsCommon' if we have a derivative request
134 blobsCommon = blobOutput.getBlobsCommon();
135 blobsCommon.setUri(getDerivativePathBase(docModel) +
136 derivativeTerm + "/" + BlobInput.URI_CONTENT_PATH);
139 blobsCommon.setRepositoryId(null); //hide the repository id from the GET results payload since it is private
140 this.setCommonPartProperties(docModel, blobsCommon);
141 // finish extracting the other parts by calling the parent
142 super.extractAllParts(wrapDoc);
146 public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
147 ServiceContext ctx = this.getServiceContext();
148 BlobInput blobInput = BlobUtil.getBlobInput(ctx);
149 if (blobInput.getBlobFile() != null) {
151 // If blobInput has a file then we just received a multipart/form-data file post or a URI query parameter
153 DocumentModel documentModel = wrapDoc.getWrappedObject();
154 RepositoryInstance repoSession = this.getRepositorySession();
155 BlobsCommon blobsCommon = NuxeoImageUtils.createPicture(ctx, repoSession, blobInput);
156 this.setCommonPartProperties(documentModel, blobsCommon);
157 blobInput.setBlobCsid(documentModel.getName());
159 super.fillAllParts(wrapDoc, action);