Search in sources :

Example 1 with Context

use of org.apache.wiki.api.core.Context in project jspwiki by apache.

the class Preferences method reloadPreferences.

/**
 *  Reloads the preferences from the PageContext into the WikiContext.
 *
 *  @param pageContext The page context.
 */
// FIXME: The way that date preferences are chosen is currently a bit wacky: it all gets saved to the cookie based on the browser state
// with which the user happened to first arrive to the site with.  This, unfortunately, means that even if the user changes e.g.
// language preferences (like in a web cafe), the old preferences still remain in a site cookie.
public static void reloadPreferences(final PageContext pageContext) {
    final Preferences prefs = new Preferences();
    final Properties props = PropertyReader.loadWebAppProps(pageContext.getServletContext());
    final Context ctx = Context.findContext(pageContext);
    final String dateFormat = ctx.getEngine().getManager(InternationalizationManager.class).get(InternationalizationManager.CORE_BUNDLE, getLocale(ctx), "common.datetimeformat");
    prefs.put("SkinName", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.skinname", "PlainVanilla"));
    prefs.put("DateFormat", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.dateformat", dateFormat));
    prefs.put("TimeZone", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.timezone", java.util.TimeZone.getDefault().getID()));
    prefs.put("Orientation", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.orientation", "fav-left"));
    prefs.put("Sidebar", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.sidebar", "active"));
    prefs.put("Layout", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.layout", "fluid"));
    prefs.put("Language", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.language", getLocale(ctx).toString()));
    prefs.put("SectionEditing", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.sectionediting", "true"));
    prefs.put("Appearance", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.appearance", "true"));
    // editor cookies
    prefs.put("autosuggest", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.autosuggest", "true"));
    prefs.put("tabcompletion", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.tabcompletion", "true"));
    prefs.put("smartpairs", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.smartpairs", "false"));
    prefs.put("livepreview", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.livepreview", "true"));
    prefs.put("previewcolumn", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.previewcolumn", "true"));
    // FIXME: editormanager reads jspwiki.editor -- which of both properties should continue
    prefs.put("editor", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.editor", "plain"));
    parseJSONPreferences((HttpServletRequest) pageContext.getRequest(), prefs);
    pageContext.getSession().setAttribute(SESSIONPREFS, prefs);
}
Also used : Context(org.apache.wiki.api.core.Context) PageContext(javax.servlet.jsp.PageContext) InternationalizationManager(org.apache.wiki.i18n.InternationalizationManager) Properties(java.util.Properties)

Example 2 with Context

use of org.apache.wiki.api.core.Context in project jspwiki by apache.

the class DefaultRSSGenerator method generate.

/**
 * {@inheritDoc}
 */
@Override
public String generate() {
    final Context context = Wiki.context().create(m_engine, Wiki.contents().page(m_engine, "__DUMMY"));
    context.setRequestContext(ContextEnum.PAGE_RSS.getRequestContext());
    final Feed feed = new RSS10Feed(context);
    return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + generateFullWikiRSS(context, feed);
}
Also used : Context(org.apache.wiki.api.core.Context)

Example 3 with Context

use of org.apache.wiki.api.core.Context in project jspwiki by apache.

the class PluginContent method getText.

/**
 *{@inheritDoc}
 */
@Override
public String getText() {
    final WikiDocument doc = (WikiDocument) getDocument();
    if (doc == null) {
        // 
        return getPluginName();
    }
    final Context context = doc.getContext();
    if (context == null) {
        log.info("WikiContext garbage-collected, cannot proceed");
        return getPluginName();
    }
    return invoke(context);
}
Also used : Context(org.apache.wiki.api.core.Context)

Example 4 with Context

use of org.apache.wiki.api.core.Context in project jspwiki by apache.

the class VariableContent method getValue.

/**
 *   Evaluates the variable and returns the contents.
 *
 *   @return The rendered value of the variable.
 */
@Override
public String getValue() {
    String result;
    final WikiDocument root = (WikiDocument) getDocument();
    if (root == null) {
        // See similar note in PluginContent
        return m_varName;
    }
    final Context context = root.getContext();
    if (context == null) {
        return "No WikiContext available: INTERNAL ERROR";
    }
    final Boolean wysiwygEditorMode = context.getVariable(Context.VAR_WYSIWYG_EDITOR_MODE);
    if (wysiwygEditorMode != null && wysiwygEditorMode) {
        result = "[" + m_varName + "]";
    } else {
        try {
            result = context.getEngine().getManager(VariableManager.class).parseAndGetValue(context, m_varName);
        } catch (final NoSuchVariableException e) {
            result = MarkupParser.makeError("No such variable: " + e.getMessage()).getText();
        }
    }
    return StringEscapeUtils.escapeXml11(result);
}
Also used : Context(org.apache.wiki.api.core.Context) WikiContext(org.apache.wiki.WikiContext) NoSuchVariableException(org.apache.wiki.api.exceptions.NoSuchVariableException)

Example 5 with Context

use of org.apache.wiki.api.core.Context in project jspwiki by apache.

the class InsertPage method execute.

/**
 *  {@inheritDoc}
 */
@Override
@SuppressWarnings("unchecked")
public String execute(final Context context, final Map<String, String> params) throws PluginException {
    final Engine engine = context.getEngine();
    final StringBuilder res = new StringBuilder();
    final String clazz = params.get(PARAM_CLASS);
    final String includedPage = params.get(PARAM_PAGENAME);
    String style = params.get(PARAM_STYLE);
    final boolean showOnce = "once".equals(params.get(PARAM_SHOW));
    final String defaultstr = params.get(PARAM_DEFAULT);
    final int section = TextUtil.parseIntParameter(params.get(PARAM_SECTION), -1);
    int maxlen = TextUtil.parseIntParameter(params.get(PARAM_MAXLENGTH), -1);
    final ResourceBundle rb = Preferences.getBundle(context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE);
    if (style == null) {
        style = DEFAULT_STYLE;
    }
    if (maxlen == -1) {
        maxlen = Integer.MAX_VALUE;
    }
    if (includedPage != null) {
        final Page page;
        try {
            final String pageName = engine.getFinalPageName(includedPage);
            if (pageName != null) {
                page = engine.getManager(PageManager.class).getPage(pageName);
            } else {
                page = engine.getManager(PageManager.class).getPage(includedPage);
            }
        } catch (final ProviderException e) {
            res.append("<span class=\"error\">Page could not be found by the page provider.</span>");
            return res.toString();
        }
        if (page != null) {
            // Check for recursivity
            List<String> previousIncludes = context.getVariable(ATTR_RECURSE);
            if (previousIncludes != null) {
                if (previousIncludes.contains(page.getName())) {
                    return "<span class=\"error\">Error: Circular reference - you can't include a page in itself!</span>";
                }
            } else {
                previousIncludes = new ArrayList<>();
            }
            // Check for permissions
            final AuthorizationManager mgr = engine.getManager(AuthorizationManager.class);
            if (!mgr.checkPermission(context.getWikiSession(), PermissionFactory.getPagePermission(page, "view"))) {
                res.append("<span class=\"error\">You do not have permission to view this included page.</span>");
                return res.toString();
            }
            // Show Once
            // Check for page-cookie, only include page if cookie is not yet set
            String cookieName = "";
            if (showOnce) {
                cookieName = ONCE_COOKIE + TextUtil.urlEncodeUTF8(page.getName()).replaceAll("\\+", "%20");
                if (HttpUtil.retrieveCookieValue(context.getHttpRequest(), cookieName) != null) {
                    // silent exit
                    return "";
                }
            }
            // move here, after premature exit points (permissions, page-cookie)
            previousIncludes.add(page.getName());
            context.setVariable(ATTR_RECURSE, previousIncludes);
            /**
             *  We want inclusion to occur within the context of
             *  its own page, because we need the links to be correct.
             */
            final Context includedContext = context.clone();
            includedContext.setPage(page);
            String pageData = engine.getManager(PageManager.class).getPureText(page);
            String moreLink = "";
            if (section != -1) {
                try {
                    pageData = TextUtil.getSection(pageData, section);
                } catch (final IllegalArgumentException e) {
                    throw new PluginException(e.getMessage());
                }
            }
            if (pageData.length() > maxlen) {
                pageData = pageData.substring(0, maxlen) + " ...";
                moreLink = "<p><a href=\"" + context.getURL(ContextEnum.PAGE_VIEW.getRequestContext(), includedPage) + "\">" + rb.getString("insertpage.more") + "</a></p>";
            }
            res.append("<div class=\"inserted-page ");
            if (clazz != null)
                res.append(clazz);
            if (!style.equals(DEFAULT_STYLE))
                res.append("\" style=\"").append(style);
            if (showOnce)
                res.append("\" data-once=\"").append(cookieName);
            res.append("\" >");
            res.append(engine.getManager(RenderingManager.class).textToHTML(includedContext, pageData));
            res.append(moreLink);
            res.append("</div>");
            // 
            // Remove the name from the stack; we're now done with this.
            // 
            previousIncludes.remove(page.getName());
            context.setVariable(ATTR_RECURSE, previousIncludes);
        } else {
            if (defaultstr != null) {
                res.append(defaultstr);
            } else {
                res.append("There is no page called '").append(includedPage).append("'.  Would you like to ");
                res.append("<a href=\"").append(context.getURL(ContextEnum.PAGE_EDIT.getRequestContext(), includedPage)).append("\">create it?</a>");
            }
        }
    } else {
        res.append("<span class=\"error\">");
        res.append("You have to define a page!");
        res.append("</span>");
    }
    return res.toString();
}
Also used : Context(org.apache.wiki.api.core.Context) ProviderException(org.apache.wiki.api.exceptions.ProviderException) PluginException(org.apache.wiki.api.exceptions.PluginException) Page(org.apache.wiki.api.core.Page) PageManager(org.apache.wiki.pages.PageManager) ResourceBundle(java.util.ResourceBundle) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager) Engine(org.apache.wiki.api.core.Engine)

Aggregations

Context (org.apache.wiki.api.core.Context)81 Page (org.apache.wiki.api.core.Page)46 PageManager (org.apache.wiki.pages.PageManager)42 Test (org.junit.jupiter.api.Test)40 RenderingManager (org.apache.wiki.render.RenderingManager)15 PageContext (javax.servlet.jsp.PageContext)11 Engine (org.apache.wiki.api.core.Engine)9 ReferenceManager (org.apache.wiki.references.ReferenceManager)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 Date (java.util.Date)6 ServletContext (javax.servlet.ServletContext)6 ProviderException (org.apache.wiki.api.exceptions.ProviderException)6 WikiContext (org.apache.wiki.WikiContext)5 StringReader (java.io.StringReader)4 Properties (java.util.Properties)4 MockHttpServletRequest (net.sourceforge.stripes.mock.MockHttpServletRequest)4 WikiSessionTest (org.apache.wiki.WikiSessionTest)4 Attachment (org.apache.wiki.api.core.Attachment)4 SearchResult (org.apache.wiki.api.search.SearchResult)4