From 307cebbdd2a0fbde3782b271ed89cd2884b3b3ea Mon Sep 17 00:00:00 2001 From: Colm O hEigeartaigh Date: Tue, 21 Jan 2025 05:09:53 +0000 Subject: [PATCH] Modernization: Replacing Paths.get with Path.of (#438) --- src/main/java/org/apache/xml/security/utils/JavaUtils.java | 6 +++--- .../utils/resolver/implementations/ResolverAnonymous.java | 2 +- .../resolver/implementations/ResolverLocalFilesystem.java | 4 ++-- .../test/dom/encryption/XMLCipherBrainpoolTest.java | 4 ++-- .../xml/security/test/dom/encryption/XMLCipherTest.java | 6 +++--- .../test/dom/encryption/XMLEncryption11BrainpoolTest.java | 4 ++-- .../security/test/dom/encryption/XMLEncryption11Test.java | 6 +++--- .../xml/security/test/dom/signature/EDDSASignatureTest.java | 6 +++--- .../test/javax/xml/crypto/dsig/EdDSATestAbstract.java | 4 ++-- .../javax/xml/crypto/dsig/SignatureValidatorEdDSATest.java | 4 ++-- .../javax/xml/crypto/dsig/XMLSignatureBrainpoolTest.java | 6 +++--- .../test/javax/xml/crypto/dsig/XMLSignatureECDSATest.java | 6 +++--- 12 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/apache/xml/security/utils/JavaUtils.java b/src/main/java/org/apache/xml/security/utils/JavaUtils.java index 352b5927c..dfe893415 100644 --- a/src/main/java/org/apache/xml/security/utils/JavaUtils.java +++ b/src/main/java/org/apache/xml/security/utils/JavaUtils.java @@ -26,7 +26,7 @@ import java.lang.System.Logger.Level; import java.lang.reflect.InvocationTargetException; import java.nio.file.Files; -import java.nio.file.Paths; +import java.nio.file.Path; import java.security.SecurityPermission; /** @@ -56,7 +56,7 @@ private JavaUtils() { @Deprecated(forRemoval = true, since = "4.0.0") public static byte[] getBytesFromFile(String filePath) throws FileNotFoundException, IOException { - try (InputStream inputStream = Files.newInputStream(Paths.get(filePath)); + try (InputStream inputStream = Files.newInputStream(Path.of(filePath)); UnsyncByteArrayOutputStream baos = new UnsyncByteArrayOutputStream()) { byte[] buf = new byte[8_192]; int len; @@ -77,7 +77,7 @@ public static byte[] getBytesFromFile(String filePath) */ public static void writeBytesToFilename(String filename, byte[] bytes) { if (filename != null && bytes != null) { - try (OutputStream outputStream = Files.newOutputStream(Paths.get(filename))) { + try (OutputStream outputStream = Files.newOutputStream(Path.of(filename))) { outputStream.write(bytes); } catch (IOException ex) { LOG.log(Level.DEBUG, ex.getMessage(), ex); diff --git a/src/main/java/org/apache/xml/security/utils/resolver/implementations/ResolverAnonymous.java b/src/main/java/org/apache/xml/security/utils/resolver/implementations/ResolverAnonymous.java index 16b87128a..e273705b1 100644 --- a/src/main/java/org/apache/xml/security/utils/resolver/implementations/ResolverAnonymous.java +++ b/src/main/java/org/apache/xml/security/utils/resolver/implementations/ResolverAnonymous.java @@ -40,7 +40,7 @@ public class ResolverAnonymous extends ResourceResolverSpi { * @throws IOException */ public ResolverAnonymous(String filename) throws IOException { - this(Paths.get(filename)); + this(Path.of(filename)); } /** diff --git a/src/main/java/org/apache/xml/security/utils/resolver/implementations/ResolverLocalFilesystem.java b/src/main/java/org/apache/xml/security/utils/resolver/implementations/ResolverLocalFilesystem.java index ccf056476..f59c2983f 100644 --- a/src/main/java/org/apache/xml/security/utils/resolver/implementations/ResolverLocalFilesystem.java +++ b/src/main/java/org/apache/xml/security/utils/resolver/implementations/ResolverLocalFilesystem.java @@ -22,7 +22,7 @@ import java.lang.System.Logger.Level; import java.net.URI; import java.net.URISyntaxException; -import java.nio.file.Paths; +import java.nio.file.Path; import org.apache.xml.security.signature.XMLSignatureInput; import org.apache.xml.security.signature.XMLSignatureFileInput; @@ -46,7 +46,7 @@ public XMLSignatureInput engineResolveURI(ResourceResolverContext context) try { // calculate new URI URI uriNew = getNewURI(context.uriToResolve, context.baseUri); - XMLSignatureInput result = new XMLSignatureFileInput(Paths.get(uriNew)); + XMLSignatureInput result = new XMLSignatureFileInput(Path.of(uriNew)); result.setSecureValidation(context.secureValidation); result.setSourceURI(uriNew.toString()); return result; diff --git a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherBrainpoolTest.java b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherBrainpoolTest.java index f4dbac9c8..753caa95e 100644 --- a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherBrainpoolTest.java +++ b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherBrainpoolTest.java @@ -28,7 +28,7 @@ import java.lang.reflect.Constructor; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.Paths; +import java.nio.file.Path; import java.security.Key; import java.security.KeyPair; import java.security.NoSuchAlgorithmException; @@ -208,7 +208,7 @@ void testAES128ElementEcdhEsKWCipher(KeyUtils.KeyType keyType) throws Exception ed = cipherEncData.doFinal(d, e); - Files.write(Paths.get("target","test-enc-"+keyType.name()+".xml"), toString(ed).getBytes()); + Files.write(Path.of("target","test-enc-"+keyType.name()+".xml"), toString(ed).getBytes()); //decrypt ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0); diff --git a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java index 4351533ff..8e541b51e 100644 --- a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java +++ b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java @@ -29,7 +29,7 @@ import java.lang.System.Logger.Level; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.Paths; +import java.nio.file.Path; import java.security.*; import java.security.spec.AlgorithmParameterSpec; @@ -377,7 +377,7 @@ void testAES128ElementEcdhEsKWCipher(KeyUtils.KeyType keyType) throws Exception ed = cipherEncData.doFinal(d, e); - Files.write(Paths.get("target","test-enc-"+keyType.name()+".xml"), toString(ed).getBytes()); + Files.write(Path.of("target","test-enc-"+keyType.name()+".xml"), toString(ed).getBytes()); //decrypt ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0); @@ -478,7 +478,7 @@ void testAES128ElementEcdhEsKWCipherHKDF(KeyUtils.KeyType keyType) throws Except org.apache.xml.security.test.javax.xml.crypto.dsig.TestUtils.validateSecurityOrEncryptionElement(ed.getDocumentElement()); - Files.write(Paths.get("target", "test-ka-dh-hkdf-" + keyType.name() + ".xml"), toString(ed).getBytes()); + Files.write(Path.of("target", "test-ka-dh-hkdf-" + keyType.name() + ".xml"), toString(ed).getBytes()); //decrypt ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0); diff --git a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLEncryption11BrainpoolTest.java b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLEncryption11BrainpoolTest.java index 409b87c92..ecfe56556 100644 --- a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLEncryption11BrainpoolTest.java +++ b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLEncryption11BrainpoolTest.java @@ -20,7 +20,7 @@ import java.io.ByteArrayInputStream; import java.nio.file.Files; -import java.nio.file.Paths; +import java.nio.file.Path; import java.security.Key; import java.security.KeyStore; import java.security.PrivateKey; @@ -145,7 +145,7 @@ void testAgreementKeyEncryptDecryptDataWithBrainpool(String w3cTag, ); if (LOG.isLoggable(System.Logger.Level.DEBUG)) { - Files.write(Paths.get("target","test-enc-"+w3cTag+".xml"), toString(doc.getFirstChild()).getBytes()); + Files.write(Path.of("target","test-enc-"+w3cTag+".xml"), toString(doc.getFirstChild()).getBytes()); XMLUtils.outputDOM(doc.getFirstChild(), System.out); } // Perform decryption diff --git a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLEncryption11Test.java b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLEncryption11Test.java index 61068afe9..d47d0551d 100644 --- a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLEncryption11Test.java +++ b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLEncryption11Test.java @@ -21,7 +21,7 @@ import java.io.*; import java.lang.System.Logger.Level; import java.nio.file.Files; -import java.nio.file.Paths; +import java.nio.file.Path; import java.security.Key; import java.security.KeyStore; import java.security.PrivateKey; @@ -522,7 +522,7 @@ void testAgreementKeyEncryptDecryptDocument(String w3cTag, encryptionAlgorithm ); - Files.write(Paths.get("target","test-enc-"+w3cTag+".xml"), toString(doc.getFirstChild()).getBytes()); + Files.write(Path.of("target","test-enc-"+w3cTag+".xml"), toString(doc.getFirstChild()).getBytes()); // XMLUtils.outputDOM(doc.getFirstChild(), System.out); // Perform decryption @@ -627,7 +627,7 @@ void testAgreementKeyEncryptDecryptData(String w3cTag, new ByteArrayInputStream(testData) ); - Files.write(Paths.get("target","test-enc-"+w3cTag+".xml"), toString(doc.getFirstChild()).getBytes()); + Files.write(Path.of("target","test-enc-"+w3cTag+".xml"), toString(doc.getFirstChild()).getBytes()); // Perform decryption byte[] result = decryptData(doc, ecKey, (X509Certificate)cert); // XMLUtils.outputDOM(dd.getFirstChild(), System.out); diff --git a/src/test/java/org/apache/xml/security/test/dom/signature/EDDSASignatureTest.java b/src/test/java/org/apache/xml/security/test/dom/signature/EDDSASignatureTest.java index 7a3b49c0b..41496b6e1 100644 --- a/src/test/java/org/apache/xml/security/test/dom/signature/EDDSASignatureTest.java +++ b/src/test/java/org/apache/xml/security/test/dom/signature/EDDSASignatureTest.java @@ -22,7 +22,7 @@ import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.nio.file.Files; -import java.nio.file.Paths; +import java.nio.file.Path; import java.security.KeyStore; import java.security.PrivateKey; import java.security.PublicKey; @@ -63,7 +63,7 @@ class EDDSASignatureTest extends EdDSATestAbstract { void testEd22519() throws Exception { Assumptions.assumeTrue(isEdDSASupported()); KeyStore keyStore = KeyStore.getInstance(EDDSA_KS_TYPE); - keyStore.load(Files.newInputStream(Paths.get(EDDSA_KS)), EDDSA_KS_PASSWORD.toCharArray()); + keyStore.load(Files.newInputStream(Path.of(EDDSA_KS)), EDDSA_KS_PASSWORD.toCharArray()); PrivateKey privateKey = (PrivateKey) keyStore.getKey("Ed25519", EDDSA_KS_PASSWORD.toCharArray()); @@ -93,7 +93,7 @@ void testEd448VerifyXML() throws Exception { void testEd448() throws Exception { Assumptions.assumeTrue(isEdDSASupported()); KeyStore keyStore = KeyStore.getInstance(EDDSA_KS_TYPE); - keyStore.load(Files.newInputStream(Paths.get(EDDSA_KS)), EDDSA_KS_PASSWORD.toCharArray()); + keyStore.load(Files.newInputStream(Path.of(EDDSA_KS)), EDDSA_KS_PASSWORD.toCharArray()); PrivateKey privateKey = (PrivateKey) keyStore.getKey("Ed448", EDDSA_KS_PASSWORD.toCharArray()); diff --git a/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/EdDSATestAbstract.java b/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/EdDSATestAbstract.java index 344e101c8..e37c09e2c 100644 --- a/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/EdDSATestAbstract.java +++ b/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/EdDSATestAbstract.java @@ -19,7 +19,7 @@ package org.apache.xml.security.test.javax.xml.crypto.dsig; import java.nio.file.Files; -import java.nio.file.Paths; +import java.nio.file.Path; import java.security.KeyStore; import java.security.Provider; import java.security.Security; @@ -83,7 +83,7 @@ public static void afterAll() { @Override KeyStore getKeyStore() throws Exception { KeyStore keyStore = KeyStore.getInstance(EDDSA_KS_TYPE); - keyStore.load(Files.newInputStream(Paths.get(EDDSA_KS)), EDDSA_KS_PASSWORD.toCharArray()); + keyStore.load(Files.newInputStream(Path.of(EDDSA_KS)), EDDSA_KS_PASSWORD.toCharArray()); return keyStore; } diff --git a/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/SignatureValidatorEdDSATest.java b/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/SignatureValidatorEdDSATest.java index 323084370..8f16e3c2a 100644 --- a/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/SignatureValidatorEdDSATest.java +++ b/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/SignatureValidatorEdDSATest.java @@ -19,7 +19,7 @@ package org.apache.xml.security.test.javax.xml.crypto.dsig; -import java.nio.file.Paths; +import java.nio.file.Path; import javax.xml.crypto.dsig.dom.DOMValidateContext; @@ -43,7 +43,7 @@ class SignatureValidatorEdDSATest extends EdDSATestAbstract { public void before() { String base = System.getProperty("basedir", "./"); testInstance = new SignatureValidator( - Paths.get(base, "src", "test", "resources", "javax", "xml", "crypto", "dsig", "eddsa").toFile()); + Path.of(base, "src", "test", "resources", "javax", "xml", "crypto", "dsig", "eddsa").toFile()); } /** diff --git a/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/XMLSignatureBrainpoolTest.java b/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/XMLSignatureBrainpoolTest.java index 3bf646a1f..11b61d280 100644 --- a/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/XMLSignatureBrainpoolTest.java +++ b/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/XMLSignatureBrainpoolTest.java @@ -20,7 +20,7 @@ import java.io.IOException; import java.nio.file.Files; -import java.nio.file.Paths; +import java.nio.file.Path; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; @@ -72,7 +72,7 @@ static void removeProvider() { @Override KeyStore getKeyStore() throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException { KeyStore keyStore = KeyStore.getInstance(ECDSA_KS_TYPE); - keyStore.load(Files.newInputStream(Paths.get(ECDSA_KS_PATH)), ECDSA_KS_PASSWORD.toCharArray()); + keyStore.load(Files.newInputStream(Path.of(ECDSA_KS_PATH)), ECDSA_KS_PASSWORD.toCharArray()); return keyStore; } @@ -89,7 +89,7 @@ char[] getKeyPassword() { void createEdDSASignatureTest(String signatureAlgorithm, String alias) throws Exception { byte[] buff = doSignWithJcpApi(signatureAlgorithm, alias, true); if (LOG.isLoggable(System.Logger.Level.DEBUG)) { - Files.write(Paths.get("target","test-sign-"+alias+".xml"), buff); + Files.write(Path.of("target","test-sign-"+alias+".xml"), buff); } Assertions.assertNotNull(buff); assertValidSignatureWithJcpApi(buff, true); diff --git a/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/XMLSignatureECDSATest.java b/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/XMLSignatureECDSATest.java index 5b82bdcfe..9e76f990b 100644 --- a/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/XMLSignatureECDSATest.java +++ b/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/XMLSignatureECDSATest.java @@ -20,7 +20,7 @@ import java.io.IOException; import java.nio.file.Files; -import java.nio.file.Paths; +import java.nio.file.Path; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; @@ -75,7 +75,7 @@ static void removeProvider() { @Override KeyStore getKeyStore() throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException { KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE); - keyStore.load(Files.newInputStream(Paths.get(KEYSTORE_PATH)), KEYSTORE_AND_KEY_PASSWORD.toCharArray()); + keyStore.load(Files.newInputStream(Path.of(KEYSTORE_PATH)), KEYSTORE_AND_KEY_PASSWORD.toCharArray()); return keyStore; } @@ -108,7 +108,7 @@ void createECDSASignatureTest(String signatureAlgorithmURI, String alias) throws byte[] buff = doSignWithJcpApi(signatureAlgorithmURI, alias, false); if (LOG.isLoggable(System.Logger.Level.DEBUG)) { - Files.write(Paths.get("target", "test-ecdsa-" + jceAlg + ".xml"), buff); + Files.write(Path.of("target", "test-ecdsa-" + jceAlg + ".xml"), buff); } Assertions.assertNotNull(buff); assertValidSignatureWithJcpApi(buff, false);