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

Get Connected