Search in sources :

Example 26 with NotFoundMediaResource

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;
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestMediaResource(org.olat.core.gui.media.HttpRequestMediaResource) User(org.olat.core.id.User) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URIBuilder(org.apache.http.client.utils.URIBuilder) ClientProtocolException(org.apache.http.client.ClientProtocolException) Header(org.apache.http.Header) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 27 with NotFoundMediaResource

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;
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VFSContainer(org.olat.core.util.vfs.VFSContainer) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) VFSItem(org.olat.core.util.vfs.VFSItem) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource) MediaResource(org.olat.core.gui.media.MediaResource) NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource)

Example 28 with NotFoundMediaResource

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;
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) Matcher(java.util.regex.Matcher) InputStream(java.io.InputStream) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) VFSItem(org.olat.core.util.vfs.VFSItem) StringMediaResource(org.olat.core.gui.media.StringMediaResource) MediaResource(org.olat.core.gui.media.MediaResource) NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource) StringMediaResource(org.olat.core.gui.media.StringMediaResource) VFSSecurityCallback(org.olat.core.util.vfs.callbacks.VFSSecurityCallback) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource)

Example 29 with NotFoundMediaResource

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;
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) MediaResource(org.olat.core.gui.media.MediaResource) NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource) VFSSecurityCallback(org.olat.core.util.vfs.callbacks.VFSSecurityCallback) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource)

Example 30 with NotFoundMediaResource

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;
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource)

Aggregations

NotFoundMediaResource (org.olat.core.gui.media.NotFoundMediaResource)38 MediaResource (org.olat.core.gui.media.MediaResource)26 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)24 VFSMediaResource (org.olat.core.util.vfs.VFSMediaResource)24 VFSItem (org.olat.core.util.vfs.VFSItem)16 StringMediaResource (org.olat.core.gui.media.StringMediaResource)10 VFSContainer (org.olat.core.util.vfs.VFSContainer)10 File (java.io.File)8 MetaInfo (org.olat.core.commons.modules.bc.meta.MetaInfo)6 MetaTagged (org.olat.core.commons.modules.bc.meta.tagged.MetaTagged)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 GlossaryItemManager (org.olat.core.commons.modules.glossary.GlossaryItemManager)4 LocalFolderImpl (org.olat.core.util.vfs.LocalFolderImpl)4 VFSSecurityCallback (org.olat.core.util.vfs.callbacks.VFSSecurityCallback)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2