use of org.olat.core.gui.media.StringMediaResource in project OpenOLAT by OpenOLAT.
the class CmdServeResource method execute.
public Controller execute(FolderComponent folderComponent, UserRequest ureq, WindowControl wControl, Translator translator) {
VFSSecurityCallback inheritedSecCallback = VFSManager.findInheritedSecurityCallback(folderComponent.getCurrentContainer());
if (inheritedSecCallback != null && !inheritedSecCallback.canRead())
throw new RuntimeException("Illegal read attempt: " + folderComponent.getCurrentContainerPath());
// extract file
String path = ureq.getModuleURI();
MediaResource mr = null;
VFSItem vfsitem = folderComponent.getRootContainer().resolve(path);
if (vfsitem == null) {
// double decoding of ++
vfsitem = FolderCommandHelper.tryDoubleDecoding(ureq, folderComponent);
}
if (vfsitem == null) {
mr = new NotFoundMediaResource();
} else if (!(vfsitem instanceof VFSLeaf)) {
mr = new NotFoundMediaResource();
} else {
VFSLeaf vfsfile = (VFSLeaf) vfsitem;
boolean forceDownload = FolderManager.isDownloadForcedFileType(vfsfile.getName());
if (path.toLowerCase().endsWith(".html") || path.toLowerCase().endsWith(".htm")) {
// setCurrentURI(path);
// set the http content-type and the encoding
// try to load in iso-8859-1
InputStream is = vfsfile.getInputStream();
if (is == null) {
mr = new NotFoundMediaResource();
} else {
String page = FileUtils.load(is, 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 useLoaded = false;
// <meta.*charset=([^"]*)"
Matcher m = PATTERN_ENCTYPE.matcher(page);
boolean found = m.find();
if (found) {
String htmlcharset = m.group(1);
enc = htmlcharset;
if (htmlcharset.equals(DEFAULT_ENCODING)) {
useLoaded = true;
}
} else {
useLoaded = true;
}
// set the new encoding to remember for any following .js file loads
g_encoding = enc;
if (useLoaded) {
StringMediaResource smr = new StringMediaResource();
String mimetype = forceDownload ? VFSMediaResource.MIME_TYPE_FORCE_DOWNLOAD : "text/html;charset=" + enc;
smr.setContentType(mimetype);
smr.setEncoding(enc);
smr.setData(page);
if (forceDownload) {
smr.setDownloadable(true, vfsfile.getName());
}
mr = smr;
} else {
// found a new charset other than iso-8859-1 -> let it load again
// as bytes (so we do not need to convert to string and back
// again)
VFSMediaResource vmr = new VFSMediaResource(vfsfile);
vmr.setEncoding(enc);
if (forceDownload) {
vmr.setDownloadable(true);
}
mr = vmr;
}
}
} else if (path.endsWith(".js")) {
// a javascript library
VFSMediaResource vmr = new VFSMediaResource(vfsfile);
// that loads the .js file
if (g_encoding != null) {
vmr.setEncoding(g_encoding);
}
if (forceDownload) {
vmr.setDownloadable(true);
}
mr = vmr;
} else if (path.endsWith(".txt")) {
// text files created in OpenOLAT are utf-8, prefer this encoding
VFSMediaResource vmr = new VFSMediaResource(vfsfile);
vmr.setEncoding("utf-8");
if (forceDownload) {
vmr.setDownloadable(true);
}
mr = vmr;
} else {
// binary data: not .html, not .htm, not .js -> treated as is
VFSMediaResource vmr = new VFSMediaResource(vfsfile);
if (forceDownload) {
vmr.setDownloadable(true);
}
mr = vmr;
}
}
ThreadLocalUserActivityLogger.log(FolderLoggingAction.BC_FILE_READ, getClass(), CoreLoggingResourceable.wrapBCFile(path));
ureq.getDispatchResult().setResultingMediaResource(mr);
// update download counter
if (vfsitem instanceof MetaTagged) {
MetaTagged itemWithMeta = (MetaTagged) vfsitem;
MetaInfo meta = itemWithMeta.getMetaInfo();
meta.increaseDownloadCount();
meta.write();
}
return null;
}
use of org.olat.core.gui.media.StringMediaResource in project OpenOLAT by OpenOLAT.
the class IFrameDeliveryMapper method prepareMediaResource.
private StringMediaResource prepareMediaResource(HttpServletRequest httpRequest, String page, String enc, String contentType, boolean isPopUp) {
StringMediaResource smr = new StringMediaResource();
if (XHTML_CONTENT_TYPE.equals(contentType)) {
// check if the application/xhtml+xml is supported (not supported by IEs)
// if not, replace the content type by text/html for compatibility
String accept = httpRequest.getHeader("Accept");
if (accept == null || accept.indexOf(XHTML_CONTENT_TYPE) < 0) {
contentType = DEFAULT_CONTENT_TYPE;
}
}
String mimetype = contentType + ";charset=" + StringHelper.check4xMacRoman(enc);
smr.setContentType(mimetype);
smr.setEncoding(enc);
// inject some javascript code to size iframe to proper height, but only when not a page with framesets
if (page.indexOf(TAG_FRAMESET) != -1 || page.indexOf(TAG_FRAMESET_UPPERC) != -1 || isPopUp) {
// is frameset -> deliver unparsed
smr.setData(page);
} else {
String agent = httpRequest.getHeader("User-Agent");
boolean firefoxWorkaround = agent != null && agent.indexOf("Firefox/") > 0;
if (rawContent) {
smr.setData(page);
} else {
smr.setData(injectJavaScript(page, mimetype, checkForInlineEvent, firefoxWorkaround));
}
// When loading next page, check if it was an inline user click
this.checkForInlineEvent = true;
}
return smr;
}
use of org.olat.core.gui.media.StringMediaResource in project OpenOLAT by OpenOLAT.
the class GlossaryTermMapper method handle.
/**
* @see org.olat.core.dispatcher.mapper.Mapper#handle(java.lang.String,
* javax.servlet.http.HttpServletRequest)
*/
public MediaResource handle(String relPath, HttpServletRequest request) {
GlossaryItemManager gIM = GlossaryItemManager.getInstance();
// security checks are done by MapperRegistry
String[] parts = relPath.split("/");
String glossaryId = parts[1];
String glossaryFolderString = FolderConfig.getCanonicalRoot() + FolderConfig.getRepositoryHome() + "/" + glossaryId + "/" + GlossaryMarkupItemController.INTERNAL_FOLDER_NAME;
File glossaryFolderFile = new File(glossaryFolderString);
if (!glossaryFolderFile.isDirectory()) {
logWarn("GlossaryTerms delivery failed; path to glossaryFolder not existing: " + relPath, null);
return new NotFoundMediaResource();
}
VFSContainer glossaryFolder = new LocalFolderImpl(glossaryFolderFile);
if (!gIM.isFolderContainingGlossary(glossaryFolder)) {
logWarn("GlossaryTerms delivery failed; glossaryFolder doesn't contain a valid Glossary: " + glossaryFolder, null);
return new NotFoundMediaResource();
}
// Create a media resource
StringMediaResource resource = new StringMediaResource() {
@Override
public void prepare(HttpServletResponse hres) {
// don't use normal string media headers which prevent caching,
// use standard browser caching based on last modified timestamp
}
};
resource.setLastModified(gIM.getGlossaryLastModifiedTime(glossaryFolder));
resource.setContentType("text/javascript");
// Get data
String glossaryArrayData = TextMarkerJsGenerator.loadGlossaryItemListAsJSArray(glossaryFolder);
resource.setData(glossaryArrayData);
// UTF-8 encoding used in this js file since explicitly set in the ajax
// call (usually js files are 8859-1)
resource.setEncoding("utf-8");
return resource;
}
use of org.olat.core.gui.media.StringMediaResource in project OpenOLAT by OpenOLAT.
the class AjaxController method extractMediaResource.
public MediaResource extractMediaResource(boolean wrapHTML) {
JSONObject json = getAndClearJSON(true);
String res;
if (wrapHTML) {
// most ajax responses are a lot smaller than 16k
StringBuilder sb = new StringBuilder(16384);
sb.append("<html><head><script type=\"text/javascript\">\n/* <![CDATA[ */\nfunction invoke(){var r=").append(json.toString()).append("; ").append("if (parent!=self&&parent.window.o_info) {").append(" parent.window.o_ainvoke(r);").append(" try{ parent.window.o_removeIframe(document.defaultView.frameElement.id); } catch(e) {} ").append("} else {").append(" this.document.location=\"").append(StaticMediaDispatcher.createStaticURIFor("msg/json/en/info.html")).append("\";").append("}}").append("\n/* ]]> */\n</script></head><body onLoad=\"invoke()\"></body></html>");
res = sb.toString();
} else {
res = json.toString();
}
StringMediaResource smr = new StringMediaResource();
smr.setContentType("text/html;charset=utf-8");
smr.setEncoding("utf-8");
smr.setData(res);
return smr;
}
use of org.olat.core.gui.media.StringMediaResource in project OpenOLAT by OpenOLAT.
the class SourceViewController method showjavaSource.
/**
* provide a class name with path and you will get an string media resource you can display
* @param cl
* @return
* @throws IOException
*/
public static MediaResource showjavaSource(String cl) throws IOException {
JavaSource jsource = null;
cl = cl.replace('.', '/');
String javaSourcePath = WebappHelper.getSourcePath() + "/" + cl + ".java";
File file = new File(javaSourcePath);
StringWriter writer = new StringWriter();
writer.append(HTML_START);
if (file.exists()) {
jsource = new JavaSourceParser().parse(file);
// Create a converter and write the JavaSource object as Html
JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter();
converter.convert(jsource, JavaSourceConversionOptions.getDefault(), writer);
} else {
writer.append("<html><body><h3>The source file could not be found in the following path:<br>" + javaSourcePath + "<br>Check if configured source path in brasatoconfig.xml is correct.</h3></body></html>");
}
StringMediaResource mr = new StringMediaResource();
mr.setContentType(TEXT_HTML_CHARSET_UTF_8);
writer.append(HTML_STOP);
mr.setData(writer.toString());
return mr;
}
Aggregations