* TODO: firefox 2.0 has an strange error when the iframe is loaded the browser requests the first file always twice
* @param isPopUp
*/
protected MediaResource deliverFile(String path, boolean isPopUp) {
MediaResource mr;
VFSLeaf vfsLeaf = null;
VFSItem vfsItem = null;
//if directory gets renamed root becomes null
if (rootDir == null) {
return new NotFoundMediaResource("directory not found"+path);
} else {
vfsItem = rootDir.resolve(path);
}
//only files are allowed, but somehow it happened that folders showed up here
if (vfsItem instanceof VFSLeaf) {
vfsLeaf = (VFSLeaf) rootDir.resolve(path);
} else {
mr = new NotFoundMediaResource(path);
}
if (vfsLeaf == null) {
mr = new NotFoundMediaResource(path);
} else {
// check if path ends with .html, .htm or .xhtml. We do this by searching for "htm"
// and accept positions of this string at length-3 or length-4
if (path.toLowerCase().lastIndexOf(FILE_SUFFIX_HTM) >= (path.length()-4)) {
// set the http content-type and the encoding
// try to load in iso-8859-1
String page = FileUtils.load(vfsLeaf.getInputStream(), DEFAULT_ENCODING);
// search for the <meta content="text/html; charset=utf-8"
// http-equiv="Content-Type" /> tag
// if none found, assume iso-8859-1
String enc = DEFAULT_ENCODING;
boolean useLoadedPageString = false;
// <meta.*charset=([^"]*)"
//extract only the charset attribute without the overhead of creating an htmlparser
Matcher m = PATTERN_ENCTYPE.matcher(page);
boolean found = m.find();
if (found) {
// use found char set
String htmlcharset = m.group(1);
//if longer than 50 the regexp did fail
if (htmlcharset.length() < 50) enc
= htmlcharset;
// reuse already loaded page when page uses the default encoding
if (htmlcharset.equalsIgnoreCase(DEFAULT_ENCODING) || htmlcharset.contains(DEFAULT_ENCODING) || htmlcharset.toLowerCase().contains(DEFAULT_ENCODING)) {
useLoadedPageString = true;
}
} else {
// try it with unicode character set as a fallback
String pageUnicode = FileUtils.load(vfsLeaf.getInputStream(), UNICODE_ENCODING);
m = PATTERN_ENCTYPE.matcher(pageUnicode);
found = m.find();
if (found) {
page = pageUnicode; // reuse already loaded page
useLoadedPageString = true;
} else {
// fallback, use the default encoding and reuse the already loaded page
useLoadedPageString = true;
}
}
//test the encoding first, sometime the regexp delivers fancy stuff. If it fails use fallback to default
try {
Charset.isSupported(enc);
} catch (IllegalCharsetNameException e) {
//TODO: see OLAT-5407
log.warn("IllegalCharsetNameException in:"+path+", enc="+enc+", overwriting to "+DEFAULT_ENCODING);
enc = DEFAULT_ENCODING;
}
// set the new encoding to remember for any following .js file loads
g_encoding = enc;
if (useLoadedPageString) {
mr = prepareMediaResource(page, enc, isPopUp);
} else {
// found a new charset other than iso-8859-1, load string with proper encoding
page = FileUtils.load(vfsLeaf.getInputStream(), enc);
mr = prepareMediaResource(page, enc, isPopUp);
}
} else if (path.endsWith(FILE_SUFFIX_JS)) { // a javascript library
VFSMediaResource vmr = new VFSMediaResource(vfsLeaf);