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