001/*
002 * (C) Copyright 2016-2018 Nuxeo (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 *     Thibaud Arguillere
018 *     Miguel Nixo
019 */
020package org.nuxeo.ecm.platform.pdf.operations;
021
022import java.util.stream.Collectors;
023
024import org.apache.commons.lang3.StringUtils;
025import org.nuxeo.ecm.automation.core.Constants;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029import org.nuxeo.ecm.automation.core.util.BlobList;
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.DocumentModelList;
033import org.nuxeo.ecm.platform.pdf.PDFEncryption;
034
035/**
036 * Removes the encryption, returns a copy of the blob.
037 *
038 * @since 8.10
039 */
040@Operation(id = PDFRemoveEncryptionOperation.ID, category = Constants.CAT_CONVERSION, label = "PDF: Remove Encryption", //
041description = "Removes the encryption, returns a copy of the blob. If the operation is ran on Document(s), xpath "
042        + "lets you specificy where to get the blob from (default: file:content).")
043public class PDFRemoveEncryptionOperation {
044
045    public static final String ID = "PDF.RemoveEncryption";
046
047    @Param(name = "ownerPwd", required = false)
048    private String ownerPwd;
049
050    @Param(name = "xpath", required = false, values = { "file:content" })
051    protected String xpath = "file:content";
052
053    @OperationMethod
054    public Blob run(Blob inBlob) {
055        PDFEncryption pdfe = new PDFEncryption(inBlob);
056        pdfe.setOriginalOwnerPwd(ownerPwd);
057        return pdfe.removeEncryption();
058    }
059
060    @OperationMethod
061    public BlobList run(BlobList inBlobs) {
062        return inBlobs.stream().map(this::run).collect(Collectors.toCollection(BlobList::new));
063    }
064
065    @OperationMethod
066    public Blob run(DocumentModel inDoc) {
067        if (StringUtils.isBlank(xpath)) {
068            xpath = "file:content";
069        }
070        Blob content = (Blob) inDoc.getPropertyValue(xpath);
071        return (content != null) ? this.run(content) : null;
072    }
073
074    @OperationMethod
075    public BlobList run(DocumentModelList inDocs) {
076        if (StringUtils.isBlank(xpath)) {
077            xpath = "file:content";
078        }
079        return inDocs.stream().map(this::run).collect(Collectors.toCollection(BlobList::new));
080    }
081
082}