Search in sources :

Example 1 with FilterManager

use of org.apache.wiki.api.engine.FilterManager in project jspwiki by apache.

the class ApprovalWorkflowTest method testSaveWikiPageWithException.

@Test
public void testSaveWikiPageWithException() throws WikiException {
    // Add a PageFilter that rejects all save attempts
    FilterManager fm = m_engine.getFilterManager();
    fm.addPageFilter(new AbortFilter(), 0);
    // Create a sample test page and try to save it
    String pageName = "SaveWikiPageWorkflow-Test" + System.currentTimeMillis();
    String text = "This is a test!";
    try {
        m_engine.saveTextAsJanne(pageName, text);
    } catch (WikiException e) {
        Assert.assertTrue(e instanceof FilterException);
        Assert.assertEquals("Page save aborted.", e.getMessage());
        return;
    }
    Assert.fail("Page save should have thrown a FilterException, but didn't.");
}
Also used : WikiException(org.apache.wiki.api.exceptions.WikiException) FilterException(org.apache.wiki.api.exceptions.FilterException) FilterManager(org.apache.wiki.api.engine.FilterManager) Test(org.junit.Test)

Example 2 with FilterManager

use of org.apache.wiki.api.engine.FilterManager in project jspwiki by apache.

the class TableOfContents method execute.

/**
 *  {@inheritDoc}
 */
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    WikiEngine engine = context.getEngine();
    WikiPage page = context.getPage();
    ResourceBundle rb = Preferences.getBundle(context, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
    if (context.getVariable(VAR_ALREADY_PROCESSING) != null) {
        // return rb.getString("tableofcontents.title");
        return "<a href=\"#section-TOC\" class=\"toc\">" + rb.getString("tableofcontents.title") + "</a>";
    }
    StringBuilder sb = new StringBuilder();
    sb.append("<div class=\"toc\">\n");
    sb.append("<div class=\"collapsebox\">\n");
    String title = params.get(PARAM_TITLE);
    sb.append("<h4 id=\"section-TOC\">");
    if (title != null) {
        // sb.append("<h4>"+TextUtil.replaceEntities(title)+"</h4>\n");
        sb.append(TextUtil.replaceEntities(title));
    } else {
        // sb.append("<h4>"+rb.getString("tableofcontents.title")+"</h4>\n");
        sb.append(rb.getString("tableofcontents.title"));
    }
    sb.append("</h4>\n");
    // should we use an ordered list?
    m_usingNumberedList = false;
    if (params.containsKey(PARAM_NUMBERED)) {
        String numbered = params.get(PARAM_NUMBERED);
        if (numbered.equalsIgnoreCase("true")) {
            m_usingNumberedList = true;
        } else if (numbered.equalsIgnoreCase("yes")) {
            m_usingNumberedList = true;
        }
    }
    // if we are using a numbered list, get the rest of the parameters (if any) ...
    if (m_usingNumberedList) {
        int start = 0;
        String startStr = params.get(PARAM_START);
        if ((startStr != null) && (startStr.matches("^\\d+$"))) {
            start = Integer.parseInt(startStr);
        }
        if (start < 0)
            start = 0;
        m_starting = start;
        m_level1Index = start - 1;
        if (m_level1Index < 0)
            m_level1Index = 0;
        m_level2Index = 0;
        m_level3Index = 0;
        m_prefix = params.get(PARAM_PREFIX);
        if (m_prefix == null)
            m_prefix = "";
        m_lastLevel = Heading.HEADING_LARGE;
    }
    try {
        String wikiText = engine.getPureText(page);
        boolean runFilters = "true".equals(engine.getVariableManager().getValue(context, WikiEngine.PROP_RUNFILTERS, "true"));
        if (runFilters) {
            try {
                FilterManager fm = engine.getFilterManager();
                wikiText = fm.doPreTranslateFiltering(context, wikiText);
            } catch (Exception e) {
                log.error("Could not construct table of contents: Filter Error", e);
                throw new PluginException("Unable to construct table of contents (see logs)");
            }
        }
        context.setVariable(VAR_ALREADY_PROCESSING, "x");
        MarkupParser parser = engine.getRenderingManager().getParser(context, wikiText);
        parser.addHeadingListener(this);
        parser.parse();
        sb.append("<ul>\n" + m_buf.toString() + "</ul>\n");
    } catch (IOException e) {
        log.error("Could not construct table of contents", e);
        throw new PluginException("Unable to construct table of contents (see logs)");
    }
    sb.append("</div>\n</div>\n");
    return sb.toString();
}
Also used : WikiPage(org.apache.wiki.WikiPage) PluginException(org.apache.wiki.api.exceptions.PluginException) ResourceBundle(java.util.ResourceBundle) IOException(java.io.IOException) WikiEngine(org.apache.wiki.WikiEngine) IOException(java.io.IOException) InternalWikiException(org.apache.wiki.InternalWikiException) PluginException(org.apache.wiki.api.exceptions.PluginException) FilterManager(org.apache.wiki.api.engine.FilterManager) MarkupParser(org.apache.wiki.parser.MarkupParser)

Example 3 with FilterManager

use of org.apache.wiki.api.engine.FilterManager in project jspwiki by apache.

the class DefaultFilterManagerTest method testInitParams.

@Test
public void testInitParams() throws Exception {
    FilterManager m = new DefaultFilterManager(engine, props);
    List l = m.getFilterList();
    Iterator i = l.iterator();
    i.next();
    TestFilter f2 = (TestFilter) i.next();
    Properties p = f2.m_properties;
    Assert.assertEquals("no foobar", "Zippadippadai", p.getProperty("foobar"));
    Assert.assertEquals("no blatblaa", "5", p.getProperty("blatblaa"));
}
Also used : Iterator(java.util.Iterator) List(java.util.List) Properties(java.util.Properties) FilterManager(org.apache.wiki.api.engine.FilterManager) Test(org.junit.Test)

Example 4 with FilterManager

use of org.apache.wiki.api.engine.FilterManager in project jspwiki by apache.

the class DefaultFilterManagerTest method testInitFilters.

@Test
public void testInitFilters() throws Exception {
    FilterManager m = new DefaultFilterManager(engine, props);
    List l = m.getFilterList();
    Assert.assertEquals("Wrong number of filters", 2, l.size());
    Iterator i = l.iterator();
    PageFilter f1 = (PageFilter) i.next();
    Assert.assertTrue("Not a Profanityfilter", f1 instanceof ProfanityFilter);
    PageFilter f2 = (PageFilter) i.next();
    Assert.assertTrue("Not a Testfilter", f2 instanceof TestFilter);
}
Also used : Iterator(java.util.Iterator) List(java.util.List) PageFilter(org.apache.wiki.api.filters.PageFilter) FilterManager(org.apache.wiki.api.engine.FilterManager) Test(org.junit.Test)

Example 5 with FilterManager

use of org.apache.wiki.api.engine.FilterManager in project jspwiki by apache.

the class UserManager method validateProfile.

/**
 * Validates a user profile, and appends any errors to the session errors
 * list. If the profile is new, the password will be checked to make sure it
 * isn't null. Otherwise, the password is checked for length and that it
 * matches the value of the 'password2' HTTP parameter. Note that we have a
 * special case when container-managed authentication is used and the user
 * is not authenticated; this will always cause validation to fail. Any
 * validation errors are added to the wiki session's messages collection
 * (see {@link WikiSession#getMessages()}.
 * @param context the current wiki context
 * @param profile the supplied UserProfile
 */
public void validateProfile(WikiContext context, UserProfile profile) {
    final boolean isNew = profile.isNew();
    final WikiSession session = context.getWikiSession();
    final InputValidator validator = new InputValidator(SESSION_MESSAGES, context);
    final ResourceBundle rb = Preferences.getBundle(context, InternationalizationManager.CORE_BUNDLE);
    // 
    // Query the SpamFilter first
    // 
    final FilterManager fm = m_engine.getFilterManager();
    final List<PageFilter> ls = fm.getFilterList();
    for (final PageFilter pf : ls) {
        if (pf instanceof SpamFilter) {
            if (((SpamFilter) pf).isValidUserProfile(context, profile) == false) {
                session.addMessage(SESSION_MESSAGES, "Invalid userprofile");
                return;
            }
            break;
        }
    }
    // If container-managed auth and user not logged in, throw an error
    if (m_engine.getAuthenticationManager().isContainerAuthenticated() && !context.getWikiSession().isAuthenticated()) {
        session.addMessage(SESSION_MESSAGES, rb.getString("security.error.createprofilebeforelogin"));
    }
    validator.validateNotNull(profile.getLoginName(), rb.getString("security.user.loginname"));
    validator.validateNotNull(profile.getFullname(), rb.getString("security.user.fullname"));
    validator.validate(profile.getEmail(), rb.getString("security.user.email"), InputValidator.EMAIL);
    // If new profile, passwords must match and can't be null
    if (!m_engine.getAuthenticationManager().isContainerAuthenticated()) {
        final String password = profile.getPassword();
        if (password == null) {
            if (isNew) {
                session.addMessage(SESSION_MESSAGES, rb.getString("security.error.blankpassword"));
            }
        } else {
            final HttpServletRequest request = context.getHttpRequest();
            final String password2 = (request == null) ? null : request.getParameter("password2");
            if (!password.equals(password2)) {
                session.addMessage(SESSION_MESSAGES, rb.getString("security.error.passwordnomatch"));
            }
        }
    }
    UserProfile otherProfile;
    final String fullName = profile.getFullname();
    final String loginName = profile.getLoginName();
    final String email = profile.getEmail();
    // It's illegal to use as a full name someone else's login name
    try {
        otherProfile = getUserDatabase().find(fullName);
        if (otherProfile != null && !profile.equals(otherProfile) && !fullName.equals(otherProfile.getFullname())) {
            final Object[] args = { fullName };
            session.addMessage(SESSION_MESSAGES, MessageFormat.format(rb.getString("security.error.illegalfullname"), args));
        }
    } catch (final NoSuchPrincipalException e) {
    /* It's clean */
    }
    // It's illegal to use as a login name someone else's full name
    try {
        otherProfile = getUserDatabase().find(loginName);
        if (otherProfile != null && !profile.equals(otherProfile) && !loginName.equals(otherProfile.getLoginName())) {
            final Object[] args = { loginName };
            session.addMessage(SESSION_MESSAGES, MessageFormat.format(rb.getString("security.error.illegalloginname"), args));
        }
    } catch (final NoSuchPrincipalException e) {
    /* It's clean */
    }
    // It's illegal to use multiple accounts with the same email
    try {
        otherProfile = getUserDatabase().findByEmail(email);
        if (otherProfile != null && // Issue JSPWIKI-1042
        !profile.getUid().equals(otherProfile.getUid()) && !profile.equals(otherProfile) && StringUtils.lowerCase(email).equals(StringUtils.lowerCase(otherProfile.getEmail()))) {
            final Object[] args = { email };
            session.addMessage(SESSION_MESSAGES, MessageFormat.format(rb.getString("security.error.email.taken"), args));
        }
    } catch (final NoSuchPrincipalException e) {
    /* It's clean */
    }
}
Also used : UserProfile(org.apache.wiki.auth.user.UserProfile) SpamFilter(org.apache.wiki.filters.SpamFilter) FilterManager(org.apache.wiki.api.engine.FilterManager) HttpServletRequest(javax.servlet.http.HttpServletRequest) WikiSession(org.apache.wiki.WikiSession) InputValidator(org.apache.wiki.ui.InputValidator) ResourceBundle(java.util.ResourceBundle) PageFilter(org.apache.wiki.api.filters.PageFilter)

Aggregations

FilterManager (org.apache.wiki.api.engine.FilterManager)6 Test (org.junit.Test)3 IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 List (java.util.List)2 ResourceBundle (java.util.ResourceBundle)2 FilterException (org.apache.wiki.api.exceptions.FilterException)2 WikiException (org.apache.wiki.api.exceptions.WikiException)2 PageFilter (org.apache.wiki.api.filters.PageFilter)2 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Date (java.util.Date)1 Properties (java.util.Properties)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 InternalWikiException (org.apache.wiki.InternalWikiException)1 WikiEngine (org.apache.wiki.WikiEngine)1 WikiPage (org.apache.wiki.WikiPage)1 WikiSession (org.apache.wiki.WikiSession)1 AdminBeanManager (org.apache.wiki.api.engine.AdminBeanManager)1 PluginManager (org.apache.wiki.api.engine.PluginManager)1