Search in sources :

Example 36 with ProviderException

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

the class AttachmentsIteratorTag method doStartTag.

/**
 *  {@inheritDoc}
 */
@Override
public final int doStartTag() {
    m_wikiContext = (WikiContext) pageContext.getAttribute(WikiTagBase.ATTR_CONTEXT, PageContext.REQUEST_SCOPE);
    WikiEngine engine = m_wikiContext.getEngine();
    AttachmentManager mgr = engine.getAttachmentManager();
    WikiPage page;
    page = m_wikiContext.getPage();
    if (!mgr.attachmentsEnabled()) {
        return SKIP_BODY;
    }
    try {
        if (page != null && engine.pageExists(page)) {
            Collection atts = mgr.listAttachments(page);
            if (atts == null) {
                log.debug("No attachments to display.");
                // There are no attachments included
                return SKIP_BODY;
            }
            m_iterator = atts.iterator();
            if (m_iterator.hasNext()) {
                Attachment att = (Attachment) m_iterator.next();
                WikiContext context = (WikiContext) m_wikiContext.clone();
                context.setPage(att);
                pageContext.setAttribute(WikiTagBase.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE);
                pageContext.setAttribute(getId(), att);
            } else {
                return SKIP_BODY;
            }
        } else {
            return SKIP_BODY;
        }
        return EVAL_BODY_BUFFERED;
    } catch (ProviderException e) {
        log.fatal("Provider failed while trying to iterator through history", e);
    // FIXME: THrow something.
    }
    return SKIP_BODY;
}
Also used : WikiContext(org.apache.wiki.WikiContext) ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiPage(org.apache.wiki.WikiPage) Collection(java.util.Collection) Attachment(org.apache.wiki.attachment.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) WikiEngine(org.apache.wiki.WikiEngine)

Example 37 with ProviderException

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

the class VersioningFileProvider method deleteVersion.

/**
 *  {@inheritDoc}
 *
 *  Deleting versions has never really worked,
 *  JSPWiki assumes that version histories are "not gappy".
 *  Using deleteVersion() is definitely not recommended.
 */
public void deleteVersion(String page, int version) throws ProviderException {
    File dir = findOldPageDir(page);
    int latest = findLatestVersion(page);
    if (version == WikiPageProvider.LATEST_VERSION || version == latest || (version == 1 && latest == -1)) {
        // 
        try {
            Properties props = getPageProperties(page);
            props.remove(((latest > 0) ? latest : 1) + ".author");
            putPageProperties(page, props);
        } catch (IOException e) {
            log.error("Unable to modify page properties", e);
            throw new ProviderException("Could not modify page properties: " + e.getMessage());
        }
        // We can let the FileSystemProvider take care
        // of the actual deletion
        super.deleteVersion(page, WikiPageProvider.LATEST_VERSION);
        // 
        // Copy the old file to the new location
        // 
        latest = findLatestVersion(page);
        File pageDir = findOldPageDir(page);
        File previousFile = new File(pageDir, Integer.toString(latest) + FILE_EXT);
        InputStream in = null;
        OutputStream out = null;
        try {
            if (previousFile.exists()) {
                in = new BufferedInputStream(new FileInputStream(previousFile));
                File pageFile = findPage(page);
                out = new BufferedOutputStream(new FileOutputStream(pageFile));
                FileUtil.copyContents(in, out);
                // 
                // We need also to set the date, since we rely on this.
                // 
                pageFile.setLastModified(previousFile.lastModified());
            }
        } catch (IOException e) {
            log.fatal("Something wrong with the page directory - you may have just lost data!", e);
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
        return;
    }
    File pageFile = new File(dir, "" + version + FILE_EXT);
    if (pageFile.exists()) {
        if (!pageFile.delete()) {
            log.error("Unable to delete page.");
        }
    } else {
        throw new NoSuchVersionException("Page " + page + ", version=" + version);
    }
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream)

Example 38 with ProviderException

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

the class VersioningFileProvider method readFile.

// FIXME: Should this really be here?
private String readFile(File pagedata) throws ProviderException {
    String result = null;
    InputStream in = null;
    if (pagedata.exists()) {
        if (pagedata.canRead()) {
            try {
                in = new FileInputStream(pagedata);
                result = FileUtil.readContents(in, m_encoding);
            } catch (IOException e) {
                log.error("Failed to read", e);
                throw new ProviderException("I/O error: " + e.getMessage());
            } finally {
                IOUtils.closeQuietly(in);
            }
        } else {
            log.warn("Failed to read page from '" + pagedata.getAbsolutePath() + "', possibly a permissions problem");
            throw new ProviderException("I cannot read the requested page.");
        }
    } else {
        // This is okay.
        // FIXME: is it?
        log.info("New page");
    }
    return result;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 39 with ProviderException

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

the class HasAttachmentsTag method doWikiStartTag.

public final int doWikiStartTag() throws IOException {
    WikiEngine engine = m_wikiContext.getEngine();
    WikiPage page = m_wikiContext.getPage();
    AttachmentManager mgr = engine.getAttachmentManager();
    try {
        if (page != null && engine.pageExists(page) && mgr.attachmentsEnabled()) {
            if (mgr.hasAttachments(page)) {
                return EVAL_BODY_INCLUDE;
            }
        }
    } catch (ProviderException e) {
        log.fatal("Provider failed while trying to check for attachements", e);
    // FIXME: THrow something.
    }
    return SKIP_BODY;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiPage(org.apache.wiki.WikiPage) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) WikiEngine(org.apache.wiki.WikiEngine)

Example 40 with ProviderException

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

the class AttachmentManager method storeAttachment.

/**
 *  Stores an attachment directly from a stream.
 *  If the attachment did not exist previously, this method
 *  will create it.  If it did exist, it stores a new version.
 *
 *  @param att Attachment to store this under.
 *  @param in  InputStream from which the attachment contents will be read.
 *
 *  @throws IOException If writing the attachment failed.
 *  @throws ProviderException If something else went wrong.
 */
public void storeAttachment(Attachment att, InputStream in) throws IOException, ProviderException {
    if (m_provider == null) {
        return;
    }
    // 
    if (!m_engine.getPageManager().pageExists(att.getParentName())) {
        // the caller should catch the exception and use the exception text as an i18n key
        throw new ProviderException("attach.parent.not.exist");
    }
    m_provider.putAttachmentData(att, in);
    m_engine.getReferenceManager().updateReferences(att.getName(), new Vector<String>());
    WikiPage parent = new WikiPage(m_engine, att.getParentName());
    m_engine.updateReferences(parent);
    m_engine.getSearchManager().reindexPage(att);
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiPage(org.apache.wiki.WikiPage)

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