From fb04f57eb521381a2a888670ca3783cbe21e7d37 Mon Sep 17 00:00:00 2001 From: Tobias Kellner Date: Mon, 12 Oct 2015 08:36:18 +0200 Subject: Allow to manually enter keystore alias --- .../composites/KeystoreConfigurationComposite.java | 50 ++++++++++++++-------- .../KeystoreAliasDoesntExistException.java | 36 ++++++++++++++++ 2 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 pdf-over-gui/src/main/java/at/asit/pdfover/gui/exceptions/KeystoreAliasDoesntExistException.java diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/KeystoreConfigurationComposite.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/KeystoreConfigurationComposite.java index 24170e24..0596f708 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/KeystoreConfigurationComposite.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/KeystoreConfigurationComposite.java @@ -52,9 +52,8 @@ import at.asit.pdfover.gui.Constants; import at.asit.pdfover.gui.controls.Dialog.BUTTONS; import at.asit.pdfover.gui.controls.ErrorDialog; import at.asit.pdfover.gui.exceptions.CantLoadKeystoreException; +import at.asit.pdfover.gui.exceptions.KeystoreAliasDoesntExistException; import at.asit.pdfover.gui.exceptions.KeystoreDoesntExistException; -import at.asit.pdfover.gui.exceptions.OutputfolderDoesntExistException; -import at.asit.pdfover.gui.exceptions.OutputfolderNotADirectoryException; import at.asit.pdfover.gui.utils.Messages; import at.asit.pdfover.gui.workflow.config.ConfigManipulator; import at.asit.pdfover.gui.workflow.config.ConfigurationContainer; @@ -89,6 +88,8 @@ public class KeystoreConfigurationComposite extends BaseConfigurationComposite { private Map keystoreTypes; private Map keystoreTypes_i; + private KeyStore ks; + /** * @param parent * @param style @@ -304,7 +305,7 @@ public class KeystoreConfigurationComposite extends BaseConfigurationComposite { this.lblKeystoreAlias.setFont(new Font(Display.getCurrent(), fD_lblKeystoreAlias[0])); - this.cmbKeystoreAlias = new Combo(this.grpKeystore, SWT.READ_ONLY); + this.cmbKeystoreAlias = new Combo(this.grpKeystore, SWT.NONE); FormData fd_cmbKeystoreAlias = new FormData(); fd_cmbKeystoreAlias.right = new FormAttachment(100, -5); fd_cmbKeystoreAlias.top = new FormAttachment(this.lblKeystoreAlias, 5); @@ -325,6 +326,13 @@ public class KeystoreConfigurationComposite extends BaseConfigurationComposite { KeystoreConfigurationComposite.this.cmbKeystoreAlias.getSelectionIndex())); } }); + this.cmbKeystoreAlias.addFocusListener(new FocusAdapter() { + @Override + public void focusLost(FocusEvent e) { + performKeystoreAliasChanged(KeystoreConfigurationComposite. + this.cmbKeystoreAlias.getText()); + } + }); this.lblKeystoreKeyPass = new Label(this.grpKeystore, SWT.NONE); FormData fd_lblKeystoreKeyPass = new FormData(); @@ -372,11 +380,11 @@ public class KeystoreConfigurationComposite extends BaseConfigurationComposite { ConfigurationContainer config = KeystoreConfigurationComposite.this.configurationContainer; File f = new File(config.getKeyStoreFile()); - KeyStore ks = KeyStore.getInstance(config.getKeyStoreType()); + this.ks = KeyStore.getInstance(config.getKeyStoreType()); FileInputStream fis = new FileInputStream(f); - ks.load(fis, config.getKeyStoreStorePass().toCharArray()); - this.cmbKeystoreAlias.removeAll(); - Enumeration aliases = ks.aliases(); + this.ks.load(fis, config.getKeyStoreStorePass().toCharArray()); + this.cmbKeystoreAlias.remove(0, this.cmbKeystoreAlias.getItemCount()-1); + Enumeration aliases = this.ks.aliases(); while (aliases.hasMoreElements()) this.cmbKeystoreAlias.add(aliases.nextElement()); } @@ -442,12 +450,7 @@ public class KeystoreConfigurationComposite extends BaseConfigurationComposite { protected void performKeystoreAliasChanged(String alias) { log.debug("Selected keystore alias: " + alias); //$NON-NLS-1$ this.configurationContainer.setKeyStoreAlias(alias); - for (int i = 0; i < this.cmbKeystoreAlias.getItemCount(); ++i) { - if (this.cmbKeystoreAlias.getItem(i).equals(alias)) { - this.cmbKeystoreAlias.select(i); - break; - } - } + this.cmbKeystoreAlias.setText(alias); } /** @@ -506,14 +509,16 @@ public class KeystoreConfigurationComposite extends BaseConfigurationComposite { @Override public void loadConfiguration() { // Initialize form fields from configuration Container - performKeystoreFileChanged( - this.configurationContainer.getKeyStoreFile()); + String ks = this.configurationContainer.getKeyStoreFile(); + performKeystoreFileChanged(ks); performKeystoreTypeChanged( this.configurationContainer.getKeyStoreType()); performKeystoreStorePassChanged( this.configurationContainer.getKeyStoreStorePass()); try { - loadKeystore(); + File ksf = new File(ks); + if (ksf.exists()) + loadKeystore(); } catch (Exception e) { log.error("Error loading keystore", e); //$NON-NLS-1$ } @@ -547,17 +552,24 @@ public class KeystoreConfigurationComposite extends BaseConfigurationComposite { public void validateSettings(int resumeFrom) throws Exception { switch (resumeFrom) { case 0: - File f = new File(this.configurationContainer.getKeyStoreFile()); + String fname = this.configurationContainer.getKeyStoreFile(); + if (fname.isEmpty()) + break; //no checks required + File f = new File(fname); if (!f.exists() || !f.isFile()) - throw new KeystoreDoesntExistException(f, 2); //skip next check + throw new KeystoreDoesntExistException(f, 3); //skip next checks // Fall through case 1: try { loadKeystore(); } catch (Exception e) { - throw new CantLoadKeystoreException(e, 2); + throw new CantLoadKeystoreException(e, 3); //skip next check } // Fall through + case 2: + String alias = this.configurationContainer.getKeyStoreAlias(); + if (!this.ks.containsAlias(alias)) + throw new KeystoreAliasDoesntExistException(alias, 3); } } diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/exceptions/KeystoreAliasDoesntExistException.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/exceptions/KeystoreAliasDoesntExistException.java new file mode 100644 index 00000000..591af5f5 --- /dev/null +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/exceptions/KeystoreAliasDoesntExistException.java @@ -0,0 +1,36 @@ +/* + * Copyright 2012 by A-SIT, Secure Information Technology Center Austria + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://joinup.ec.europa.eu/software/page/eupl + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + */ +package at.asit.pdfover.gui.exceptions; + +import at.asit.pdfover.gui.utils.Messages; + +/** + * + */ +public class KeystoreAliasDoesntExistException extends ResumableException { + /** + * + */ + private static final long serialVersionUID = 2264150118185552023L; + + /** + * @param alias The keystore key alias + * @param resumeIndex The resume Index + */ + public KeystoreAliasDoesntExistException(final String alias, int resumeIndex) { + super(String.format(Messages.getString("error.KeyStoreAlias"), alias), resumeIndex); //$NON-NLS-1$ + } +} -- cgit v1.2.3