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("<b>Bar</b>"), "has proper Bar");
}
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");
}
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");
}
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);
}
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());
}
}
Aggregations