001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Vincent Vergnolle 016 */ 017package org.nuxeo.ecm.automation.core.operations.blob; 018 019import java.io.Serializable; 020import java.util.Collections; 021import java.util.HashMap; 022import java.util.Map; 023import java.util.Map.Entry; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.nuxeo.ecm.automation.core.Constants; 028import org.nuxeo.ecm.automation.core.annotations.Context; 029import org.nuxeo.ecm.automation.core.annotations.Operation; 030import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 031import org.nuxeo.ecm.automation.core.annotations.Param; 032import org.nuxeo.ecm.automation.core.util.Properties; 033import org.nuxeo.ecm.core.api.Blob; 034import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 035import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder; 036import org.nuxeo.ecm.core.convert.api.ConversionService; 037 038/** 039 * @since 7.1 040 * @author Vincent Vergnolle 041 */ 042@Operation(id = RunConverter.ID, category = Constants.CAT_CONVERSION, label = RunConverter.ID, description = "Simply call a converter based on the 'converter' parameter. You can pass the converter properties with the 'properties' parameter.", since = "7.1") 043public class RunConverter { 044 045 public static final String ID = "Blob.RunConverter"; 046 047 public static final Log log = LogFactory.getLog(RunConverter.class); 048 049 @Param(name = "converter", description = "The name of the converter to call") 050 protected String converter; 051 052 @Param(name = "parameters", description = "The converter parameters to pass", required = false) 053 protected Properties parameters; 054 055 @Context 056 protected ConversionService conversionService; 057 058 @OperationMethod 059 public Blob run(Blob blob) { 060 if (log.isDebugEnabled()) { 061 log.debug("Call converter named: " + converter); 062 } 063 064 BlobHolder holder = conversionService.convert(converter, new SimpleBlobHolder(blob), propertiesToMap()); 065 066 return holder.getBlob(); 067 } 068 069 private Map<String, Serializable> propertiesToMap() { 070 if (parameters == null) { 071 return Collections.emptyMap(); 072 } 073 074 Map<String, Serializable> params = new HashMap<>(parameters.size()); 075 for (Entry<String, String> entry : parameters.entrySet()) { 076 params.put(entry.getKey(), entry.getValue()); 077 } 078 079 return params; 080 } 081}