Search in sources :

Example 71 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class AttachmentsIteratorTag method doStartTag.

/**
 *  {@inheritDoc}
 */
@Override
public final int doStartTag() {
    m_wikiContext = (Context) pageContext.getAttribute(Context.ATTR_CONTEXT, PageContext.REQUEST_SCOPE);
    final Engine engine = m_wikiContext.getEngine();
    final AttachmentManager mgr = engine.getManager(AttachmentManager.class);
    final Page page;
    page = m_wikiContext.getPage();
    if (!mgr.attachmentsEnabled()) {
        return SKIP_BODY;
    }
    try {
        if (page != null && engine.getManager(PageManager.class).wikiPageExists(page)) {
            final List<Attachment> 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()) {
                final Attachment att = (Attachment) m_iterator.next();
                final Context context = m_wikiContext.clone();
                context.setPage(att);
                pageContext.setAttribute(Context.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE);
                pageContext.setAttribute(getId(), att);
            } else {
                return SKIP_BODY;
            }
        } else {
            return SKIP_BODY;
        }
        return EVAL_BODY_BUFFERED;
    } catch (final ProviderException e) {
        log.fatal("Provider failed while trying to iterator through history", e);
    // FIXME: THrow something.
    }
    return SKIP_BODY;
}
Also used : Context(org.apache.wiki.api.core.Context) PageContext(javax.servlet.jsp.PageContext) ProviderException(org.apache.wiki.api.exceptions.ProviderException) Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) Engine(org.apache.wiki.api.core.Engine)

Example 72 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class LinkTag method figureOutURL.

/**
 *  This method figures out what kind of an URL should be output.  It mirrors heavily on JSPWikiMarkupParser.handleHyperlinks();
 *
 * @return the URL
 * @throws ProviderException
 */
private String figureOutURL() throws ProviderException {
    String url = null;
    final Engine engine = m_wikiContext.getEngine();
    if (m_pageName == null) {
        final Page page = m_wikiContext.getPage();
        if (page != null) {
            m_pageName = page.getName();
        }
    }
    if (m_templatefile != null) {
        final String params = addParamsForRecipient(null, m_containedParams);
        final String template = engine.getTemplateDir();
        url = engine.getURL(ContextEnum.PAGE_NONE.getRequestContext(), "templates/" + template + "/" + m_templatefile, params);
    } else if (m_jsp != null) {
        final String params = addParamsForRecipient(null, m_containedParams);
        // url = m_wikiContext.getURL( ContextEnum.PAGE_NONE.getRequestContext(), m_jsp, params );
        url = engine.getURL(ContextEnum.PAGE_NONE.getRequestContext(), m_jsp, params);
    } else if (m_ref != null) {
        final int interwikipoint;
        if (new LinkParsingOperations(m_wikiContext).isExternalLink(m_ref)) {
            url = m_ref;
        } else if ((interwikipoint = m_ref.indexOf(":")) != -1) {
            final String extWiki = m_ref.substring(0, interwikipoint);
            final String wikiPage = m_ref.substring(interwikipoint + 1);
            url = engine.getInterWikiURL(extWiki);
            if (url != null) {
                url = TextUtil.replaceString(url, "%s", wikiPage);
            }
        } else if (m_ref.startsWith("#")) {
        // Local link
        } else if (TextUtil.isNumber(m_ref)) {
        // Reference
        } else {
            final int hashMark;
            final String parms = (m_version != null) ? "version=" + getVersion() : null;
            // Internal wiki link, but is it an attachment link?
            final Page p = engine.getManager(PageManager.class).getPage(m_pageName);
            if (p instanceof Attachment) {
                url = m_wikiContext.getURL(ContextEnum.PAGE_ATTACH.getRequestContext(), m_pageName);
            } else if ((hashMark = m_ref.indexOf('#')) != -1) {
                // It's an internal Wiki link, but to a named section
                final String namedSection = m_ref.substring(hashMark + 1);
                String reallink = m_ref.substring(0, hashMark);
                reallink = MarkupParser.cleanLink(reallink);
                String matchedLink;
                String sectref = "";
                if ((matchedLink = engine.getFinalPageName(reallink)) != null) {
                    sectref = "section-" + engine.encodeName(matchedLink) + "-" + namedSection;
                    sectref = "#" + sectref.replace('%', '_');
                } else {
                    matchedLink = reallink;
                }
                url = makeBasicURL(m_context, matchedLink, parms) + sectref;
            } else {
                final String reallink = MarkupParser.cleanLink(m_ref);
                url = makeBasicURL(m_context, reallink, parms);
            }
        }
    } else if (m_pageName != null && !m_pageName.isEmpty()) {
        final Page p = engine.getManager(PageManager.class).getPage(m_pageName);
        String parms = (m_version != null) ? "version=" + getVersion() : null;
        parms = addParamsForRecipient(parms, m_containedParams);
        if (p instanceof Attachment) {
            String ctx = m_context;
            // attachment, but don't override the context setting otherwise
            if (m_context == null || m_context.equals(ContextEnum.PAGE_VIEW.getRequestContext())) {
                ctx = ContextEnum.PAGE_ATTACH.getRequestContext();
            }
            url = engine.getURL(ctx, m_pageName, parms);
        // url = m_wikiContext.getURL( ctx, m_pageName, parms );
        } else {
            url = makeBasicURL(m_context, m_pageName, parms);
        }
    } else {
        final String page = engine.getFrontPage();
        url = makeBasicURL(m_context, page, null);
    }
    return url;
}
Also used : LinkParsingOperations(org.apache.wiki.parser.LinkParsingOperations) Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment) Engine(org.apache.wiki.api.core.Engine)

Example 73 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class LinkToParentTag method doWikiStartTag.

public int doWikiStartTag() throws IOException {
    final Page p = m_wikiContext.getPage();
    // We just simply set the page to be our parent page and call the superclass.
    if (p instanceof Attachment) {
        setPage(((Attachment) p).getParentName());
    } else {
        final String name = p.getName();
        final int entrystart = name.indexOf("_blogentry_");
        if (entrystart != -1) {
            setPage(name.substring(0, entrystart));
        }
        final int commentstart = name.indexOf("_comments_");
        if (commentstart != -1) {
            setPage(name.substring(0, commentstart));
        }
    }
    return super.doWikiStartTag();
}
Also used : Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment)

Example 74 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class RSS20Feed method getItems.

private List<Element> getItems() {
    final ArrayList<Element> list = new ArrayList<>();
    final SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'");
    final Engine engine = m_wikiContext.getEngine();
    ServletContext servletContext = null;
    if (m_wikiContext.getHttpRequest() != null)
        servletContext = m_wikiContext.getHttpRequest().getSession().getServletContext();
    for (final Entry e : m_entries) {
        final Page p = e.getPage();
        final String url = e.getURL();
        final 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.getManager(AttachmentManager.class).hasAttachments(p) && servletContext != null) {
            try {
                final List<Attachment> c = engine.getManager(AttachmentManager.class).listAttachments(p);
                for (final Attachment att : c) {
                    final Element attEl = new Element("enclosure");
                    attEl.setAttribute("url", engine.getURL(ContextEnum.PAGE_ATTACH.getRequestContext(), att.getName(), null));
                    attEl.setAttribute("length", Long.toString(att.getSize()));
                    attEl.setAttribute("type", getMimeType(servletContext, att.getFileName()));
                    item.addContent(attEl);
                }
            } catch (final ProviderException ex) {
            // FIXME: log.info("Can't get attachment data",ex);
            }
        }
        // 
        // Modification date.
        // 
        final 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) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment) ServletContext(javax.servlet.ServletContext) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) SimpleDateFormat(java.text.SimpleDateFormat) Engine(org.apache.wiki.api.core.Engine)

Example 75 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class RPCHandlerTest method testRecentChangesWithAttachments.

@Test
public void testRecentChangesWithAttachments() throws Exception {
    Date time = getCalendarTime(Calendar.getInstance().getTime());
    final Vector previousChanges = m_handler.getRecentChanges(time);
    m_engine.saveText(NAME1, "Foo");
    final Attachment att = Wiki.contents().attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    m_engine.getManager(AttachmentManager.class).storeAttachment(att, m_engine.makeAttachmentFile());
    final Page directInfo = m_engine.getManager(PageManager.class).getPage(NAME1);
    time = getCalendarTime(directInfo.getLastModified());
    final Vector recentChanges = m_handler.getRecentChanges(time);
    Assertions.assertEquals(1, recentChanges.size() - previousChanges.size(), "wrong number of changes");
}
Also used : PageManager(org.apache.wiki.pages.PageManager) Attachment(org.apache.wiki.api.core.Attachment) Page(org.apache.wiki.api.core.Page) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) Vector(java.util.Vector) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

Attachment (org.apache.wiki.api.core.Attachment)76 Test (org.junit.jupiter.api.Test)37 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)35 Page (org.apache.wiki.api.core.Page)27 ProviderException (org.apache.wiki.api.exceptions.ProviderException)20 PageManager (org.apache.wiki.pages.PageManager)19 File (java.io.File)15 Date (java.util.Date)10 Engine (org.apache.wiki.api.core.Engine)10 ReferenceManager (org.apache.wiki.references.ReferenceManager)9 InputStream (java.io.InputStream)8 FileInputStream (java.io.FileInputStream)6 IOException (java.io.IOException)6 InputStreamReader (java.io.InputStreamReader)6 StringWriter (java.io.StringWriter)6 Vector (java.util.Vector)6 WikiContext (org.apache.wiki.WikiContext)6 Permission (java.security.Permission)4 ArrayList (java.util.ArrayList)4 Hashtable (java.util.Hashtable)4