Search in sources :

Example 36 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class ParentPageNameTag method doWikiStartTag.

/**
 * {@inheritDoc}
 */
@Override
public final int doWikiStartTag() throws IOException {
    final Engine engine = m_wikiContext.getEngine();
    final Page page = m_wikiContext.getPage();
    if (page != null) {
        if (page instanceof Attachment) {
            pageContext.getOut().print(engine.getManager(RenderingManager.class).beautifyTitle(((Attachment) page).getParentName()));
        } else {
            String name = page.getName();
            final int entrystart = name.indexOf("_blogentry_");
            if (entrystart != -1) {
                name = name.substring(0, entrystart);
            }
            final int commentstart = name.indexOf("_comments_");
            if (commentstart != -1) {
                name = name.substring(0, commentstart);
            }
            pageContext.getOut().print(engine.getManager(RenderingManager.class).beautifyTitle(name));
        }
    }
    return SKIP_BODY;
}
Also used : Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment) Engine(org.apache.wiki.api.core.Engine)

Example 37 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class DefaultAttachmentManager method getAttachmentInfo.

/**
 * {@inheritDoc}
 */
@Override
public Attachment getAttachmentInfo(final Context context, String attachmentname, final int version) throws ProviderException {
    if (m_provider == null) {
        return null;
    }
    Page currentPage = null;
    if (context != null) {
        currentPage = context.getPage();
    }
    // Figure out the parent page of this attachment.  If we can't find it, we'll assume this refers directly to the attachment.
    final int cutpt = attachmentname.lastIndexOf('/');
    if (cutpt != -1) {
        String parentPage = attachmentname.substring(0, cutpt);
        parentPage = MarkupParser.cleanLink(parentPage);
        attachmentname = attachmentname.substring(cutpt + 1);
        // If we for some reason have an empty parent page name; this can't be an attachment
        if (parentPage.isEmpty()) {
            return null;
        }
        currentPage = m_engine.getManager(PageManager.class).getPage(parentPage);
        // legacy charset is a subset of the full allowed set.
        if (currentPage == null) {
            currentPage = m_engine.getManager(PageManager.class).getPage(MarkupParser.wikifyLink(parentPage));
        }
    }
    // If the page cannot be determined, we cannot possibly find the attachments.
    if (currentPage == null || currentPage.getName().isEmpty()) {
        return null;
    }
    // Finally, figure out whether this is a real attachment or a generated attachment.
    Attachment att = getDynamicAttachment(currentPage.getName() + "/" + attachmentname);
    if (att == null) {
        att = m_provider.getAttachmentInfo(currentPage, attachmentname, version);
    }
    return att;
}
Also used : Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment)

Example 38 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class RPCHandlerUTF8 method getAllPages.

public Vector<String> getAllPages() {
    checkPermission(PagePermission.VIEW);
    final Set<Page> pages = m_engine.getManager(PageManager.class).getRecentChanges();
    final Vector<String> result = new Vector<>();
    for (final Page p : pages) {
        if (!(p instanceof Attachment)) {
            result.add(p.getName());
        }
    }
    return result;
}
Also used : PageManager(org.apache.wiki.pages.PageManager) Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment) Vector(java.util.Vector)

Example 39 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class WikiEngineTest method testAttachmentRefs4.

/**
 *  Checks, if ReferenceManager is informed if a third page references an attachment.
 */
@Test
public void testAttachmentRefs4() throws Exception {
    final ReferenceManager refMgr = m_engine.getManager(ReferenceManager.class);
    final AttachmentManager attMgr = m_engine.getManager(AttachmentManager.class);
    m_engine.saveText(NAME1, "[TestPage2]");
    final Attachment att = Wiki.contents().attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
    m_engine.saveText("TestPage2", "[" + NAME1 + "/TestAtt.txt]");
    // and check post-conditions
    Collection<String> c = refMgr.findUncreated();
    Assertions.assertTrue(c == null || c.size() == 0, "attachment exists");
    c = refMgr.findUnreferenced();
    Assertions.assertEquals(c.size(), 1, "unreferenced count");
    Assertions.assertEquals(NAME1, c.iterator().next(), "unreferenced");
}
Also used : Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) ReferenceManager(org.apache.wiki.references.ReferenceManager) Test(org.junit.jupiter.api.Test)

Example 40 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class WikiEngineTest method testAttachmentRefs2.

/**
 *  Is ReferenceManager updated properly if a page references
 *  its own attachments?
 */
/*
      FIXME: This is a deep problem.  The real problem is that the reference
      manager cannot know when it encounters a link like "testatt.txt" that it
      is really a link to an attachment IF the link is created before
      the attachment.  This means that when the attachment is created,
      the link will stay in the "uncreated" list.

      There are two issues here: first of all, TranslatorReader should
      able to return the proper attachment references (which I think
      it does), and second, the ReferenceManager should be able to
      remove any links that are not referred to, nor they are created.

      However, doing this in a relatively sane timeframe can be a problem.
    */
@Test
public void testAttachmentRefs2() throws Exception {
    final ReferenceManager refMgr = m_engine.getManager(ReferenceManager.class);
    final AttachmentManager attMgr = m_engine.getManager(AttachmentManager.class);
    m_engine.saveText(NAME1, "[TestAtt.txt]");
    // check a few pre-conditions
    Collection<String> c = refMgr.findReferrers("TestAtt.txt");
    Assertions.assertTrue(c != null && c.iterator().next().equals(NAME1), "normal, unexisting page");
    c = refMgr.findReferrers(NAME1 + "/TestAtt.txt");
    Assertions.assertTrue(c == null || c.size() == 0, "no attachment");
    c = refMgr.findUncreated();
    Assertions.assertTrue(c != null && c.size() == 1 && c.iterator().next().equals("TestAtt.txt"), "unknown attachment");
    // now we create the attachment
    final Attachment att = Wiki.contents().attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
    // and check post-conditions
    c = refMgr.findUncreated();
    Assertions.assertTrue(c == null || c.size() == 0, "attachment exists: ");
    c = refMgr.findReferrers("TestAtt.txt");
    Assertions.assertTrue(c == null || c.size() == 0, "no normal page");
    c = refMgr.findReferrers(NAME1 + "/TestAtt.txt");
    Assertions.assertTrue(c != null && c.iterator().next().equals(NAME1), "attachment exists now");
    c = refMgr.findUnreferenced();
    Assertions.assertTrue(c.size() == 1 && c.iterator().next().equals(NAME1), "unreferenced");
}
Also used : Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) ReferenceManager(org.apache.wiki.references.ReferenceManager) Test(org.junit.jupiter.api.Test)

Aggregations

Attachment (org.apache.wiki.api.core.Attachment)76 Test (org.junit.jupiter.api.Test)37 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)35 Page (org.apache.wiki.api.core.Page)27 ProviderException (org.apache.wiki.api.exceptions.ProviderException)20 PageManager (org.apache.wiki.pages.PageManager)19 File (java.io.File)15 Date (java.util.Date)10 Engine (org.apache.wiki.api.core.Engine)10 ReferenceManager (org.apache.wiki.references.ReferenceManager)9 InputStream (java.io.InputStream)8 FileInputStream (java.io.FileInputStream)6 IOException (java.io.IOException)6 InputStreamReader (java.io.InputStreamReader)6 StringWriter (java.io.StringWriter)6 Vector (java.util.Vector)6 WikiContext (org.apache.wiki.WikiContext)6 Permission (java.security.Permission)4 ArrayList (java.util.ArrayList)4 Hashtable (java.util.Hashtable)4