Commit 33e33ba3 authored by Frédéric Praca's avatar Frédéric Praca
Browse files

- fix #WEBLAB-403 by handling opaque and relative URI

parent ab34c123
......@@ -102,17 +102,20 @@ public class FileContentManager implements ContentReader, ContentWriter {
@Override
public File readContent(final URI destUri) throws WebLabCheckedException {
final String scheme = destUri.getScheme();
if (scheme == null) {
//URI does not have scheme specified
if (destUri.isAbsolute() == false) {
throw new WebLabCheckedException("Invalid URI, scheme is null in "+destUri);
}
final String scheme = destUri.getScheme();
URI finalURI = destUri;
if (FileContentManager.NETWORK_SCHEMES.contains(scheme)){
final File toRead;
if (FileContentManager.NETWORK_SCHEMES.contains(scheme)) {
// Read the stream to a temporary file in the content Folder
try {
URL url = destUri.toURL();
finalURI = writeContent(url.openStream());
toRead = new File(finalURI);
} catch (MalformedURLException e) {
logger.error(e,e);
throw new WebLabCheckedException("Network resource URL is malformed : "+destUri,e);
......@@ -122,11 +125,34 @@ public class FileContentManager implements ContentReader, ContentWriter {
}
}else if (!FileContentManager.FILE_SCHEME.equalsIgnoreCase(scheme)) {
throw new WebLabCheckedException("Invalid URI scheme [" + scheme + "], only [" + FileContentManager.FILE_SCHEME + "] is valid.");
} else {
//URI is fully parseable and thus begins with a leading /
if (finalURI.isOpaque() == false) {
toRead = new File(finalURI);
} else {
//In the following case, we have to parse the URI by hand
//When opaque, the URI is always relative
//remove the scheme from uri and build the correct relative path
String path_l = finalURI.getPath();
path_l = finalURI.toASCIIString().replaceFirst(finalURI.getScheme() +":", "");
//when leading slash does not appear, we should search in the defined path
logger.debug("Relative Path used for reading : " + path_l);
toRead = new File(contentFolderPath, path_l);
}
}
final File toRead = new File(finalURI);
if (!toRead.isFile() || !toRead.exists() || !toRead.canRead()) {
throw new WebLabCheckedException("File [" + toRead.getPath() + "] does not exists, is not a file or is not readable.");
if (!toRead.exists()) {
throw new WebLabCheckedException("File [" + toRead.getPath() + "] does not exists.");
}
//Should be tested after existence. In the opposite, we miss the existence problem
//as isFile => exists AND normal file
if (!toRead.isFile()) {
throw new WebLabCheckedException("File [" + toRead.getPath() + "] is not a file.");
}
if (!toRead.canRead()) {
throw new WebLabCheckedException("File [" + toRead.getPath() + "] is not readable.");
}
return toRead;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment