001/* 
002 * Copyright (C) 2016 Hobrasoft s.r.o.
003 *
004 * This program is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU Affero General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * This program 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
012 * GNU Affero General Public License for more details.
013 *
014 * You should have received a copy of the GNU Affero General Public License
015 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
016 */
017package cz.hobrasoft.pdfmu.operation.args;
018
019import java.util.logging.Logger;
020import net.sourceforge.argparse4j.inf.Argument;
021import net.sourceforge.argparse4j.inf.ArgumentParser;
022import net.sourceforge.argparse4j.inf.Namespace;
023import org.apache.commons.lang3.StringUtils;
024
025public class PasswordArgs implements ArgsConfiguration {
026
027    private static final Logger logger = Logger.getLogger(PasswordArgs.class.getName());
028
029    private final String title;
030
031    public Argument passwordArgument;
032    public Argument environmentVariableArgument;
033
034    private String password;
035
036    public PasswordArgs(String title) {
037        assert title != null;
038        this.title = title;
039    }
040
041    public String getPassword() {
042        return password;
043    }
044
045    public char[] getPasswordCharArray() {
046        if (password == null) {
047            return null;
048        }
049        return password.toCharArray();
050    }
051
052    @Deprecated
053    @Override
054    public void addArguments(ArgumentParser parser) {
055        finalizeArguments();
056    }
057
058    public void finalizeArguments() {
059        assert passwordArgument != null;
060        passwordArgument.type(String.class);
061
062        assert environmentVariableArgument != null;
063        environmentVariableArgument.type(String.class);
064    }
065
066    @Override
067    public void setFromNamespace(Namespace namespace) {
068        assert title != null;
069        assert passwordArgument != null;
070        password = namespace.getString(passwordArgument.getDest());
071        if (password == null) {
072            // Load the password from an environment variable
073            assert environmentVariableArgument != null;
074            String envVar = namespace.getString(environmentVariableArgument.getDest());
075            assert envVar != null; // The argument has a default value
076            logger.info(String.format("%s environment variable: %s", StringUtils.capitalize(title), envVar));
077            password = System.getenv(envVar);
078            if (password != null) {
079                logger.info(String.format("%s loaded from the environment variable %s.", StringUtils.capitalize(title), envVar));
080            } else {
081                logger.info(String.format("%s was not set.", StringUtils.capitalize(title)));
082            }
083        } else {
084            logger.info(String.format("%s loaded from the command line.", StringUtils.capitalize(title)));
085        }
086    }
087
088}