001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 */ 019package org.nuxeo.ecm.automation.core.operations.blob; 020 021import org.nuxeo.ecm.automation.core.Constants; 022import org.nuxeo.ecm.automation.core.annotations.Operation; 023import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 024import org.nuxeo.ecm.automation.core.annotations.Param; 025import org.nuxeo.ecm.automation.core.collectors.BlobCollector; 026import org.nuxeo.ecm.core.api.Blob; 027import org.nuxeo.ecm.core.api.Blobs; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 030 031/** 032 * Get document blob inside the file:content property 033 * 034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 035 * @author tiry 036 */ 037@Operation(id = GetDocumentBlob.ID, category = Constants.CAT_BLOB, label = "Get Document File", description = "Gets a file attached to the input document. The file location is specified using an xpath to the blob property of the document. Returns the file.", aliases = { "Blob.Get" }) 038public class GetDocumentBlob { 039 040 public static final String ID = "Document.GetBlob"; 041 042 @Param(name = "xpath", required = false, values = "file:content") 043 protected String xpath = "file:content"; 044 045 @OperationMethod(collector = BlobCollector.class) 046 public Blob run(DocumentModel doc) { 047 Blob blob = (Blob) doc.getPropertyValue(xpath); 048 if (blob == null) { 049 BlobHolder bh = doc.getAdapter(BlobHolder.class); 050 if (bh != null) { 051 blob = bh.getBlob(); 052 } 053 } 054 // cannot return null since it may break the next operation 055 if (blob == null) { // create an empty blob 056 blob = Blobs.createBlob(""); 057 blob.setFilename(doc.getName() + ".null"); 058 } 059 return blob; 060 } 061 062}