Search in sources :

Example 1 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class RPCHandlerTest method testListLinksWithAttachments.

@Test
public void testListLinksWithAttachments() throws Exception {
    String text = "[Foobar] [Test/TestAtt.txt]";
    String pageName = NAME1;
    m_engine.saveText(pageName, text);
    Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    m_engine.getAttachmentManager().storeAttachment(att, m_engine.makeAttachmentFile());
    // Test.
    Vector links = m_handler.listLinks(pageName);
    Assert.assertEquals("link count", 2, links.size());
    Hashtable linkinfo = (Hashtable) links.elementAt(0);
    Assert.assertEquals("edit name", "Foobar", linkinfo.get("page"));
    Assert.assertEquals("edit type", "local", linkinfo.get("type"));
    Assert.assertEquals("edit href", "/test/Edit.jsp?page=Foobar", linkinfo.get("href"));
    linkinfo = (Hashtable) links.elementAt(1);
    Assert.assertEquals("att name", NAME1 + "/TestAtt.txt", linkinfo.get("page"));
    Assert.assertEquals("att type", "local", linkinfo.get("type"));
    Assert.assertEquals("att href", "/test/attach/" + NAME1 + "/TestAtt.txt", linkinfo.get("href"));
}
Also used : Hashtable(java.util.Hashtable) Attachment(org.apache.wiki.attachment.Attachment) Vector(java.util.Vector) Test(org.junit.Test)

Example 2 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class MetaWeblogHandler method newMediaObject.

/**
 *  Creates an attachment and adds it to the blog.  The attachment
 *  is created into the main blog page, not the actual post page,
 *  because we do not know it at this point.
 *
 *  @param blogid The id of the blog.
 *  @param username The username to use
 *  @param password The password
 *  @param content As per the MetaweblogAPI contract
 *  @return As per the MetaweblogAPI contract
 *  @throws XmlRpcException If something goes wrong
 */
public Hashtable newMediaObject(String blogid, String username, String password, Hashtable content) throws XmlRpcException {
    WikiEngine engine = m_context.getEngine();
    String url = "";
    log.info("metaWeblog.newMediaObject() called");
    WikiPage page = engine.getPage(blogid);
    checkPermissions(page, username, password, "upload");
    String name = (String) content.get("name");
    byte[] data = (byte[]) content.get("bits");
    AttachmentManager attmgr = engine.getAttachmentManager();
    try {
        Attachment att = new Attachment(engine, blogid, name);
        att.setAuthor(username);
        attmgr.storeAttachment(att, new ByteArrayInputStream(data));
        url = engine.getURL(WikiContext.ATTACH, att.getName(), null, true);
    } catch (Exception e) {
        log.error("Failed to upload attachment", e);
        throw new XmlRpcException(0, "Failed to upload media object: " + e.getMessage());
    }
    Hashtable<String, Object> result = new Hashtable<String, Object>();
    result.put("url", url);
    return result;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Hashtable(java.util.Hashtable) WikiPage(org.apache.wiki.WikiPage) Attachment(org.apache.wiki.attachment.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) WikiEngine(org.apache.wiki.WikiEngine) XmlRpcException(org.apache.xmlrpc.XmlRpcException) WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 3 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class RPCHandler method getAllPages.

public Vector getAllPages() {
    checkPermission(PagePermission.VIEW);
    Collection<WikiPage> pages = m_engine.getRecentChanges();
    Vector<String> result = new Vector<String>();
    for (WikiPage p : pages) {
        if (!(p instanceof Attachment)) {
            result.add(toRPCString(p.getName()));
        }
    }
    return result;
}
Also used : WikiPage(org.apache.wiki.WikiPage) Attachment(org.apache.wiki.attachment.Attachment) Vector(java.util.Vector)

Example 4 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class RPCHandlerUTF8 method getRecentChanges.

public Vector getRecentChanges(Date since) {
    checkPermission(PagePermission.VIEW);
    Collection<WikiPage> pages = m_engine.getRecentChanges();
    Vector<Hashtable<String, Object>> result = new Vector<Hashtable<String, Object>>();
    Calendar cal = Calendar.getInstance();
    cal.setTime(since);
    // 
    // Convert UTC to our time.
    // 
    cal.add(Calendar.MILLISECOND, (cal.get(Calendar.ZONE_OFFSET) + (cal.getTimeZone().inDaylightTime(since) ? cal.get(Calendar.DST_OFFSET) : 0)));
    since = cal.getTime();
    for (WikiPage page : pages) {
        if (page.getLastModified().after(since) && !(page instanceof Attachment)) {
            result.add(encodeWikiPage(page));
        }
    }
    return result;
}
Also used : Hashtable(java.util.Hashtable) WikiPage(org.apache.wiki.WikiPage) Calendar(java.util.Calendar) Attachment(org.apache.wiki.attachment.Attachment) Vector(java.util.Vector)

Example 5 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class WikiEngineTest method testDeletePageAndAttachments.

@Test
public void testDeletePageAndAttachments() throws Exception {
    m_engine.saveText(NAME1, "Test");
    Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    m_engine.getAttachmentManager().storeAttachment(att, m_engine.makeAttachmentFile());
    String files = props.getProperty(FileSystemProvider.PROP_PAGEDIR);
    File saved = new File(files, NAME1 + FileSystemProvider.FILE_EXT);
    String atts = props.getProperty(BasicAttachmentProvider.PROP_STORAGEDIR);
    File attfile = new File(atts, NAME1 + "-att/TestAtt.txt-dir");
    Assert.assertTrue("Didn't create it!", saved.exists());
    Assert.assertTrue("Attachment dir does not exist", attfile.exists());
    WikiPage page = m_engine.getPage(NAME1, WikiProvider.LATEST_VERSION);
    m_engine.deletePage(page.getName());
    Assert.assertFalse("Page has not been removed!", saved.exists());
    Assert.assertFalse("Attachment has not been removed", attfile.exists());
}
Also used : Attachment(org.apache.wiki.attachment.Attachment) File(java.io.File) Test(org.junit.Test)

Aggregations

Attachment (org.apache.wiki.attachment.Attachment)62 WikiPage (org.apache.wiki.WikiPage)26 Test (org.junit.Test)23 File (java.io.File)20 ProviderException (org.apache.wiki.api.exceptions.ProviderException)17 Collection (java.util.Collection)13 Date (java.util.Date)11 Iterator (java.util.Iterator)10 WikiEngine (org.apache.wiki.WikiEngine)10 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)7 FileInputStream (java.io.FileInputStream)6 IOException (java.io.IOException)6 List (java.util.List)6 Vector (java.util.Vector)6 WikiContext (org.apache.wiki.WikiContext)5 Hashtable (java.util.Hashtable)4 Element (net.sf.ehcache.Element)4 PagePermission (org.apache.wiki.auth.permissions.PagePermission)4 ArrayList (java.util.ArrayList)3 Calendar (java.util.Calendar)3