use of org.apache.wiki.api.exceptions.ProviderException in project jspwiki by apache.
the class AuthorizationManagerTest method testRoleAcl.
@Test
public void testRoleAcl() throws Exception {
// Create test page & attachment
String src = "[{ALLOW edit Authenticated}] ";
m_engine.saveText("Test", src);
WikiPage p = m_engine.getPage("Test");
Permission view = PermissionFactory.getPagePermission(p, "view");
Permission edit = PermissionFactory.getPagePermission(p, "edit");
// Create session with authenticated user 'Alice', who can read & edit
WikiSession session;
session = WikiSessionTest.authenticatedSession(m_engine, Users.ALICE, Users.ALICE_PASS);
Assert.assertTrue("Alice view Test", m_auth.checkPermission(session, view));
Assert.assertTrue("Alice edit Test", m_auth.checkPermission(session, edit));
// Create session with asserted user 'Bob', who can't read or edit (not in ACL)
session = WikiSessionTest.assertedSession(m_engine, Users.BOB);
Assert.assertFalse("Bob !view Test", m_auth.checkPermission(session, view));
Assert.assertFalse("Bob !edit Test", m_auth.checkPermission(session, edit));
// Cleanup
try {
m_engine.deletePage("Test");
} catch (ProviderException e) {
Assert.assertTrue(false);
}
}
use of org.apache.wiki.api.exceptions.ProviderException in project jspwiki by apache.
the class AuthorizationManagerTest method testPrincipalAcl.
@Test
public void testPrincipalAcl() throws Exception {
// Create test page & attachment
String src = "[{ALLOW edit Alice}] ";
m_engine.saveText("Test", src);
WikiPage p = m_engine.getPage("Test");
Permission view = PermissionFactory.getPagePermission(p, "view");
Permission edit = PermissionFactory.getPagePermission(p, "edit");
// Create session with authenticated user 'Alice', who can read & edit (in ACL)
WikiSession session;
session = WikiSessionTest.authenticatedSession(m_engine, Users.ALICE, Users.ALICE_PASS);
Assert.assertTrue("Alice view Test", m_auth.checkPermission(session, view));
Assert.assertTrue("Alice edit Test", m_auth.checkPermission(session, edit));
// Create session with authenticated user 'Bob', who can't read or edit (not in ACL)
session = WikiSessionTest.authenticatedSession(m_engine, Users.BOB, Users.BOB_PASS);
Assert.assertFalse("Bob !view Test", m_auth.checkPermission(session, view));
Assert.assertFalse("Bob !edit Test", m_auth.checkPermission(session, edit));
// Cleanup
try {
m_engine.deletePage("Test");
} catch (ProviderException e) {
Assert.fail("Could not delete page");
}
}
use of org.apache.wiki.api.exceptions.ProviderException in project jspwiki by apache.
the class PageRenamer method updateReferrers.
/**
* This method finds all the pages which have anything to do with the fromPage and
* change any referrers it can figure out in that page.
*
* @param context WikiContext in which we operate
* @param fromPage The old page
* @param toPage The new page
*/
private void updateReferrers(WikiContext context, WikiPage fromPage, WikiPage toPage, Set<String> referrers) {
WikiEngine engine = context.getEngine();
// No referrers
if (referrers.isEmpty())
return;
for (String pageName : referrers) {
// small kludge.
if (pageName.equals(fromPage.getName())) {
pageName = toPage.getName();
}
WikiPage p = engine.getPage(pageName);
String sourceText = engine.getPureText(p);
String newText = replaceReferrerString(context, sourceText, fromPage.getName(), toPage.getName());
if (m_camelCase)
newText = replaceCCReferrerString(context, newText, fromPage.getName(), toPage.getName());
if (!sourceText.equals(newText)) {
p.setAttribute(WikiPage.CHANGENOTE, fromPage.getName() + " ==> " + toPage.getName());
p.setAuthor(context.getCurrentUser().getName());
try {
engine.getPageManager().putPageText(p, newText);
engine.updateReferences(p);
} catch (ProviderException e) {
//
// We fail with an error, but we will try to continue to rename
// other referrers as well.
//
log.error("Unable to perform rename.", e);
}
}
}
}
use of org.apache.wiki.api.exceptions.ProviderException in project jspwiki by apache.
the class WeblogPlugin method execute.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
Calendar startTime;
Calendar stopTime;
int numDays = DEFAULT_DAYS;
WikiEngine engine = context.getEngine();
AuthorizationManager mgr = engine.getAuthorizationManager();
//
// Parse parameters.
//
String days;
DateFormat entryFormat;
String startDay = null;
boolean hasComments = false;
int maxEntries;
String weblogName;
if ((weblogName = params.get(PARAM_PAGE)) == null) {
weblogName = context.getPage().getName();
}
if ((days = context.getHttpParameter("weblog." + PARAM_DAYS)) == null) {
days = params.get(PARAM_DAYS);
}
if ((params.get(PARAM_ENTRYFORMAT)) == null) {
entryFormat = Preferences.getDateFormat(context, TimeFormat.DATETIME);
} else {
entryFormat = new SimpleDateFormat(params.get(PARAM_ENTRYFORMAT));
}
if (days != null) {
if (days.equalsIgnoreCase("all")) {
numDays = Integer.MAX_VALUE;
} else {
numDays = TextUtil.parseIntParameter(days, DEFAULT_DAYS);
}
}
if ((startDay = params.get(PARAM_STARTDATE)) == null) {
startDay = context.getHttpParameter("weblog." + PARAM_STARTDATE);
}
if (TextUtil.isPositive(params.get(PARAM_ALLOWCOMMENTS))) {
hasComments = true;
}
maxEntries = TextUtil.parseIntParameter(params.get(PARAM_MAXENTRIES), Integer.MAX_VALUE);
//
// Determine the date range which to include.
//
startTime = Calendar.getInstance();
stopTime = Calendar.getInstance();
if (startDay != null) {
SimpleDateFormat fmt = new SimpleDateFormat(DEFAULT_DATEFORMAT);
try {
Date d = fmt.parse(startDay);
startTime.setTime(d);
stopTime.setTime(d);
} catch (ParseException e) {
return "Illegal time format: " + startDay;
}
}
//
// Mark this to be a weblog
//
context.getPage().setAttribute(ATTR_ISWEBLOG, "true");
//
// We make a wild guess here that nobody can do millisecond
// accuracy here.
//
startTime.add(Calendar.DAY_OF_MONTH, -numDays);
startTime.set(Calendar.HOUR, 0);
startTime.set(Calendar.MINUTE, 0);
startTime.set(Calendar.SECOND, 0);
stopTime.set(Calendar.HOUR, 23);
stopTime.set(Calendar.MINUTE, 59);
stopTime.set(Calendar.SECOND, 59);
StringBuilder sb = new StringBuilder();
try {
List<WikiPage> blogEntries = findBlogEntries(engine, weblogName, startTime.getTime(), stopTime.getTime());
Collections.sort(blogEntries, new PageDateComparator());
sb.append("<div class=\"weblog\">\n");
for (Iterator<WikiPage> i = blogEntries.iterator(); i.hasNext() && maxEntries-- > 0; ) {
WikiPage p = i.next();
if (mgr.checkPermission(context.getWikiSession(), new PagePermission(p, PagePermission.VIEW_ACTION))) {
addEntryHTML(context, entryFormat, hasComments, sb, p);
}
}
sb.append("</div>\n");
} catch (ProviderException e) {
log.error("Could not locate blog entries", e);
throw new PluginException("Could not locate blog entries: " + e.getMessage());
}
return sb.toString();
}
use of org.apache.wiki.api.exceptions.ProviderException in project jspwiki by apache.
the class WeblogPlugin method findBlogEntries.
/**
* Attempts to locate all pages that correspond to the
* blog entry pattern. Will only consider the days on the dates; not the hours and minutes.
*
* @param engine WikiEngine which is used to get the pages
* @param baseName The basename (e.g. "Main" if you want "Main_blogentry_xxxx")
* @param start The date which is the first to be considered
* @param end The end date which is the last to be considered
* @return a list of pages with their FIRST revisions.
* @throws ProviderException If something goes wrong
*/
public List findBlogEntries(WikiEngine engine, String baseName, Date start, Date end) throws ProviderException {
PageManager mgr = engine.getPageManager();
Set allPages = engine.getReferenceManager().findCreated();
ArrayList<WikiPage> result = new ArrayList<WikiPage>();
baseName = makeEntryPage(baseName);
for (Iterator i = allPages.iterator(); i.hasNext(); ) {
String pageName = (String) i.next();
if (pageName.startsWith(baseName)) {
try {
WikiPage firstVersion = mgr.getPageInfo(pageName, 1);
Date d = firstVersion.getLastModified();
if (d.after(start) && d.before(end)) {
result.add(firstVersion);
}
} catch (Exception e) {
log.debug("Page name :" + pageName + " was suspected as a blog entry but it isn't because of parsing errors", e);
}
}
}
return result;
}
Aggregations