001/*
002 * (C) Copyright 2015 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 *     Vincent Dutat
018 */
019package org.nuxeo.ecm.platform.signature.core.sign;
020
021import java.text.SimpleDateFormat;
022import java.util.Date;
023
024import org.apache.commons.io.FilenameUtils;
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.ecm.platform.signature.api.sign.SignatureService.SigningDisposition;
028import org.nuxeo.runtime.api.Framework;
029
030public class SignatureHelper {
031
032    private static final Log log = LogFactory.getLog(SignatureHelper.class);
033
034    /**
035     * If this system property is set to "true", then signature will use PDF/A.
036     */
037    public static final String SIGNATURE_USE_PDFA_PROP = "org.nuxeo.ecm.signature.pdfa";
038
039    /**
040     * Signature disposition for PDF files. Can be "replace", "archive" or "attach".
041     */
042    public static final String SIGNATURE_DISPOSITION_PDF = "org.nuxeo.ecm.signature.disposition.pdf";
043
044    /**
045     * Signature disposition for non-PDF files. Can be "replace", "archive" or "attach".
046     */
047    public static final String SIGNATURE_DISPOSITION_NOTPDF = "org.nuxeo.ecm.signature.disposition.notpdf";
048
049    public static final String SIGNATURE_ARCHIVE_FILENAME_FORMAT_PROP = "org.nuxeo.ecm.signature.archive.filename.format";
050
051    /** Used with {@link SimpleDateFormat}. */
052    public static final String DEFAULT_ARCHIVE_FORMAT = " ('archive' yyyy-MM-dd HH:mm:ss)";
053
054    private SignatureHelper() {}
055
056    public static boolean getPDFA() {
057        return Framework.isBooleanPropertyTrue(SIGNATURE_USE_PDFA_PROP);
058    }
059
060    public static SigningDisposition getDisposition(boolean originalIsPdf) {
061        String disp;
062        if (originalIsPdf) {
063            disp = Framework.getProperty(SIGNATURE_DISPOSITION_PDF, SigningDisposition.ARCHIVE.name());
064        } else {
065            disp = Framework.getProperty(SIGNATURE_DISPOSITION_NOTPDF, SigningDisposition.ATTACH.name());
066        }
067        try {
068            return Enum.valueOf(SigningDisposition.class, disp.toUpperCase());
069        } catch (RuntimeException e) {
070            log.warn("Invalid signing disposition: " + disp);
071            return SigningDisposition.ATTACH;
072        }
073    }
074
075    public static String getArchiveFilename(String filename) {
076        String format = Framework.getProperty(SIGNATURE_ARCHIVE_FILENAME_FORMAT_PROP, DEFAULT_ARCHIVE_FORMAT);
077        return FilenameUtils.getBaseName(filename) + new SimpleDateFormat(format).format(new Date()) + "."
078                + FilenameUtils.getExtension(filename);
079    }
080}