001/*
002 * (C) Copyright 2018-2020 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 *     Luís Duarte
018 *     Florent Guillaume
019 *     Mickaël Schoentgen
020 */
021package org.nuxeo.ecm.automation.server.jaxrs.batch;
022
023import java.util.Map;
024
025import org.nuxeo.ecm.automation.server.jaxrs.batch.handler.BatchFileInfo;
026import org.nuxeo.ecm.core.transientstore.api.TransientStore;
027
028/**
029 * Batch Handler encapsulates functionality to handle Batch Upload behaviours.
030 *
031 * @since 10.1
032 */
033public interface BatchHandler {
034
035    /**
036     * Initializes this batch handler with the given name and configuration properties.
037     *
038     * @param name the batch handler's name
039     * @param properties the configuration properties
040     */
041    void initialize(String name, Map<String, String> properties);
042
043    /**
044     * Gets the batch handler's name.
045     *
046     * @return the batch handler's name
047     */
048    String getName();
049
050    /**
051     * Initiates a new batch with an optional externally provided id.
052     *
053     * @param batchId the id to use for the batch, or {@code null} to generate it
054     * @return a newly created batch
055     */
056    Batch newBatch(String batchId);
057
058    /**
059     * Attempts to fetch a batch with the given id.
060     *
061     * @param batchId the batch id to fetch
062     * @return the batch with the given id, or {@code null} if not found
063     */
064    Batch getBatch(String batchId);
065
066    /**
067     * Attempts to renew the credentials associated to this batch handler.
068     * This is only typically used in third-party batch handlers.
069     *
070     * @param batchId the batch id
071     * @return the new credentials
072     *
073     * @since 11.1
074     */
075    Map<String, Object> refreshToken(String batchId);
076
077    /**
078     * Callback for the batch handler to execute post-upload actions. This is only typically used in third-party batch
079     * handlers.
080     *
081     * @param batchId the batch id
082     * @param fileIdx the file index within the batch
083     * @param fileInfo file information regarting the uploaded file
084     * @return {@code true} if the action was success
085     */
086    default boolean completeUpload(String batchId, String fileIdx, BatchFileInfo fileInfo) {
087        return true;
088    }
089
090    /**
091     * Gets the transient store used by this batch handler.
092     *
093     * @since 10.10
094     */
095    TransientStore getTransientStore();
096
097}