Wiki source code of XML-RPC Java Examples

Version 14.2 by mawoki on 2010/11/25 08:30

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 }
585 {{/code}}
586
587 = Attachment: List Attachments For Page =
588
589 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.
590 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.
591
592 {{error}}
593 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.
594 {{/error}}
595
596 {{code language="java"}}
597
598 import org.codehaus.swizzle.confluence.Attachment;
599 import java.net.MalformedURLException;
600 import java.util.List;
601 import org.apache.xmlrpc.XmlRpcException;
602 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
603
604 public class AttachmentList {
605 public static void main(String[] args) throws MalformedURLException, XmlRpcException {
606 //Replace the url with your xwiki server address
607 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
608
609 //Replace user & pass with desired xwiki username & password
610 String user = "Admin";
611 String pass = "admin";
612
613 //Perform Login & Authentication using above url address
614 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
615
616 try {
617 rpc.login(user, pass);
618
619 //Get the list of all attachments for the Page named "Main.Host Page"
620 List<Attachment> att = rpc.getAttachments("Main.Host Page");
621
622 //Iterate through all the attachments in the list obtained
623 for (Attachment attachment : att) {
624
625 //Name of the file attached to the "Main.Host Page"
626 System.out.println(attachment.getFileName());
627 //Size of the file (in Bytes)
628 System.out.println(attachment.getFileSize());
629 //Absolute URL of the attachment for direct access
630 System.out.println(attachment.getUrl());
631 }
632
633
634 } catch (XmlRpcException e) {
635 System.out.println("invalid username/password was specified or communication problem or ");
636 System.out.println(e);
637 } finally {
638 rpc.logout();
639 }
640 }
641 }
642 {{/code}}
643
644 = Attachment: Add An Attachment To A Page =
645
646 This is a simple example, how to add an attachment to a specific page.
647
648 {{code language="java"}}
649 import java.io.ByteArrayOutputStream;
650 import java.io.File;
651 import java.io.FileInputStream;
652
653 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
654 import org.codehaus.swizzle.confluence.Attachment;
655
656 public class UploadAttachment {
657 public static void main(String[] args) {
658 File f = new File("xwiki.txt"); // put your file HERE
659
660 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
661 String user = "Admin";
662 String pass = "admin";
663
664 XWikiXmlRpcClient rpc = null;
665 try {
666 rpc = new XWikiXmlRpcClient(url);
667 rpc.login(user, pass);
668
669 // read file content into memory
670 FileInputStream fis = new FileInputStream(f);
671 ByteArrayOutputStream baos = new ByteArrayOutputStream();
672 byte[] buffer = new byte[8192];
673 int read = -1;
674 while ((read = fis.read(buffer)) > 0) {
675 baos.write(buffer, 0, read);
676 }
677 fis.close();
678
679 // prepare upload, set meta information
680 Attachment a = new Attachment();
681 a.setFileName(f.getName());
682 a.setFileSize(Long.toString(f.length()));
683 a.setPageId("Sandbox.WebHome"); // your page id
684
685 // do upload
686 // note: the first integer parameter has no impact,
687 // cause it gets ignored on the server side.
688 rpc.addAttachment(new Integer(f.getName().hashCode()), a, baos.toByteArray());
689 } catch (Exception e) {
690 e.printStackTrace();
691 } finally {
692 if (rpc != null) {
693 try {
694 rpc.logout();
695 } catch (Exception e2) {
696 // something real bad happens.
697 }
698 }
699 }
700 }
701 }
702 {{/code}}
703
704 = User: Create A New User =
705
706 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:
707
708 1. Create a Page with the desired "UserName" as the Page Name
709 1. Create this Page in the "XWiki" space
710 1. Set the properties of the newly created User
711
712 Please read the comments in the example below to understand the detailed steps:
713
714 {{code language="java"}}
715
716 import java.net.MalformedURLException;
717 import java.security.NoSuchAlgorithmException;
718 import org.apache.xmlrpc.XmlRpcException;
719 import org.codehaus.swizzle.confluence.Page;
720 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
721 import org.xwiki.xmlrpc.model.XWikiObject;
722
723 public class CreateUser {
724
725 public static void main(String[] args) throws MalformedURLException, XmlRpcException, NoSuchAlgorithmException {
726
727 //URL of the xwiki instance
728 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
729
730 //Replace user & pass with desired xwiki username & password
731 String user = "Admin";
732 String pass = "admin";
733
734
735 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
736 try {
737 //Perform Login & Authentication
738 rpc.login(user, pass);
739
740 //Create a Page object & set it's three important attributes viz. Space, Title, Content
741 //In our example, testuser is the Page Name
742 //We set the content of the Page to automatically load XWiki.XWikiUserSheet
743 //This sheet is used to display the User Profile in the default format
744
745 Page page = new Page();
746 page.setSpace("XWiki");
747 page.setTitle("testuser");
748 page.setId("XWiki.testuser");
749 page.setContent("{{include document=\"XWiki.XWikiUserSheet\"/}}");
750 rpc.storePage(page);
751
752 //Set properties for the newly created user "testuser"
753 //List of all properties available in the XWiki.XWikiUsers class in your wiki
754 //Edit XWikiUsers class in class editing mode to access all properties or define new ones
755
756 XWikiObject xobj = new XWikiObject();
757 xobj.setClassName("XWiki.XWikiUsers");
758 xobj.setPageId("XWiki.testuser");
759 xobj.setProperty("first_name", "Test");
760 xobj.setProperty("last_name", "User");
761 xobj.setProperty("password","asdfjk");
762 rpc.storeObject(xobj);
763
764 //Finally, associate the user to the XWikiAllGroup
765 //We simply associate the XWiki.testuser Page to the XWiki.XWikiAllGroup Page
766 //Set the "member" property to the desired XWiki UserName
767
768 XWikiObject xobjgrp = new XWikiObject();
769 xobjgrp.setClassName("XWiki.XWikiGroups");
770 xobjgrp.setPageId("XWiki.XWikiAllGroup");
771 xobjgrp.setProperty("member","XWiki.testuser");
772 rpc.storeObject(xobjgrp);
773
774 } catch (XmlRpcException e) {
775 System.out.println(e);
776 } finally {
777 rpc.logout();
778 }
779 }
780 }
781
782 {{/code}}
783
784 = User: Add User To Groups =
785
786 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:
787
788 {{code langauge="java"}}
789
790 import java.net.MalformedURLException;
791 import java.security.NoSuchAlgorithmException;
792 import org.apache.xmlrpc.XmlRpcException;
793 import org.codehaus.swizzle.confluence.Page;
794 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
795 import org.xwiki.xmlrpc.model.XWikiObject;
796
797
798 public class AddUserToGroup {
799 public static void main(String[] args) throws MalformedURLException, XmlRpcException, NoSuchAlgorithmException {
800
801 //URL of the xwiki instance
802 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
803
804 //Replace user & pass with desired xwiki username & password
805 String user = "Admin";
806 String pass = "admin";
807
808
809 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
810 try {
811 //Perform Login & Authentication
812 rpc.login(user, pass);
813
814 //Associate an existing user to the XWikiAllGroup
815 //We simply associate the XWiki.testuser Page to the XWiki.XWikiAllGroup Page
816 //Set the "member" property to the desired XWiki UserName
817 //You may associate the member to any custom Group you may have created
818
819 XWikiObject xobjgrp = new XWikiObject();
820 xobjgrp.setClassName("XWiki.XWikiGroups");
821 xobjgrp.setPageId("XWiki.XWikiAllGroup");
822 xobjgrp.setProperty("member","XWiki.testuser");
823 rpc.storeObject(xobjgrp);
824
825 } catch (XmlRpcException e) {
826 System.out.println(e);
827 } finally {
828 rpc.logout();
829 }
830 }
831 }
832
833 {{/code}}

Get Connected