Search in sources :

Example 6 with ProviderException

use of org.apache.wiki.api.exceptions.ProviderException in project jspwiki by apache.

the class VersioningFileProvider method putPageText.

// FIXME: This method has no rollback whatsoever.
/*
      This is how the page directory should look like:

         version    pagedir       olddir
          none       empty         empty
           1         Main.txt (1)  empty
           2         Main.txt (2)  1.txt
           3         Main.txt (3)  1.txt, 2.txt
    */
/**
 *  {@inheritDoc}
 */
public synchronized void putPageText(WikiPage page, String text) throws ProviderException {
    // 
    // This is a bit complicated.  We'll first need to
    // copy the old file to be the newest file.
    // 
    File pageDir = findOldPageDir(page.getName());
    if (!pageDir.exists()) {
        pageDir.mkdirs();
    }
    int latest = findLatestVersion(page.getName());
    try {
        // 
        // Copy old data to safety, if one exists.
        // 
        File oldFile = findPage(page.getName());
        // Figure out which version should the old page be?
        // Numbers should always start at 1.
        // "most recent" = -1 ==> 1
        // "first"       = 1  ==> 2
        int versionNumber = (latest > 0) ? latest : 1;
        boolean firstUpdate = (versionNumber == 1);
        if (oldFile != null && oldFile.exists()) {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = new BufferedInputStream(new FileInputStream(oldFile));
                File pageFile = new File(pageDir, Integer.toString(versionNumber) + FILE_EXT);
                out = new BufferedOutputStream(new FileOutputStream(pageFile));
                FileUtil.copyContents(in, out);
                // 
                // We need also to set the date, since we rely on this.
                // 
                pageFile.setLastModified(oldFile.lastModified());
                // 
                // Kludge to make the property code to work properly.
                // 
                versionNumber++;
            } finally {
                IOUtils.closeQuietly(out);
                IOUtils.closeQuietly(in);
            }
        }
        // 
        // Let superclass handler writing data to a new version.
        // 
        super.putPageText(page, text);
        // 
        // Finally, write page version data.
        // 
        // FIXME: No rollback available.
        Properties props = getPageProperties(page.getName());
        String authorFirst = null;
        // if the following file exists, we are NOT migrating from FileSystemProvider
        File pagePropFile = new File(getPageDirectory() + File.separator + PAGEDIR + File.separator + mangleName(page.getName()) + File.separator + "page" + FileSystemProvider.PROP_EXT);
        if (firstUpdate && !pagePropFile.exists()) {
            // we might not yet have a versioned author because the
            // old page was last maintained by FileSystemProvider
            Properties props2 = getHeritagePageProperties(page.getName());
            // remember the simulated original author (or something)
            // in the new properties
            authorFirst = props2.getProperty("1.author", "unknown");
            props.setProperty("1.author", authorFirst);
        }
        String newAuthor = page.getAuthor();
        if (newAuthor == null) {
            newAuthor = (authorFirst != null) ? authorFirst : "unknown";
        }
        page.setAuthor(newAuthor);
        props.setProperty(versionNumber + ".author", newAuthor);
        String changeNote = (String) page.getAttribute(WikiPage.CHANGENOTE);
        if (changeNote != null) {
            props.setProperty(versionNumber + ".changenote", changeNote);
        }
        // Get additional custom properties from page and add to props
        getCustomProperties(page, props);
        putPageProperties(page.getName(), props);
    } catch (IOException e) {
        log.error("Saving failed", e);
        throw new ProviderException("Could not save page text: " + e.getMessage());
    }
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 7 with ProviderException

use of org.apache.wiki.api.exceptions.ProviderException in project jspwiki by apache.

the class BasicSearchProvider method findPages.

private Collection findPages(QueryItem[] query, WikiContext wikiContext) {
    TreeSet<SearchResult> res = new TreeSet<SearchResult>(new SearchResultComparator());
    SearchMatcher matcher = new SearchMatcher(m_engine, query);
    Collection allPages = null;
    try {
        allPages = m_engine.getPageManager().getAllPages();
    } catch (ProviderException pe) {
        log.error("Unable to retrieve page list", pe);
        return null;
    }
    AuthorizationManager mgr = m_engine.getAuthorizationManager();
    Iterator it = allPages.iterator();
    while (it.hasNext()) {
        try {
            WikiPage page = (WikiPage) it.next();
            if (page != null) {
                PagePermission pp = new PagePermission(page, PagePermission.VIEW_ACTION);
                if (wikiContext == null || mgr.checkPermission(wikiContext.getWikiSession(), pp)) {
                    String pageName = page.getName();
                    String pageContent = m_engine.getPageManager().getPageText(pageName, WikiPageProvider.LATEST_VERSION) + attachmentNames(page, " ");
                    SearchResult comparison = matcher.matchPageContent(pageName, pageContent);
                    if (comparison != null) {
                        res.add(comparison);
                    }
                }
            }
        } catch (ProviderException pe) {
            log.error("Unable to retrieve page from cache", pe);
        } catch (IOException ioe) {
            log.error("Failed to search page", ioe);
        }
    }
    return res;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiPage(org.apache.wiki.WikiPage) IOException(java.io.IOException) TreeSet(java.util.TreeSet) Iterator(java.util.Iterator) Collection(java.util.Collection) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager) PagePermission(org.apache.wiki.auth.permissions.PagePermission)

Example 8 with ProviderException

use of org.apache.wiki.api.exceptions.ProviderException 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 9 with ProviderException

use of org.apache.wiki.api.exceptions.ProviderException 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)

Example 10 with ProviderException

use of org.apache.wiki.api.exceptions.ProviderException in project jspwiki by apache.

the class AtomFeed method getItems.

private Collection getItems() {
    ArrayList<Element> list = new ArrayList<Element>();
    WikiEngine engine = m_wikiContext.getEngine();
    ServletContext servletContext = null;
    if (m_wikiContext.getHttpRequest() != null)
        servletContext = m_wikiContext.getHttpRequest().getSession().getServletContext();
    for (Iterator i = m_entries.iterator(); i.hasNext(); ) {
        Entry e = (Entry) i.next();
        WikiPage p = e.getPage();
        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()));
        if (engine.getAttachmentManager().hasAttachments(p) && servletContext != null) {
            try {
                Collection c = engine.getAttachmentManager().listAttachments(p);
                for (Iterator a = c.iterator(); a.hasNext(); ) {
                    Attachment att = (Attachment) a.next();
                    Element attEl = getElement("link");
                    attEl.setAttribute("rel", "enclosure");
                    attEl.setAttribute("href", engine.getURL(WikiContext.ATTACH, att.getName(), null, true));
                    attEl.setAttribute("length", Long.toString(att.getSize()));
                    attEl.setAttribute("type", getMimeType(servletContext, att.getFileName()));
                    entryEl.addContent(attEl);
                }
            } catch (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) WikiPage(org.apache.wiki.WikiPage) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ServletContext(javax.servlet.ServletContext) Collection(java.util.Collection) Attachment(org.apache.wiki.attachment.Attachment) WikiEngine(org.apache.wiki.WikiEngine)

Aggregations

ProviderException (org.apache.wiki.api.exceptions.ProviderException)42 WikiPage (org.apache.wiki.WikiPage)19 Attachment (org.apache.wiki.attachment.Attachment)16 IOException (java.io.IOException)13 WikiEngine (org.apache.wiki.WikiEngine)11 Collection (java.util.Collection)9 Iterator (java.util.Iterator)9 File (java.io.File)8 InputStream (java.io.InputStream)7 Date (java.util.Date)6 PagePermission (org.apache.wiki.auth.permissions.PagePermission)6 Permission (java.security.Permission)5 ArrayList (java.util.ArrayList)5 WikiContext (org.apache.wiki.WikiContext)5 PluginException (org.apache.wiki.api.exceptions.PluginException)5 AuthorizationManager (org.apache.wiki.auth.AuthorizationManager)5 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)4 BufferedInputStream (java.io.BufferedInputStream)3 FileInputStream (java.io.FileInputStream)3 OutputStream (java.io.OutputStream)3