From 12fe32df6f5b17abb5d1f9bac9f5fb87b961f0c2 Mon Sep 17 00:00:00 2001 From: tkellner Date: Wed, 10 Apr 2013 18:56:29 +0000 Subject: Configuration Changes git-svn-id: https://joinup.ec.europa.eu/svn/pdf-over/trunk@49 174cde9d-5d70-4d2a-aa98-46368bc2aaf7 --- .../workflow/states/PrepareConfigurationState.java | 237 +++++++++++++++++++-- 1 file changed, 221 insertions(+), 16 deletions(-) (limited to 'pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java') diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java index 2a71890d..b4b05318 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java @@ -16,12 +16,28 @@ package at.asit.pdfover.gui.workflow.states; //Imports +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.asit.pdfover.gui.cliarguments.ArgumentHandler; +import at.asit.pdfover.gui.cliarguments.BKUArgument; +import at.asit.pdfover.gui.cliarguments.ConfigFileArgument; +import at.asit.pdfover.gui.cliarguments.HelpArgument; +import at.asit.pdfover.gui.cliarguments.PhoneNumberArgument; +import at.asit.pdfover.gui.exceptions.InitializationException; +import at.asit.pdfover.gui.workflow.ConfigManipulator; import at.asit.pdfover.gui.workflow.StateMachine; +import at.asit.pdfover.signator.BKUs; import at.asit.pdfover.signator.Signator; - +import at.asit.pdfover.signator.SignaturePosition; /** * Starting state of workflow proccess @@ -30,35 +46,222 @@ import at.asit.pdfover.signator.Signator; */ public class PrepareConfigurationState extends State { + /** + * Regex for parsing signature position + */ + public static final String SIGN_POS_REGEX = "(x=(\\d\\.?\\d?);y=(\\d\\.?\\d?);p=(\\d))|(auto)|(x=(\\d\\.?\\d?);y=(\\d\\.?\\d?))"; //$NON-NLS-1$ + /** * @param stateMachine */ public PrepareConfigurationState(StateMachine stateMachine) { super(stateMachine); + this.handler = new ArgumentHandler(this.stateMachine); + this.handler.addCLIArgument(new HelpArgument()); + this.handler.addCLIArgument(new BKUArgument()); + this.handler.addCLIArgument(new PhoneNumberArgument()); + + // adding config file argument to this handler so it appears in help + this.handler.addCLIArgument(new ConfigFileArgument()); + + this.configFilehandler = new ArgumentHandler(this.stateMachine); + this.configFilehandler.addCLIArgument(new ConfigFileArgument()); } + private ArgumentHandler handler; + + private ArgumentHandler configFilehandler; + /** * SFL4J Logger instance **/ - @SuppressWarnings("unused") private static final Logger log = LoggerFactory .getLogger(PrepareConfigurationState.class); + private void initializeFromConfigurationFile(String filename) + throws InitializationException { + try { + Properties config = new Properties(); + + try { + config.load(new FileInputStream(filename)); + } catch (FileNotFoundException ex) { + if (filename.equals(ConfigManipulator.DEFAULT_CONFIG_FILE)) { + // we only check for resource config file if it is the + // default value! + try { + InputStream is = this.getClass().getResourceAsStream( + "/" + filename); //$NON-NLS-1$ + config.load(is); + } catch (Exception eex) { + throw ex; + } + } else { + throw ex; + } + } + + // Load ok ... + ConfigManipulator configManipulator = this.stateMachine + .getConfigManipulator(); + + // Set Emblem + configManipulator.setDefaultEmblem(config + .getProperty(ConfigManipulator.EMBLEM_CONFIG)); + + // Set Mobile Phone Number + configManipulator.setDefaultMobileNumber(config + .getProperty(ConfigManipulator.MOBILE_NUMBER_CONFIG)); + + // Set Proxy Host + configManipulator.setProxyHost(config + .getProperty(ConfigManipulator.PROXY_HOST_CONFIG)); + + // Set Proxy Port + String proxyPortString = config + .getProperty(ConfigManipulator.PROXY_PORT_CONFIG); + + if (proxyPortString != null && !proxyPortString.trim().equals("")) { //$NON-NLS-1$ + int port = Integer.parseInt(proxyPortString); + + if (port > 0 && port <= 0xFFFF) { + configManipulator.setProxyPort(port); + } else { + log.warn("Proxy port is out of range!: " + port); //$NON-NLS-1$ + } + } + + // Set Default BKU + String bkuString = config.getProperty(ConfigManipulator.BKU_CONFIG); + + BKUs defaultBKU = BKUs.NONE; + + try { + defaultBKU = BKUs.valueOf(bkuString); + } catch (IllegalArgumentException ex) { + log.error("Invalid BKU config value " + bkuString + " using none!"); //$NON-NLS-1$ //$NON-NLS-2$ + defaultBKU = BKUs.NONE; + } catch (NullPointerException ex) { + log.error("Invalid BKU config value " + bkuString + " using none!"); //$NON-NLS-1$ //$NON-NLS-2$ + defaultBKU = BKUs.NONE; + } + + configManipulator.setDefaultBKU(defaultBKU); + + // Set Signature Position + String signaturePosition = config + .getProperty(ConfigManipulator.SIGNATURE_POSITION_CONFIG); + + SignaturePosition position = null; + + if (signaturePosition != null + && !signaturePosition.trim().equals("")) { //$NON-NLS-1$ + + signaturePosition = signaturePosition.trim().toLowerCase(); + + Pattern pattern = Pattern.compile(SIGN_POS_REGEX); + + Matcher matcher = pattern.matcher(signaturePosition); + + if (matcher.matches()) { + if (matcher.groupCount() == 8) { + if (matcher.group(1) != null) { + // we have format: x=..;y=..;p=... + try { + // group 2 = x value + float x = Float.parseFloat(matcher.group(2)); + + // group 3 = y value + float y = Float.parseFloat(matcher.group(3)); + + // group 4 = p value + int p = Integer.parseInt(matcher.group(3)); + + position = new SignaturePosition(x, y, p); + } catch (NumberFormatException ex) { + log.error( + "Signature Position read from config failed: Not a valid number", ex); //$NON-NLS-1$ + } + } else if (matcher.group(5) != null) { + // we have format auto + position = new SignaturePosition(); + } else if (matcher.group(6) != null) { + // we have format x=...;y=...; + // group 7 = x value + float x = Float.parseFloat(matcher.group(7)); + + // group 8 = y value + float y = Float.parseFloat(matcher.group(8)); + + position = new SignaturePosition(x, y); + } + } else { + log.error("Signature Position read from config failed: wrong group Count!"); //$NON-NLS-1$ + } + } else { + log.error("Signature Position read from config failed: not matching string"); //$NON-NLS-1$ + } + + } + + configManipulator.setDefaultSignaturePosition(position); + + } catch (IOException ex) { + throw new InitializationException( + "Failed to read configuration from config file", ex); //$NON-NLS-1$ + } + } + + private void initializeFromArguments(String[] args, ArgumentHandler handler) + throws InitializationException { + handler.handleArguments(args); + + if (handler.IsRequireExit()) { + this.stateMachine.exit(); + } + } + @Override public void run() { - // TODO: Read config file and command line arguments - // Set usedSignerLib ... - this.stateMachine.getPDFSigner().setUsedPDFSignerLibrary(Signator.Signers.PDFAS); - - // Create PDF Signer - this.stateMachine.getStatus().setBKU(this.stateMachine.getConfigProvider().getDefaultBKU()); - - this.stateMachine.getStatus().setSignaturePosition(this.stateMachine.getConfigProvider().getDefaultSignaturePosition()); - - this.setNextState(new OpenState(this.stateMachine)); + // Read config file + try { + // Read cli arguments with for config file! + this.initializeFromArguments(this.stateMachine.getCmdArgs(), + this.configFilehandler); + + // initialize from config file + this.initializeFromConfigurationFile(this.stateMachine + .getConfigProvider().getConfigurationFile()); + + // Read cli arguments + this.initializeFromArguments(this.stateMachine.getCmdArgs(), + this.handler); + + // Set usedSignerLib ... + this.stateMachine.getPDFSigner().setUsedPDFSignerLibrary( + Signator.Signers.PDFAS); + + // Create PDF Signer + this.stateMachine.getStatus().setBKU( + this.stateMachine.getConfigProvider().getDefaultBKU()); + + this.stateMachine.getStatus().setSignaturePosition( + this.stateMachine.getConfigProvider() + .getDefaultSignaturePosition()); + + this.setNextState(new OpenState(this.stateMachine)); + + } catch (InitializationException e) { + log.error("Failed to initialize: ", e); //$NON-NLS-1$ + ErrorState error = new ErrorState(this.stateMachine); + error.setException(e); + this.setNextState(error); + } } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see at.asit.pdfover.gui.workflow.states.State#cleanUp() */ @Override @@ -66,16 +269,18 @@ public class PrepareConfigurationState extends State { // No composite - no cleanup necessary } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see at.asit.pdfover.gui.workflow.states.State#setMainWindowBehavior() */ @Override public void updateMainWindowBehavior() { - //no behavior necessary yet + // no behavior necessary yet } @Override - public String toString() { + public String toString() { return this.getClass().getName(); } } -- cgit v1.2.3