Search in sources :

Example 21 with ProviderException

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);
    }
}
Also used : WikiSession(org.apache.wiki.WikiSession) ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiPage(org.apache.wiki.WikiPage) WikiPermission(org.apache.wiki.auth.permissions.WikiPermission) PagePermission(org.apache.wiki.auth.permissions.PagePermission) AllPermission(org.apache.wiki.auth.permissions.AllPermission) Permission(java.security.Permission) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Example 22 with ProviderException

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");
    }
}
Also used : WikiSession(org.apache.wiki.WikiSession) ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiPage(org.apache.wiki.WikiPage) WikiPermission(org.apache.wiki.auth.permissions.WikiPermission) PagePermission(org.apache.wiki.auth.permissions.PagePermission) AllPermission(org.apache.wiki.auth.permissions.AllPermission) Permission(java.security.Permission) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Example 23 with ProviderException

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);
            }
        }
    }
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiPage(org.apache.wiki.WikiPage) WikiEngine(org.apache.wiki.WikiEngine)

Example 24 with ProviderException

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();
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) Calendar(java.util.Calendar) WikiPage(org.apache.wiki.WikiPage) PluginException(org.apache.wiki.api.exceptions.PluginException) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager) ParseException(java.text.ParseException) WikiEngine(org.apache.wiki.WikiEngine) SimpleDateFormat(java.text.SimpleDateFormat) PagePermission(org.apache.wiki.auth.permissions.PagePermission)

Example 25 with ProviderException

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;
}
Also used : PageManager(org.apache.wiki.PageManager) Set(java.util.Set) WikiPage(org.apache.wiki.WikiPage) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Date(java.util.Date) PluginException(org.apache.wiki.api.exceptions.PluginException) ParseException(java.text.ParseException) ProviderException(org.apache.wiki.api.exceptions.ProviderException)

Aggregations

ProviderException (org.apache.wiki.api.exceptions.ProviderException)42 WikiPage (org.apache.wiki.WikiPage)19 Attachment (org.apache.wiki.attachment.Attachment)16 IOException (java.io.IOException)13 WikiEngine (org.apache.wiki.WikiEngine)11 Collection (java.util.Collection)9 Iterator (java.util.Iterator)9 File (java.io.File)8 InputStream (java.io.InputStream)7 Date (java.util.Date)6 PagePermission (org.apache.wiki.auth.permissions.PagePermission)6 Permission (java.security.Permission)5 ArrayList (java.util.ArrayList)5 WikiContext (org.apache.wiki.WikiContext)5 PluginException (org.apache.wiki.api.exceptions.PluginException)5 AuthorizationManager (org.apache.wiki.auth.AuthorizationManager)5 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)4 BufferedInputStream (java.io.BufferedInputStream)3 FileInputStream (java.io.FileInputStream)3 OutputStream (java.io.OutputStream)3