Search in sources :

Example 21 with Context

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

the class RSSGeneratorTest method testBlogRSS.

@Test
public void testBlogRSS() throws Exception {
    final WeblogEntryPlugin plugin = new WeblogEntryPlugin();
    m_testEngine.saveText("TestBlog", "Foo1");
    String newPage = plugin.getNewEntryPage(m_testEngine, "TestBlog");
    m_testEngine.saveText(newPage, "!Title1\r\nFoo");
    newPage = plugin.getNewEntryPage(m_testEngine, "TestBlog");
    m_testEngine.saveText(newPage, "!Title2\r\n__Bar__");
    final RSSGenerator gen = m_testEngine.getManager(RSSGenerator.class);
    final Context context = Wiki.context().create(m_testEngine, m_testEngine.getManager(PageManager.class).getPage("TestBlog"));
    final WeblogPlugin blogplugin = new WeblogPlugin();
    final List<Page> entries = blogplugin.findBlogEntries(m_testEngine, "TestBlog", new Date(0), new Date(Long.MAX_VALUE));
    final Feed feed = new RSS10Feed(context);
    final String blog = gen.generateBlogRSS(context, entries, feed);
    Assertions.assertTrue(blog.contains("<description>Foo</description>"), "has Foo");
    Assertions.assertTrue(blog.contains("&lt;b&gt;Bar&lt;/b&gt;"), "has proper Bar");
}
Also used : Context(org.apache.wiki.api.core.Context) WeblogPlugin(org.apache.wiki.plugin.WeblogPlugin) Page(org.apache.wiki.api.core.Page) WeblogEntryPlugin(org.apache.wiki.plugin.WeblogEntryPlugin) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 22 with Context

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

the class PageViewPluginTest method testShowCountsExclude.

@Test
public void testShowCountsExclude() throws Exception {
    testEngine.saveText("TestPageExcluded", "this is test page that should be excluded [{PageViewPlugin}]");
    final Page page1 = testEngine.getManager(PageManager.class).getPage("TestPage01");
    final Context context1 = Wiki.context().create(testEngine, page1);
    final Page page2 = testEngine.getManager(PageManager.class).getPage("TestPage02");
    final Context context2 = Wiki.context().create(testEngine, page2);
    // generate counts:
    testEngine.getManager(RenderingManager.class).getHTML(context1, page1);
    testEngine.getManager(RenderingManager.class).getHTML(context2, page2);
    testEngine.getManager(RenderingManager.class).getHTML(context2, page2);
    // mind the double \n in the following string:
    final String pageViewPageContent = "[{PageViewPlugin show='list' exclude='TestPageExcl*' '\n\n* {1} ({2} views)\n}]";
    testEngine.saveText("PageViews", pageViewPageContent);
    final Page pageviews = testEngine.getManager(PageManager.class).getPage("PageViews");
    final Context contextPV = Wiki.context().create(testEngine, pageviews);
    final String result = testEngine.getManager(RenderingManager.class).getHTML(contextPV, pageviews);
    // System.out.println( result );
    Assertions.assertTrue(result.contains("Test Page 01"));
    // this page should not have been shown:
    Assertions.assertFalse(result.contains("Test Page Excluded"));
    testEngine.deleteTestPage("TestPageExcluded");
}
Also used : Context(org.apache.wiki.api.core.Context) PageManager(org.apache.wiki.pages.PageManager) RenderingManager(org.apache.wiki.render.RenderingManager) Page(org.apache.wiki.api.core.Page) Test(org.junit.jupiter.api.Test)

Example 23 with Context

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

the class PageViewPluginTest method testShowCountEntries.

@Test
public void testShowCountEntries() throws Exception {
    // create pages that should be counted
    testEngine.saveText("TestPage03", "this is test page 03 [{PageViewPlugin}]");
    testEngine.saveText("TestPage04", "this is test page 04 [{PageViewPlugin}]");
    final Page page1 = testEngine.getManager(PageManager.class).getPage("TestPage01");
    final Context context1 = Wiki.context().create(testEngine, page1);
    final Page page2 = testEngine.getManager(PageManager.class).getPage("TestPage02");
    final Context context2 = Wiki.context().create(testEngine, page2);
    final Page page3 = testEngine.getManager(PageManager.class).getPage("TestPage03");
    final Context context3 = Wiki.context().create(testEngine, page3);
    final Page page4 = testEngine.getManager(PageManager.class).getPage("TestPage04");
    final Context context4 = Wiki.context().create(testEngine, page4);
    // generate counts:
    testEngine.getManager(RenderingManager.class).getHTML(context1, page1);
    testEngine.getManager(RenderingManager.class).getHTML(context2, page2);
    testEngine.getManager(RenderingManager.class).getHTML(context2, page2);
    testEngine.getManager(RenderingManager.class).getHTML(context3, page3);
    testEngine.getManager(RenderingManager.class).getHTML(context4, page4);
    // mind the double \n in the following string:
    final String pageViewPageContent = "[{PageViewPlugin show='list' entries=3'\n\n* {1} ({2} views)\n}]";
    testEngine.saveText("PageViews", pageViewPageContent);
    final Page pageviews = testEngine.getManager(PageManager.class).getPage("PageViews");
    final Context contextPV = Wiki.context().create(testEngine, pageviews);
    final String result = testEngine.getManager(RenderingManager.class).getHTML(contextPV, pageviews);
    // System.out.println( result );
    Assertions.assertTrue(result.contains("Test Page 03"));
    Assertions.assertFalse(result.contains("Test Page 04"));
    testEngine.deleteTestPage("TestPage03");
    testEngine.deleteTestPage("TestPage04");
}
Also used : Context(org.apache.wiki.api.core.Context) PageManager(org.apache.wiki.pages.PageManager) RenderingManager(org.apache.wiki.render.RenderingManager) Page(org.apache.wiki.api.core.Page) Test(org.junit.jupiter.api.Test)

Example 24 with Context

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

the class ReferringPagesPluginTest method testSingleReferral.

@Test
public void testSingleReferral() throws Exception {
    final Context context2 = Wiki.context().create(engine, Wiki.contents().page(engine, "Foobar"));
    final String res = manager.execute(context2, "{INSERT org.apache.wiki.plugin.ReferringPagesPlugin WHERE max=5}");
    Assertions.assertEquals(mkLink("TestPage") + "<br />", res);
}
Also used : Context(org.apache.wiki.api.core.Context) Test(org.junit.jupiter.api.Test)

Example 25 with Context

use of org.apache.wiki.api.core.Context 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.
@Override
public void doGet(final HttpServletRequest req, final HttpServletResponse res) throws IOException {
    final Context context = Wiki.context().create(m_engine, req, ContextEnum.PAGE_ATTACH.getRequestContext());
    final AttachmentManager mgr = m_engine.getManager(AttachmentManager.class);
    final AuthorizationManager authmgr = m_engine.getManager(AuthorizationManager.class);
    final String version = req.getParameter(HDR_VERSION);
    final String nextPage = req.getParameter("nextpage");
    final String page = context.getPage().getName();
    int ver = WikiProvider.LATEST_VERSION;
    if (page == null) {
        log.info("Invalid attachment name.");
        res.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    try (final OutputStream out = res.getOutputStream()) {
        log.debug("Attempting to download att " + page + ", version " + version);
        if (version != null) {
            ver = Integer.parseInt(version);
        }
        final Attachment att = mgr.getAttachmentInfo(page, ver);
        if (att != null) {
            // 
            // Check if the user has permission for this attachment
            // 
            final 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;
            }
            final 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());
            }
            try (final InputStream in = mgr.getAttachmentStream(context, att)) {
                int read;
                final byte[] buffer = new byte[BUFFER_SIZE];
                while ((read = in.read(buffer)) > -1) {
                    out.write(buffer, 0, read);
                }
            }
            log.debug("Attachment {} sent to {} on {}", att.getFileName(), req.getRemoteUser(), HttpUtil.getRemoteAddress(req));
            if (nextPage != null) {
                res.sendRedirect(validateNextPage(TextUtil.urlEncodeUTF8(nextPage), m_engine.getURL(ContextEnum.WIKI_ERROR.getRequestContext(), "", null)));
            }
        } else {
            final String msg = "Attachment '" + page + "', version " + ver + " does not exist.";
            log.info(msg);
            res.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
        }
    } catch (final ProviderException pe) {
        log.debug("Provider failed while reading", pe);
        // 
        // This might fail, if the response is already committed.  So in that
        // case we just log it.
        // 
        sendError(res, "Provider error: " + pe.getMessage());
    } catch (final NumberFormatException nfe) {
        log.warn("Invalid version number: " + version);
        res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid version number");
    } catch (final 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 (final 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.
        // 
        log.debug("I/O exception during download", ioe);
        sendError(res, "Error: " + ioe.getMessage());
    }
}
Also used : Context(org.apache.wiki.api.core.Context) ServletContext(javax.servlet.ServletContext) SocketException(java.net.SocketException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Attachment(org.apache.wiki.api.core.Attachment) IOException(java.io.IOException) Permission(java.security.Permission) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager)

Aggregations

Context (org.apache.wiki.api.core.Context)81 Page (org.apache.wiki.api.core.Page)46 PageManager (org.apache.wiki.pages.PageManager)42 Test (org.junit.jupiter.api.Test)40 RenderingManager (org.apache.wiki.render.RenderingManager)15 PageContext (javax.servlet.jsp.PageContext)11 Engine (org.apache.wiki.api.core.Engine)9 ReferenceManager (org.apache.wiki.references.ReferenceManager)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 Date (java.util.Date)6 ServletContext (javax.servlet.ServletContext)6 ProviderException (org.apache.wiki.api.exceptions.ProviderException)6 WikiContext (org.apache.wiki.WikiContext)5 StringReader (java.io.StringReader)4 Properties (java.util.Properties)4 MockHttpServletRequest (net.sourceforge.stripes.mock.MockHttpServletRequest)4 WikiSessionTest (org.apache.wiki.WikiSessionTest)4 Attachment (org.apache.wiki.api.core.Attachment)4 SearchResult (org.apache.wiki.api.search.SearchResult)4