001/*
002 * (C) Copyright 2012-2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *      Vladimir Pasquier <vpasquier@nuxeo.com>
016 *      Mickael Vachette <mv@nuxeo.com>
017 */
018package org.nuxeo.ecm.platform.signature.core.operations;
019
020import org.nuxeo.ecm.automation.core.Constants;
021import org.nuxeo.ecm.automation.core.annotations.Context;
022import org.nuxeo.ecm.automation.core.annotations.Operation;
023import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
024import org.nuxeo.ecm.automation.core.annotations.Param;
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
028import org.nuxeo.ecm.platform.signature.api.sign.SignatureService;
029import org.nuxeo.ecm.platform.signature.api.sign.SignatureService.SigningDisposition;
030import org.nuxeo.ecm.platform.signature.core.sign.SignatureHelper;
031import org.nuxeo.ecm.platform.usermanager.UserManager;
032
033@Operation(id = SignPDFDocument.ID, category = Constants.CAT_SERVICES, label = "Sign PDF", description = "Applies a digital signature to the"
034        + " PDF blob of the input document.")
035public class SignPDFDocument {
036
037    public static final String ID = "Services.SignPDFDocument";
038
039    private static final String MIME_TYPE_PDF = "application/pdf";
040
041    @Context
042    protected UserManager userManager;
043
044    @Context
045    protected SignatureService signatureService;
046
047    @Param(name = "username", required = true, description = "The user ID for" + " signing PDF document.")
048    protected String username;
049
050    @Param(name = "password", required = true, description = "Certificate " + "password.")
051    protected String password;
052
053    @Param(name = "reason", required = true, description = "Signature reason.")
054    protected String reason;
055
056    @OperationMethod
057    public Blob run(DocumentModel doc) {
058        DocumentModel user = userManager.getUserModel(username);
059        Blob originalBlob = doc.getAdapter(BlobHolder.class).getBlob();
060        boolean originalIsPdf = MIME_TYPE_PDF.equals(originalBlob.getMimeType());
061        // decide if we want PDF/A
062        boolean pdfa = SignatureHelper.getPDFA();
063        // decide disposition
064        SigningDisposition disposition = SignatureHelper.getDisposition(originalIsPdf);
065        // decide archive filename
066        String filename = originalBlob.getFilename();
067        String archiveFilename = SignatureHelper.getArchiveFilename(filename);
068        return signatureService.signDocument(doc, user, password, reason, pdfa, disposition, archiveFilename);
069    }
070}