Wiki source code of XML-RPC Java Examples

Version 10.1 by mawoki on 2010/08/09 12:04

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 And Add Tags =
491
492 {{code language="java"}}
493 import java.net.MalformedURLException;
494 import java.util.List;
495
496 import org.apache.xmlrpc.XmlRpcException;
497 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
498 import org.xwiki.xmlrpc.model.XWikiObject;
499 import org.xwiki.xmlrpc.model.XWikiObjectSummary;
500
501 public class ListAndAddTags {
502 public static void main(String[] args) throws MalformedURLException,
503 XmlRpcException {
504
505 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
506 String user = "Admin";
507 String pass = "admin";
508
509 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
510 try {
511 // Perform Login & Authentication
512 rpc.login(user, pass);
513
514 XWikiObjectSummary xosum = new XWikiObjectSummary();
515 xosum.setClassName("XWiki.TagClass");
516 // your page id
517 xosum.setPageId("Sandbox.WebHome");
518 // retrieve current page object informations
519 XWikiObject xwo = rpc.getObject(xosum);
520 // The tags are one String, separated by | or , or 'SPACE'
521 List<String> tags = (List<String>) xwo.getProperty("tags");
522 System.out.println("--- CURRENT TAGS -----------------------");
523 for (String tag : tags) {
524 System.out.println(tag);
525 }
526 System.out.println("--- NEW TAG -----------------------");
527 // add a new random tag
528 String tag = "NOW"+System.currentTimeMillis();
529 System.out.println(tag);
530 tags.add(tag);
531 rpc.storeObject(xwo);
532 } catch (XmlRpcException e) {
533 System.out.println(e.getMessage());
534 } finally {
535 rpc.logout();
536 }
537 }
538 }
539 {{/code}}
540
541 = Attachment: List Attachments For Page =
542
543 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.
544 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.
545
546 {{error}}
547 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.
548 {{/error}}
549
550 {{code language="java"}}
551
552 import org.codehaus.swizzle.confluence.Attachment;
553 import java.net.MalformedURLException;
554 import java.util.List;
555 import org.apache.xmlrpc.XmlRpcException;
556 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
557
558 public class AttachmentList {
559 public static void main(String[] args) throws MalformedURLException, XmlRpcException {
560 //Replace the url with your xwiki server address
561 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
562
563 //Replace user & pass with desired xwiki username & password
564 String user = "Admin";
565 String pass = "admin";
566
567 //Perform Login & Authentication using above url address
568 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
569
570 try {
571 rpc.login(user, pass);
572
573 //Get the list of all attachments for the Page named "Main.Host Page"
574 List<Attachment> att = rpc.getAttachments("Main.Host Page");
575
576 //Iterate through all the attachments in the list obtained
577 for (Attachment attachment : att) {
578
579 //Name of the file attached to the "Main.Host Page"
580 System.out.println(attachment.getFileName());
581 //Size of the file (in Bytes)
582 System.out.println(attachment.getFileSize());
583 //Absolute URL of the attachment for direct access
584 System.out.println(attachment.getUrl());
585 }
586
587
588 } catch (XmlRpcException e) {
589 System.out.println("invalid username/password was specified or communication problem or ");
590 System.out.println(e);
591 } finally {
592 rpc.logout();
593 }
594 }
595 }
596 {{/code}}
597
598 = Attachment: Add An Attachment To A Page =
599
600 This is a simple example, how to add an attachment to a specific page.
601
602 {{code language="java"}}
603 import java.io.ByteArrayOutputStream;
604 import java.io.File;
605 import java.io.FileInputStream;
606
607 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
608 import org.codehaus.swizzle.confluence.Attachment;
609
610 public class UploadAttachment {
611 public static void main(String[] args) {
612 File f = new File("xwiki.txt"); // put your file HERE
613
614 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
615 String user = "Admin";
616 String pass = "admin";
617
618 XWikiXmlRpcClient rpc = null;
619 try {
620 rpc = new XWikiXmlRpcClient(url);
621 rpc.login(user, pass);
622
623 // read file content into memory
624 FileInputStream fis = new FileInputStream(f);
625 ByteArrayOutputStream baos = new ByteArrayOutputStream();
626 byte[] buffer = new byte[8192];
627 int read = -1;
628 while ((read = fis.read(buffer)) > 0) {
629 baos.write(buffer, 0, read);
630 }
631 fis.close();
632
633 // prepare upload, set meta information
634 Attachment a = new Attachment();
635 a.setFileName(f.getName());
636 a.setFileSize(Long.toString(f.length()));
637 a.setPageId("Sandbox.WebHome"); // your page id
638
639 // do upload
640 // note: the first integer parameter has no impact,
641 // cause it gets ignored on the server side.
642 rpc.addAttachment(new Integer(f.getName().hashCode()), a, baos.toByteArray());
643 } catch (Exception e) {
644 e.printStackTrace();
645 } finally {
646 if (rpc != null) {
647 try {
648 rpc.logout();
649 } catch (Exception e2) {
650 // something real bad happens.
651 }
652 }
653 }
654 }
655 }
656 {{/code}}
657
658 = User: Create A New User =
659
660 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:
661
662 1. Create a Page with the desired "UserName" as the Page Name
663 1. Create this Page in the "XWiki" space
664 1. Set the properties of the newly created User
665
666 Please read the comments in the example below to understand the detailed steps:
667
668 {{code language="java"}}
669
670 import java.net.MalformedURLException;
671 import java.security.NoSuchAlgorithmException;
672 import org.apache.xmlrpc.XmlRpcException;
673 import org.codehaus.swizzle.confluence.Page;
674 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
675 import org.xwiki.xmlrpc.model.XWikiObject;
676
677 public class CreateUser {
678
679 public static void main(String[] args) throws MalformedURLException, XmlRpcException, NoSuchAlgorithmException {
680
681 //URL of the xwiki instance
682 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
683
684 //Replace user & pass with desired xwiki username & password
685 String user = "Admin";
686 String pass = "admin";
687
688
689 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
690 try {
691 //Perform Login & Authentication
692 rpc.login(user, pass);
693
694 //Create a Page object & set it's three important attributes viz. Space, Title, Content
695 //In our example, testuser is the Page Name
696 //We set the content of the Page to automatically load XWiki.XWikiUserSheet
697 //This sheet is used to display the User Profile in the default format
698
699 Page page = new Page();
700 page.setSpace("XWiki");
701 page.setTitle("testuser");
702 page.setId("XWiki.testuser");
703 page.setContent("{{include document=\"XWiki.XWikiUserSheet\"/}}");
704 rpc.storePage(page);
705
706 //Set properties for the newly created user "testuser"
707 //List of all properties available in the XWiki.XWikiUsers class in your wiki
708 //Edit XWikiUsers class in class editing mode to access all properties or define new ones
709
710 XWikiObject xobj = new XWikiObject();
711 xobj.setClassName("XWiki.XWikiUsers");
712 xobj.setPageId("XWiki.testuser");
713 xobj.setProperty("first_name", "Test");
714 xobj.setProperty("last_name", "User");
715 xobj.setProperty("password","asdfjk");
716 rpc.storeObject(xobj);
717
718 //Finally, associate the user to the XWikiAllGroup
719 //We simply associate the XWiki.testuser Page to the XWiki.XWikiAllGroup Page
720 //Set the "member" property to the desired XWiki UserName
721
722 XWikiObject xobjgrp = new XWikiObject();
723 xobjgrp.setClassName("XWiki.XWikiGroups");
724 xobjgrp.setPageId("XWiki.XWikiAllGroup");
725 xobjgrp.setProperty("member","XWiki.testuser");
726 rpc.storeObject(xobjgrp);
727
728 } catch (XmlRpcException e) {
729 System.out.println(e);
730 } finally {
731 rpc.logout();
732 }
733 }
734 }
735
736 {{/code}}
737
738 = User: Add User To Groups =
739
740 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:
741
742 {{code langauge="java"}}
743
744 import java.net.MalformedURLException;
745 import java.security.NoSuchAlgorithmException;
746 import org.apache.xmlrpc.XmlRpcException;
747 import org.codehaus.swizzle.confluence.Page;
748 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
749 import org.xwiki.xmlrpc.model.XWikiObject;
750
751
752 public class AddUserToGroup {
753 public static void main(String[] args) throws MalformedURLException, XmlRpcException, NoSuchAlgorithmException {
754
755 //URL of the xwiki instance
756 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
757
758 //Replace user & pass with desired xwiki username & password
759 String user = "Admin";
760 String pass = "admin";
761
762
763 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
764 try {
765 //Perform Login & Authentication
766 rpc.login(user, pass);
767
768 //Associate an existing user to the XWikiAllGroup
769 //We simply associate the XWiki.testuser Page to the XWiki.XWikiAllGroup Page
770 //Set the "member" property to the desired XWiki UserName
771 //You may associate the member to any custom Group you may have created
772
773 XWikiObject xobjgrp = new XWikiObject();
774 xobjgrp.setClassName("XWiki.XWikiGroups");
775 xobjgrp.setPageId("XWiki.XWikiAllGroup");
776 xobjgrp.setProperty("member","XWiki.testuser");
777 rpc.storeObject(xobjgrp);
778
779 } catch (XmlRpcException e) {
780 System.out.println(e);
781 } finally {
782 rpc.logout();
783 }
784 }
785 }
786
787 {{/code}}

Get Connected