Search in sources :

Example 6 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class WikiEngineTest method testDeletePageAndAttachments2.

@Test
public void testDeletePageAndAttachments2() throws Exception {
    m_engine.saveText(NAME1, "Test");
    Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    m_engine.getAttachmentManager().storeAttachment(att, m_engine.makeAttachmentFile());
    String files = props.getProperty(FileSystemProvider.PROP_PAGEDIR);
    File saved = new File(files, NAME1 + FileSystemProvider.FILE_EXT);
    String atts = props.getProperty(BasicAttachmentProvider.PROP_STORAGEDIR);
    File attfile = new File(atts, NAME1 + "-att/TestAtt.txt-dir");
    Assert.assertTrue("Didn't create it!", saved.exists());
    Assert.assertTrue("Attachment dir does not exist", attfile.exists());
    WikiPage page = m_engine.getPage(NAME1, WikiProvider.LATEST_VERSION);
    Assert.assertNotNull("page", page);
    att = m_engine.getAttachmentManager().getAttachmentInfo(NAME1 + "/TestAtt.txt");
    m_engine.deletePage(att.getName());
    m_engine.deletePage(NAME1);
    Assert.assertNull("Page not removed", m_engine.getPage(NAME1));
    Assert.assertNull("Att not removed", m_engine.getPage(NAME1 + "/TestAtt.txt"));
    Collection refs = m_engine.getReferenceManager().findReferrers(NAME1);
    Assert.assertNull("referrers", refs);
}
Also used : Collection(java.util.Collection) Attachment(org.apache.wiki.attachment.Attachment) File(java.io.File) Test(org.junit.Test)

Example 7 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class WikiEngineTest method testAttachmentRefs3.

/**
 *  Checks, if ReferenceManager is informed if a link to an attachment is added.
 */
@Test
public void testAttachmentRefs3() throws Exception {
    ReferenceManager refMgr = m_engine.getReferenceManager();
    AttachmentManager attMgr = m_engine.getAttachmentManager();
    m_engine.saveText(NAME1, "fooBar");
    Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
    m_engine.saveText(NAME1, " [" + NAME1 + "/TestAtt.txt] ");
    try {
        // and check post-conditions
        Collection c = refMgr.findUncreated();
        Assert.assertTrue("attachment exists", c == null || c.size() == 0);
        c = refMgr.findUnreferenced();
        Assert.assertEquals("unreferenced count", c.size(), 1);
        Assert.assertTrue("unreferenced", ((String) c.iterator().next()).equals(NAME1));
    } finally {
        // do cleanup
        String files = props.getProperty(FileSystemProvider.PROP_PAGEDIR);
        TestEngine.deleteAll(new File(files, NAME1 + BasicAttachmentProvider.DIR_EXTENSION));
    }
}
Also used : Collection(java.util.Collection) Attachment(org.apache.wiki.attachment.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) File(java.io.File) Test(org.junit.Test)

Example 8 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class TestEngine method addAttachment.

/**
 *  Adds an attachment to a page for testing purposes.
 * @param pageName
 * @param attachmentName
 * @param data
 */
public void addAttachment(String pageName, String attachmentName, byte[] data) throws ProviderException, IOException {
    Attachment att = new Attachment(this, pageName, attachmentName);
    getAttachmentManager().storeAttachment(att, new ByteArrayInputStream(data));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Attachment(org.apache.wiki.attachment.Attachment)

Example 9 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class BasicAttachmentProvider method listAllChanged.

/**
 *  {@inheritDoc}
 */
// FIXME: Very unoptimized.
public List listAllChanged(Date timestamp) throws ProviderException {
    File attDir = new File(m_storageDir);
    if (!attDir.exists()) {
        throw new ProviderException("Specified attachment directory " + m_storageDir + " does not exist!");
    }
    ArrayList<Attachment> list = new ArrayList<Attachment>();
    String[] pagesWithAttachments = attDir.list(new AttachmentFilter());
    for (int i = 0; i < pagesWithAttachments.length; i++) {
        String pageId = unmangleName(pagesWithAttachments[i]);
        pageId = pageId.substring(0, pageId.length() - DIR_EXTENSION.length());
        Collection c = listAttachments(new WikiPage(m_engine, pageId));
        for (Iterator it = c.iterator(); it.hasNext(); ) {
            Attachment att = (Attachment) it.next();
            if (att.getLastModified().after(timestamp)) {
                list.add(att);
            }
        }
    }
    Collections.sort(list, new PageTimeComparator());
    return list;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiPage(org.apache.wiki.WikiPage) ArrayList(java.util.ArrayList) Attachment(org.apache.wiki.attachment.Attachment) Iterator(java.util.Iterator) Collection(java.util.Collection) File(java.io.File) PageTimeComparator(org.apache.wiki.util.comparators.PageTimeComparator)

Example 10 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class CachingAttachmentProvider method getAttachmentInfo.

/**
 * {@inheritDoc}
 */
public Attachment getAttachmentInfo(WikiPage page, String name, int version) throws ProviderException {
    if (log.isDebugEnabled()) {
        log.debug("Getting attachments for " + page + ", name=" + name + ", version=" + version);
    }
    // 
    if (version != WikiProvider.LATEST_VERSION) {
        log.debug("...we don't cache old versions");
        return m_provider.getAttachmentInfo(page, name, version);
    }
    Collection<Attachment> c = null;
    Element element = m_cache.get(page.getName());
    if (element == null) {
        log.debug(page.getName() + " wasn't in the cache");
        c = refresh(page);
        // No such attachment
        if (c == null)
            return null;
    } else {
        log.debug(page.getName() + " FOUND in the cache");
        c = (Collection<Attachment>) element.getObjectValue();
    }
    return findAttachmentFromCollection(c, name);
}
Also used : Element(net.sf.ehcache.Element) Attachment(org.apache.wiki.attachment.Attachment)

Aggregations

Attachment (org.apache.wiki.attachment.Attachment)62 WikiPage (org.apache.wiki.WikiPage)26 Test (org.junit.Test)23 File (java.io.File)20 ProviderException (org.apache.wiki.api.exceptions.ProviderException)17 Collection (java.util.Collection)13 Date (java.util.Date)11 Iterator (java.util.Iterator)10 WikiEngine (org.apache.wiki.WikiEngine)10 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)7 FileInputStream (java.io.FileInputStream)6 IOException (java.io.IOException)6 List (java.util.List)6 Vector (java.util.Vector)6 WikiContext (org.apache.wiki.WikiContext)5 Hashtable (java.util.Hashtable)4 Element (net.sf.ehcache.Element)4 PagePermission (org.apache.wiki.auth.permissions.PagePermission)4 ArrayList (java.util.ArrayList)3 Calendar (java.util.Calendar)3