Hi list,
I am trying to use the addAttachment call to add an attachment to a page via
xmlrpc.
So far I have used groovy and python (code further down), both give me the
same error message:
xmlrpclib.Fault: <Fault 0: 'No method matching arguments: java.lang.String,
java.lang.String, java.util.HashMap, [B'>
After digging into the src I found addAttachment in:
        XWikiXmlRpcClient.java (part of -core)
    /**
     * Add a new attachment.
     *
     * @param contentId Ignored. (It is here because the Confluence API signature declares
it)
     * @param attachment The Attachment object describing the attachment.
     * @param attachmentData The actual attachment data.
     * @return An Attachment object describing the newly added attachment.
     * @throws XmlRpcException
     * @category ConfluenceAPI
     */
    public Attachment addAttachment(Integer contentId, Attachment attachment,
        byte[] attachmentData) throws XmlRpcException
    {
        Object object =
            invokeRpc("addAttachment", token, contentId, attachment.toMap(),
attachmentData);
        return (Attachment) new Attachment((Map<String, Object>) object);
    }
As you can see contentId is defined as Integer not String. When using an integer
in the rpc call I get this error message:
xmlrpclib.Fault: <Fault 0: 'Failed to invoke method addAttachment in class
com.xpn.xwiki.xmlrpc.XWikiXmlRpcHandler: An XWiki id must be in the form
Space.Page[?param=value¶m=value&...]'>
When looking at the server log I can see:
Caused by:
java.lang.IllegalArgumentException: An XWiki id must be in the form
Space.Page[?param=value¶m=value&...]
at org.xwiki.xmlrpc.model.XWikiExtendedId.<init>(XWikiExtendedId.java:37)
at com.xpn.xwiki.xmlrpc.XWikiXmlRpcHandler.addAttachment(XWikiXmlRpcHandler.java:929)
...
And when looking at that method:
    public XWikiExtendedId(String string)
    {
        if (string.indexOf(".") == -1) {
            throw new IllegalArgumentException(
                "An XWiki id must be in the form
Space.Page[?param=value¶m=value&...]");
        }
So question is, how do I actually add an attachment to a page?
        Stefan
------ python -------8<----------------------
#!/usr/bin/python
import xmlrpclib
f = open('images/foo.jpg', 'rb') #Open file as a binary file
file = f.read()
data = xmlrpclib.Binary(file) #Make the byte array
server_url = 'http://localhost:8080/xwiki/xmlrpc/confluence';
s = xmlrpclib.Server(server_url);
token = s.confluence1.login('login', 'pass');
page_list = s.confluence1.getPages(token, 'MySpace') #Get the pages in the space
new_attachment = dict(id = '', pageId = '', title = '', fileName =
'filename', fileSize = '', contentType = 'content type', created =
'', creator = '', url = '', comment = 'Some comment')
#Make a attachment container
for page in page_list:
        if page['title'] == 'AttachMe':
                        s.confluence1.addAttachment(token, page['id'],
new_attachment, data)
------ python ------->8----------------------
------ groovy -------8<----------------------
import groovy.net.xmlrpc.XMLRPCServerProxy
import java.io.File
server = "localhost:8080";
username = "user";
password = "pass";
serverProxy = new XMLRPCServerProxy("http://${server}/xwiki/xmlrpc/confluence")
try {
        println "Logging in...";
        token = serverProxy.confluence1.login(username, password)
} catch (Exception e) {
        println "Cannot login! Check username or password..."
                throw e;
}
println "Logged in";
try {
        def attach = [:];
        # load file with PAGENAME<SPACE>IMAGETOATTACH per line
        println "Loading image list";
        new File("images.txt").eachLine { line ->
                array = line.split(" ");
                attach.put(array[0], array[1]);
                attachment = [id : '', pageId : '', title : '',
fileName : 'filename', fileSize : '', contentType : 'content
type', created : '', creator : '', url : '', comment :
'Some comment'];
                bytes = new File(array[1]).readBytes();
                page = serverProxy.confluence1.getPage(token,
"MySpace.${array[0]}");
                serverProxy.confluence1.addAttachment(token, page.id, attachment, bytes);
        }
} finally {
        serverProxy.confluence1.logout(token);
}
------ groovy ------->8----------------------
regards
        Stefan
--
I did it just to piss you off.  :-P
                -- Branden Robinson in a message to debian-devel