use of org.olat.core.gui.media.NotFoundMediaResource in project openolat by klemens.
the class TunnelMapper method handle.
@Override
public MediaResource handle(String relPath, HttpServletRequest hreq) {
String method = hreq.getMethod();
String uri = relPath;
HttpUriRequest meth = null;
try {
URIBuilder builder = new URIBuilder();
builder.setScheme(proto).setHost(host).setPort(port.intValue());
if (uri == null) {
uri = (startUri == null) ? "" : startUri;
}
if (uri.length() > 0 && uri.charAt(0) != '/') {
uri = "/" + uri;
}
if (StringHelper.containsNonWhitespace(uri)) {
builder.setPath(uri);
}
if (method.equals("GET")) {
String queryString = hreq.getQueryString();
if (StringHelper.containsNonWhitespace(queryString)) {
builder.setCustomQuery(queryString);
}
meth = new HttpGet(builder.build());
} else if (method.equals("POST")) {
Map<String, String[]> params = hreq.getParameterMap();
HttpPost pmeth = new HttpPost(builder.build());
List<BasicNameValuePair> pairs = new ArrayList<>();
for (String key : params.keySet()) {
String[] vals = params.get(key);
for (String val : vals) {
pairs.add(new BasicNameValuePair(key, val));
}
}
HttpEntity entity = new UrlEncodedFormEntity(pairs, "UTF-8");
pmeth.setEntity(entity);
meth = pmeth;
}
// test page e.g. http://cgi.algonet.se/htbin/cgiwrap/ug/test.py
if ("enabled".equals(CoreSpringFactory.getImpl(BaseSecurityModule.class).getUserInfosTunnelCourseBuildingBlock())) {
User u = ident.getUser();
meth.addHeader("X-OLAT-USERNAME", ident.getName());
meth.addHeader("X-OLAT-LASTNAME", u.getProperty(UserConstants.LASTNAME, null));
meth.addHeader("X-OLAT-FIRSTNAME", u.getProperty(UserConstants.FIRSTNAME, null));
meth.addHeader("X-OLAT-EMAIL", u.getProperty(UserConstants.EMAIL, null));
meth.addHeader("X-OLAT-USERIP", ipAddress);
}
HttpResponse response = httpClient.execute(meth);
if (response == null) {
// error
return new NotFoundMediaResource();
}
// get or post successfully
Header responseHeader = response.getFirstHeader("Content-Type");
if (responseHeader == null) {
// error
EntityUtils.consumeQuietly(response.getEntity());
return new NotFoundMediaResource();
}
return new HttpRequestMediaResource(response);
} catch (ClientProtocolException | URISyntaxException e) {
log.error("", e);
return null;
} catch (IOException e) {
log.error("Error loading URI: " + (meth == null ? "???" : meth.getURI()), e);
return null;
}
}
use of org.olat.core.gui.media.NotFoundMediaResource in project openolat by klemens.
the class CatalogEntryImageMapper method handle.
@Override
public MediaResource handle(String relPath, HttpServletRequest request) {
if (relPath.startsWith("/")) {
relPath = relPath.substring(1, relPath.length());
}
VFSContainer categoryResources = catalogManager.getCatalogResourcesHome();
VFSItem image = categoryResources.resolve(relPath);
MediaResource resource = null;
if (image instanceof VFSLeaf) {
if (image instanceof MetaTagged) {
MetaInfo info = ((MetaTagged) image).getMetaInfo();
if (info != null) {
VFSLeaf thumbnail = info.getThumbnail(180, 180, true);
if (thumbnail != null) {
resource = new VFSMediaResource(thumbnail);
}
}
}
if (resource == null) {
resource = new VFSMediaResource((VFSLeaf) image);
}
} else {
resource = new NotFoundMediaResource();
}
return resource;
}
use of org.olat.core.gui.media.NotFoundMediaResource in project openolat by klemens.
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.NotFoundMediaResource in project openolat by klemens.
the class CmdServeThumbnailResource method execute.
@Override
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;
VFSLeaf vfsfile = (VFSLeaf) folderComponent.getRootContainer().resolve(path);
if (vfsfile == null) {
// double decoding of ++
vfsfile = (VFSLeaf) FolderCommandHelper.tryDoubleDecoding(ureq, folderComponent);
}
if (vfsfile instanceof MetaTagged) {
MetaInfo info = ((MetaTagged) vfsfile).getMetaInfo();
if (info != null && info.isThumbnailAvailable()) {
VFSLeaf thumbnail = info.getThumbnail(200, 200, false);
if (thumbnail != null) {
mr = new VFSMediaResource(thumbnail);
}
}
}
if (mr == null) {
mr = new NotFoundMediaResource();
}
ureq.getDispatchResult().setResultingMediaResource(mr);
return null;
}
use of org.olat.core.gui.media.NotFoundMediaResource in project openolat by klemens.
the class HtmlStaticMapper method handle.
public MediaResource handle(String relPath, HttpServletRequest request) {
if (log.isDebug())
log.debug("CPComponent Mapper relPath=" + relPath);
VFSItem currentItem = mapperRootContainer.resolve(relPath);
if (currentItem == null || (currentItem instanceof VFSContainer)) {
return new NotFoundMediaResource();
}
VFSMediaResource vmr = new VFSMediaResource((VFSLeaf) currentItem);
String encoding = SimpleHtmlParser.extractHTMLCharset(((VFSLeaf) currentItem));
if (log.isDebug())
log.debug("CPComponent Mapper set encoding=" + encoding);
//
vmr.setEncoding(encoding);
return vmr;
}
Aggregations