From c383796741a3d9601281d7635dc6ae0e8d238af7 Mon Sep 17 00:00:00 2001 From: "bright.ma RMSH06" Date: Sun, 11 Apr 2021 12:08:05 +0800 Subject: [PATCH 1/6] java: add param encoding when read Properties file Signed-off-by: bright.ma RMSH06 --- .../utility/steps/conf/ReadPropertiesStep.java | 11 +++++++++++ .../steps/conf/ReadPropertiesStepExecution.java | 8 +++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep.java b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep.java index 29cf51fb..bfab5e8f 100644 --- a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep.java +++ b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep.java @@ -26,6 +26,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; import hudson.Extension; +import hudson.Util; import org.jenkinsci.plugins.pipeline.utility.steps.AbstractFileOrTextStep; import org.jenkinsci.plugins.pipeline.utility.steps.AbstractFileOrTextStepDescriptorImpl; import org.jenkinsci.plugins.workflow.steps.StepContext; @@ -43,6 +44,7 @@ public class ReadPropertiesStep extends AbstractFileOrTextStep { private Map defaults; private boolean interpolate; + private String encoding; @DataBoundConstructor public ReadPropertiesStep() { @@ -93,6 +95,15 @@ public void setInterpolate(Boolean interpolate) { this.interpolate = interpolate; } + public String getEncoding() { + return encoding; + } + + @DataBoundSetter + public void setEncoding(String encoding) { + this.encoding = Util.fixEmptyAndTrim(encoding); + } + @Extension public static class DescriptorImpl extends AbstractFileOrTextStepDescriptorImpl { public DescriptorImpl() { diff --git a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepExecution.java b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepExecution.java index 1cd9bdff..8425cb72 100644 --- a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepExecution.java +++ b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepExecution.java @@ -42,6 +42,8 @@ import java.io.InputStream; import java.io.PrintStream; import java.io.StringReader; +import java.io.BufferedReader; +import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -84,7 +86,11 @@ protected Map doRun() throws Exception { FilePath f = ws.child(step.getFile()); if (f.exists() && !f.isDirectory()) { try(InputStream is = f.read()){ - properties.load(is); + if(StringUtils.isEmpty(step.getEncoding())){ + properties.load(is); + } else { + properties.load( new BufferedReader(new InputStreamReader(is, step.getEncoding()))); + } } } else if (f.isDirectory()) { logger.print("warning: "); From 799d14083800e019040af7756f484f91a17bc252 Mon Sep 17 00:00:00 2001 From: "bright.ma" Date: Mon, 10 May 2021 08:50:56 +0800 Subject: [PATCH 2/6] java: fixup add try block to close the IO streams or it will break things, especially on windows. Change-Id: Icc7a9a393d4fae9d546239f3545caf256f3a5b78 Signed-off-by: bright.ma --- .../utility/steps/conf/ReadPropertiesStepExecution.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepExecution.java b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepExecution.java index 8425cb72..ef491b25 100644 --- a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepExecution.java +++ b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepExecution.java @@ -89,7 +89,10 @@ protected Map doRun() throws Exception { if(StringUtils.isEmpty(step.getEncoding())){ properties.load(is); } else { - properties.load( new BufferedReader(new InputStreamReader(is, step.getEncoding()))); + try(InputStreamReader isr = new InputStreamReader(is, step.getEncoding()); + BufferedReader br = new BufferedReader(isr)) { + properties.load(br); + } } } } else if (f.isDirectory()) { From 8d1d51cdbeabd3cae8151cfd116faf1d86e106ff Mon Sep 17 00:00:00 2001 From: "bright.ma" Date: Thu, 3 Nov 2022 13:52:52 +0800 Subject: [PATCH 3/6] java: rename var name to charset for ReadPropertiesStep Keep the style same as WriteYamlStep --- .../utility/steps/conf/ReadPropertiesStep.java | 15 +++++++++------ .../steps/conf/ReadPropertiesStepExecution.java | 5 ++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep.java b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep.java index bfab5e8f..49d4a603 100644 --- a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep.java +++ b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep.java @@ -26,7 +26,6 @@ import edu.umd.cs.findbugs.annotations.NonNull; import hudson.Extension; -import hudson.Util; import org.jenkinsci.plugins.pipeline.utility.steps.AbstractFileOrTextStep; import org.jenkinsci.plugins.pipeline.utility.steps.AbstractFileOrTextStepDescriptorImpl; import org.jenkinsci.plugins.workflow.steps.StepContext; @@ -44,7 +43,7 @@ public class ReadPropertiesStep extends AbstractFileOrTextStep { private Map defaults; private boolean interpolate; - private String encoding; + private String charset; @DataBoundConstructor public ReadPropertiesStep() { @@ -95,13 +94,17 @@ public void setInterpolate(Boolean interpolate) { this.interpolate = interpolate; } - public String getEncoding() { - return encoding; + public String getCharset() { + return charset; } + /** + * The charset encoding to use when read the properties file. Defaults to ISO 8859-1 . + * @param charset the charset + */ @DataBoundSetter - public void setEncoding(String encoding) { - this.encoding = Util.fixEmptyAndTrim(encoding); + public void setCharset(String charset) { + this.charset = charset; } @Extension diff --git a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepExecution.java b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepExecution.java index ef491b25..e5a786fb 100644 --- a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepExecution.java +++ b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepExecution.java @@ -86,11 +86,10 @@ protected Map doRun() throws Exception { FilePath f = ws.child(step.getFile()); if (f.exists() && !f.isDirectory()) { try(InputStream is = f.read()){ - if(StringUtils.isEmpty(step.getEncoding())){ + if (StringUtils.isEmpty(step.getCharset())) { properties.load(is); } else { - try(InputStreamReader isr = new InputStreamReader(is, step.getEncoding()); - BufferedReader br = new BufferedReader(isr)) { + try (InputStreamReader isr = new InputStreamReader(is, step.getCharset()); BufferedReader br = new BufferedReader(isr)) { properties.load(br); } } From faf8e8b6f8f43343658fdd7d2ec1e9a8bf028e44 Mon Sep 17 00:00:00 2001 From: "bright.ma" Date: Thu, 3 Nov 2022 14:26:01 +0800 Subject: [PATCH 4/6] resources: update ReadPropertiesStep config.jelly for snippet generator resources: update ReadPropertiesStep config.jelly for snippet generator --- .../steps/conf/ReadPropertiesStep/config.jelly | 12 +++++++++--- .../steps/conf/ReadPropertiesStep/config.properties | 7 +++++++ .../utility/steps/conf/ReadPropertiesStep/help.html | 10 ++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/config.properties diff --git a/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/config.jelly b/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/config.jelly index efd97998..e63a96f1 100644 --- a/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/config.jelly +++ b/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/config.jelly @@ -24,7 +24,13 @@ --> - -

This is a special step. No snippet generation available. See inline help for more information.

-
+ + + + + + + + +
diff --git a/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/config.properties b/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/config.properties new file mode 100644 index 00000000..3f8ab258 --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/config.properties @@ -0,0 +1,7 @@ +file.title=File +file.description=(Optional) path to a file in the workspace to read the properties from +text.title=Text +text.description=An Optional String containing properties formatted data + +interpolate.title=Interpolate +interpolate.description=Flag to indicate if the properties should be interpolated or not. \ No newline at end of file diff --git a/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/help.html b/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/help.html index 8719e130..6ebc89bd 100644 --- a/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/help.html +++ b/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/help.html @@ -59,6 +59,10 @@ In case of error or cyclic dependencies, the original properties will be returned. +
  • + charset: + What charset to use when read properties file. Default is empty, then will use jvm ISO 8859-1. +
  • Example:
    @@ -82,4 +86,10 @@ assert props.fullUrl = 'http://localhost/README.txt' + Example with charset: + +

    +        def props = readProperties charset: 'UTF-8', file: 'test.properties'
    +        
    +

    From 29963d168d29c4342c922f0c3fc8eb94f7fb1d88 Mon Sep 17 00:00:00 2001 From: "bright.ma" Date: Thu, 3 Nov 2022 15:18:48 +0800 Subject: [PATCH 5/6] test: add test ReadPropertiesStep with charset --- .../steps/conf/ReadPropertiesStepTest.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/test/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepTest.java b/src/test/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepTest.java index c5e575a8..66f355ea 100644 --- a/src/test/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepTest.java +++ b/src/test/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepTest.java @@ -114,6 +114,30 @@ public void readFileWithDefaults() throws Exception { j.assertBuildStatusSuccess(p.scheduleBuild2(0)); } + @Test + public void readFileWithCharset() throws Exception { + Properties props = new Properties(); + props.setProperty("test", "One"); + props.setProperty("another", "Two"); + props.setProperty("zh", "中文"); + File file = temp.newFile(); + try (FileWriter f = new FileWriter(file)) { + props.store(f, "Pipeline test"); + } + + WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); + p.setDefinition(new CpsFlowDefinition( + "node('slaves') {\n" + + " def props = readProperties charset: 'utf-8', file: '" + separatorsToSystemEscaped(file.getAbsolutePath()) + "'\n" + + " assert props['test'] == 'One'\n" + + " assert props['another'] == 'Two'\n" + + " assert props.test == 'One'\n" + + " assert props.another == 'Two'\n" + + " assert props.zh == '中文' \n" + + "}", true)); + j.assertBuildStatusSuccess(p.scheduleBuild2(0)); + } + @Test public void readText() throws Exception { Properties props = new Properties(); From de2145ab4280112ada3facc7f773d6ea711a496e Mon Sep 17 00:00:00 2001 From: "bright.ma" Date: Mon, 25 Mar 2024 23:02:49 +0800 Subject: [PATCH 6/6] resources: add charset entry Change-Id: I5e2952cd3c4c92f7966bb7bc3be47a7042af27f3 --- .../utility/steps/conf/ReadPropertiesStep/config.jelly | 3 +++ .../utility/steps/conf/ReadPropertiesStep/config.properties | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/config.jelly b/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/config.jelly index e63a96f1..8445cb70 100644 --- a/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/config.jelly +++ b/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/config.jelly @@ -33,4 +33,7 @@ + + + diff --git a/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/config.properties b/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/config.properties index 3f8ab258..4e395452 100644 --- a/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/config.properties +++ b/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStep/config.properties @@ -2,6 +2,7 @@ file.title=File file.description=(Optional) path to a file in the workspace to read the properties from text.title=Text text.description=An Optional String containing properties formatted data - interpolate.title=Interpolate -interpolate.description=Flag to indicate if the properties should be interpolated or not. \ No newline at end of file +interpolate.description=Flag to indicate if the properties should be interpolated or not. +charset.title=Charset +charset.description=(Optional) What charset to use when read the properties file. Default is empty, then will use jvm ISO 8859-1.