Search in sources :

Example 11 with ProviderException

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

the class HistoryIteratorTag method doStartTag.

public final int doStartTag() {
    m_wikiContext = (WikiContext) pageContext.getAttribute(WikiTagBase.ATTR_CONTEXT, PageContext.REQUEST_SCOPE);
    WikiEngine engine = m_wikiContext.getEngine();
    WikiPage page;
    page = m_wikiContext.getPage();
    try {
        if (page != null && engine.pageExists(page)) {
            Collection versions = engine.getVersionHistory(page.getName());
            if (versions == null) {
                // There is no history
                return SKIP_BODY;
            }
            m_iterator = versions.iterator();
            if (m_iterator.hasNext()) {
                WikiContext context = (WikiContext) m_wikiContext.clone();
                context.setPage((WikiPage) m_iterator.next());
                pageContext.setAttribute(WikiTagBase.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE);
                pageContext.setAttribute(getId(), context.getPage());
            } 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) WikiEngine(org.apache.wiki.WikiEngine)

Example 12 with ProviderException

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

the class PageSizeTag method doWikiStartTag.

public final int doWikiStartTag() throws IOException {
    WikiEngine engine = m_wikiContext.getEngine();
    WikiPage page = m_wikiContext.getPage();
    try {
        if (page != null) {
            long size = page.getSize();
            if (// should never happen with attachments
            size == -1 && engine.pageExists(page)) {
                size = engine.getPureText(page.getName(), page.getVersion()).length();
                page.setSize(size);
            }
            pageContext.getOut().write(Long.toString(size));
        }
    } catch (ProviderException e) {
        log.warn("Providers did not work: ", e);
        pageContext.getOut().write("Error determining page size: " + e.getMessage());
    }
    return SKIP_BODY;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiPage(org.apache.wiki.WikiPage) WikiEngine(org.apache.wiki.WikiEngine)

Example 13 with ProviderException

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

the class AttachmentServlet method doGet.

/**
 *  Serves a GET with two parameters: 'wikiname' specifying the wikiname
 *  of the attachment, 'version' specifying the version indicator.
 */
// FIXME: Messages would need to be localized somehow.
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    WikiContext context = m_engine.createContext(req, WikiContext.ATTACH);
    String version = req.getParameter(HDR_VERSION);
    String nextPage = req.getParameter("nextpage");
    String msg = "An error occurred. Ouch.";
    int ver = WikiProvider.LATEST_VERSION;
    AttachmentManager mgr = m_engine.getAttachmentManager();
    AuthorizationManager authmgr = m_engine.getAuthorizationManager();
    String page = context.getPage().getName();
    if (page == null) {
        log.info("Invalid attachment name.");
        res.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    OutputStream out = null;
    InputStream in = null;
    try {
        log.debug("Attempting to download att " + page + ", version " + version);
        if (version != null) {
            ver = Integer.parseInt(version);
        }
        Attachment att = mgr.getAttachmentInfo(page, ver);
        if (att != null) {
            // 
            // Check if the user has permission for this attachment
            // 
            Permission permission = PermissionFactory.getPagePermission(att, "view");
            if (!authmgr.checkPermission(context.getWikiSession(), permission)) {
                log.debug("User does not have permission for this");
                res.sendError(HttpServletResponse.SC_FORBIDDEN);
                return;
            }
            // 
            if (HttpUtil.checkFor304(req, att.getName(), att.getLastModified())) {
                log.debug("Client has latest version already, sending 304...");
                res.sendError(HttpServletResponse.SC_NOT_MODIFIED);
                return;
            }
            String mimetype = getMimeType(context, att.getFileName());
            res.setContentType(mimetype);
            // 
            // We use 'inline' instead of 'attachment' so that user agents
            // can try to automatically open the file.
            // 
            res.addHeader("Content-Disposition", "inline; filename=\"" + att.getFileName() + "\";");
            res.addDateHeader("Last-Modified", att.getLastModified().getTime());
            if (!att.isCacheable()) {
                res.addHeader("Pragma", "no-cache");
                res.addHeader("Cache-control", "no-cache");
            }
            // If a size is provided by the provider, report it.
            if (att.getSize() >= 0) {
                // log.info("size:"+att.getSize());
                res.setContentLength((int) att.getSize());
            }
            out = res.getOutputStream();
            in = mgr.getAttachmentStream(context, att);
            int read = 0;
            byte[] buffer = new byte[BUFFER_SIZE];
            while ((read = in.read(buffer)) > -1) {
                out.write(buffer, 0, read);
            }
            if (log.isDebugEnabled()) {
                msg = "Attachment " + att.getFileName() + " sent to " + req.getRemoteUser() + " on " + HttpUtil.getRemoteAddress(req);
                log.debug(msg);
            }
            if (nextPage != null) {
                res.sendRedirect(validateNextPage(nextPage, m_engine.getURL(WikiContext.ERROR, "", null, false)));
            }
        } else {
            msg = "Attachment '" + page + "', version " + ver + " does not exist.";
            log.info(msg);
            res.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
        }
    } catch (ProviderException pe) {
        msg = "Provider error: " + pe.getMessage();
        log.debug("Provider failed while reading", pe);
        // 
        try {
            res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
        } catch (IllegalStateException e) {
        }
    } catch (NumberFormatException nfe) {
        log.warn("Invalid version number: " + version);
        res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid version number");
    } catch (SocketException se) {
        // 
        // These are very common in download situations due to aggressive
        // clients.  No need to try and send an error.
        // 
        log.debug("I/O exception during download", se);
    } catch (IOException ioe) {
        // 
        // Client dropped the connection or something else happened.
        // We don't know where the error came from, so we'll at least
        // try to send an error and catch it quietly if it doesn't quite work.
        // 
        msg = "Error: " + ioe.getMessage();
        log.debug("I/O exception during download", ioe);
        try {
            res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
        } catch (IllegalStateException e) {
        }
    } finally {
        IOUtils.closeQuietly(in);
        // 
        // Quite often, aggressive clients close the connection when they have
        // received the last bits.  Therefore, we close the output, but ignore
        // any exception that might come out of it.
        // 
        IOUtils.closeQuietly(out);
    }
}
Also used : SocketException(java.net.SocketException) WikiContext(org.apache.wiki.WikiContext) ProviderException(org.apache.wiki.api.exceptions.ProviderException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Permission(java.security.Permission) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager)

Example 14 with ProviderException

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

the class AttachmentServlet method executeUpload.

/**
 * @param context the wiki context
 * @param data the input stream data
 * @param filename the name of the file to upload
 * @param errorPage the place to which you want to get a redirection
 * @param parentPage the page to which the file should be attached
 * @param changenote The change note
 * @param contentLength The content length
 * @return <code>true</code> if upload results in the creation of a new page;
 * <code>false</code> otherwise
 * @throws RedirectException If the content needs to be redirected
 * @throws IOException       If there is a problem in the upload.
 * @throws ProviderException If there is a problem in the backend.
 */
protected boolean executeUpload(WikiContext context, InputStream data, String filename, String errorPage, String parentPage, String changenote, long contentLength) throws RedirectException, IOException, ProviderException {
    boolean created = false;
    try {
        filename = AttachmentManager.validateFileName(filename);
    } catch (WikiException e) {
        // here we have the context available, so we can internationalize it properly :
        throw new RedirectException(Preferences.getBundle(context, InternationalizationManager.CORE_BUNDLE).getString(e.getMessage()), errorPage);
    }
    if (!context.hasAdminPermissions()) {
        if (contentLength > m_maxSize) {
            // FIXME: Does not delete the received files.
            throw new RedirectException("File exceeds maximum size (" + m_maxSize + " bytes)", errorPage);
        }
        if (!isTypeAllowed(filename)) {
            throw new RedirectException("Files of this type may not be uploaded to this wiki", errorPage);
        }
    }
    Principal user = context.getCurrentUser();
    AttachmentManager mgr = m_engine.getAttachmentManager();
    log.debug("file=" + filename);
    if (data == null) {
        log.error("File could not be opened.");
        throw new RedirectException("File could not be opened.", errorPage);
    }
    // 
    // Check whether we already have this kind of a page.
    // If the "page" parameter already defines an attachment
    // name for an update, then we just use that file.
    // Otherwise we create a new attachment, and use the
    // filename given.  Incidentally, this will also mean
    // that if the user uploads a file with the exact
    // same name than some other previous attachment,
    // then that attachment gains a new version.
    // 
    Attachment att = mgr.getAttachmentInfo(context.getPage().getName());
    if (att == null) {
        att = new Attachment(m_engine, parentPage, filename);
        created = true;
    }
    att.setSize(contentLength);
    // 
    // Check if we're allowed to do this?
    // 
    Permission permission = PermissionFactory.getPagePermission(att, "upload");
    if (m_engine.getAuthorizationManager().checkPermission(context.getWikiSession(), permission)) {
        if (user != null) {
            att.setAuthor(user.getName());
        }
        if (changenote != null && changenote.length() > 0) {
            att.setAttribute(WikiPage.CHANGENOTE, changenote);
        }
        try {
            m_engine.getAttachmentManager().storeAttachment(att, data);
        } catch (ProviderException pe) {
            // here we have the context available, so we can internationalize it properly :
            throw new ProviderException(Preferences.getBundle(context, InternationalizationManager.CORE_BUNDLE).getString(pe.getMessage()));
        }
        log.info("User " + user + " uploaded attachment to " + parentPage + " called " + filename + ", size " + att.getSize());
    } else {
        throw new RedirectException("No permission to upload a file", errorPage);
    }
    return created;
}
Also used : WikiException(org.apache.wiki.api.exceptions.WikiException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) Permission(java.security.Permission) Principal(java.security.Principal) RedirectException(org.apache.wiki.api.exceptions.RedirectException)

Example 15 with ProviderException

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

the class DefaultAclManager method setPermissions.

/**
 * Sets the access control list for the page and persists it by prepending
 * it to the wiki page markup and saving the page. When this method is
 * called, all other ACL markup in the page is removed. This method will forcibly
 * expire locks on the wiki page if they exist. Any ProviderExceptions will be
 * re-thrown as WikiSecurityExceptions.
 *
 * @param page the wiki page
 * @param acl  the access control list
 * @throws WikiSecurityException of the Acl cannot be set
 * @since 2.5
 */
public void setPermissions(WikiPage page, Acl acl) throws WikiSecurityException {
    PageManager pageManager = m_engine.getPageManager();
    // Forcibly expire any page locks
    PageLock lock = pageManager.getCurrentLock(page);
    if (lock != null) {
        pageManager.unlockPage(lock);
    }
    // Remove all of the existing ACLs.
    String pageText = m_engine.getPureText(page);
    Matcher matcher = DefaultAclManager.ACL_PATTERN.matcher(pageText);
    String cleansedText = matcher.replaceAll("");
    String newText = DefaultAclManager.printAcl(page.getAcl()) + cleansedText;
    try {
        pageManager.putPageText(page, newText);
    } catch (ProviderException e) {
        throw new WikiSecurityException("Could not set Acl. Reason: ProviderExcpetion " + e.getMessage(), e);
    }
}
Also used : WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) PageManager(org.apache.wiki.PageManager) Matcher(java.util.regex.Matcher) ProviderException(org.apache.wiki.api.exceptions.ProviderException) PageLock(org.apache.wiki.PageLock)

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