static URI validateId(URI blobId) throws UnsupportedIdException {
if (blobId == null)
throw new NullPointerException("Id cannot be null");
if (!blobId.getScheme().equalsIgnoreCase(scheme))
throw new UnsupportedIdException(blobId, "Id must be in " + scheme + " scheme");
String path = blobId.getRawSchemeSpecificPart();
if (path.startsWith("/"))
throw new UnsupportedIdException(blobId, "Id must specify a relative path");
try {
// insert a '/' so java.net.URI normalization works
URI tmp = new URI(scheme + ":/" + path);
String nPath = tmp.normalize().getRawSchemeSpecificPart().substring(1);
if (nPath.equals("..") || nPath.startsWith("../"))
throw new UnsupportedIdException(blobId, "Id cannot be outside top-level directory");
if (nPath.endsWith("/"))
throw new UnsupportedIdException(blobId, "Id cannot specify a directory");
return new URI(scheme + ":" + nPath);
} catch (URISyntaxException wontHappen) {
throw new Error(wontHappen);
}
}