Search in sources :

Example 51 with Attachment

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

the class AttachmentManagerTest method testSimpleStoreWithoutExt.

@Test
public void testSimpleStoreWithoutExt() throws Exception {
    final Attachment att = Wiki.contents().attachment(m_engine, NAME1, "test1");
    att.setAuthor("FirstPost");
    m_manager.storeAttachment(att, makeAttachmentFile());
    final Attachment att2 = m_manager.getAttachmentInfo(new WikiContext(m_engine, Wiki.contents().page(m_engine, NAME1)), "test1");
    Assertions.assertNotNull(att2, "attachment disappeared");
    Assertions.assertEquals(att.getName(), att2.getName(), "name");
    Assertions.assertEquals(att.getAuthor(), att2.getAuthor(), "author");
    Assertions.assertEquals(c_fileContents.length(), att2.getSize(), "size");
    Assertions.assertEquals(1, att2.getVersion(), "version");
    final InputStream in = m_manager.getAttachmentStream(att2);
    Assertions.assertNotNull(in, "stream");
    final StringWriter sout = new StringWriter();
    FileUtil.copyContents(new InputStreamReader(in), sout);
    in.close();
    sout.close();
    Assertions.assertEquals(c_fileContents, sout.toString(), "contents");
}
Also used : WikiContext(org.apache.wiki.WikiContext) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Attachment(org.apache.wiki.api.core.Attachment) Test(org.junit.jupiter.api.Test)

Example 52 with Attachment

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

the class KendraSearchProvider method newDocument.

/**
 * Given a {@link Page}, returns the corresponding Kendra {@link Document}.
 *
 * @param page        the {@link Page} to be indexed
 * @param executionId an execution id to identify when the {@link Page} was
 *                    indexed for the last time.
 * @return a {@link Document} containing the searchable attributes.
 * @throws IOException if the {@link Page}'s {@link Attachment} can not be read.
 */
private Document newDocument(final Page page, final String executionId) throws IOException {
    final String pageName = page.getName();
    final List<DocumentAttribute> attrs = new ArrayList<>();
    // These 2 are required as per
    // https://docs.aws.amazon.com/kendra/latest/dg/data-source-custom.html#custom-required-attributes
    attrs.add(newAttribute("_data_source_id", dataSourceId));
    attrs.add(newAttribute("_data_source_sync_job_execution_id", executionId));
    final String title = TextUtil.beautifyString(pageName);
    ByteBuffer blob;
    ContentType contentType = ContentType.PLAIN_TEXT;
    if (page instanceof Attachment) {
        final Attachment attachment = (Attachment) page;
        InputStream is = null;
        try {
            final String filename = attachment.getFileName();
            contentType = getContentType(filename);
            is = engine.getManager(AttachmentManager.class).getAttachmentStream(attachment);
            blob = ByteBuffer.wrap(IOUtils.toByteArray(is));
        } catch (final ProviderException e) {
            throw new IOException(e);
        } finally {
            IOUtils.closeQuietly(is, null);
        }
    // contentType should be set to its real value
    } else {
        final String text = engine.getManager(PageManager.class).getPureText(page);
        blob = ByteBuffer.wrap(text.getBytes(StandardCharsets.UTF_8));
    }
    return new Document().withId(pageName).withTitle(title).withAttributes(attrs).withBlob(blob).withContentType(contentType);
}
Also used : PageManager(org.apache.wiki.pages.PageManager) ProviderException(org.apache.wiki.api.exceptions.ProviderException) InputStream(java.io.InputStream) Attachment(org.apache.wiki.api.core.Attachment) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 53 with Attachment

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

the class BasicAttachmentProvider method listAttachments.

/**
 *  {@inheritDoc}
 */
@Override
public List<Attachment> listAttachments(final Page page) throws ProviderException {
    final List<Attachment> result = new ArrayList<>();
    final File dir = findPageDir(page.getName());
    final String[] attachments = dir.list();
    if (attachments != null) {
        // We now have a list of all potential attachments in the directory.
        for (final String attachment : attachments) {
            final File f = new File(dir, attachment);
            if (f.isDirectory()) {
                String attachmentName = unmangleName(attachment);
                // we'll check if there's a suitable property file in the directory.
                if (attachmentName.endsWith(ATTDIR_EXTENSION)) {
                    attachmentName = attachmentName.substring(0, attachmentName.length() - ATTDIR_EXTENSION.length());
                } else {
                    final File propFile = new File(f, PROPERTY_FILE);
                    if (!propFile.exists()) {
                        // This is not obviously a JSPWiki attachment, so let's just skip it.
                        continue;
                    }
                }
                final Attachment att = getAttachmentInfo(page, attachmentName, WikiProvider.LATEST_VERSION);
                // Sanity check - shouldn't really be happening, unless you mess with the repository directly.
                if (att == null) {
                    throw new ProviderException("Attachment disappeared while reading information:" + " if you did not touch the repository, there is a serious bug somewhere. " + "Attachment = " + attachment + ", decoded = " + attachmentName);
                }
                result.add(att);
            }
        }
    }
    return result;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) ArrayList(java.util.ArrayList) Attachment(org.apache.wiki.api.core.Attachment) File(java.io.File)

Example 54 with Attachment

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

the class BasicAttachmentProvider method getAttachmentInfo.

/**
 *  {@inheritDoc}
 */
@Override
public Attachment getAttachmentInfo(final Page page, final String name, int version) throws ProviderException {
    final Attachment att = new org.apache.wiki.attachment.Attachment(m_engine, page.getName(), name);
    final File dir = findAttachmentDir(att);
    if (!dir.exists()) {
        // log.debug("Attachment dir not found - thus no attachment can exist.");
        return null;
    }
    if (version == WikiProvider.LATEST_VERSION) {
        version = findLatestVersion(att);
    }
    att.setVersion(version);
    // Should attachment be cachable by the client (browser)?
    if (m_disableCache != null) {
        final Matcher matcher = m_disableCache.matcher(name);
        if (matcher.matches()) {
            att.setCacheable(false);
        }
    }
    // System.out.println("Fetching info on version "+version);
    try {
        final Properties props = getPageProperties(att);
        att.setAuthor(props.getProperty(version + ".author"));
        final String changeNote = props.getProperty(version + ".changenote");
        if (changeNote != null) {
            att.setAttribute(Page.CHANGENOTE, changeNote);
        }
        final File f = findFile(dir, att);
        att.setSize(f.length());
        att.setLastModified(new Date(f.lastModified()));
    } catch (final FileNotFoundException e) {
        log.error("Can't get attachment properties for " + att, e);
        return null;
    } catch (final IOException e) {
        log.error("Can't read page properties", e);
        throw new ProviderException("Cannot read page properties: " + e.getMessage());
    }
    return att;
}
Also used : Matcher(java.util.regex.Matcher) ProviderException(org.apache.wiki.api.exceptions.ProviderException) FileNotFoundException(java.io.FileNotFoundException) Attachment(org.apache.wiki.api.core.Attachment) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) Date(java.util.Date)

Example 55 with Attachment

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

the class DefaultPageManager method deletePage.

/**
 * {@inheritDoc}
 * @see org.apache.wiki.pages.PageManager#deletePage(java.lang.String)
 */
@Override
public void deletePage(final String pageName) throws ProviderException {
    final Page p = getPage(pageName);
    if (p != null) {
        if (p instanceof Attachment) {
            m_engine.getManager(AttachmentManager.class).deleteAttachment((Attachment) p);
        } else {
            final Collection<String> refTo = m_engine.getManager(ReferenceManager.class).findRefersTo(pageName);
            if (m_engine.getManager(AttachmentManager.class).hasAttachments(p)) {
                final List<Attachment> attachments = m_engine.getManager(AttachmentManager.class).listAttachments(p);
                for (final Attachment attachment : attachments) {
                    if (refTo != null) {
                        refTo.remove(attachment.getName());
                    }
                    m_engine.getManager(AttachmentManager.class).deleteAttachment(attachment);
                }
            }
            deletePage(p);
            fireEvent(WikiPageEvent.PAGE_DELETED, pageName);
        }
    }
}
Also used : Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) ReferenceManager(org.apache.wiki.references.ReferenceManager)

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