This is an automated email from the ASF dual-hosted git repository.

schultz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 79a0f7e9ddf9733bbb5a43c525703731436b74db
Author: Christopher Schultz <ch...@christopherschultz.net>
AuthorDate: Wed Oct 18 21:31:50 2023 -0400

    Add a "chomp" capability to optionally remove a trailing newline from a 
file-based value.
---
 .../digester/ServiceBindingPropertySource.java     | 33 +++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git 
a/java/org/apache/tomcat/util/digester/ServiceBindingPropertySource.java 
b/java/org/apache/tomcat/util/digester/ServiceBindingPropertySource.java
index 1ae9528609..89617c9cfb 100644
--- a/java/org/apache/tomcat/util/digester/ServiceBindingPropertySource.java
+++ b/java/org/apache/tomcat/util/digester/ServiceBindingPropertySource.java
@@ -39,6 +39,7 @@ import org.apache.tomcat.util.IntrospectionUtils;
  *                                            /keyFile
  *                                            /file
  *                                            /chainFile
+ *                                            /keyPassword
  * </pre>
  * <pre>
  *   {@code
@@ -46,10 +47,19 @@ import org.apache.tomcat.util.IntrospectionUtils;
  *           <Certificate certificateKeyFile="${custom-certificate.keyFile}"
  *                        certificateFile="${custom-certificate.file}"
  *                        
certificateChainFile="${custom-certificate.chainFile}"
+ *                        
certificateKeyPassword="${chomp:custom-certificate.keyPassword}"
  *                        type="RSA" />
  *     </SSLHostConfig> }
  * </pre>
  *
+ * <p>
+ *   The optional <code>chomp:</code> prefix will cause the 
ServiceBindingPropertySource
+ *   to trim a single newline (<code>\r\n</code>, <code>\r</code>, or 
<code>\n</code>)
+ *   from the end of the file, if it exists. This is a convenience for 
hand-edited
+ *   files/values where removing a trailing newline is difficult, and trailing
+ *   whitespace changes the meaning of the value.
+ * </p>
+ *
  * How to configure:
  * <pre>
  * {@code
@@ -82,6 +92,12 @@ public class ServiceBindingPropertySource implements 
IntrospectionUtils.Property
             return null;
         }
 
+        boolean chomp = false;
+        if (key.startsWith("chomp:")) {
+            chomp = true;
+            key = key.substring(6); // Remove the "chomp:" prefix
+        }
+
         // we expect the keys to be in the format 
$SERVICE_BINDING_ROOT/<binding-name>/<key>
         String[] parts = key.split("\\.");
         if (parts.length != 2) {
@@ -95,7 +111,22 @@ public class ServiceBindingPropertySource implements 
IntrospectionUtils.Property
         }
 
         try {
-            return new String(Files.readAllBytes(path));
+            byte[] bytes = Files.readAllBytes(path);
+
+            int length = bytes.length;
+
+            if (chomp) {
+                if(length > 1 && bytes[length - 2] == '\r' && bytes[length - 
2] == '\n') {
+                    length -= 2;
+                } else if (length > 0) {
+                    byte c = bytes[length - 1];
+                    if (c == '\r' || c == '\n') {
+                        length -= 1;
+                    }
+                }
+            }
+
+            return new String(bytes, 0, length);
         } catch (IOException e) {
             return null;
         }


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to