Search in sources :

Example 16 with Attachment

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

the class WikiEngineTest method testAttachmentRefs3.

/**
 *  Checks, if ReferenceManager is informed if a link to an attachment is added.
 */
@Test
public void testAttachmentRefs3() throws Exception {
    final ReferenceManager refMgr = m_engine.getManager(ReferenceManager.class);
    final AttachmentManager attMgr = m_engine.getManager(AttachmentManager.class);
    m_engine.saveText(NAME1, "fooBar");
    final Attachment att = Wiki.contents().attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
    m_engine.saveText(NAME1, " [" + NAME1 + "/TestAtt.txt] ");
    // and check post-conditions
    Collection<String> c = refMgr.findUncreated();
    Assertions.assertTrue(c == null || c.size() == 0, "attachment exists");
    c = refMgr.findUnreferenced();
    Assertions.assertEquals(c.size(), 1, "unreferenced count");
    Assertions.assertEquals(NAME1, c.iterator().next(), "unreferenced");
}
Also used : Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) ReferenceManager(org.apache.wiki.references.ReferenceManager) Test(org.junit.jupiter.api.Test)

Example 17 with Attachment

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

the class DefaultPageRenamer method getReferencesToChange.

private Set<String> getReferencesToChange(final Page fromPage, final Engine engine) {
    final Set<String> referrers = new TreeSet<>();
    final Collection<String> r = engine.getManager(ReferenceManager.class).findReferrers(fromPage.getName());
    if (r != null) {
        referrers.addAll(r);
    }
    try {
        final List<Attachment> attachments = engine.getManager(AttachmentManager.class).listAttachments(fromPage);
        for (final Attachment att : attachments) {
            final Collection<String> c = engine.getManager(ReferenceManager.class).findReferrers(att.getName());
            if (c != null) {
                referrers.addAll(c);
            }
        }
    } catch (final ProviderException e) {
        // We will continue despite this error
        log.error("Provider error while fetching attachments for rename", e);
    }
    return referrers;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) TreeSet(java.util.TreeSet) Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) ReferenceManager(org.apache.wiki.references.ReferenceManager)

Example 18 with Attachment

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

the class LinkTag method doEndTag.

@Override
public int doEndTag() {
    try {
        final Engine engine = m_wikiContext.getEngine();
        final JspWriter out = pageContext.getOut();
        final String url = figureOutURL();
        final StringBuilder sb = new StringBuilder(20);
        sb.append((m_cssClass != null) ? "class=\"" + m_cssClass + "\" " : "");
        sb.append((m_style != null) ? "style=\"" + m_style + "\" " : "");
        sb.append((m_target != null) ? "target=\"" + m_target + "\" " : "");
        sb.append((m_title != null) ? "title=\"" + m_title + "\" " : "");
        sb.append((m_rel != null) ? "rel=\"" + m_rel + "\" " : "");
        sb.append((m_accesskey != null) ? "accesskey=\"" + m_accesskey + "\" " : "");
        sb.append((m_tabindex != null) ? "tabindex=\"" + m_tabindex + "\" " : "");
        if (engine.getManager(PageManager.class).getPage(m_pageName) instanceof Attachment) {
            sb.append(engine.getManager(AttachmentManager.class).forceDownload(m_pageName) ? "download " : "");
        }
        switch(m_format) {
            case URL:
                out.print(url);
                break;
            default:
            case ANCHOR:
                out.print("<a " + sb + " href=\"" + url + "\">");
                break;
        }
        // Add any explicit body content. This is not the intended use of LinkTag, but happens to be the way it has worked previously.
        if (m_bodyContent != null) {
            final String linktext = m_bodyContent.getString().trim();
            out.write(linktext);
        }
        // Finish off by closing opened anchor
        if (m_format == ANCHOR)
            out.print("</a>");
    } catch (final Exception e) {
        // Yes, we want to catch all exceptions here, including RuntimeExceptions
        log.error("Tag failed", e);
    }
    return EVAL_PAGE;
}
Also used : Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) JspWriter(javax.servlet.jsp.JspWriter) Engine(org.apache.wiki.api.core.Engine) ProviderException(org.apache.wiki.api.exceptions.ProviderException)

Example 19 with Attachment

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

the class LinkToTag method doWikiStartTag.

@Override
public int doWikiStartTag() throws IOException {
    String pageName = m_pageName;
    boolean isattachment = false;
    if (m_pageName == null) {
        final Page p = m_wikiContext.getPage();
        if (p != null) {
            pageName = p.getName();
            isattachment = p instanceof Attachment;
        } else {
            return SKIP_BODY;
        }
    }
    final JspWriter out = pageContext.getOut();
    final String url;
    final String linkclass;
    String forceDownload = "";
    if (isattachment) {
        url = m_wikiContext.getURL(ContextEnum.PAGE_ATTACH.getRequestContext(), pageName, (getVersion() != null) ? "version=" + getVersion() : null);
        linkclass = "attachment";
        if (m_wikiContext.getEngine().getManager(AttachmentManager.class).forceDownload(pageName)) {
            forceDownload = "download ";
        }
    } else {
        final StringBuilder params = new StringBuilder();
        if (getVersion() != null) {
            params.append("version=").append(getVersion());
        }
        if (getTemplate() != null) {
            params.append(params.length() > 0 ? "&amp;" : "").append("skin=").append(getTemplate());
        }
        url = m_wikiContext.getURL(ContextEnum.PAGE_VIEW.getRequestContext(), pageName, params.toString());
        linkclass = "wikipage";
    }
    switch(m_format) {
        case ANCHOR:
            out.print("<a class=\"" + linkclass + "\" href=\"" + url + "\" accesskey=\"" + m_accesskey + "\" title=\"" + m_title + "\" " + forceDownload + ">");
            break;
        case URL:
            out.print(url);
            break;
    }
    return EVAL_BODY_INCLUDE;
}
Also used : Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) JspWriter(javax.servlet.jsp.JspWriter)

Example 20 with Attachment

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

the class AttachmentsIteratorTag method doAfterBody.

/**
 *  {@inheritDoc}
 */
@Override
public final int doAfterBody() {
    if (bodyContent != null) {
        try {
            final JspWriter out = getPreviousOut();
            out.print(bodyContent.getString());
            bodyContent.clearBody();
        } catch (final IOException e) {
            log.error("Unable to get inner tag text", e);
        // FIXME: throw something?
        }
    }
    if (m_iterator != null && 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);
        return EVAL_BODY_BUFFERED;
    }
    return SKIP_BODY;
}
Also used : Context(org.apache.wiki.api.core.Context) PageContext(javax.servlet.jsp.PageContext) Attachment(org.apache.wiki.api.core.Attachment) IOException(java.io.IOException) JspWriter(javax.servlet.jsp.JspWriter)

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