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.");
}
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();
}
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"));
}
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);
}
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 */
}
}
Aggregations