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 *      Vladimir Pasquier <vpasquier@nuxeo.com>
018 */
019package org.nuxeo.binary.metadata.internals.operations;
020
021import java.util.ArrayList;
022
023import org.nuxeo.binary.metadata.api.BinaryMetadataService;
024import org.nuxeo.ecm.automation.OperationContext;
025import org.nuxeo.ecm.automation.core.Constants;
026import org.nuxeo.ecm.automation.core.annotations.Context;
027import org.nuxeo.ecm.automation.core.annotations.Operation;
028import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
029import org.nuxeo.ecm.automation.core.annotations.Param;
030import org.nuxeo.ecm.automation.core.util.StringList;
031import org.nuxeo.ecm.core.api.Blob;
032
033/**
034 * @since 7.1
035 */
036@Operation(id = ReadMetadataFromBinaryToContext.ID, category = Constants.CAT_EXECUTION, label = "Read Metadata From "
037        + "Binary to Context", description = "Read Metadata From binary to Context "
038        + "for a given input blob and given metadata to inject into the "
039        + "Operation context (if not specified, all metadata will be injected) " + "", since = "7.1", addToStudio = true, aliases = { "Context.ReadMetadataFromBinary" })
040public class ReadMetadataFromBinaryToContext {
041
042    public static final String ID = "Context.SetMetadataFromBlob";
043
044    public static final String CTX_BINARY_METADATA = "binaryMetadata";
045
046    @Context
047    protected BinaryMetadataService binaryMetadataService;
048
049    @Context
050    protected OperationContext operationContext;
051
052    @Param(name = "ignorePrefix", required = false, description = "Ignore metadata prefixes or not")
053    boolean ignorePrefix = true;
054
055    @Param(name = "processor", required = false, description = "The processor to execute for overriding the input blob.")
056    protected String processor = "exifTool";
057
058    @Param(name = "metadata", required = false, description = "Metadata list to filter on the blob.")
059    protected StringList metadata;
060
061    @OperationMethod
062    public void run(Blob blob) {
063        if (metadata == null || metadata.isEmpty()) {
064            operationContext.put(CTX_BINARY_METADATA, binaryMetadataService.readMetadata(blob, ignorePrefix));
065        } else {
066            ArrayList<String> metadataList = new ArrayList<>();
067            for (String meta : metadata) {
068                metadataList.add(meta);
069            }
070            operationContext.put(CTX_BINARY_METADATA,
071                    binaryMetadataService.readMetadata(blob, metadataList,
072                            ignorePrefix));
073        }
074    }
075}