Search in sources :

Example 31 with NotFoundMediaResource

use of org.olat.core.gui.media.NotFoundMediaResource in project openolat by klemens.

the class HtmlStaticPageComponent method getAsyncMediaResource.

/**
 * @see org.olat.core.gui.media.AsyncMediaResponsible#getAsyncMediaResource(org.olat.core.gui.UserRequest)
 */
public MediaResource getAsyncMediaResource(UserRequest ureq) {
    // is the path to the desired resource (put together by the webbrowser by
    // combining latesturl and relative link)
    String moduleURI = ureq.getModuleURI();
    MediaResource mr = null;
    if (moduleURI != null) {
        // 1. check for an olat command (special link to indicate a command)
        if (moduleURI.startsWith(OLAT_CMD_PREFIX)) {
            String cmdAndSub = moduleURI.substring(OLAT_CMD_PREFIX.length());
            int slpos = cmdAndSub.indexOf('/');
            if (slpos != -1) {
                String cmd = cmdAndSub.substring(0, slpos);
                String subcmd = cmdAndSub.substring(slpos + 1);
                OlatCmdEvent aev = new OlatCmdEvent(cmd, subcmd);
                fireEvent(ureq, aev);
                // Mediaresourse)
                if (aev.isAccepted())
                    return null;
            }
        // else ignore (syntax error in command
        }
        // make sure moduleURI does not contain ".." or such (hack attempt)
        // -> ok, userrequest class asserts this.
        VFSItem sourceItem = rootContainer.resolve(moduleURI);
        // return 404 if the requested file does not exist
        if (sourceItem == null || (sourceItem instanceof VFSContainer)) {
            return new NotFoundMediaResource();
        }
        // we know the file exists.
        boolean checkRegular = true;
        // check for special case: render the follwing link in a new (server) window and all following clicks as well
        if ((ureq.getParameter("olatsite") != null) || ((moduleURI.endsWith(".html") || moduleURI.endsWith(".htm")) && (ureq.getParameter("olatraw") != null))) {
            log.debug("moduleURI=" + moduleURI);
            ExternalSiteEvent ese = new ExternalSiteEvent(moduleURI);
            fireEvent(ureq, ese);
            if (ese.isAccepted()) {
                mr = ese.getResultingMediaResource();
                log.debug("ExternalSiteEvent is accepted");
                checkRegular = false;
            } else {
                // it is a html page with olatraw parameter => redirect to mapper
                Mapper mapper = new HtmlStaticMapper(rootContainer);
                // NOTE: do not deregister this mapper, since it could be used a lot later (since it is opened in a new browser window)
                // Register mapper as cacheable
                String mapperID = VFSManager.getRealPath(rootContainer);
                if (mapperID == null) {
                    // Can't cache mapper, no cacheable context available
                    amapPath = CoreSpringFactory.getImpl(MapperService.class).register(ureq.getUserSession(), mapper);
                } else {
                    // Add classname to the file path to remove conflicts with other
                    // usages of the same file path
                    mapperID = this.getClass().getSimpleName() + ":" + mapperID;
                    amapPath = CoreSpringFactory.getImpl(MapperService.class).register(ureq.getUserSession(), mapperID, mapper);
                }
                String path = amapPath.getUrl() + "/" + moduleURI;
                ese.setResultingMediaResource(new RedirectMediaResource(path));
                if (log.isDebug())
                    log.debug("RedirectMediaResource=" + path);
                ese.accept();
                mr = ese.getResultingMediaResource();
                checkRegular = false;
            }
        }
        if (checkRegular) {
            // mediaresource (raw inputstream)
            if ((moduleURI.endsWith(".html") || moduleURI.endsWith(".htm")) && (ureq.getParameter("olatraw") == null)) {
                // we remember what to render inline later and return null to indicate
                // inline rendering
                currentURI = moduleURI;
                getFileContent((VFSLeaf) sourceItem);
                fireEvent(ureq, new NewInlineUriEvent(currentURI));
            } else {
                // it is indeed an image or such -> serve it
                mr = new VFSMediaResource((VFSLeaf) sourceItem);
            }
        }
    }
    // -> do a normal inline rendering (reload)
    return mr;
}
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) Mapper(org.olat.core.dispatcher.mapper.Mapper) RedirectMediaResource(org.olat.core.gui.media.RedirectMediaResource) MediaResource(org.olat.core.gui.media.MediaResource) RedirectMediaResource(org.olat.core.gui.media.RedirectMediaResource) NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource)

Example 32 with NotFoundMediaResource

use of org.olat.core.gui.media.NotFoundMediaResource in project openolat by klemens.

the class IFrameDeliveryMapper method deliverFile.

protected MediaResource deliverFile(HttpServletRequest httpRequest, 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();
    } 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();
    }
    if (vfsLeaf == null) {
        mr = new NotFoundMediaResource();
    } else {
        // 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
            Page page = loadPageWithGuess(vfsLeaf);
            String pageEncoding = page.getEncoding();
            if (page.isUseLoadedPageString()) {
                mr = prepareMediaResource(httpRequest, page.getPage(), pageEncoding, page.getContentType(), isPopUp);
            } else {
                // found a new charset other than iso-8859-1, load string with proper encoding
                String content = FileUtils.load(vfsLeaf.getInputStream(), pageEncoding);
                mr = prepareMediaResource(httpRequest, content, pageEncoding, page.getContentType(), isPopUp);
            }
            if (contentEncoding == null) {
                contentEncoding = pageEncoding;
            }
        } else if (path.endsWith(FILE_SUFFIX_JS)) {
            // a javascript library
            VFSMediaResource vmr = new VFSMediaResource(vfsLeaf);
            // that loads the .js file
            if (jsEncoding != null) {
                vmr.setEncoding(jsEncoding);
            } else if (contentEncoding != null) {
                vmr.setEncoding(contentEncoding);
            }
            mr = vmr;
        } else {
            // binary data: not .html, not .htm, not .js -> treated as is
            VFSMediaResource vmr = new VFSMediaResource(vfsLeaf);
            String filename = vfsLeaf.getName();
            // This is to prevent the login prompt in Excel, Word and PowerPoint
            if (filename.endsWith(".xlsx") || filename.endsWith(".pptx") || filename.endsWith(".docx")) {
                vmr.setDownloadable(true);
            }
            mr = vmr;
        }
    }
    return mr;
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) 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) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource)

Example 33 with NotFoundMediaResource

use of org.olat.core.gui.media.NotFoundMediaResource in project openolat by klemens.

the class MailAttachmentMapper method handle.

@Override
public MediaResource handle(String relPath, HttpServletRequest request) {
    if (relPath != null && relPath.indexOf(ATTACHMENT_CONTEXT) >= 0) {
        int startIndex = relPath.indexOf(ATTACHMENT_CONTEXT);
        int endIndex = relPath.indexOf("/", startIndex + ATTACHMENT_CONTEXT.length());
        if (startIndex >= 0 && endIndex > startIndex) {
            String attachmentKey = relPath.substring(startIndex + ATTACHMENT_CONTEXT.length(), endIndex);
            try {
                Long key = new Long(attachmentKey);
                VFSLeaf datas = mailManager.getAttachmentDatas(key);
                MediaResource resource = new VFSMediaResource(datas);
                return resource;
            } catch (NumberFormatException e) {
                return new NotFoundMediaResource();
            }
        }
    }
    return new NotFoundMediaResource();
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) 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 34 with NotFoundMediaResource

use of org.olat.core.gui.media.NotFoundMediaResource in project openolat by klemens.

the class DialogElementController method doDownload.

private void doDownload(UserRequest ureq) {
    VFSLeaf file = dialogElmsMgr.getDialogLeaf(element);
    if (file != null) {
        ureq.getDispatchResult().setResultingMediaResource(new VFSMediaResource(file));
        ThreadLocalUserActivityLogger.log(CourseLoggingAction.DIALOG_ELEMENT_FILE_DOWNLOADED, getClass(), LoggingResourceable.wrapBCFile(element.getFilename()));
    } else {
        ureq.getDispatchResult().setResultingMediaResource(new NotFoundMediaResource());
        logError("No file to discuss: " + element, null);
    }
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource)

Example 35 with NotFoundMediaResource

use of org.olat.core.gui.media.NotFoundMediaResource in project openolat by klemens.

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;
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSContainer(org.olat.core.util.vfs.VFSContainer) GlossaryItemManager(org.olat.core.commons.modules.glossary.GlossaryItemManager) HttpServletResponse(javax.servlet.http.HttpServletResponse) StringMediaResource(org.olat.core.gui.media.StringMediaResource) File(java.io.File) LocalFolderImpl(org.olat.core.util.vfs.LocalFolderImpl)

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