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