Search in sources :

Example 61 with Attachment

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

the class BasicAttachmentProviderTest method testListAllExtrafile.

/**
 *  Check that the system does not Assertions.fail if there are extra files in the directory.
 */
@Test
public void testListAllExtrafile() throws Exception {
    final File in = makeAttachmentFile();
    final File sDir = new File(m_engine.getWikiProperties().getProperty(BasicAttachmentProvider.PROP_STORAGEDIR));
    makeExtraFile(sDir, "foobar.blob");
    final Attachment att = Wiki.contents().attachment(m_engine, NAME1, "test1.txt");
    m_provider.putAttachmentData(att, new FileInputStream(in));
    awaitility().await("testListAllExtrafile").until(attachmentIsSaved(att));
    final Attachment att2 = Wiki.contents().attachment(m_engine, NAME2, "test2.txt");
    m_provider.putAttachmentData(att2, new FileInputStream(in));
    final List<Attachment> res = m_provider.listAllChanged(new Date(0L));
    Assertions.assertEquals(2, res.size(), "list size");
    // Most recently changed
    final Attachment a2 = res.get(0);
    // Least recently changed
    final Attachment a1 = res.get(1);
    Assertions.assertEquals(att.getName(), a1.getName(), "a1 name :: " + res);
    Assertions.assertEquals(att2.getName(), a2.getName(), "a2 name :: " + res);
}
Also used : Attachment(org.apache.wiki.api.core.Attachment) File(java.io.File) FileInputStream(java.io.FileInputStream) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 62 with Attachment

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

the class BasicAttachmentProviderTest method testListAllExtradirInAttachmentDir.

/**
 *  Check that the system does not Assertions.fail if there are extra dirs in the attachment directory.
 */
@Test
public void testListAllExtradirInAttachmentDir() throws Exception {
    final File in = makeAttachmentFile();
    final File sDir = new File(m_engine.getWikiProperties().getProperty(BasicAttachmentProvider.PROP_STORAGEDIR));
    final File attDir = new File(sDir, NAME1 + "-att");
    final Attachment att = Wiki.contents().attachment(m_engine, NAME1, "test1.txt");
    m_provider.putAttachmentData(att, new FileInputStream(in));
    // This is our extraneous directory.
    final File extrafile = new File(attDir, "ping.pong");
    extrafile.mkdir();
    awaitility().await("testListAllExtradirInAttachmentDir").until(attachmentIsSaved(att));
    final Attachment att2 = Wiki.contents().attachment(m_engine, NAME2, "test2.txt");
    m_provider.putAttachmentData(att2, new FileInputStream(in));
    final List<Attachment> res = m_provider.listAllChanged(new Date(0L));
    Assertions.assertEquals(2, res.size(), "list size");
    // Most recently changed
    final Attachment a2 = res.get(0);
    // Least recently changed
    final Attachment a1 = res.get(1);
    Assertions.assertEquals(att.getName(), a1.getName(), "a1 name :: " + res);
    Assertions.assertEquals(att2.getName(), a2.getName(), "a2 name :: " + res);
}
Also used : Attachment(org.apache.wiki.api.core.Attachment) File(java.io.File) FileInputStream(java.io.FileInputStream) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 63 with Attachment

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

the class BasicAttachmentProviderTest method testListAllNoExtension.

@Test
public void testListAllNoExtension() throws Exception {
    final File in = makeAttachmentFile();
    final Attachment att = Wiki.contents().attachment(m_engine, NAME1, "test1.");
    m_provider.putAttachmentData(att, new FileInputStream(in));
    awaitility().await("testListAllNoExtension").until(attachmentIsSaved(att));
    final Attachment att2 = Wiki.contents().attachment(m_engine, NAME2, "test2.");
    m_provider.putAttachmentData(att2, new FileInputStream(in));
    final List<Attachment> res = m_provider.listAllChanged(new Date(0L));
    Assertions.assertEquals(2, res.size(), "list size");
    // Most recently changed
    final Attachment a2 = res.get(0);
    // Least recently changed
    final Attachment a1 = res.get(1);
    Assertions.assertEquals(att.getName(), a1.getName(), "a1 name :: " + res);
    Assertions.assertEquals(att2.getName(), a2.getName(), "a2 name :: " + res);
}
Also used : Attachment(org.apache.wiki.api.core.Attachment) File(java.io.File) FileInputStream(java.io.FileInputStream) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 64 with Attachment

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

the class BasicAttachmentProviderTest method testListAll.

@Test
public void testListAll() throws Exception {
    final File in = makeAttachmentFile();
    final Attachment att = Wiki.contents().attachment(m_engine, NAME1, "test1.txt");
    m_provider.putAttachmentData(att, new FileInputStream(in));
    awaitility().await("testListAll").until(attachmentIsSaved(att));
    final Attachment att2 = Wiki.contents().attachment(m_engine, NAME2, "test2.txt");
    m_provider.putAttachmentData(att2, new FileInputStream(in));
    final List<Attachment> res = m_provider.listAllChanged(new Date(0L));
    Assertions.assertEquals(2, res.size(), "list size");
    // Most recently changed
    final Attachment a2 = res.get(0);
    // Least recently changed
    final Attachment a1 = res.get(1);
    Assertions.assertEquals(att.getName(), a1.getName(), "a1 name :: " + res);
    Assertions.assertEquals(att2.getName(), a2.getName(), "a2 name :: " + res);
}
Also used : Attachment(org.apache.wiki.api.core.Attachment) File(java.io.File) FileInputStream(java.io.FileInputStream) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 65 with Attachment

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

the class LuceneSearchProvider method luceneIndexPage.

/**
 *  Indexes page using the given IndexWriter.
 *
 *  @param page WikiPage
 *  @param text Page text to index
 *  @param writer The Lucene IndexWriter to use for indexing
 *  @return the created index Document
 *  @throws IOException If there's an indexing problem
 */
protected Document luceneIndexPage(final Page page, final String text, final IndexWriter writer) throws IOException {
    log.debug("Indexing {}...", page.getName());
    // make a new, empty document
    final Document doc = new Document();
    if (text == null) {
        return doc;
    }
    // be nice to Language Analyzers - cfr. JSPWIKI-893
    final String indexedText = text.replace("__", " ");
    // Raw name is the keyword we'll use to refer to this document for updates.
    Field field = new Field(LUCENE_ID, page.getName(), StringField.TYPE_STORED);
    doc.add(field);
    // Body text.  It is stored in the doc for search contexts.
    field = new Field(LUCENE_PAGE_CONTENTS, indexedText, TextField.TYPE_STORED);
    doc.add(field);
    // Allow searching by page name. Both beautified and raw
    final String unTokenizedTitle = StringUtils.replaceChars(page.getName(), TextUtil.PUNCTUATION_CHARS_ALLOWED, PUNCTUATION_TO_SPACES);
    field = new Field(LUCENE_PAGE_NAME, TextUtil.beautifyString(page.getName()) + " " + unTokenizedTitle, TextField.TYPE_STORED);
    doc.add(field);
    // Allow searching by authorname
    if (page.getAuthor() != null) {
        field = new Field(LUCENE_AUTHOR, page.getAuthor(), TextField.TYPE_STORED);
        doc.add(field);
    }
    // Now add the names of the attachments of this page
    try {
        final List<Attachment> attachments = m_engine.getManager(AttachmentManager.class).listAttachments(page);
        final StringBuilder attachmentNames = new StringBuilder();
        for (final Attachment att : attachments) {
            attachmentNames.append(att.getName()).append(";");
        }
        field = new Field(LUCENE_ATTACHMENTS, attachmentNames.toString(), TextField.TYPE_STORED);
        doc.add(field);
    } catch (final ProviderException e) {
        // Unable to read attachments
        log.error("Failed to get attachments for page", e);
    }
    // also index page keywords, if available
    if (page.getAttribute("keywords") != null) {
        field = new Field(LUCENE_PAGE_KEYWORDS, page.getAttribute("keywords").toString(), TextField.TYPE_STORED);
        doc.add(field);
    }
    synchronized (writer) {
        writer.addDocument(doc);
    }
    return doc;
}
Also used : StringField(org.apache.lucene.document.StringField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) ProviderException(org.apache.wiki.api.exceptions.ProviderException) Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) Document(org.apache.lucene.document.Document)

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