} else if (NATIVE_SCHEME.equalsIgnoreCase(scheme)) {
// If the "k2" scheme is specified, the path need not have the extension.
mustHaveExtension = false;
} else {
// Unrecognized scheme
throw new IllegalAddressException(
address, IllegalAddressException.Reason.INVALID_SCHEME, null);
}
// Extract path. We are assuming (below) that any encoded unreserved
// characters have already been decoded by K2Storage.
String path = extractRawPath(address);
// Check if the file extension is included in the path.
if (!EXTENSION_REGEX.matcher(path).find()) {
if (mustHaveExtension) {
throw new IllegalAddressException(
address, IllegalAddressException.Reason.INVALID_PATH, null);
}
// Append if missing
path = path + '.' + FILE_EXTENSION;
}
try {
// Resolve the disk address of the provided path
final URI diskAddress = new File("").toURI().resolve(path).normalize();
// Create all file objects before checking
final File pri = new File(diskAddress);
final File parent = pri.getParentFile();
final String filename = pri.getName();
final File tmpA =
new File(parent, TEMP_PREFIX + filename + TEMP_A_EXTENSION);
final File tmpB =
new File(parent, TEMP_PREFIX + filename + TEMP_B_EXTENSION);
// Grab path from the file for checking and later usage
path = pri.toURI().getRawPath();
// Filename should be a valid
if (FILENAME_REGEX.matcher(filename).matches()
// Path should be absolute after normalization
&& !path.startsWith("/../")
// Parent file should be an existing directory
&& parent != null && parent.isDirectory()
// Everything else should NOT be a directory
&& !pri.isDirectory() && !tmpA.isDirectory() && !tmpB.isDirectory()) {
// All OK. Generate final address with scheme and without extension.
path = path.substring(0, path.length() - FILE_EXTENSION.length() - 1);
URI finalAddress = URI.create(NATIVE_SCHEME + ':' + path);
// Initialize the driver.
this.keyFile = pri;
this.tempFileA = tmpA;
this.tempFileB = tmpB;
return finalAddress;
}
} catch (IllegalArgumentException ex) {
// The path is invalid (from URI.create or new File).
// Fall-through for exception throw.
}
// Falling through to here implies the path is invalid
throw new IllegalAddressException(address,
IllegalAddressException.Reason.INVALID_PATH, null);
}