changeset 1277:c4e9fb236625

Add -defaultfile switch to PolicyEditor 2015-07-30 Andrew Azores <aazores@redhat.com> Add -defaultfile switch to PolicyEditor * NEWS: add note about defaultfile flag * netx/net/sourceforge/jnlp/OptionsDefinitions.java (OPTIONS, getPolicyEditorOptions): add DEFAULTFILE for PolicyEditor * netx/net/sourceforge/jnlp/resources/Messages.properties (PBODefaultFile, PEDefaultFileFilePathSpecifiedError, PEMainArgAndFileSwitchSpecifiedError): new messages * netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java (main): add handling for -defaultfile, die when both -defaultfile and -file are given (openDefaultButtonAction): extract method (getDefaultPolicyFilePath): new method extracted from openDefaultButtonAction (getFilePathArgument): new method (cleanFilePathArgument): new method
author Andrew Azores <aazores@redhat.com>
date Thu, 30 Jul 2015 12:37:24 -0400
parents e09b5c8bf3cb
children efb1342a55aa
files ChangeLog NEWS netx/net/sourceforge/jnlp/OptionsDefinitions.java netx/net/sourceforge/jnlp/resources/Messages.properties netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java
diffstat 5 files changed, 68 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Jul 30 10:57:21 2015 -0400
+++ b/ChangeLog	Thu Jul 30 12:37:24 2015 -0400
@@ -1,3 +1,22 @@
+2015-07-30  Andrew Azores  <aazores@redhat.com>
+
+	Add -defaultfile switch to PolicyEditor
+	* NEWS: add note about defaultfile flag
+	* netx/net/sourceforge/jnlp/OptionsDefinitions.java (OPTIONS,
+	getPolicyEditorOptions): add DEFAULTFILE for
+	PolicyEditor
+	* netx/net/sourceforge/jnlp/resources/Messages.properties (PBODefaultFile,
+	PEDefaultFileFilePathSpecifiedError, PEMainArgAndFileSwitchSpecifiedError):
+	new messages
+	* netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java
+	(main): add handling for -defaultfile, die when both -defaultfile and
+	-file are given
+	(openDefaultButtonAction): extract method
+	(getDefaultPolicyFilePath): new method extracted from
+	openDefaultButtonAction
+	(getFilePathArgument): new method
+	(cleanFilePathArgument): new method
+
 2015-07-29  Jiri Vanek  <jvanek@redhat.com>
 
 	Added tests for single signed jar with manifest containing trusted-only
--- a/NEWS	Thu Jul 30 10:57:21 2015 -0400
+++ b/NEWS	Thu Jul 30 12:37:24 2015 -0400
@@ -30,6 +30,7 @@
   - fixed issue with -html receiving garbage in width and height
 * PolicyEditor
   - file flag made to work when used standalone
+  - defaultfile flag added
   - support for SignedBy and Principals along with existing Codebase
 
 New in release 1.6 (2015-XX-XX):
--- a/netx/net/sourceforge/jnlp/OptionsDefinitions.java	Thu Jul 30 10:57:21 2015 -0400
+++ b/netx/net/sourceforge/jnlp/OptionsDefinitions.java	Thu Jul 30 12:37:24 2015 -0400
@@ -86,6 +86,7 @@
         //policyeditor
         //-help
         FILE("-file", "policy_file", "PBOFile", NumberOfArguments.ONE),
+        DEFAULTFILE("-defaultfile", "PBODefaultFile"),
         CODEBASE("-codebase", "url", "PBOCodebase", NumberOfArguments.ONE),
         SIGNEDBY("-signedby", "certificate_alias", "PBOSignedBy", NumberOfArguments.ONE),
         PRINCIPALS("-principals", "class_name principal_name", "PBOPrincipals", NumberOfArguments.EVEN_NUMBER_SUPPORTS_EQUALS_CHAR);
@@ -176,6 +177,7 @@
         return Arrays.asList(new OPTIONS[]{
             OPTIONS.HELP1,
             OPTIONS.FILE,
+            OPTIONS.DEFAULTFILE,
             OPTIONS.CODEBASE,
             OPTIONS.SIGNEDBY,
             OPTIONS.PRINCIPALS,
--- a/netx/net/sourceforge/jnlp/resources/Messages.properties	Thu Jul 30 10:57:21 2015 -0400
+++ b/netx/net/sourceforge/jnlp/resources/Messages.properties	Thu Jul 30 12:37:24 2015 -0400
@@ -357,6 +357,8 @@
 
 PBOFile=Specifies a policy file path to open. If exactly one argument is given, and it is not this flag, it is interpreted as a file path to open, as if this flag was given first. This flag exists \
 mostly for compatibility with Policy Tool.
+PBODefaultFile=Specifies that the default user-level policy file should be opened. This is the file which is normally used by IcedTea-Web to make decisions about custom policies and permissions \
+for applets at runtime, unless configured otherwise.
 PBOCodebase=Specifies an applet codebase URL. This can be used with the other selector options to select a policy entry upon opening the editor; \
 if no such identifier exists then it will be created and then selected.
 PBOSignedBy=Specifies a certificate alias for a certificate stored in the keystore. This can be used with the other selector options to select a policy entry upon opening the editor; \
@@ -776,6 +778,8 @@
 PEInvalidIdentifier=Please fill in/modify at least one of the fields.
 PEIdentifierMatchesAll=Please fill in/modify at least one of the fields.
 PEClipboardAccessError=Could not read from clipboard
+PEDefaultFileFilePathSpecifiedError=Either -file (or simply a main argument) or -defaultfile may be specified, but not both
+PEMainArgAndFileSwitchSpecifiedError=Either -file may be specified or a main argument may be specified, but not both
 
 PEHelpMenu=Help
 PEAboutPolicyEditorItem=About PolicyEditor
--- a/netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java	Thu Jul 30 10:57:21 2015 -0400
+++ b/netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java	Thu Jul 30 12:37:24 2015 -0400
@@ -356,7 +356,7 @@
                     return;
                 }
                 try {
-                    PolicyEditor.this.setFile(new File(new URI(PathsAndFiles.JAVA_POLICY.getFullPath())).getAbsolutePath());
+                    PolicyEditor.this.setFile(getDefaultPolicyFilePath());
                     PolicyEditor.this.getFile().createNewFile();
                 } catch (final IOException | URISyntaxException e) {
                     OutputController.getLogger().log(e);
@@ -516,6 +516,10 @@
         setupLayout();
     }
 
+    private static String getDefaultPolicyFilePath() throws URISyntaxException {
+        return new File(new URI(PathsAndFiles.JAVA_POLICY.getFullPath())).getAbsolutePath();
+    }
+
     private void addDefaultAllAppletsIdentifier() {
         addNewEntry(PolicyIdentifier.ALL_APPLETS_IDENTIFIER);
     }
@@ -1758,14 +1762,7 @@
         SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
-                String filepath = optionParser.getParam(OptionsDefinitions.OPTIONS.FILE);
-                if (filepath == null || filepath.isEmpty() || filepath.trim().isEmpty()) {
-                    // maybe the user just forgot the -file flag, so try to open anyway
-                    filepath = optionParser.getMainArg();
-                }
-                if (filepath == null || filepath.isEmpty() || filepath.trim().isEmpty()) {
-                    filepath = null;
-                }
+                final String filepath = getFilePathArgument(optionParser);
                 final PolicyEditorWindow frame = getPolicyEditorFrame(filepath);
                 final String codebase = optionParser.getParam(OptionsDefinitions.OPTIONS.CODEBASE);
                 final String signedBy = optionParser.getParam(OptionsDefinitions.OPTIONS.SIGNEDBY);
@@ -1781,6 +1778,42 @@
         });
     }
 
+    private static String getFilePathArgument(OptionParser optionParser) {
+        final boolean openDefaultFile = optionParser.hasOption(OptionsDefinitions.OPTIONS.DEFAULTFILE);
+        final boolean hasFileArgument = optionParser.hasOption(OptionsDefinitions.OPTIONS.FILE);
+        final boolean hasMainArgument = optionParser.mainArgExists();
+        if ((hasFileArgument && openDefaultFile) || (hasMainArgument && openDefaultFile)) {
+            throw new IllegalArgumentException(R("PEDefaultFileFilePathSpecifiedError"));
+        } else if (hasFileArgument && hasMainArgument) {
+            throw new IllegalArgumentException(R("PEMainArgAndFileSwitchSpecifiedError"));
+        }
+
+        String filepath = null;
+        if (hasFileArgument) {
+            filepath = cleanFilePathArgument(optionParser.getParam(OptionsDefinitions.OPTIONS.FILE));
+        } else if (hasMainArgument) {
+            filepath = cleanFilePathArgument(optionParser.getMainArg());
+        } else if (openDefaultFile) {
+            try {
+                filepath = getDefaultPolicyFilePath();
+            } catch (URISyntaxException e) {
+                OutputController.getLogger().log(e);
+                throw new RuntimeException(e);
+            }
+        }
+        return filepath;
+    }
+
+    private static String cleanFilePathArgument(String filepath) {
+        if (filepath == null) {
+            return null;
+        } else if (filepath.isEmpty() || filepath.trim().isEmpty()) {
+            return null;
+        } else {
+            return filepath;
+        }
+    }
+
     /**
      * Create a new PolicyEditor instance without passing argv. The returned instance is not
      * yet set visible.