Wiki source code of XML-RPC Java Examples

Version 13.1 by mawoki on 2010/08/09 14:40

Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
4
5 = Requirements =
6
7 The following libraries should be added to your application classpath:
8
9 {{info}}
10 Most of the libraries can be easily found in the WEB-INF/lib folder of your XWiki instance. As mentioned at [[platform:Features.XMLRPC]], version numbers of these libraries for the most part are irrelevant. In the examples listed here, XWiki version 2.1.1 is used.
11 {{/info}}
12
13 |=Library|=Source
14 |commons-logging-1.1.1.jar|WEB-INF/lib folder of XWiki 2.1.1
15 |ws-commons-util-1.0.2.jar|WEB-INF/lib folder of XWiki 2.1.1
16 |xmlrpc-common-3.1.jar|WEB-INF/lib folder of XWiki 2.1.1
17 |xmlrpc-client-3.1.jar|WEB-INF/lib folder of XWiki 2.1.1
18 |xwiki-core-2.1.1.jar|WEB-INF/lib folder of XWiki 2.1.1
19 |swizzle-confluence-1.2-20080419-xwiki.jar|WEB-INF/lib folder of XWiki 2.1.1
20 |xwiki-core-xmlrpc-client-2.1.1.jar|Manually download the jar from [[Maven repository>>http://maven.xwiki.org/releases/org/xwiki/platform/xwiki-core-xmlrpc-client/2.1.1/]]
21 |xwiki-core-xmlrpc-model-2.1.1.jar|WEB-INF/lib folder of XWiki 2.1.1
22
23 = Authentication: Login Example =
24
25 Try Providing an incorrect username & password. The application will throw an exception
26 For correct username & password, the application will compile & run successfully.
27
28 {{code language="java"}}
29 import java.net.MalformedURLException;
30 import org.apache.xmlrpc.XmlRpcException;
31 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
32 public class XmlRpcLogin {
33 public static void main(String[] args) throws MalformedURLException{
34
35 //URL of the xwiki instance
36 String url="http://localhost:8080/xwiki/xmlrpc/confluence";
37
38 //Replace user & pass with desired xwiki username & password
39 String user="Admin";
40 String pass="admin";
41
42 //Perform Login & Authentication using above url address
43 try{
44 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
45 rpc.login(user, pass);
46
47 }
48 catch(XmlRpcException e){
49 System.out.println("invalid username/password was specified or communication problem");
50 }
51 }
52 }
53 {{/code}}
54
55 = Authentication: Logout Example =
56
57 Let's build on the previous example where we "Logged In". We test logout action using a boolean variable since rpc.logout() method returns true on successful logout.
58
59 {{code language="java"}}
60
61 import java.net.MalformedURLException;
62 import org.apache.xmlrpc.XmlRpcException;
63 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
64 public class XmlRpcLogout {
65 public static void main(String[] args) throws MalformedURLException{
66
67 //Variable to test whether user logged out successfully. Default set to false
68 boolean loggedOut=false;
69
70 //URL of the xwiki instance
71 String url="http://localhost:8080/xwiki/xmlrpc/confluence";
72
73 //Replace user & pass with desired xwiki username & password
74 String user="Admin";
75 String pass="admin";
76
77 try{
78 //Perform Login & Authentication using above url address
79 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
80 rpc.login(user, pass);
81
82 //Log out action. Returns true if user successfully logged out
83 loggedOut=rpc.logout();
84 System.out.println(loggedOut);
85 }
86 catch(XmlRpcException e){
87 System.out.println("invalid username/password was specified or communication problem");
88 }
89 }
90 }
91 {{/code}}
92
93
94 = Space: Get A List Of Spaces =
95
96 Now, that we can log in and log out, let's move to finding out what's inside your wiki. How about a list of all the spaces inside the wiki. Compare the above two examples and you will see the new import that we added viz. the SpaceSummary class.
97
98 {{code language="java"}}
99
100 import java.net.MalformedURLException;
101 import java.util.List;
102 import org.apache.xmlrpc.XmlRpcException;
103 import org.codehaus.swizzle.confluence.SpaceSummary;
104 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
105 public class SpaceList {
106
107 public static void main(String[] args) throws MalformedURLException {
108 //URL of the xwiki instance
109 String url="http://localhost:8080/xwiki/xmlrpc/confluence";
110
111 //Replace user & pass with desired xwiki username & password
112 String user="Admin";
113 String pass="admin";
114
115 //Perform Login & Authentication using above url address
116 try{
117 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
118 rpc.login(user, pass);
119
120 List<SpaceSummary> spaceList = rpc.getSpaces();
121 System.out.println("Total Number of Spaces: " +spaceList.size());
122 for(int i=0;i<spaceList.size();i++){
123 System.out.println(spaceList.get(i).getKey());
124 }
125 rpc.logout();
126 }
127 catch(XmlRpcException e){
128 System.out.println("invalid username/password was specified or communication problem");
129 }
130 }
131 }
132 {{/code}}
133
134 = Space: Create A Space =
135
136 So far, we logged in, logged out & also were able to see a list of spaces in the xwiki instance. Let's go ahead and create a new space. And not just that we will also assign the default home page for the space. In this example, we use "XMLRPC" as the space name & "xmlrpc.WebHome" as the home page
137
138 {{code language="java"}}
139 import java.net.MalformedURLException;
140 import org.apache.xmlrpc.XmlRpcException;
141 import org.codehaus.swizzle.confluence.Space;
142 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
143
144 public class CreateSpace {
145
146 public static void main(String[] args) throws MalformedURLException {
147 //URL of the xwiki instance
148 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
149
150 //Replace user & pass with desired xwiki username & password
151 String user = "Admin";
152 String pass = "admin";
153
154 //Perform Login & Authentication using above url address
155 try {
156 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
157 rpc.login(user, pass);
158
159 //Create a Space object which holds the Key, Homepage, Name & description.
160 //Key is visible name for the space
161 Space space = new Space();
162 space.setKey("XMLRPC");
163 space.setHomepage("xmlrpc.WebHome");
164 space.setName("xmlrpc");
165 space.setDescription("Demo Space Created To Test XMLRPC");
166
167 //One simple method adds the space created to your xwiki instance
168 rpc.addSpace(space);
169
170 rpc.logout();
171 } catch (XmlRpcException e) {
172 System.out.println("invalid username/password was specified or communication problem");
173 }
174 }
175 }
176 {{/code}}
177
178 = Space: Delete A Space =
179
180 Removal of a space is far more easier than creating one. All it requires is the "Key" (the visible name of the space) to be provided to the remove() method.
181
182 {{warning}}
183 Please note that deletion of space removes all pages in the space too. It is recommended to always make sure that the space is empty before deleting it.
184 {{/warning}}
185
186 {{code language="java"}}
187
188 import java.net.MalformedURLException;
189 import org.apache.xmlrpc.XmlRpcException;
190 import org.codehaus.swizzle.confluence.Space;
191 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
192
193 public class DeleteSpace {
194
195 public static void main(String[] args) throws MalformedURLException {
196 //URL of the xwiki instance
197 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
198
199 //Replace user & pass with desired xwiki username & password
200 String user = "Admin";
201 String pass = "admin";
202
203 //Perform Login & Authentication using above url address
204 try {
205 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
206 rpc.login(user, pass);
207
208 //Delete the space which is reference by "XMLRPC" key
209 rpc.removeSpace("XMLRPC");
210
211 rpc.logout();
212 } catch (XmlRpcException e) {
213 System.out.println("invalid username/password was specified or communication problem");
214 }
215 }
216 }
217 {{/code}}
218
219 = Page: Retrieve/Search A Page =
220
221 A page is probably the most important unit of any wiki around which everything else is built. Spaces/Categories, Attachments, Comments, etc are more or less meaningless without a supporting page in a wiki system.
222 One of the most widely performed action on any wiki has to be searching for pages. Searches may happen through search boxes provided in a wiki or may be through following links from one page to another. On the XMLRPC side too, searching pages has been given a lot of importance and quite a few methods are available for the same.
223 Since, this topic is an important one, all the page retrieval/search examples [[can be found here>>XMLRPCJavaExamples2]]
224
225 = Page: Create A Page =
226
227 Now for the moment of truth. Addition of Pages to a Wiki is as simple as searching for them. Thankfully, the XWiki XMLRPC api has just the tools you need to do your job.
228 At the very least, you would require the following three parameters to create a Page:
229
230 1. Space - The Space where the Page is to be stored
231 1. Title - The title for the Page
232 1. Content - The content to be displayed inside the Page
233
234 {{warning}}
235 Please beware that using a title for an existing Page will overwrite all the contents of that Page. Make sure no Page *with the same title* exists in the same Space before you attempt to use the create Page functionality of XMLRPC.
236 {{/warning}}
237
238 In our example below, we would use
239
240 |=Parameter|=Value
241 |Space|demo code
242 |title|New Page
243 |Content|New Page Created
244 {{info}}This is XMLRPC Test{{/info}}
245
246 {{code language="java"}}
247 import java.net.MalformedURLException;
248 import org.apache.xmlrpc.XmlRpcException;
249 import org.codehaus.swizzle.confluence.Page;
250 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
251
252 public class CreatePage {
253
254 public static void main(String[] args) throws MalformedURLException, XmlRpcException {
255
256 //URL of the xwiki instance
257 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
258
259 //Replace user & pass with desired xwiki username & password
260 String user = "Admin";
261 String pass = "admin";
262
263
264 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
265 try {
266
267 //Perform Login & Authentication
268 rpc.login(user, pass);
269
270 //Create a Page object & set it's three important attributes viz. Space, Title, Content
271 //Observe how the \\\\ has been used to create a new line in the final wiki Page
272 //Also, XWiki syntax can be passed as it is. Here, we passed the info macro
273 //The info macro would get rendered an info box in the Page
274 Page page = new Page();
275 page.setSpace("demo code");
276 page.setTitle("New Page");
277 page.setContent("New Page Created \\\\ {{info}}This is XMLRPC Test{{/info}}");
278
279 //Also set the parent Page to "demo code.WebHome" so that the "New Page" we created is not
280 //an orphan Page
281 page.setParentId("demo code.WebHome");
282
283
284 //Store the page object into XWiki
285 rpc.storePage(page);
286
287
288 } catch (XmlRpcException e) {
289 System.out.println("invalid username/password was specified or communication problem or ");
290 System.out.println(e);
291 } finally {
292 rpc.logout();
293 }
294 }
295 }
296
297 {{/code}}
298
299
300 = Page: Get Page History =
301
302 Now, that we are adding content to Pages, there are going to be versions of the same document. These versions are called History of the Page. The XWiki API has methods available to access the history of the Page. The class that would help us in this case is the XWikiPageHistorySummary class.
303
304 {{info}}
305 Please note that the methods of XWikiPageHistorySummary class only help you access the various historical revisions of the document & not the content of the document itself. Eg. You would get to know that a particular Page called Main.WebHome has highest version as 4.1 or maybe 10.9. In order to access the content of the 4.1 revision of the document, you would have to use the [[Page retrieval code provided here>>http://platform.xwiki.org/xwiki/bin/view/Features/XMLRPCJavaExamples2#HRetrievePageByMajorMinorVersion]] by passing the appropriate version of the document.
306 {{/info}}
307
308 {{code language="java"}}
309 import java.net.MalformedURLException;
310 import java.util.List;
311 import org.apache.xmlrpc.XmlRpcException;
312 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
313 import org.xwiki.xmlrpc.model.XWikiPageHistorySummary;
314
315
316 public class PageHistory {
317
318 public static void main(String[] args) throws MalformedURLException, XmlRpcException {
319
320 //URL of the xwiki instance
321 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
322
323 //Replace user & pass with desired xwiki username & password
324 String user = "Admin";
325 String pass = "admin";
326
327
328 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
329 try {
330
331 //Perform Login & Authentication
332 rpc.login(user, pass);
333
334 //Create a XWikiPageHistorySummary object to hold all the revisions/history
335 //of the Main.WebHome Page
336 List<XWikiPageHistorySummary> hist = rpc.getPageHistory("Main.WebHome");
337
338 //Iterate through all the available versions for Main.WebHome
339 for(XWikiPageHistorySummary xphs:hist){
340
341 //Print the fully qualified name of the Page i.e. SpaceName.PageName
342 //In our example this would be Main.WebHome
343 System.out.println(xphs.getBasePageId());
344
345 //Print the historical page ID or name of the Main.WebHome page
346 //This would print something like:
347 //Main.WebHome?minorVersion=1&language=&version=2
348 //It means page=Main.WebHome, version=2, minor version=1 & language=default
349 System.out.println(xphs.getId());
350
351 //Printing the version of Main.WebHome in the majorversion.minorversion format
352 System.out.println("Version: "+xphs.getVersion()+"."+xphs.getMinorVersion());
353
354 //Date when the Page was last modified
355 System.out.println(xphs.getModified());
356
357 //User who last modified the Page
358 System.out.println(xphs.getModifier());
359
360 //Just a seperator between various versions of the document when printing
361 //to console
362 System.out.println("------------------------------");
363 }
364
365 } catch (XmlRpcException e) {
366 System.out.println("invalid username/password was specified or communication problem or ");
367 System.out.println(e);
368 } finally {
369 rpc.logout();
370 }
371 }
372 }
373 {{/code}}
374
375
376 = Page: Update A Page =
377
378 We saw code to search a Page, create a Page and get all it's revisions. Now, we move to updating a Page. In our example, we have used the Page called "Update Page" in the "demo code" Space. The basic intent is to get the content first from XWiki. Then add our updated content or "concatenate" new content with the existing content. Then, update the final content back to XWiki. Only the methods of the Page class & the rpc.getPage() & rpc.storePage() would be used here.
379
380 {{warning}}It is strongly advised that if this is the first time you are using XWiki XMLRPC, go through the examples at the start of this article. Each example tries to build up on the previous one & you would find understanding the API easier.{{/warning}}
381
382 {{code language="java"}}
383
384 import java.net.MalformedURLException;
385 import org.apache.xmlrpc.XmlRpcException;
386 import org.codehaus.swizzle.confluence.Page;
387 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
388
389
390 public class UpdatePage {
391
392 public static void main(String[] args) throws MalformedURLException, XmlRpcException {
393
394 //URL of the xwiki instance
395 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
396
397 //Replace user & pass with desired xwiki username & password
398 String user = "Admin";
399 String pass = "admin";
400
401
402 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
403 try {
404
405 //Perform Login & Authentication
406 rpc.login(user, pass);
407
408 //Create a Page object to hold our Document information
409 Page page = new Page();
410 //Fetch the required page. In our example, the page is in Space "demo code"
411 //and the Page is "Update Page"
412 page=rpc.getPage("demo code.Update Page");
413 //Fetch the content of the page & store it in a string for temporary storage
414 //This is the present content of the Page
415 String presentContent=page.getContent();
416 //Create a string that will hold the new content that is to be added to the Page
417 String newContent="\\\\Some new content added";
418 //Set the content of the page as: present content + new content
419 //However, this page is not yet stored to XWiki. It only resides in your application
420 page.setContent(presentContent+newContent);
421 //Finally, store the "updated" Page to XWiki
422 rpc.storePage(page);
423
424 //Just to make sure everything saved all right, fetch the content again for the Page
425 System.out.println(page.getContent());
426
427 } catch (XmlRpcException e) {
428 System.out.println("invalid username/password was specified or communication problem or ");
429 System.out.println(e);
430 } finally {
431 rpc.logout();
432 }
433 }
434 }
435 {{/code}}
436
437
438
439 = Page: Remove A Page =
440
441 Removal of Pages is straight forward. You are required to know the fully qualified name of the Page i.e. SpaceName.PageName (in our example, this would be "demo code.New Page"). We test the safe deletion of the Page using a boolean variable. The rpc.removePage() method returns true if the Page was successfully removed from the wiki.
442
443 {{warning}}You would need to have the deletion rights assigned to the user credentials you are passing to the rpc.login() method{{/warning}}
444 {{warning}}rpc.removePage() method does not completely delete a Page in ordinary circumstances. If you have a recycle bin option enabled (by default, it is enabled for XWiki), then the "removed/deleted Page" is stored in the recycle bin from where it will cleaned out as per the settings for recycle bin in the xwiki.cfg file of your XWiki instance.{{/warning}}
445
446 {{code language="java"}}
447
448
449 import java.net.MalformedURLException;
450 import org.apache.xmlrpc.XmlRpcException;
451 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
452
453
454 public class DeletePage {
455 public static void main(String[] args) throws MalformedURLException, XmlRpcException {
456
457 //URL of the xwiki instance
458 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
459
460 //Replace user & pass with desired xwiki username & password
461 String user = "Admin";
462 String pass = "admin";
463
464
465 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
466 try {
467
468 //Perform Login & Authentication
469 rpc.login(user, pass);
470
471 //Delete Page called "New Page" from Space called "demo code"
472 //Test deletion using a Boolean variable
473 Boolean b=rpc.removePage("demo code.New Page");
474
475 //Should print "true" if deletion of page was successful
476 System.out.println(b);
477
478
479 } catch (XmlRpcException e) {
480 System.out.println("invalid username/password was specified or communication problem or ");
481 System.out.println(e);
482 } finally {
483 rpc.logout();
484 }
485 }
486 }
487
488 {{/code}}
489
490 = Page: List Tags =
491
492 Pages are tagged via objects of type XWiki.TagClass. Thus you can switch to object edit mode on any page and edit tags this way. This is also the way, done by XMLRPC-API.
493
494 {{code language="java"}}
495 import java.net.MalformedURLException;
496 import java.util.List;
497
498 import org.apache.xmlrpc.XmlRpcException;
499 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
500 import org.xwiki.xmlrpc.model.XWikiObject;
501 import org.xwiki.xmlrpc.model.XWikiObjectSummary;
502
503 public class ListTags {
504 public static void main(String[] args) throws MalformedURLException,
505 XmlRpcException {
506
507 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
508 String user = "Admin";
509 String pass = "admin";
510
511 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
512 XWikiObjectSummary xosum = new XWikiObjectSummary();
513 xosum.setClassName("XWiki.TagClass");
514 // your page id
515 final String pageId = "Sandbox.Wdscfaq";
516 xosum.setPageId(pageId);
517 try {
518 // Perform Login & Authentication
519 rpc.login(user, pass);
520
521 // retrieve current page object informations
522 XWikiObject xwo = rpc.getObject(xosum);
523 List<String> tags = (List<String>) xwo.getProperty("tags");
524 System.out.println("--- CURRENT TAGS -----------------------");
525 for (String tag : tags) {
526 System.out.println(tag);
527 }
528 } catch (Exception e) {
529 System.out.println(e.getMessage());
530 } finally {
531 rpc.logout();
532 }
533 }
534 }
535 {{/code}}
536
537 = Page: Add Tags =
538
539 Adding tags is a similar procedure to listening them. There are two starting points: a) retrieve current tags and a new one or b) set entirely new ones at once. Most often, you want to add tags, so this is one way to do it ...
540 {{code language="java"}}import java.net.MalformedURLException;
541 import java.util.List;
542
543 import org.apache.xmlrpc.XmlRpcException;
544 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
545 import org.xwiki.xmlrpc.model.XWikiObject;
546 import org.xwiki.xmlrpc.model.XWikiObjectSummary;
547
548 public class AddTags {
549 public static void main(String[] args) throws MalformedURLException, XmlRpcException {
550
551 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
552 String user = "Admin";
553 String pass = "admin";
554
555 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
556 XWikiObjectSummary xosum = new XWikiObjectSummary();
557 xosum.setClassName("XWiki.TagClass");
558 // your page id
559 String pageId = "Sandbox.Wdscfaq";
560 xosum.setPageId(pageId);
561 try {
562 // Perform Login & Authentication
563 rpc.login(user, pass);
564
565 // retrieve current page object informations
566 XWikiObject xwo = rpc.getObject(xosum);
567 List<String> tags = (List<String>) xwo.getProperty("tags");
568 System.out.println("--- CURRENT TAGS -----------------------");
569 for (String tag : tags) {
570 System.out.println(tag);
571 }
572 System.out.println("--- NEW TAG -----------------------");
573 // add a new random tag
574 String tag = "NOW"+System.currentTimeMillis();
575 System.out.println(tag);
576 tags.add(tag);
577 rpc.storeObject(xwo);
578 } catch (XmlRpcException e) {
579 System.out.println(e.getMessage());
580 } finally {
581 rpc.logout();
582 }
583 }
584 }{{/code}}
585
586 = Page: Add Tags =
587
588 {{code language="java"}}{{/code}}
589
590 = Attachment: List Attachments For Page =
591
592 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.
593 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.
594
595 {{error}}
596 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.
597 {{/error}}
598
599 {{code language="java"}}
600
601 import org.codehaus.swizzle.confluence.Attachment;
602 import java.net.MalformedURLException;
603 import java.util.List;
604 import org.apache.xmlrpc.XmlRpcException;
605 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
606
607 public class AttachmentList {
608 public static void main(String[] args) throws MalformedURLException, XmlRpcException {
609 //Replace the url with your xwiki server address
610 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
611
612 //Replace user & pass with desired xwiki username & password
613 String user = "Admin";
614 String pass = "admin";
615
616 //Perform Login & Authentication using above url address
617 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
618
619 try {
620 rpc.login(user, pass);
621
622 //Get the list of all attachments for the Page named "Main.Host Page"
623 List<Attachment> att = rpc.getAttachments("Main.Host Page");
624
625 //Iterate through all the attachments in the list obtained
626 for (Attachment attachment : att) {
627
628 //Name of the file attached to the "Main.Host Page"
629 System.out.println(attachment.getFileName());
630 //Size of the file (in Bytes)
631 System.out.println(attachment.getFileSize());
632 //Absolute URL of the attachment for direct access
633 System.out.println(attachment.getUrl());
634 }
635
636
637 } catch (XmlRpcException e) {
638 System.out.println("invalid username/password was specified or communication problem or ");
639 System.out.println(e);
640 } finally {
641 rpc.logout();
642 }
643 }
644 }
645 {{/code}}
646
647 = Attachment: Add An Attachment To A Page =
648
649 This is a simple example, how to add an attachment to a specific page.
650
651 {{code language="java"}}
652 import java.io.ByteArrayOutputStream;
653 import java.io.File;
654 import java.io.FileInputStream;
655
656 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
657 import org.codehaus.swizzle.confluence.Attachment;
658
659 public class UploadAttachment {
660 public static void main(String[] args) {
661 File f = new File("xwiki.txt"); // put your file HERE
662
663 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
664 String user = "Admin";
665 String pass = "admin";
666
667 XWikiXmlRpcClient rpc = null;
668 try {
669 rpc = new XWikiXmlRpcClient(url);
670 rpc.login(user, pass);
671
672 // read file content into memory
673 FileInputStream fis = new FileInputStream(f);
674 ByteArrayOutputStream baos = new ByteArrayOutputStream();
675 byte[] buffer = new byte[8192];
676 int read = -1;
677 while ((read = fis.read(buffer)) > 0) {
678 baos.write(buffer, 0, read);
679 }
680 fis.close();
681
682 // prepare upload, set meta information
683 Attachment a = new Attachment();
684 a.setFileName(f.getName());
685 a.setFileSize(Long.toString(f.length()));
686 a.setPageId("Sandbox.WebHome"); // your page id
687
688 // do upload
689 // note: the first integer parameter has no impact,
690 // cause it gets ignored on the server side.
691 rpc.addAttachment(new Integer(f.getName().hashCode()), a, baos.toByteArray());
692 } catch (Exception e) {
693 e.printStackTrace();
694 } finally {
695 if (rpc != null) {
696 try {
697 rpc.logout();
698 } catch (Exception e2) {
699 // something real bad happens.
700 }
701 }
702 }
703 }
704 }
705 {{/code}}
706
707 = User: Create A New User =
708
709 This is an Administrative function in XWiki & needs Admin privileges for the User with whose credential we connect through the XML-RPC. Creating a new User involves the following steps:
710
711 1. Create a Page with the desired "UserName" as the Page Name
712 1. Create this Page in the "XWiki" space
713 1. Set the properties of the newly created User
714
715 Please read the comments in the example below to understand the detailed steps:
716
717 {{code language="java"}}
718
719 import java.net.MalformedURLException;
720 import java.security.NoSuchAlgorithmException;
721 import org.apache.xmlrpc.XmlRpcException;
722 import org.codehaus.swizzle.confluence.Page;
723 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
724 import org.xwiki.xmlrpc.model.XWikiObject;
725
726 public class CreateUser {
727
728 public static void main(String[] args) throws MalformedURLException, XmlRpcException, NoSuchAlgorithmException {
729
730 //URL of the xwiki instance
731 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
732
733 //Replace user & pass with desired xwiki username & password
734 String user = "Admin";
735 String pass = "admin";
736
737
738 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
739 try {
740 //Perform Login & Authentication
741 rpc.login(user, pass);
742
743 //Create a Page object & set it's three important attributes viz. Space, Title, Content
744 //In our example, testuser is the Page Name
745 //We set the content of the Page to automatically load XWiki.XWikiUserSheet
746 //This sheet is used to display the User Profile in the default format
747
748 Page page = new Page();
749 page.setSpace("XWiki");
750 page.setTitle("testuser");
751 page.setId("XWiki.testuser");
752 page.setContent("{{include document=\"XWiki.XWikiUserSheet\"/}}");
753 rpc.storePage(page);
754
755 //Set properties for the newly created user "testuser"
756 //List of all properties available in the XWiki.XWikiUsers class in your wiki
757 //Edit XWikiUsers class in class editing mode to access all properties or define new ones
758
759 XWikiObject xobj = new XWikiObject();
760 xobj.setClassName("XWiki.XWikiUsers");
761 xobj.setPageId("XWiki.testuser");
762 xobj.setProperty("first_name", "Test");
763 xobj.setProperty("last_name", "User");
764 xobj.setProperty("password","asdfjk");
765 rpc.storeObject(xobj);
766
767 //Finally, associate the user to the XWikiAllGroup
768 //We simply associate the XWiki.testuser Page to the XWiki.XWikiAllGroup Page
769 //Set the "member" property to the desired XWiki UserName
770
771 XWikiObject xobjgrp = new XWikiObject();
772 xobjgrp.setClassName("XWiki.XWikiGroups");
773 xobjgrp.setPageId("XWiki.XWikiAllGroup");
774 xobjgrp.setProperty("member","XWiki.testuser");
775 rpc.storeObject(xobjgrp);
776
777 } catch (XmlRpcException e) {
778 System.out.println(e);
779 } finally {
780 rpc.logout();
781 }
782 }
783 }
784
785 {{/code}}
786
787 = User: Add User To Groups =
788
789 Adding User to Groups is a very simple task. This requires us to add the desired user to the properties of the Group in question. The example below details all the steps needed to complete this task:
790
791 {{code langauge="java"}}
792
793 import java.net.MalformedURLException;
794 import java.security.NoSuchAlgorithmException;
795 import org.apache.xmlrpc.XmlRpcException;
796 import org.codehaus.swizzle.confluence.Page;
797 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
798 import org.xwiki.xmlrpc.model.XWikiObject;
799
800
801 public class AddUserToGroup {
802 public static void main(String[] args) throws MalformedURLException, XmlRpcException, NoSuchAlgorithmException {
803
804 //URL of the xwiki instance
805 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
806
807 //Replace user & pass with desired xwiki username & password
808 String user = "Admin";
809 String pass = "admin";
810
811
812 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
813 try {
814 //Perform Login & Authentication
815 rpc.login(user, pass);
816
817 //Associate an existing user to the XWikiAllGroup
818 //We simply associate the XWiki.testuser Page to the XWiki.XWikiAllGroup Page
819 //Set the "member" property to the desired XWiki UserName
820 //You may associate the member to any custom Group you may have created
821
822 XWikiObject xobjgrp = new XWikiObject();
823 xobjgrp.setClassName("XWiki.XWikiGroups");
824 xobjgrp.setPageId("XWiki.XWikiAllGroup");
825 xobjgrp.setProperty("member","XWiki.testuser");
826 rpc.storeObject(xobjgrp);
827
828 } catch (XmlRpcException e) {
829 System.out.println(e);
830 } finally {
831 rpc.logout();
832 }
833 }
834 }
835
836 {{/code}}

Get Connected