Search in sources :

Example 6 with Engine

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

the class Note method imageUrl.

private String imageUrl(final Context ctx) {
    final Engine engine = ctx.getEngine();
    String commentImage = engine.getWikiProperties().getProperty(PROP_NOTE_IMAGE, DEFAULT_NOTE_IMAGE);
    commentImage = "images/" + commentImage;
    String resource = engine.getManager(TemplateManager.class).findResource(ctx, engine.getTemplateDir(), commentImage);
    // JSPWIKI-876 Fixed error with Note Plugin. Only one preceding "/" is needed.
    if (resource != null && resource.startsWith("/")) {
        resource = resource.substring(1);
    }
    return ctx.getURL(ContextEnum.PAGE_NONE.getRequestContext(), resource);
}
Also used : TemplateManager(org.apache.wiki.ui.TemplateManager) Engine(org.apache.wiki.api.core.Engine)

Example 7 with Engine

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

the class BugReportHandler method findNextPage.

/**
 *  Finds a free page name for adding the bug report.  Tries to construct a page, and if it's found, adds a number to it
 *  and tries again.
 */
private synchronized String findNextPage(final Context context, final String title, final String baseName) {
    final String basicPageName = ((baseName != null) ? baseName : "Bug") + MarkupParser.cleanLink(title);
    final Engine engine = context.getEngine();
    String pageName = basicPageName;
    long lastbug = 2;
    while (engine.getManager(PageManager.class).wikiPageExists(pageName)) {
        pageName = basicPageName + lastbug++;
    }
    return pageName;
}
Also used : PageManager(org.apache.wiki.pages.PageManager) Engine(org.apache.wiki.api.core.Engine)

Example 8 with Engine

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

the class Image method execute.

/**
 *  {@inheritDoc}
 */
@Override
public String execute(final Context context, final Map<String, String> params) throws PluginException {
    final Engine engine = context.getEngine();
    String src = getCleanParameter(params, PARAM_SRC);
    final String align = getCleanParameter(params, PARAM_ALIGN);
    final String ht = getCleanParameter(params, PARAM_HEIGHT);
    final String wt = getCleanParameter(params, PARAM_WIDTH);
    final String alt = getCleanParameter(params, PARAM_ALT);
    final String caption = getCleanParameter(params, PARAM_CAPTION);
    final String link = getCleanParameter(params, PARAM_LINK);
    String target = getCleanParameter(params, PARAM_TARGET);
    final String style = getCleanParameter(params, PARAM_STYLE);
    final String cssclass = getCleanParameter(params, PARAM_CLASS);
    // String map        = getCleanParameter( params, PARAM_MAP );
    final String border = getCleanParameter(params, PARAM_BORDER);
    final String title = getCleanParameter(params, PARAM_TITLE);
    if (src == null) {
        throw new PluginException("Parameter 'src' is required for Image plugin");
    }
    if (target != null && !validTargetValue(target)) {
        // not a valid value so ignore
        target = null;
    }
    try {
        final AttachmentManager mgr = engine.getManager(AttachmentManager.class);
        final Attachment att = mgr.getAttachmentInfo(context, src);
        if (att != null) {
            src = context.getURL(ContextEnum.PAGE_ATTACH.getRequestContext(), att.getName());
        }
    } catch (final ProviderException e) {
        throw new PluginException("Attachment info failed: " + e.getMessage());
    }
    final StringBuilder result = new StringBuilder();
    result.append("<table border=\"0\" class=\"imageplugin\"");
    if (title != null) {
        result.append(" title=\"").append(title).append("\"");
    }
    if (align != null) {
        if (align.equals("center")) {
            result.append(" style=\"margin-left: auto; margin-right: auto; text-align:center; vertical-align:middle;\"");
        } else {
            result.append(" style=\"float:").append(align).append(";\"");
        }
    }
    result.append(">\n");
    if (caption != null) {
        result.append("<caption>").append(caption).append("</caption>\n");
    }
    // move css class and style to the container of the image, so it doesn't affect the caption
    result.append("<tr><td");
    if (cssclass != null) {
        result.append(" class=\"").append(cssclass).append("\"");
    }
    if (style != null) {
        result.append(" style=\"").append(style);
        // Make sure that we add a ";" to the end of the style string
        if (result.charAt(result.length() - 1) != ';')
            result.append(";");
        result.append("\"");
    }
    result.append(">");
    if (link != null) {
        result.append("<a href=\"").append(link).append("\"");
        if (target != null) {
            result.append(" target=\"").append(target).append("\"");
        }
        result.append(">");
    }
    result.append("<img src=\"").append(src).append("\"");
    if (ht != null) {
        result.append(" height=\"").append(ht).append("\"");
    }
    if (wt != null) {
        result.append(" width=\"").append(wt).append("\"");
    }
    if (alt != null) {
        result.append(" alt=\"").append(alt).append("\"");
    }
    if (border != null) {
        result.append(" border=\"").append(border).append("\"");
    }
    // if( map != null )    result.append(" map=\""+map+"\"");
    result.append(" />");
    if (link != null) {
        result.append("</a>");
    }
    result.append("</td></tr>\n");
    result.append("</table>\n");
    return result.toString();
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) PluginException(org.apache.wiki.api.exceptions.PluginException) Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) Engine(org.apache.wiki.api.core.Engine)

Example 9 with Engine

use of org.apache.wiki.api.core.Engine 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)

Example 10 with Engine

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

the class MetaWeblogHandler method newPost.

/**
 *  Adds a new post to the blog.
 *
 *  @param blogid The id of the blog.
 *  @param username The username to use
 *  @param password The password
 *  @param content As per Metaweblogapi contract
 *  @param publish This parameter is ignored for JSPWiki.
 *  @return Returns an empty string
 *  @throws XmlRpcException If something goes wrong
 */
public String newPost(final String blogid, final String username, final String password, final Hashtable<String, Object> content, final boolean publish) throws XmlRpcException {
    log.info("metaWeblog.newPost() called");
    final Engine engine = m_context.getEngine();
    final Page page = engine.getManager(PageManager.class).getPage(blogid);
    checkPermissions(page, username, password, "createPages");
    try {
        final WeblogEntryPlugin plugin = new WeblogEntryPlugin();
        final String pageName = plugin.getNewEntryPage(engine, blogid);
        final Page entryPage = Wiki.contents().page(engine, pageName);
        entryPage.setAuthor(username);
        final Context context = Wiki.context().create(engine, entryPage);
        final StringBuilder text = new StringBuilder();
        text.append("!").append(content.get("title"));
        text.append("\n\n");
        text.append(content.get("description"));
        log.debug("Writing entry: " + text);
        engine.getManager(PageManager.class).saveText(context, text.toString());
    } catch (final Exception e) {
        log.error("Failed to create weblog entry", e);
        throw new XmlRpcException(0, "Failed to create weblog entry: " + e.getMessage());
    }
    // FIXME:
    return "";
}
Also used : Context(org.apache.wiki.api.core.Context) PageManager(org.apache.wiki.pages.PageManager) Page(org.apache.wiki.api.core.Page) WeblogEntryPlugin(org.apache.wiki.plugin.WeblogEntryPlugin) Engine(org.apache.wiki.api.core.Engine) XmlRpcException(org.apache.xmlrpc.XmlRpcException) WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Aggregations

Engine (org.apache.wiki.api.core.Engine)60 Page (org.apache.wiki.api.core.Page)30 PageManager (org.apache.wiki.pages.PageManager)20 ProviderException (org.apache.wiki.api.exceptions.ProviderException)11 Attachment (org.apache.wiki.api.core.Attachment)10 JspWriter (javax.servlet.jsp.JspWriter)9 Context (org.apache.wiki.api.core.Context)9 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)8 Element (org.jdom2.Element)7 IOException (java.io.IOException)6 Calendar (java.util.Calendar)6 ResourceBundle (java.util.ResourceBundle)6 SimpleDateFormat (java.text.SimpleDateFormat)5 Date (java.util.Date)5 PageContext (javax.servlet.jsp.PageContext)5 Principal (java.security.Principal)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 PluginException (org.apache.wiki.api.exceptions.PluginException)4 RenderingManager (org.apache.wiki.render.RenderingManager)4 ArrayList (java.util.ArrayList)3