001/* 002 * (C) Copyright 2014 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 * Vincent Vergnolle 018 */ 019package org.nuxeo.ecm.automation.core.operations.blob; 020 021import java.io.Serializable; 022import java.util.Collections; 023import java.util.HashMap; 024import java.util.Map; 025import java.util.Map.Entry; 026 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029import org.nuxeo.ecm.automation.core.Constants; 030import org.nuxeo.ecm.automation.core.annotations.Context; 031import org.nuxeo.ecm.automation.core.annotations.Operation; 032import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 033import org.nuxeo.ecm.automation.core.annotations.Param; 034import org.nuxeo.ecm.automation.core.util.Properties; 035import org.nuxeo.ecm.core.api.Blob; 036import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 037import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder; 038import org.nuxeo.ecm.core.convert.api.ConversionService; 039 040/** 041 * @since 7.1 042 * @author Vincent Vergnolle 043 */ 044@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") 045public class RunConverter { 046 047 public static final String ID = "Blob.RunConverter"; 048 049 public static final Log log = LogFactory.getLog(RunConverter.class); 050 051 @Param(name = "converter", description = "The name of the converter to call") 052 protected String converter; 053 054 @Param(name = "parameters", description = "The converter parameters to pass", required = false) 055 protected Properties parameters; 056 057 @Context 058 protected ConversionService conversionService; 059 060 @OperationMethod 061 public Blob run(Blob blob) { 062 if (log.isDebugEnabled()) { 063 log.debug("Call converter named: " + converter); 064 } 065 066 BlobHolder holder = conversionService.convert(converter, new SimpleBlobHolder(blob), propertiesToMap()); 067 068 return holder.getBlob(); 069 } 070 071 private Map<String, Serializable> propertiesToMap() { 072 if (parameters == null) { 073 return Collections.emptyMap(); 074 } 075 076 Map<String, Serializable> params = new HashMap<>(parameters.size()); 077 for (Entry<String, String> entry : parameters.entrySet()) { 078 params.put(entry.getKey(), entry.getValue()); 079 } 080 081 return params; 082 } 083}