Search in sources :

Example 11 with StringMediaResource

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

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 {
    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()) {
        JavaSource 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 {
        appendError(writer, javaSourcePath);
    }
    writer.append(HTML_STOP);
    StringMediaResource mr = new StringMediaResource();
    mr.setContentType(TEXT_HTML_CHARSET_UTF_8);
    mr.setData(writer.toString());
    return mr;
}
Also used : JavaSource2HTMLConverter(de.java2html.converter.JavaSource2HTMLConverter) StringWriter(java.io.StringWriter) JavaSource(de.java2html.javasource.JavaSource) StringMediaResource(org.olat.core.gui.media.StringMediaResource) File(java.io.File) JavaSourceParser(de.java2html.javasource.JavaSourceParser)

Example 12 with StringMediaResource

use of org.olat.core.gui.media.StringMediaResource in project OpenOLAT by OpenOLAT.

the class CalendarPrintMapper method handle.

@Override
public MediaResource handle(String relPath, HttpServletRequest request) {
    StringBuilder sb = new StringBuilder();
    sb.append("<html><head><title>").append("Calendar").append("</title>").append("<meta http-equiv='Content-type' content='text/html; charset=utf-8' />").append("<link href='").append(themeBaseUri).append("theme.css' rel='stylesheet' type='text/css' />\n").append("<!--[if IE 9]><link id='o_theme_css_ie' href='").append(themeBaseUri).append("theme_ie_completions.css' rel='stylesheet' type='text/css' /><![endif]-->\n").append("</head><body class='o_cal_print' onload='window.focus();window.print()'>");
    // collect all events
    List<KalendarEventRenderWrapper> sortedEventsWithin = new ArrayList<>();
    collectEvents(sortedEventsWithin, calendarWrappers);
    Collections.sort(sortedEventsWithin, KalendarEventDateComparator.getInstance());
    // list of events
    renderEvents(sb, sortedEventsWithin, from, to);
    // list of calendars
    renderCalendars(sb);
    sb.append("</body></html>");
    StringMediaResource smr = new StringMediaResource();
    smr.setContentType("text/html");
    smr.setEncoding("UTF-8");
    smr.setData(sb.toString());
    return smr;
}
Also used : ArrayList(java.util.ArrayList) StringMediaResource(org.olat.core.gui.media.StringMediaResource) KalendarEventRenderWrapper(org.olat.commons.calendar.ui.components.KalendarEventRenderWrapper)

Example 13 with StringMediaResource

use of org.olat.core.gui.media.StringMediaResource in project OpenOLAT by OpenOLAT.

the class GlossaryDefinitionMapper 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();
    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("GlossaryDefinition delivery failed; path to glossaryFolder not existing: " + relPath, null);
        return new NotFoundMediaResource();
    }
    VFSContainer glossaryFolder = new LocalFolderImpl(glossaryFolderFile);
    if (!gIM.isFolderContainingGlossary(glossaryFolder)) {
        logWarn("GlossaryDefinition delivery failed; glossaryFolder doesn't contain a valid Glossary: " + glossaryFolder, null);
        return new NotFoundMediaResource();
    }
    String glossaryMainTerm = parts[2];
    if (parts.length > 2) {
        // this handle / or \ in a term
        for (int i = 3; i < parts.length; i++) {
            glossaryMainTerm += "/" + parts[i];
        }
    }
    // cut away ".html" if necessary
    if (glossaryMainTerm.endsWith(".html")) {
        glossaryMainTerm = glossaryMainTerm.substring(0, glossaryMainTerm.length() - 5);
    }
    glossaryMainTerm = glossaryMainTerm.toLowerCase();
    Set<String> alternatives = new HashSet<>();
    prepareAlternatives(glossaryMainTerm, alternatives);
    // 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/html");
    List<GlossaryItem> glossItems = gIM.getGlossaryItemListByVFSItem(glossaryFolder);
    GlossaryItem foundItem = null;
    for (GlossaryItem glossaryItem : glossItems) {
        String item = glossaryItem.getGlossTerm().toLowerCase();
        if (alternatives.contains(item)) {
            foundItem = glossaryItem;
            break;
        }
    }
    if (foundItem == null) {
        return new NotFoundMediaResource();
    }
    StringBuilder sb = new StringBuilder();
    sb.append("<dd><dt>").append(foundItem.getGlossTerm()).append("</dt><dl>").append(foundItem.getGlossDef()).append("</dl></dd>");
    resource.setData(sb.toString());
    resource.setEncoding("utf-8");
    if (isLogDebugEnabled())
        logDebug("loaded definition for " + glossaryMainTerm, null);
    return resource;
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSContainer(org.olat.core.util.vfs.VFSContainer) HttpServletResponse(javax.servlet.http.HttpServletResponse) LocalFolderImpl(org.olat.core.util.vfs.LocalFolderImpl) GlossaryItemManager(org.olat.core.commons.modules.glossary.GlossaryItemManager) StringMediaResource(org.olat.core.gui.media.StringMediaResource) File(java.io.File) HashSet(java.util.HashSet) GlossaryItem(org.olat.core.commons.modules.glossary.GlossaryItem)

Example 14 with StringMediaResource

use of org.olat.core.gui.media.StringMediaResource in project OpenOLAT by OpenOLAT.

the class SourceViewController method event.

/**
 * @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest, org.olat.core.gui.components.Component, org.olat.core.gui.control.Event)
 */
@Override
protected void event(UserRequest ureq, Component source, Event event) {
    if (source == viewVeloctiySource) {
        String velocityTemplatePath = WebappHelper.getSourcePath() + "/" + vc.getPage();
        String vcContent = FileUtils.load(new File(velocityTemplatePath), "utf-8");
        sourceview.contextPut("content", vcContent);
        sourceview.contextPut("vcname", vc.getPage());
        view = new CloseableModalController(getWindowControl(), "close...", sourceview);
        listenTo(view);
        view.activate();
    } else if (source == viewJSource) {
        // Parse the raw text to a JavaSource object
        JavaSource jsource = null;
        try {
            String className = clazz.getCanonicalName();
            className = className.replace('.', '/');
            String sourcePath = WebappHelper.getSourcePath() + "/" + className + ".java";
            jsource = new JavaSourceParser().parse(new File(sourcePath));
        } catch (IOException e) {
            showInfo("todo");
        }
        // Create a converter and write the JavaSource object as Html
        JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter();
        StringWriter writer = new StringWriter();
        writer.append(HTML_START);
        try {
            JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault();
            options.setShowLineNumbers(true);
            converter.convert(jsource, options, writer);
        } catch (IOException e) {
        // 
        }
        StringMediaResource mr = new StringMediaResource();
        mr.setContentType(TEXT_HTML_CHARSET_UTF_8);
        writer.append(HTML_STOP);
        mr.setData(writer.toString());
        ureq.getDispatchResult().setResultingMediaResource(mr);
    }
}
Also used : JavaSource2HTMLConverter(de.java2html.converter.JavaSource2HTMLConverter) JavaSourceConversionOptions(de.java2html.options.JavaSourceConversionOptions) StringWriter(java.io.StringWriter) CloseableModalController(org.olat.core.gui.control.generic.closablewrapper.CloseableModalController) JavaSource(de.java2html.javasource.JavaSource) IOException(java.io.IOException) StringMediaResource(org.olat.core.gui.media.StringMediaResource) File(java.io.File) JavaSourceParser(de.java2html.javasource.JavaSourceParser)

Example 15 with StringMediaResource

use of org.olat.core.gui.media.StringMediaResource in project OpenOLAT by OpenOLAT.

the class PostDataMapper method handle.

@Override
public MediaResource handle(String relPath, HttpServletRequest request) {
    Map<String, String> props = CoreSpringFactory.getImpl(LTIManager.class).sign(unsignedProps, baseUrl, oauthKey, oauthSecret);
    String postData = BasicLTIUtil.postLaunchHTML(props, launchUrl, debug);
    StringMediaResource mediares = new StringMediaResource();
    mediares.setData(postData);
    mediares.setContentType("text/html");
    mediares.setEncoding("UTF-8");
    return mediares;
}
Also used : LTIManager(org.olat.ims.lti.LTIManager) StringMediaResource(org.olat.core.gui.media.StringMediaResource)

Aggregations

StringMediaResource (org.olat.core.gui.media.StringMediaResource)26 File (java.io.File)8 NotFoundMediaResource (org.olat.core.gui.media.NotFoundMediaResource)6 StringWriter (java.io.StringWriter)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 GlossaryItemManager (org.olat.core.commons.modules.glossary.GlossaryItemManager)4 LocalFolderImpl (org.olat.core.util.vfs.LocalFolderImpl)4 VFSContainer (org.olat.core.util.vfs.VFSContainer)4 JavaSource2HTMLConverter (de.java2html.converter.JavaSource2HTMLConverter)3 JavaSource (de.java2html.javasource.JavaSource)3 JavaSourceParser (de.java2html.javasource.JavaSourceParser)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Matcher (java.util.regex.Matcher)2 JSONObject (org.json.JSONObject)2 KalendarEventRenderWrapper (org.olat.commons.calendar.ui.components.KalendarEventRenderWrapper)2 MetaInfo (org.olat.core.commons.modules.bc.meta.MetaInfo)2 MetaTagged (org.olat.core.commons.modules.bc.meta.tagged.MetaTagged)2 GlossaryItem (org.olat.core.commons.modules.glossary.GlossaryItem)2