use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.
the class JSPWikiMarkupParserTest method testCollectingLinksAttachment.
@Test
public void testCollectingLinksAttachment() throws Exception {
// First, make an attachment.
try {
testEngine.saveText(PAGE_NAME, "content");
final Attachment att = Wiki.contents().attachment(testEngine, PAGE_NAME, "TestAtt.txt");
att.setAuthor("FirstPost");
testEngine.getManager(AttachmentManager.class).storeAttachment(att, testEngine.makeAttachmentFile());
final LinkCollector coll = new LinkCollector();
final LinkCollector coll_others = new LinkCollector();
final String src = "[TestAtt.txt]";
final WikiContext context = new WikiContext(testEngine, Wiki.contents().page(testEngine, PAGE_NAME));
final MarkupParser p = new JSPWikiMarkupParser(context, new BufferedReader(new StringReader(src)));
p.addLocalLinkHook(coll_others);
p.addExternalLinkHook(coll_others);
p.addAttachmentLinkHook(coll);
p.parse();
final Collection<String> links = coll.getLinks();
Assertions.assertEquals(1, links.size(), "no links found");
Assertions.assertEquals(PAGE_NAME + "/TestAtt.txt", links.iterator().next(), "wrong link");
Assertions.assertEquals(0, coll_others.getLinks().size(), "wrong links found");
} finally {
final String files = testEngine.getWikiProperties().getProperty(AttachmentProvider.PROP_STORAGEDIR);
final File storagedir = new File(files, PAGE_NAME + BasicAttachmentProvider.DIR_EXTENSION);
if (storagedir.exists() && storagedir.isDirectory()) {
TestEngine.deleteAll(storagedir);
}
}
}
use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.
the class PageRenamerTest method testAttachmentChange.
@Test
public void testAttachmentChange() throws Exception {
m_engine.saveText("TestPage", "foofoo");
m_engine.saveText("TestPage2", "[TestPage/foo.txt] [linktext|TestPage/bar.jpg]");
m_engine.addAttachment("TestPage", "foo.txt", "testing".getBytes());
m_engine.addAttachment("TestPage", "bar.jpg", "pr0n".getBytes());
final Page p = m_engine.getManager(PageManager.class).getPage("TestPage");
final Context context = Wiki.context().create(m_engine, p);
m_engine.getManager(PageRenamer.class).renamePage(context, "TestPage", "FooTest", true);
final String data = m_engine.getManager(PageManager.class).getPureText("TestPage2", WikiProvider.LATEST_VERSION);
Assertions.assertEquals("[FooTest/foo.txt] [linktext|FooTest/bar.jpg]", data.trim(), "no rename");
Attachment att = m_engine.getManager(AttachmentManager.class).getAttachmentInfo("FooTest/foo.txt");
Assertions.assertNotNull(att, "footext");
att = m_engine.getManager(AttachmentManager.class).getAttachmentInfo("FooTest/bar.jpg");
Assertions.assertNotNull(att, "barjpg");
att = m_engine.getManager(AttachmentManager.class).getAttachmentInfo("TestPage/bar.jpg");
Assertions.assertNull(att, "testpage/bar.jpg exists");
att = m_engine.getManager(AttachmentManager.class).getAttachmentInfo("TestPage/foo.txt");
Assertions.assertNull(att, "testpage/foo.txt exists");
Collection<String> refs = m_engine.getManager(ReferenceManager.class).findReferrers("TestPage/bar.jpg");
Assertions.assertNull(refs, "oldpage");
refs = m_engine.getManager(ReferenceManager.class).findReferrers("FooTest/bar.jpg");
Assertions.assertEquals(1, refs.size(), "new size");
Assertions.assertEquals("TestPage2", refs.iterator().next(), "wrong ref");
}
use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.
the class DefaultPageManagerTest method testDeletePageAndAttachments2.
@Test
public void testDeletePageAndAttachments2() throws Exception {
engine.saveText(NAME1, "Test");
Attachment att = Wiki.contents().attachment(engine, NAME1, "TestAtt.txt");
att.setAuthor("FirstPost");
engine.getManager(AttachmentManager.class).storeAttachment(att, engine.makeAttachmentFile());
final String files = engine.getWikiProperties().getProperty(FileSystemProvider.PROP_PAGEDIR);
final File saved = new File(files, NAME1 + FileSystemProvider.FILE_EXT);
final String atts = engine.getWikiProperties().getProperty(AttachmentProvider.PROP_STORAGEDIR);
final File attfile = new File(atts, NAME1 + "-att/TestAtt.txt-dir");
Assertions.assertTrue(saved.exists(), "Didn't create it!");
Assertions.assertTrue(attfile.exists(), "Attachment dir does not exist");
final Page page = engine.getManager(PageManager.class).getPage(NAME1, WikiProvider.LATEST_VERSION);
Assertions.assertNotNull(page, "page");
att = engine.getManager(AttachmentManager.class).getAttachmentInfo(NAME1 + "/TestAtt.txt");
engine.getManager(PageManager.class).deletePage(att.getName());
engine.getManager(PageManager.class).deletePage(NAME1);
Assertions.assertNull(engine.getManager(PageManager.class).getPage(NAME1), "Page not removed");
Assertions.assertNull(engine.getManager(PageManager.class).getPage(NAME1 + "/TestAtt.txt"), "Att not removed");
final Collection<String> refs = engine.getManager(ReferenceManager.class).findReferrers(NAME1);
Assertions.assertNull(refs, "referrers");
}
use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.
the class DefaultPageRenamer method renamePage.
/**
* Renames a page.
*
* @param context The current context.
* @param renameFrom The name from which to rename.
* @param renameTo The new name.
* @param changeReferrers If true, also changes all the referrers.
* @return The final new name (in case it had to be modified)
* @throws WikiException If the page cannot be renamed.
*/
@Override
public String renamePage(final Context context, final String renameFrom, final String renameTo, final boolean changeReferrers) throws WikiException {
// Sanity checks first
if (renameFrom == null || renameFrom.isEmpty()) {
throw new WikiException("From name may not be null or empty");
}
if (renameTo == null || renameTo.isEmpty()) {
throw new WikiException("To name may not be null or empty");
}
// Clean up the "to" -name so that it does not contain anything illegal
final String renameToClean = MarkupParser.cleanLink(renameTo.trim());
if (renameToClean.equals(renameFrom)) {
throw new WikiException("You cannot rename the page to itself");
}
// Preconditions: "from" page must exist, and "to" page must not yet exist.
final Engine engine = context.getEngine();
final Page fromPage = engine.getManager(PageManager.class).getPage(renameFrom);
if (fromPage == null) {
throw new WikiException("No such page " + renameFrom);
}
Page toPage = engine.getManager(PageManager.class).getPage(renameToClean);
if (toPage != null) {
throw new WikiException("Page already exists " + renameToClean);
}
final Set<String> referrers = getReferencesToChange(fromPage, engine);
// Do the actual rename by changing from the frompage to the topage, including all the attachments
// Remove references to attachments under old name
final List<Attachment> attachmentsOldName = engine.getManager(AttachmentManager.class).listAttachments(fromPage);
for (final Attachment att : attachmentsOldName) {
final Page fromAttPage = engine.getManager(PageManager.class).getPage(att.getName());
engine.getManager(ReferenceManager.class).pageRemoved(fromAttPage);
}
engine.getManager(PageManager.class).getProvider().movePage(renameFrom, renameToClean);
if (engine.getManager(AttachmentManager.class).attachmentsEnabled()) {
engine.getManager(AttachmentManager.class).getCurrentProvider().moveAttachmentsForPage(renameFrom, renameToClean);
}
// Add a comment to the page notifying what changed. This adds a new revision to the repo with no actual change.
toPage = engine.getManager(PageManager.class).getPage(renameToClean);
if (toPage == null) {
throw new ProviderException("Rename seems to have failed for some strange reason - please check logs!");
}
toPage.setAttribute(Page.CHANGENOTE, fromPage.getName() + " ==> " + toPage.getName());
toPage.setAuthor(context.getCurrentUser().getName());
engine.getManager(PageManager.class).putPageText(toPage, engine.getManager(PageManager.class).getPureText(toPage));
// Update the references
engine.getManager(ReferenceManager.class).pageRemoved(fromPage);
engine.getManager(ReferenceManager.class).updateReferences(toPage);
// Update referrers
if (changeReferrers) {
updateReferrers(context, fromPage, toPage, referrers);
}
// re-index the page including its attachments
engine.getManager(SearchManager.class).reindexPage(toPage);
final Collection<Attachment> attachmentsNewName = engine.getManager(AttachmentManager.class).listAttachments(toPage);
for (final Attachment att : attachmentsNewName) {
final Page toAttPage = engine.getManager(PageManager.class).getPage(att.getName());
// add reference to attachment under new page name
engine.getManager(ReferenceManager.class).updateReferences(toAttPage);
engine.getManager(SearchManager.class).reindexPage(att);
}
firePageRenameEvent(renameFrom, renameToClean);
// Done, return the new name.
return renameToClean;
}
use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.
the class SpamFilter method refreshBlacklists.
/**
* If the spam filter notices changes in the black list page, it will refresh them automatically.
*
* @param context associated WikiContext
*/
private void refreshBlacklists(final Context context) {
try {
boolean rebuild = false;
// Rebuild, if the spam words page, the attachment or the IP ban page has changed since.
final Page sourceSpam = context.getEngine().getManager(PageManager.class).getPage(m_forbiddenWordsPage);
if (sourceSpam != null) {
if (m_spamPatterns == null || m_spamPatterns.isEmpty() || sourceSpam.getLastModified().after(m_lastRebuild)) {
rebuild = true;
}
}
final Attachment att = context.getEngine().getManager(AttachmentManager.class).getAttachmentInfo(context, m_blacklist);
if (att != null) {
if (m_spamPatterns == null || m_spamPatterns.isEmpty() || att.getLastModified().after(m_lastRebuild)) {
rebuild = true;
}
}
final Page sourceIPs = context.getEngine().getManager(PageManager.class).getPage(m_forbiddenIPsPage);
if (sourceIPs != null) {
if (m_IPPatterns == null || m_IPPatterns.isEmpty() || sourceIPs.getLastModified().after(m_lastRebuild)) {
rebuild = true;
}
}
// Do the actual rebuilding. For simplicity's sake, we always rebuild the complete filter list regardless of what changed.
if (rebuild) {
m_lastRebuild = new Date();
m_spamPatterns = parseWordList(sourceSpam, (sourceSpam != null) ? sourceSpam.getAttribute(LISTVAR) : null);
log.info("Spam filter reloaded - recognizing " + m_spamPatterns.size() + " patterns from page " + m_forbiddenWordsPage);
m_IPPatterns = parseWordList(sourceIPs, (sourceIPs != null) ? sourceIPs.getAttribute(LISTIPVAR) : null);
log.info("IP filter reloaded - recognizing " + m_IPPatterns.size() + " patterns from page " + m_forbiddenIPsPage);
if (att != null) {
final InputStream in = context.getEngine().getManager(AttachmentManager.class).getAttachmentStream(att);
final StringWriter out = new StringWriter();
FileUtil.copyContents(new InputStreamReader(in, StandardCharsets.UTF_8), out);
final Collection<Pattern> blackList = parseBlacklist(out.toString());
log.info("...recognizing additional " + blackList.size() + " patterns from blacklist " + m_blacklist);
m_spamPatterns.addAll(blackList);
}
}
} catch (final IOException ex) {
log.info("Unable to read attachment data, continuing...", ex);
} catch (final ProviderException ex) {
log.info("Failed to read spam filter attachment, continuing...", ex);
}
}
Aggregations