Changes for page XML-RPC Java Examples

Last modified by Thomas Mortagne on 2023/10/10 14:31

From version 3.1
edited by dilipkumarj
on 2010/01/30 16:54
Change comment: Added Remove Page example
To version 3.2
edited by dilipkumarj
on 2010/01/30 17:30
Change comment: Added example for retrieving a list of attachments for a Page

Summary

Details

Page properties
Content
... ... @@ -489,3 +489,61 @@
489 489  }
490 490  
491 491  {{/code}}
492 +
493 +
494 += Attachment: List Attachments For Page =
495 +
496 +So far, we worked with the Spaces (which are like directories/folders), Pages (which are the actual documents inside directories/folders). Both, Spaces & Pages can reside on their own & still be meaningful. However, there are other entities such as Attachments/Comments which are meaningful only when attached to Pages.
497 +The first entity that we explore are Attachments. A very common operation in a Wiki is to attach files to an existing Page. However, let's start with an example where we assume that there are a number of attachments already associated with a Page. Our job here is to find out what are those attachments.
498 +
499 +{{error}}
500 +Please be aware that there are two class files for "Attachment" in two different packages and both look like they are exactly the same thing. One of them is the org.codehaus.swizzle.confluence.Attachment & the other is the com.xpn.xwiki.api.Attachment. We use the former one which is org.codehaus.swizzle.confluence.Attachment. The other one is part of the XWiki Core & used for Attachment related actions in the XWiki itself.
501 +{{/error}}
502 +
503 +{{code language="java"}}
504 +
505 +import org.codehaus.swizzle.confluence.Attachment;
506 +import java.net.MalformedURLException;
507 +import java.util.List;
508 +import org.apache.xmlrpc.XmlRpcException;
509 +import org.xwiki.xmlrpc.XWikiXmlRpcClient;
510 +
511 +public class AttachmentList {
512 +public static void main(String[] args) throws MalformedURLException, XmlRpcException {
513 + //Replace the url with your xwiki server address
514 + String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
515 +
516 + //Replace user & pass with desired xwiki username & password
517 + String user = "Admin";
518 + String pass = "admin";
519 +
520 + //Perform Login & Authentication using above url address
521 + XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
522 +
523 + try {
524 + rpc.login(user, pass);
525 +
526 + //Get the list of all attachments for the Page named "Main.Host Page"
527 + List<Attachment> att = rpc.getAttachments("Main.Host Page");
528 +
529 + //Iterate through all the attachments in the list obtained
530 + for (Attachment attachment : att) {
531 +
532 + //Name of the file attached to the "Main.Host Page"
533 + System.out.println(attachment.getFileName());
534 + //Size of the file (in Bytes)
535 + System.out.println(attachment.getFileSize());
536 + //Absolute URL of the attachment for direct access
537 + System.out.println(attachment.getUrl());
538 + }
539 +
540 +
541 + } catch (XmlRpcException e) {
542 + System.out.println("invalid username/password was specified or communication problem or ");
543 + System.out.println(e);
544 + } finally {
545 + rpc.logout();
546 + }
547 + }
548 +}
549 +{{/code}}

Get Connected