Search in sources :

Example 1 with Attachment

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

the class BasicSearchProvider method attachmentNames.

private String attachmentNames(final Page page) {
    if (m_engine.getManager(AttachmentManager.class).hasAttachments(page)) {
        final List<Attachment> attachments;
        try {
            attachments = m_engine.getManager(AttachmentManager.class).listAttachments(page);
        } catch (final ProviderException e) {
            log.error("Unable to get attachments for page", e);
            return "";
        }
        final StringBuilder attachmentNames = new StringBuilder();
        for (final Iterator<Attachment> it = attachments.iterator(); it.hasNext(); ) {
            final Attachment att = it.next();
            attachmentNames.append(att.getName());
            if (it.hasNext()) {
                attachmentNames.append(" ");
            }
        }
        return attachmentNames.toString();
    }
    return "";
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager)

Example 2 with Attachment

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

the class LuceneSearchProvider method doFullLuceneReindex.

/**
 *  Performs a full Lucene reindex, if necessary.
 *
 *  @throws IOException If there's a problem during indexing
 */
protected void doFullLuceneReindex() throws IOException {
    final File dir = new File(m_luceneDirectory);
    final String[] filelist = dir.list();
    if (filelist == null) {
        throw new IOException("Invalid Lucene directory: cannot produce listing: " + dir.getAbsolutePath());
    }
    try {
        if (filelist.length == 0) {
            // 
            // No files? Reindex!
            // 
            final Date start = new Date();
            log.info("Starting Lucene reindexing, this can take a couple of minutes...");
            final Directory luceneDir = new NIOFSDirectory(dir.toPath());
            try (final IndexWriter writer = getIndexWriter(luceneDir)) {
                final Collection<Page> allPages = m_engine.getManager(PageManager.class).getAllPages();
                for (final Page page : allPages) {
                    try {
                        final String text = m_engine.getManager(PageManager.class).getPageText(page.getName(), WikiProvider.LATEST_VERSION);
                        luceneIndexPage(page, text, writer);
                    } catch (final IOException e) {
                        log.warn("Unable to index page " + page.getName() + ", continuing to next ", e);
                    }
                }
                final Collection<Attachment> allAttachments = m_engine.getManager(AttachmentManager.class).getAllAttachments();
                for (final Attachment att : allAttachments) {
                    try {
                        final String text = getAttachmentContent(att.getName(), WikiProvider.LATEST_VERSION);
                        luceneIndexPage(att, text, writer);
                    } catch (final IOException e) {
                        log.warn("Unable to index attachment " + att.getName() + ", continuing to next", e);
                    }
                }
            }
            final Date end = new Date();
            log.info("Full Lucene index finished in " + (end.getTime() - start.getTime()) + " milliseconds.");
        } else {
            log.info("Files found in Lucene directory, not reindexing.");
        }
    } catch (final IOException e) {
        log.error("Problem while creating Lucene index - not using Lucene.", e);
    } catch (final ProviderException e) {
        log.error("Problem reading pages while creating Lucene index (JSPWiki won't start.)", e);
        throw new IllegalArgumentException("unable to create Lucene index");
    } catch (final Exception e) {
        log.error("Unable to start lucene", e);
    }
}
Also used : NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) ProviderException(org.apache.wiki.api.exceptions.ProviderException) Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment) IOException(java.io.IOException) Date(java.util.Date) NoRequiredPropertyException(org.apache.wiki.api.exceptions.NoRequiredPropertyException) InternalWikiException(org.apache.wiki.InternalWikiException) ParseException(org.apache.lucene.queryparser.classic.ParseException) InvalidTokenOffsetsException(org.apache.lucene.search.highlight.InvalidTokenOffsetsException) IOException(java.io.IOException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) PageManager(org.apache.wiki.pages.PageManager) IndexWriter(org.apache.lucene.index.IndexWriter) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) File(java.io.File) Directory(org.apache.lucene.store.Directory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory)

Example 3 with Attachment

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

the class BasicAttachmentProvider method listAllChanged.

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

Example 4 with Attachment

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

the class DefaultRenderingManager method beautifyTitle.

/**
 * {@inheritDoc}
 */
@Override
public String beautifyTitle(final String title) {
    if (m_beautifyTitle) {
        try {
            final Attachment att = m_engine.getManager(AttachmentManager.class).getAttachmentInfo(title);
            if (att == null) {
                return TextUtil.beautifyString(title);
            }
            final String parent = TextUtil.beautifyString(att.getParentName());
            return parent + "/" + att.getFileName();
        } catch (final ProviderException e) {
            return title;
        }
    }
    return title;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager)

Example 5 with Attachment

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

the class AtomFeed method getItems.

private Collection<Element> getItems() {
    final ArrayList<Element> list = new ArrayList<>();
    final Engine engine = m_wikiContext.getEngine();
    ServletContext servletContext = null;
    if (m_wikiContext.getHttpRequest() != null) {
        servletContext = m_wikiContext.getHttpRequest().getSession().getServletContext();
    }
    for (final Entry e : m_entries) {
        final Page p = e.getPage();
        final Element entryEl = getElement("entry");
        // Mandatory elements
        entryEl.addContent(getElement("id").setText(getEntryID(e)));
        entryEl.addContent(getElement("title").setAttribute("type", "html").setText(e.getTitle()));
        entryEl.addContent(getElement("updated").setText(DateFormatUtils.formatUTC(p.getLastModified(), RFC3339FORMAT)));
        // Optional elements
        entryEl.addContent(getElement("author").addContent(getElement("name").setText(e.getAuthor())));
        entryEl.addContent(getElement("link").setAttribute("rel", "alternate").setAttribute("href", e.getURL()));
        entryEl.addContent(getElement("content").setAttribute("type", "html").setText(e.getContent()));
        // Check for enclosures
        if (engine.getManager(AttachmentManager.class).hasAttachments(p) && servletContext != null) {
            try {
                final List<Attachment> c = engine.getManager(AttachmentManager.class).listAttachments(p);
                for (final Attachment att : c) {
                    final Element attEl = getElement("link");
                    attEl.setAttribute("rel", "enclosure");
                    attEl.setAttribute("href", engine.getURL(ContextEnum.PAGE_ATTACH.getRequestContext(), att.getName(), null));
                    attEl.setAttribute("length", Long.toString(att.getSize()));
                    attEl.setAttribute("type", getMimeType(servletContext, att.getFileName()));
                    entryEl.addContent(attEl);
                }
            } catch (final ProviderException ex) {
            // FIXME: log.info("Can't get attachment data",ex);
            }
        }
        list.add(entryEl);
    }
    return list;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) ServletContext(javax.servlet.ServletContext) Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) Engine(org.apache.wiki.api.core.Engine)

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