[xwiki-devs] XML/RPC addAttachment not workink
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
On 27 juin 08, at 20:38, [email protected] wrote:
So question is, how do I actually add an attachment to a page?
Hi, as you quoted in the previous post, the contentId (which is not a pageId) is actually ignored and is there because of the Confluence API compatibility. In order to specify the page to attach data to, you must use the pageId field of the Attachment class. Here is a snippet from the unit tests: String attachmentName = String.format("test.png", random.nextInt()); byte[] data = ... Attachment attachment = new Attachment(); attachment.setPageId("Foo.Bar"); attachment.setFileName(attachmentName); attachment = rpc.addAttachment(0, attachment, data); This will attach to page Foo.Bar a file called test.png with the content set to data. I hope this answers your question. -Fabio
On 27 juin 08, at 20:38, [email protected] wrote:
So question is, how do I actually add an attachment to a page?
Hi,
as you quoted in the previous post, the contentId (which is not a pageId) is actually ignored and is there because of the Confluence API compatibility.
In order to specify the page to attach data to, you must use the pageId field of the Attachment class.
Here is a snippet from the unit tests:
String attachmentName = String.format("test.png", random.nextInt()); byte[] data = ... Attachment attachment = new Attachment(); attachment.setPageId("Foo.Bar"); attachment.setFileName(attachmentName); attachment = rpc.addAttachment(0, attachment, data);
This will attach to page Foo.Bar a file called test.png with the content set to data.
This does not work but gives me this error: 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&...]'> As I said, addAttachment is defined as (Integer, Attachment, Byte[]), but when passing in an integer it creates the above exception. When using a String it bombs out with, no such message signature. Stefan -- Lay off the muses, it's a very tough dollar. -- S. J. Perelman
[email protected] wrote:
On 27 juin 08, at 20:38, [email protected] wrote:
So question is, how do I actually add an attachment to a page?
Hi,
as you quoted in the previous post, the contentId (which is not a pageId) is actually ignored and is there because of the Confluence API compatibility.
In order to specify the page to attach data to, you must use the pageId field of the Attachment class.
Here is a snippet from the unit tests:
String attachmentName = String.format("test.png", random.nextInt()); byte[] data = ... Attachment attachment = new Attachment(); attachment.setPageId("Foo.Bar"); attachment.setFileName(attachmentName); attachment = rpc.addAttachment(0, attachment, data);
This will attach to page Foo.Bar a file called test.png with the content set to data.
This does not work but gives me this error:
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&...]'>
As I said, addAttachment is defined as (Integer, Attachment, Byte[]), but when passing in an integer it creates the above exception.
When using a String it bombs out with, no such message signature.
Stefan
Can you try passing new Integer(0) as the first parameter? -- Sergiu Dumitriu http://purl.org/net/sergiu/
On 27 juin 08, at 22:51, [email protected] wrote:
This does not work but gives me this error:
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&...]'>
As I said, addAttachment is defined as (Integer, Attachment, Byte[]), but when passing in an integer it creates the above exception.
When using a String it bombs out with, no such message signature.
Hi In your previous message you wrote the following code: new_attachment = dict(id = '', pageId = '', title = '', fileName = 'filename', fileSize = '', contentType = 'content type', created = '', creator = '', url = '', comment = 'Some comment') #Make a attachment container I am not fluent in Python but here I understand that you are not setting the pageId. So basically you are not writing the equivalent of the snippet I posted earlier: you are leaving out the attachment.setPageId("Foo.Bar") method call (which is the one that sets the page to attach to). You should initialize your "attachment container" by specifying the pageId: new_attachment = dict(id = '', *pageId = 'Foo.Bar'*, title = '', fileName = 'filename', fileSize = '', contentType = 'content type', created = '', creator = '', url = '', comment = 'Some comment') #Make a attachment container That's the reason why IMHO your code raises the exception: pageId = '' and that's not in the form 'Space.Page[? param=value¶m=value&...]', as correctly reported by the exception. Try to correctly initialize the pageId in your dict and things should work fine. Let me know. -Fabio
Hello,
You should initialize your "attachment container" by specifying the pageId:
That did it :) When initializing pageId to Space.Page it will work: new_attachment = dict(id = '', pageId = 'Space.Page', title = '', fileName = 'filename', fileSize = '', contentType = 'content type', created = '', creator = '', url = '', comment = 'Some comment') #Make a attachment container Thanks for the help. Stefan -- 1 1 was a race-horse, 2 2 was 1 2. When 1 1 1 1 race, 2 2 1 1 2.
participants (3)
-
bd@bc-bd.org -
Fabio Mancinelli -
Sergiu Dumitriu