001/*
002 * (C) Copyright 2018 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 *     pierre
018 */
019package org.nuxeo.ecm.core.bulk.action.computation;
020
021import java.io.IOException;
022
023import org.apache.logging.log4j.LogManager;
024import org.apache.logging.log4j.Logger;
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.bulk.BulkCodecs;
027import org.nuxeo.ecm.core.bulk.BulkService;
028import org.nuxeo.ecm.core.bulk.message.DataBucket;
029import org.nuxeo.ecm.core.utils.BlobUtils;
030import org.nuxeo.lib.stream.codec.Codec;
031import org.nuxeo.lib.stream.computation.ComputationContext;
032import org.nuxeo.lib.stream.computation.Record;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * @since 10.3
037 */
038public class ZipBlob extends AbstractTransientBlobComputation {
039
040    private static final Logger log = LogManager.getLogger(ZipBlob.class);
041
042    public static final String NAME = "bulk/zipBlob";
043
044    public static final String ZIP_PARAMETER = "zip";
045
046    public ZipBlob() {
047        super(NAME);
048    }
049
050    @Override
051    public void processRecord(ComputationContext context, String inputStreamName, Record record) {
052        Codec<DataBucket> codec = BulkCodecs.getDataBucketCodec();
053        DataBucket in = codec.decode(record.getData());
054
055        String storeName = Framework.getService(BulkService.class).getStatus(in.getCommandId()).getAction();
056        Blob blob = getBlob(in.getDataAsString(), storeName);
057        try {
058            blob = BlobUtils.zip(blob, blob.getFilename() + ".zip");
059        } catch (IOException e) {
060            log.error("Unable to zip blob", e);
061        }
062        storeBlob(blob, in.getCommandId(), storeName);
063
064        DataBucket out = new DataBucket(in.getCommandId(), in.getCount(), getTransientStoreKey(in.getCommandId()));
065        context.produceRecord(OUTPUT_1, Record.of(in.getCommandId(), codec.encode(out)));
066        context.askForCheckpoint();
067    }
068
069}