Search in sources :

Example 16 with ProviderException

use of org.apache.wiki.api.exceptions.ProviderException in project jspwiki by apache.

the class ReferenceManager method initialize.

/**
 *  Initializes the entire reference manager with the initial set of pages
 *  from the collection.
 *
 *  @param pages A collection of all pages you want to be included in the reference
 *               count.
 *  @since 2.2
 *  @throws ProviderException If reading of pages fail.
 */
public void initialize(Collection pages) throws ProviderException {
    log.debug("Initializing new ReferenceManager with " + pages.size() + " initial pages.");
    StopWatch sw = new StopWatch();
    sw.start();
    log.info("Starting cross reference scan of WikiPages");
    // 
    try {
        // 
        // Unserialize things.  The loop below cannot be combined with
        // the other loop below, simply because engine.getPage() has
        // side effects such as loading initializing the user databases,
        // which in turn want all of the pages to be read already...
        // 
        // Yes, this is a kludge.  We know.  Will be fixed.
        // 
        long saved = unserializeFromDisk();
        for (Iterator it = pages.iterator(); it.hasNext(); ) {
            WikiPage page = (WikiPage) it.next();
            unserializeAttrsFromDisk(page);
        }
        // 
        // Now we must check if any of the pages have been changed
        // while we were in the electronic la-la-land, and update
        // the references for them.
        // 
        Iterator it = pages.iterator();
        while (it.hasNext()) {
            WikiPage page = (WikiPage) it.next();
            if (page instanceof Attachment) {
            // Skip attachments
            } else {
                // Refresh with the latest copy
                page = m_engine.getPage(page.getName());
                if (page.getLastModified() == null) {
                    log.fatal("Provider returns null lastModified.  Please submit a bug report.");
                } else if (page.getLastModified().getTime() > saved) {
                    updatePageReferences(page);
                }
            }
        }
    } catch (Exception e) {
        log.info("Unable to unserialize old refmgr information, rebuilding database: " + e.getMessage());
        buildKeyLists(pages);
        // Scan the existing pages from disk and update references in the manager.
        Iterator it = pages.iterator();
        while (it.hasNext()) {
            WikiPage page = (WikiPage) it.next();
            if (page instanceof Attachment) {
            // We cannot build a reference list from the contents
            // of attachments, so we skip them.
            } else {
                updatePageReferences(page);
                serializeAttrsToDisk(page);
            }
        }
        serializeToDisk();
    }
    sw.stop();
    log.info("Cross reference scan done in " + sw);
    WikiEventUtils.addWikiEventListener(m_engine.getPageManager(), WikiPageEvent.PAGE_DELETED, this);
}
Also used : Iterator(java.util.Iterator) Attachment(org.apache.wiki.attachment.Attachment) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConcurrentModificationException(java.util.ConcurrentModificationException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) StopWatch(org.apache.commons.lang.time.StopWatch)

Example 17 with ProviderException

use of org.apache.wiki.api.exceptions.ProviderException in project jspwiki by apache.

the class WikiEngine method beautifyTitle.

/**
 *  Beautifies the title of the page by appending spaces in suitable
 *  places, if the user has so decreed in the properties when constructing
 *  this WikiEngine.  However, attachment names are only beautified by
 *  the name.
 *
 *  @param title The title to beautify
 *  @return A beautified title (or, if beautification is off,
 *          returns the title without modification)
 *  @since 1.7.11
 */
public String beautifyTitle(String title) {
    if (m_beautifyTitle) {
        try {
            Attachment att = m_attachmentManager.getAttachmentInfo(title);
            if (att == null) {
                return TextUtil.beautifyString(title);
            }
            String parent = TextUtil.beautifyString(att.getParentName());
            return parent + "/" + att.getFileName();
        } catch (ProviderException e) {
            return title;
        }
    }
    return title;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) Attachment(org.apache.wiki.attachment.Attachment)

Example 18 with ProviderException

use of org.apache.wiki.api.exceptions.ProviderException in project jspwiki by apache.

the class WikiEngine method getRecentChanges.

/**
 *  Returns a Collection of WikiPages, sorted in time
 *  order of last change (i.e. first object is the most
 *  recently changed).  This method also includes attachments.
 *
 *  @return Collection of WikiPage objects.  In reality, the returned
 *          collection is a Set, but due to API compatibility reasons,
 *          we're not changing the signature soon...
 */
// FIXME: Should really get a Date object and do proper comparisons.
// This is terribly wasteful.
@SuppressWarnings("unchecked")
public Collection getRecentChanges() {
    try {
        Collection<WikiPage> pages = m_pageManager.getAllPages();
        Collection<Attachment> atts = m_attachmentManager.getAllAttachments();
        TreeSet<WikiPage> sortedPages = new TreeSet<WikiPage>(new PageTimeComparator());
        sortedPages.addAll(pages);
        sortedPages.addAll(atts);
        return sortedPages;
    } catch (ProviderException e) {
        log.error("Unable to fetch all pages: ", e);
        return null;
    }
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) TreeSet(java.util.TreeSet) Attachment(org.apache.wiki.attachment.Attachment) PageTimeComparator(org.apache.wiki.util.comparators.PageTimeComparator)

Example 19 with ProviderException

use of org.apache.wiki.api.exceptions.ProviderException in project jspwiki by apache.

the class PageRenamer method getReferencesToChange.

private Set<String> getReferencesToChange(WikiPage fromPage, WikiEngine engine) {
    Set<String> referrers = new TreeSet<String>();
    @SuppressWarnings("unchecked") Collection<String> r = engine.getReferenceManager().findReferrers(fromPage.getName());
    if (r != null)
        referrers.addAll(r);
    try {
        @SuppressWarnings("unchecked") Collection<Attachment> attachments = engine.getAttachmentManager().listAttachments(fromPage);
        for (Attachment att : attachments) {
            @SuppressWarnings("unchecked") Collection<String> c = engine.getReferenceManager().findReferrers(att.getName());
            if (c != null)
                referrers.addAll(c);
        }
    } catch (ProviderException e) {
        // We will continue despite this error
        log.error("Provider error while fetching attachments for rename", e);
    }
    return referrers;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) TreeSet(java.util.TreeSet) Attachment(org.apache.wiki.attachment.Attachment)

Example 20 with ProviderException

use of org.apache.wiki.api.exceptions.ProviderException in project jspwiki by apache.

the class AuthorizationManagerTest method testDefaultPermissions.

/**
 * Tests the default policy. Anonymous users can read, Authenticated can
 * edit, etc. Uses the default tests/etc/jspwiki.policy file installed by
 * the JRE at startup.
 * @throws Exception
 */
@Test
public void testDefaultPermissions() throws Exception {
    // Save a page without an ACL
    m_engine.saveText("TestDefaultPage", "Foo");
    Permission view = PermissionFactory.getPagePermission("*:TestDefaultPage", "view");
    Permission edit = PermissionFactory.getPagePermission("*:TestDefaultPage", "edit");
    WikiSession session;
    // Alice is asserted
    session = WikiSessionTest.assertedSession(m_engine, Users.ALICE);
    Assert.assertTrue("Alice view", m_auth.checkPermission(session, view));
    Assert.assertTrue("Alice edit", m_auth.checkPermission(session, edit));
    // Bob is logged in
    session = WikiSessionTest.authenticatedSession(m_engine, Users.BOB, Users.BOB_PASS);
    Assert.assertTrue("Bob view", m_auth.checkPermission(session, view));
    Assert.assertTrue("Bob edit", m_auth.checkPermission(session, edit));
    // Delete the test page
    try {
        m_engine.deletePage("TestDefaultPage");
    } catch (ProviderException e) {
        Assert.assertTrue(false);
    }
}
Also used : WikiSession(org.apache.wiki.WikiSession) ProviderException(org.apache.wiki.api.exceptions.ProviderException) 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)

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