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.handler.impl;
022
023import java.io.Serializable;
024import java.util.Map;
025import java.util.Objects;
026
027import org.nuxeo.ecm.automation.server.jaxrs.batch.Batch;
028import org.nuxeo.ecm.automation.server.jaxrs.batch.BatchFileEntry;
029import org.nuxeo.ecm.automation.server.jaxrs.batch.handler.AbstractBatchHandler;
030import org.nuxeo.ecm.automation.server.jaxrs.batch.handler.BatchFileInfo;
031
032/**
033 * Default batch handler,
034 *
035 * @since 10.1
036 */
037public class DefaultBatchHandler extends AbstractBatchHandler {
038
039    @Override
040    public Batch getBatch(String batchId) {
041        Map<String, Serializable> parameters = getBatchParameters(batchId);
042        if (parameters == null) {
043            return null;
044        }
045
046        // create the batch
047        return new Batch(batchId, parameters, getName(), getTransientStore());
048    }
049
050    @Override
051    public boolean completeUpload(String batchId, String fileIndex, BatchFileInfo fileInfo) {
052        Batch batch = getBatch(batchId);
053        BatchFileEntry fileEntry = batch.getFileEntry(fileIndex, true);
054        return fileEntry.getFileSize() == fileInfo.getLength()
055                && Objects.equals(fileEntry.getMimeType(), fileInfo.getMimeType())
056                && Objects.equals(fileEntry.getBlob().getDigest(), fileInfo.getMd5());
057    }
058
059    @Override
060    public Map<String, Object> refreshToken(String batchId) {
061        throw new UnsupportedOperationException("Refresh token is not supported for the default batch handler.");
062    }
063
064}