Search in sources :

Example 11 with Attachment

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

the class CachingAttachmentProvider method refresh.

/**
 *  Refreshes the cache content and updates counters.
 *
 *  @return The newly fetched object from the provider.
 */
private Collection<Attachment> refresh(WikiPage page) throws ProviderException {
    Collection<Attachment> c = m_provider.listAttachments(page);
    m_cache.put(new Element(page.getName(), c));
    return c;
}
Also used : Element(net.sf.ehcache.Element) Attachment(org.apache.wiki.attachment.Attachment)

Example 12 with Attachment

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

the class CachingAttachmentProvider method listAttachments.

/**
 * {@inheritDoc}
 */
public Collection listAttachments(WikiPage page) throws ProviderException {
    log.debug("Listing attachments for " + page);
    Collection<Attachment> c = null;
    Element element = m_cache.get(page.getName());
    if (element != null) {
        c = (Collection<Attachment>) element.getObjectValue();
        log.debug("LIST from cache, " + page.getName() + ", size=" + c.size());
        return cloneCollection(c);
    }
    log.debug("list NOT in cache, " + page.getName());
    return refresh(page);
}
Also used : Element(net.sf.ehcache.Element) Attachment(org.apache.wiki.attachment.Attachment)

Example 13 with Attachment

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

the class SpamFilter method refreshBlacklists.

/**
 *  If the spam filter notices changes in the black list page, it will refresh them automatically.
 *
 *  @param context
 */
private void refreshBlacklists(WikiContext context) {
    try {
        boolean rebuild = false;
        // 
        // Rebuild, if the spam words page, the attachment or the IP ban page has changed since.
        // 
        WikiPage sourceSpam = context.getEngine().getPage(m_forbiddenWordsPage);
        if (sourceSpam != null) {
            if (m_spamPatterns == null || m_spamPatterns.isEmpty() || sourceSpam.getLastModified().after(m_lastRebuild)) {
                rebuild = true;
            }
        }
        Attachment att = context.getEngine().getAttachmentManager().getAttachmentInfo(context, m_blacklist);
        if (att != null) {
            if (m_spamPatterns == null || m_spamPatterns.isEmpty() || att.getLastModified().after(m_lastRebuild)) {
                rebuild = true;
            }
        }
        WikiPage sourceIPs = context.getEngine().getPage(m_forbiddenIPsPage);
        if (sourceIPs != null) {
            if (m_IPPatterns == null || m_IPPatterns.isEmpty() || sourceIPs.getLastModified().after(m_lastRebuild)) {
                rebuild = true;
            }
        }
        // 
        if (rebuild) {
            m_lastRebuild = new Date();
            m_spamPatterns = parseWordList(sourceSpam, (sourceSpam != null) ? (String) sourceSpam.getAttribute(LISTVAR) : null);
            log.info("Spam filter reloaded - recognizing " + m_spamPatterns.size() + " patterns from page " + m_forbiddenWordsPage);
            m_IPPatterns = parseWordList(sourceIPs, (sourceIPs != null) ? (String) sourceIPs.getAttribute(LISTIPVAR) : null);
            log.info("IP filter reloaded - recognizing " + m_IPPatterns.size() + " patterns from page " + m_forbiddenIPsPage);
            if (att != null) {
                InputStream in = context.getEngine().getAttachmentManager().getAttachmentStream(att);
                StringWriter out = new StringWriter();
                FileUtil.copyContents(new InputStreamReader(in, "UTF-8"), out);
                Collection<Pattern> blackList = parseBlacklist(out.toString());
                log.info("...recognizing additional " + blackList.size() + " patterns from blacklist " + m_blacklist);
                m_spamPatterns.addAll(blackList);
            }
        }
    } catch (IOException ex) {
        log.info("Unable to read attachment data, continuing...", ex);
    } catch (ProviderException ex) {
        log.info("Failed to read spam filter attachment, continuing...", ex);
    }
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) ProviderException(org.apache.wiki.api.exceptions.ProviderException) InputStream(java.io.InputStream) WikiPage(org.apache.wiki.WikiPage) Attachment(org.apache.wiki.attachment.Attachment) IOException(java.io.IOException) Date(java.util.Date)

Example 14 with Attachment

use of org.apache.wiki.attachment.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(WikiPage page, String text, IndexWriter writer) throws IOException {
    if (log.isDebugEnabled())
        log.debug("Indexing " + page.getName() + "...");
    // make a new, empty document
    Document doc = new Document();
    if (text == null)
        return doc;
    // 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, text, TextField.TYPE_STORED);
    doc.add(field);
    // Allow searching by page name. Both beautified and raw
    String unTokenizedTitle = StringUtils.replaceChars(page.getName(), MarkupParser.PUNCTUATION_CHARS_ALLOWED, c_punctuationSpaces);
    field = new Field(LUCENE_PAGE_NAME, TextUtil.beautifyString(page.getName()) + " " + unTokenizedTitle, TextField.TYPE_STORED);
    doc.add(field);
    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 {
        Collection attachments = m_engine.getAttachmentManager().listAttachments(page);
        String attachmentNames = "";
        for (Iterator it = attachments.iterator(); it.hasNext(); ) {
            Attachment att = (Attachment) it.next();
            attachmentNames += att.getName() + ";";
        }
        field = new Field(LUCENE_ATTACHMENTS, attachmentNames, TextField.TYPE_STORED);
        doc.add(field);
    } catch (ProviderException e) {
        // Unable to read attachments
        log.error("Failed to get attachments for page", e);
    }
    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) Iterator(java.util.Iterator) Collection(java.util.Collection) Attachment(org.apache.wiki.attachment.Attachment) Document(org.apache.lucene.document.Document)

Example 15 with Attachment

use of org.apache.wiki.attachment.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 {
    File dir = new File(m_luceneDirectory);
    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!
            // 
            Date start = new Date();
            IndexWriter writer = null;
            log.info("Starting Lucene reindexing, this can take a couple of minutes...");
            Directory luceneDir = new SimpleFSDirectory(dir, null);
            try {
                writer = getIndexWriter(luceneDir);
                Collection allPages = m_engine.getPageManager().getAllPages();
                for (Iterator iterator = allPages.iterator(); iterator.hasNext(); ) {
                    WikiPage page = (WikiPage) iterator.next();
                    try {
                        String text = m_engine.getPageManager().getPageText(page.getName(), WikiProvider.LATEST_VERSION);
                        luceneIndexPage(page, text, writer);
                    } catch (IOException e) {
                        log.warn("Unable to index page " + page.getName() + ", continuing to next ", e);
                    }
                }
                Collection allAttachments = m_engine.getAttachmentManager().getAllAttachments();
                for (Iterator iterator = allAttachments.iterator(); iterator.hasNext(); ) {
                    Attachment att = (Attachment) iterator.next();
                    try {
                        String text = getAttachmentContent(att.getName(), WikiProvider.LATEST_VERSION);
                        luceneIndexPage(att, text, writer);
                    } catch (IOException e) {
                        log.warn("Unable to index attachment " + att.getName() + ", continuing to next", e);
                    }
                }
            } finally {
                close(writer);
            }
            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 (NoClassDefFoundError e) {
        log.info("Lucene libraries do not exist - not using Lucene.");
    } catch (IOException e) {
        log.error("Problem while creating Lucene index - not using Lucene.", e);
    } catch (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 (Exception e) {
        log.error("Unable to start lucene", e);
    }
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiPage(org.apache.wiki.WikiPage) Attachment(org.apache.wiki.attachment.Attachment) IOException(java.io.IOException) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) Date(java.util.Date) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) NoRequiredPropertyException(org.apache.wiki.api.exceptions.NoRequiredPropertyException) InternalWikiException(org.apache.wiki.InternalWikiException) ParseException(org.apache.lucene.queryparser.classic.ParseException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) InvalidTokenOffsetsException(org.apache.lucene.search.highlight.InvalidTokenOffsetsException) IOException(java.io.IOException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) IndexWriter(org.apache.lucene.index.IndexWriter) Iterator(java.util.Iterator) Collection(java.util.Collection) File(java.io.File) Directory(org.apache.lucene.store.Directory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory)

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