Search in sources :

Example 26 with ProviderException

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

the class BasicAttachmentProvider method getAttachmentInfo.

/**
 *  {@inheritDoc}
 */
public Attachment getAttachmentInfo(WikiPage page, String name, int version) throws ProviderException {
    Attachment att = new Attachment(m_engine, page.getName(), name);
    File dir = findAttachmentDir(att);
    if (!dir.exists()) {
        // log.debug("Attachment dir not found - thus no attachment can exist.");
        return null;
    }
    if (version == WikiProvider.LATEST_VERSION) {
        version = findLatestVersion(att);
    }
    att.setVersion(version);
    // Should attachment be cachable by the client (browser)?
    if (m_disableCache != null) {
        Matcher matcher = m_disableCache.matcher(name);
        if (matcher.matches()) {
            att.setCacheable(false);
        }
    }
    // System.out.println("Fetching info on version "+version);
    try {
        Properties props = getPageProperties(att);
        att.setAuthor(props.getProperty(version + ".author"));
        String changeNote = props.getProperty(version + ".changenote");
        if (changeNote != null) {
            att.setAttribute(WikiPage.CHANGENOTE, changeNote);
        }
        File f = findFile(dir, att);
        att.setSize(f.length());
        att.setLastModified(new Date(f.lastModified()));
    } catch (FileNotFoundException e) {
        log.error("Can't get attachment properties for " + att, e);
        return null;
    } catch (IOException e) {
        log.error("Can't read page properties", e);
        throw new ProviderException("Cannot read page properties: " + e.getMessage());
    }
    return att;
}
Also used : Matcher(java.util.regex.Matcher) ProviderException(org.apache.wiki.api.exceptions.ProviderException) FileNotFoundException(java.io.FileNotFoundException) Attachment(org.apache.wiki.attachment.Attachment) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) Date(java.util.Date)

Example 27 with ProviderException

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

the class BasicAttachmentProvider method listAttachments.

/**
 *  {@inheritDoc}
 */
public Collection listAttachments(WikiPage page) throws ProviderException {
    Collection<Attachment> result = new ArrayList<Attachment>();
    File dir = findPageDir(page.getName());
    if (dir != null) {
        String[] attachments = dir.list();
        if (attachments != null) {
            // 
            for (int i = 0; i < attachments.length; i++) {
                File f = new File(dir, attachments[i]);
                if (f.isDirectory()) {
                    String attachmentName = unmangleName(attachments[i]);
                    // 
                    if (attachmentName.endsWith(ATTDIR_EXTENSION)) {
                        attachmentName = attachmentName.substring(0, attachmentName.length() - ATTDIR_EXTENSION.length());
                    } else {
                        File propFile = new File(f, PROPERTY_FILE);
                        if (!propFile.exists()) {
                            // 
                            continue;
                        }
                    }
                    Attachment att = getAttachmentInfo(page, attachmentName, WikiProvider.LATEST_VERSION);
                    // 
                    if (att == null) {
                        throw new ProviderException("Attachment disappeared while reading information:" + " if you did not touch the repository, there is a serious bug somewhere. " + "Attachment = " + attachments[i] + ", decoded = " + attachmentName);
                    }
                    result.add(att);
                }
            }
        }
    }
    return result;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) ArrayList(java.util.ArrayList) Attachment(org.apache.wiki.attachment.Attachment) File(java.io.File)

Example 28 with ProviderException

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

the class RSS20Feed method getItems.

private List getItems() {
    ArrayList<Element> list = new ArrayList<Element>();
    SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'");
    WikiEngine engine = m_wikiContext.getEngine();
    ServletContext servletContext = null;
    if (m_wikiContext.getHttpRequest() != null)
        servletContext = m_wikiContext.getHttpRequest().getSession().getServletContext();
    for (Iterator<Entry> i = m_entries.iterator(); i.hasNext(); ) {
        Entry e = i.next();
        WikiPage p = e.getPage();
        String url = e.getURL();
        Element item = new Element("item");
        item.addContent(new Element("link").setText(url));
        item.addContent(new Element("title").setText(e.getTitle()));
        item.addContent(new Element("description").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 = new Element("enclosure");
                    attEl.setAttribute("url", engine.getURL(WikiContext.ATTACH, att.getName(), null, true));
                    attEl.setAttribute("length", Long.toString(att.getSize()));
                    attEl.setAttribute("type", getMimeType(servletContext, att.getFileName()));
                    item.addContent(attEl);
                }
            } catch (ProviderException ex) {
            // FIXME: log.info("Can't get attachment data",ex);
            }
        }
        // 
        // Modification date.
        // 
        Calendar cal = Calendar.getInstance();
        cal.setTime(p.getLastModified());
        cal.add(Calendar.MILLISECOND, -(cal.get(Calendar.ZONE_OFFSET) + (cal.getTimeZone().inDaylightTime(p.getLastModified()) ? cal.get(Calendar.DST_OFFSET) : 0)));
        item.addContent(new Element("pubDate").setText(fmt.format(cal.getTime())));
        list.add(item);
    }
    return list;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) Element(org.jdom2.Element) WikiPage(org.apache.wiki.WikiPage) Attachment(org.apache.wiki.attachment.Attachment) ServletContext(javax.servlet.ServletContext) SimpleDateFormat(java.text.SimpleDateFormat) WikiEngine(org.apache.wiki.WikiEngine)

Example 29 with ProviderException

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

the class Image method execute.

/**
 *  {@inheritDoc}
 */
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    WikiEngine engine = context.getEngine();
    String src = getCleanParameter(params, PARAM_SRC);
    String align = getCleanParameter(params, PARAM_ALIGN);
    String ht = getCleanParameter(params, PARAM_HEIGHT);
    String wt = getCleanParameter(params, PARAM_WIDTH);
    String alt = getCleanParameter(params, PARAM_ALT);
    String caption = getCleanParameter(params, PARAM_CAPTION);
    String link = getCleanParameter(params, PARAM_LINK);
    String target = getCleanParameter(params, PARAM_TARGET);
    String style = getCleanParameter(params, PARAM_STYLE);
    String cssclass = getCleanParameter(params, PARAM_CLASS);
    // String map     = getCleanParameter( params, PARAM_MAP );
    String border = getCleanParameter(params, PARAM_BORDER);
    String title = getCleanParameter(params, PARAM_TITLE);
    if (src == null) {
        throw new PluginException("Parameter 'src' is required for Image plugin");
    }
    if (target != null && !validTargetValue(target)) {
        // not a valid value so ignore
        target = null;
    }
    try {
        AttachmentManager mgr = engine.getAttachmentManager();
        Attachment att = mgr.getAttachmentInfo(context, src);
        if (att != null) {
            src = context.getURL(WikiContext.ATTACH, att.getName());
        }
    } catch (ProviderException e) {
        throw new PluginException("Attachment info failed: " + e.getMessage());
    }
    StringBuilder result = new StringBuilder();
    result.append("<table border=\"0\" class=\"imageplugin\"");
    if (title != null) {
        result.append(" title=\"" + title + "\"");
    }
    if (align != null) {
        if (align.equals("center")) {
            result.append(" style=\"margin-left: auto; margin-right: auto; text-align:center; vertical-align:middle;\"");
        } else {
            result.append(" style=\"float:" + align + ";\"");
        }
    }
    result.append(">\n");
    if (caption != null) {
        result.append("<caption>" + caption + "</caption>\n");
    }
    // move css class and style to the container of the image,
    // so it doesn't affect the caption
    result.append("<tr><td");
    if (cssclass != null) {
        result.append(" class=\"" + cssclass + "\"");
    }
    if (style != null) {
        result.append(" style=\"" + style);
        // Make sure that we add a ";" to the end of the style string
        if (result.charAt(result.length() - 1) != ';')
            result.append(";");
        result.append("\"");
    }
    result.append(">");
    if (link != null) {
        result.append("<a href=\"" + link + "\"");
        if (target != null) {
            result.append(" target=\"" + target + "\"");
        }
        result.append(">");
    }
    result.append("<img src=\"" + src + "\"");
    if (ht != null)
        result.append(" height=\"" + ht + "\"");
    if (wt != null)
        result.append(" width=\"" + wt + "\"");
    if (alt != null)
        result.append(" alt=\"" + alt + "\"");
    if (border != null)
        result.append(" border=\"" + border + "\"");
    // if( map != null )    result.append(" map=\""+map+"\"");
    result.append(" />");
    if (link != null)
        result.append("</a>");
    result.append("</td></tr>\n");
    result.append("</table>\n");
    return result.toString();
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) PluginException(org.apache.wiki.api.exceptions.PluginException) Attachment(org.apache.wiki.attachment.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) WikiEngine(org.apache.wiki.WikiEngine)

Example 30 with ProviderException

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

the class IndexPlugin method execute.

/**
 * {@inheritDoc}
 */
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    String include = params.get(PARAM_INCLUDE);
    String exclude = params.get(PARAM_EXCLUDE);
    Element masterDiv = getElement("div", "index");
    Element indexDiv = getElement("div", "header");
    masterDiv.addContent(indexDiv);
    try {
        List<String> pages = listPages(context, include, exclude);
        context.getEngine().getPageSorter().sort(pages);
        char initialChar = ' ';
        Element currentDiv = new Element("div", xmlns_XHTML);
        for (String name : pages) {
            if (name.charAt(0) != initialChar) {
                if (initialChar != ' ') {
                    indexDiv.addContent(" - ");
                }
                initialChar = name.charAt(0);
                masterDiv.addContent(makeHeader(String.valueOf(initialChar)));
                currentDiv = getElement("div", "body");
                masterDiv.addContent(currentDiv);
                indexDiv.addContent(getLink("#" + initialChar, String.valueOf(initialChar)));
            } else {
                currentDiv.addContent(", ");
            }
            currentDiv.addContent(getLink(context.getURL(WikiContext.VIEW, name), name));
        }
    } catch (ProviderException e) {
        log.warn("could not load page index", e);
        throw new PluginException(e.getMessage());
    }
    // serialize to raw format string (no changes to whitespace)
    XMLOutputter out = new XMLOutputter(Format.getRawFormat());
    return out.outputString(masterDiv);
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) ProviderException(org.apache.wiki.api.exceptions.ProviderException) Element(org.jdom2.Element) PluginException(org.apache.wiki.api.exceptions.PluginException)

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