Search in sources :

Example 1 with AttachmentManager

use of org.apache.wiki.attachment.AttachmentManager 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 2 with AttachmentManager

use of org.apache.wiki.attachment.AttachmentManager 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 {
    ReferenceManager refMgr = m_engine.getReferenceManager();
    AttachmentManager attMgr = m_engine.getAttachmentManager();
    m_engine.saveText(NAME1, "fooBar");
    Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
    m_engine.saveText(NAME1, " [" + NAME1 + "/TestAtt.txt] ");
    try {
        // and check post-conditions
        Collection c = refMgr.findUncreated();
        Assert.assertTrue("attachment exists", c == null || c.size() == 0);
        c = refMgr.findUnreferenced();
        Assert.assertEquals("unreferenced count", c.size(), 1);
        Assert.assertTrue("unreferenced", ((String) c.iterator().next()).equals(NAME1));
    } finally {
        // do cleanup
        String files = props.getProperty(FileSystemProvider.PROP_PAGEDIR);
        TestEngine.deleteAll(new File(files, NAME1 + BasicAttachmentProvider.DIR_EXTENSION));
    }
}
Also used : Collection(java.util.Collection) Attachment(org.apache.wiki.attachment.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) File(java.io.File) Test(org.junit.Test)

Example 3 with AttachmentManager

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

the class WikiEngineTest method testAttachmentRefs.

/**
 *  Checks, if ReferenceManager is informed of new attachments.
 */
@Test
public void testAttachmentRefs() throws Exception {
    ReferenceManager refMgr = m_engine.getReferenceManager();
    AttachmentManager attMgr = m_engine.getAttachmentManager();
    m_engine.saveText(NAME1, "fooBar");
    Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
    try {
        // and check post-conditions
        Collection c = refMgr.findUncreated();
        Assert.assertTrue("attachment exists: " + c, c == null || c.size() == 0);
        c = refMgr.findUnreferenced();
        Assert.assertEquals("unreferenced count", 2, c.size());
        Iterator<String> i = c.iterator();
        String first = i.next();
        String second = i.next();
        Assert.assertTrue("unreferenced", (first.equals(NAME1) && second.equals(NAME1 + "/TestAtt.txt")) || (first.equals(NAME1 + "/TestAtt.txt") && second.equals(NAME1)));
    } finally {
        // do cleanup
        String files = props.getProperty(FileSystemProvider.PROP_PAGEDIR);
        TestEngine.deleteAll(new File(files, NAME1 + BasicAttachmentProvider.DIR_EXTENSION));
    }
}
Also used : Collection(java.util.Collection) Attachment(org.apache.wiki.attachment.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) File(java.io.File) Test(org.junit.Test)

Example 4 with AttachmentManager

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

the class WikiEngineTest method testAttachmentRefs4.

/**
 *  Checks, if ReferenceManager is informed if a third page references an attachment.
 */
@Test
public void testAttachmentRefs4() throws Exception {
    ReferenceManager refMgr = m_engine.getReferenceManager();
    AttachmentManager attMgr = m_engine.getAttachmentManager();
    m_engine.saveText(NAME1, "[TestPage2]");
    Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
    m_engine.saveText("TestPage2", "[" + NAME1 + "/TestAtt.txt]");
    try {
        // and check post-conditions
        Collection c = refMgr.findUncreated();
        Assert.assertTrue("attachment exists", c == null || c.size() == 0);
        c = refMgr.findUnreferenced();
        Assert.assertEquals("unreferenced count", c.size(), 1);
        Assert.assertTrue("unreferenced", ((String) c.iterator().next()).equals(NAME1));
    } finally {
        // do cleanup
        String files = props.getProperty(FileSystemProvider.PROP_PAGEDIR);
        TestEngine.deleteAll(new File(files, NAME1 + BasicAttachmentProvider.DIR_EXTENSION));
        new File(files, "TestPage2" + FileSystemProvider.FILE_EXT).delete();
    }
}
Also used : Collection(java.util.Collection) Attachment(org.apache.wiki.attachment.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) File(java.io.File) Test(org.junit.Test)

Example 5 with AttachmentManager

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

the class WikiEngineTest method testAttachmentRefs2.

/**
 *  Is ReferenceManager updated properly if a page references
 *  its own attachments?
 */
/*
      FIXME: This is a deep problem.  The real problem is that the reference
      manager cannot know when it encounters a link like "testatt.txt" that it
      is really a link to an attachment IF the link is created before
      the attachment.  This means that when the attachment is created,
      the link will stay in the "uncreated" list.

      There are two issues here: first of all, TranslatorReader should
      able to return the proper attachment references (which I think
      it does), and second, the ReferenceManager should be able to
      remove any links that are not referred to, nor they are created.

      However, doing this in a relatively sane timeframe can be a problem.
    */
@Test
public void testAttachmentRefs2() throws Exception {
    ReferenceManager refMgr = m_engine.getReferenceManager();
    AttachmentManager attMgr = m_engine.getAttachmentManager();
    m_engine.saveText(NAME1, "[TestAtt.txt]");
    // check a few pre-conditions
    Collection c = refMgr.findReferrers("TestAtt.txt");
    Assert.assertTrue("normal, unexisting page", c != null && ((String) c.iterator().next()).equals(NAME1));
    c = refMgr.findReferrers(NAME1 + "/TestAtt.txt");
    Assert.assertTrue("no attachment", c == null || c.size() == 0);
    c = refMgr.findUncreated();
    Assert.assertTrue("unknown attachment", c != null && c.size() == 1 && ((String) c.iterator().next()).equals("TestAtt.txt"));
    // now we create the attachment
    Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
    try {
        // and check post-conditions
        c = refMgr.findUncreated();
        Assert.assertTrue("attachment exists: ", c == null || c.size() == 0);
        c = refMgr.findReferrers("TestAtt.txt");
        Assert.assertTrue("no normal page", c == null || c.size() == 0);
        c = refMgr.findReferrers(NAME1 + "/TestAtt.txt");
        Assert.assertTrue("attachment exists now", c != null && ((String) c.iterator().next()).equals(NAME1));
        c = refMgr.findUnreferenced();
        Assert.assertTrue("unreferenced", c.size() == 1 && ((String) c.iterator().next()).equals(NAME1));
    } finally {
        // do cleanup
        String files = props.getProperty(FileSystemProvider.PROP_PAGEDIR);
        TestEngine.deleteAll(new File(files, NAME1 + BasicAttachmentProvider.DIR_EXTENSION));
    }
}
Also used : Collection(java.util.Collection) Attachment(org.apache.wiki.attachment.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) File(java.io.File) Test(org.junit.Test)

Aggregations

AttachmentManager (org.apache.wiki.attachment.AttachmentManager)10 Attachment (org.apache.wiki.attachment.Attachment)7 ProviderException (org.apache.wiki.api.exceptions.ProviderException)6 File (java.io.File)5 Collection (java.util.Collection)5 WikiEngine (org.apache.wiki.WikiEngine)4 Test (org.junit.Test)4 WikiPage (org.apache.wiki.WikiPage)3 IOException (java.io.IOException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 StringWriter (java.io.StringWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Date (java.util.Date)1 Hashtable (java.util.Hashtable)1 WikiContext (org.apache.wiki.WikiContext)1 AdminBeanManager (org.apache.wiki.api.engine.AdminBeanManager)1 FilterManager (org.apache.wiki.api.engine.FilterManager)1 PluginManager (org.apache.wiki.api.engine.PluginManager)1