Skip to content

Commit d542254

Browse files
committed
enhancing the validate path function
1 parent 6b8ce2c commit d542254

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

src/main/java/utilities/pgpBatchUpload/BatchUploadUtility.java

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import java.io.FileInputStream;
66
import java.io.IOException;
77
import java.io.InputStream;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
811
import java.security.PrivateKey;
912
import java.security.cert.CertificateException;
1013
import java.security.cert.CertificateFactory;
@@ -56,7 +59,7 @@ public static X509Certificate loadCertificateFromPemFile(String certFilePath) th
5659
* @throws IllegalArgumentException If the file path is null or empty.
5760
*/
5861
public static PGPPublicKey readPGPPublicKey(String filePath) throws IOException, PGPException {
59-
validatePath(filePath, "pgp public key path");
62+
validatePathAndFile(filePath, "pgp public key path");
6063
logger.debug("Reading pgp public key from file: {}", filePath);
6164
try (InputStream keyIn = new BufferedInputStream(new FileInputStream(filePath))) {
6265
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(keyIn),
@@ -109,9 +112,9 @@ public static void validateBatchApiJKSInputs(File inputFile, String environmentH
109112
logger.error("Environment Host Name for Batch Upload API cannot be null or empty.");
110113
throw new IllegalArgumentException("Environment Host Name for Batch Upload API cannot be null or empty.");
111114
}
112-
validatePath(pgpEncryptionCertPath, "PGP Encryption Cert Path");
113-
validatePath(keystorePath, "Keystore Path");
114-
validatePath(truststorePath, "Truststore Path");
115+
validatePathAndFile(pgpEncryptionCertPath, "PGP Encryption Cert Path");
116+
validatePathAndFile(keystorePath, "Keystore Path");
117+
validatePathAndFile(truststorePath, "Truststore Path");
115118
}
116119

117120
/**
@@ -130,9 +133,9 @@ public static void validateBatchApiP12Inputs(File inputFile, String environmentH
130133
logger.error("Environment Host Name for Batch Upload API cannot be null or empty.");
131134
throw new IllegalArgumentException("Environment Host Name for Batch Upload API cannot be null or empty.");
132135
}
133-
validatePath(pgpEncryptionCertPath, "PGP Encryption Cert Path");
134-
validatePath(clientCertP12FilePath, "Client Cert P12 File Path");
135-
validatePath(serverTrustCertPath, "Server Trust Cert Path");
136+
validatePathAndFile(pgpEncryptionCertPath, "PGP Encryption Cert Path");
137+
validatePathAndFile(clientCertP12FilePath, "Client Cert P12 File Path");
138+
validatePathAndFile(serverTrustCertPath, "Server Trust Cert Path");
136139
}
137140

138141
/**
@@ -190,17 +193,31 @@ private static void validateInputFile(File inputFile) {
190193
* @throws IOException If the file does not exist.
191194
* @throws IllegalArgumentException If the path is null or empty.
192195
*/
193-
private static void validatePath(String path, String pathType) throws IOException {
194-
if (path == null || path.trim().isEmpty()) {
195-
logger.error(pathType + " path cannot be null or empty");
196+
private static void validatePathAndFile(String filePath, String pathType) throws IOException {
197+
if (filePath == null || filePath.trim().isEmpty()) {
198+
logger.error(pathType + " path cannot be null or empty");
196199
throw new IllegalArgumentException(pathType + " path cannot be null or empty");
197200
}
198-
199-
File file = new File(path);
200-
if (!file.exists()) {
201-
logger.error(pathType + " does not exist: " + path);
201+
202+
// Normalize Windows-style paths that start with a slash before the drive letter
203+
String normalizedPath = filePath;
204+
if (File.separatorChar == '\\' && normalizedPath.matches("^/[A-Za-z]:.*")) {
205+
normalizedPath = normalizedPath.substring(1);
206+
}
207+
208+
Path path = Paths.get(normalizedPath);
209+
if (!Files.exists(path)) {
210+
logger.error(pathType + " does not exist: " + path);
202211
throw new IOException(pathType + " does not exist: " + path);
203212
}
213+
if (!Files.isRegularFile(path)) {
214+
logger.error(pathType + " does not have valid file: " + path);
215+
throw new IOException(pathType + " does not have valid file: " + path);
216+
}
217+
if (!Files.isReadable(path)) {
218+
logger.error(pathType + " is not readable: " + path);
219+
throw new IOException(pathType + " is not readable: " + path);
220+
}
204221
}
205222

206223
}

0 commit comments

Comments
 (0)