Wiki source code of XML-RPC Java Examples

Version 1.6 by dilipkumarj on 2010/01/30 04:18

Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}}
2
3 = Requirements =
4
5 The following libraries should be added to your application classpath:
6
7 {{info}}
8 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.
9 {{/info}}
10
11 |=Library|=Source
12 |commons-logging-1.1.1.jar|WEB-INF/lib folder of XWiki 2.1.1
13 |ws-commons-util-1.0.2.jar|WEB-INF/lib folder of XWiki 2.1.1
14 |xmlrpc-common-3.1.jar|WEB-INF/lib folder of XWiki 2.1.1
15 |xmlrpc-client-3.1.jar|WEB-INF/lib folder of XWiki 2.1.1
16 |xwiki-core-2.1.1.jar|WEB-INF/lib folder of XWiki 2.1.1
17 |swizzle-confluence-1.2-20080419-xwiki.jar|WEB-INF/lib folder of XWiki 2.1.1
18 |xwiki-core-xmlrpc-client-2.1.1.jar|For XWiki 2.1.1 the jar is available [[here>>http://maven.xwiki.org/releases/org/xwiki/platform/xwiki-core-xmlrpc-client/2.1.1/xwiki-core-xmlrpc-client-2.1.1jar]]
19 |xwiki-core-xmlrpc-model-2.1.1.jar|WEB-INF/lib folder of XWiki 2.1.1
20
21 = Authentication: Login Example =
22
23 Try Providing an incorrect username & password. The application will throw an exception
24 For correct username & password, the application will compile & run successfully.
25
26 {{code language="java"}}
27 import java.net.MalformedURLException;
28 import org.apache.xmlrpc.XmlRpcException;
29 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
30 public class XmlRpcLogin {
31 public static void main(String[] args) throws MalformedURLException{
32
33 //URL of the xwiki instance
34 String url="http://localhost:8080/xwiki/xmlrpc/confluence";
35
36 //Replace user & pass with desired xwiki username & password
37 String user="Admin";
38 String pass="admin";
39
40 //Perform Login & Authentication using above url address
41 try{
42 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
43 rpc.login(user, pass);
44
45 }
46 catch(XmlRpcException e){
47 System.out.println("invalid username/password was specified or communication problem");
48 }
49 }
50 }
51 {{/code}}
52
53 = Authentication: Logout Example =
54
55 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.
56
57 {{code language="java"}}
58
59 import java.net.MalformedURLException;
60 import org.apache.xmlrpc.XmlRpcException;
61 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
62 public class XmlRpcLogout {
63 public static void main(String[] args) throws MalformedURLException{
64
65 //Variable to test whether user logged out successfully. Default set to false
66 boolean loggedOut=false;
67
68 //URL of the xwiki instance
69 String url="http://localhost:8080/xwiki/xmlrpc/confluence";
70
71 //Replace user & pass with desired xwiki username & password
72 String user="Admin";
73 String pass="admin";
74
75 try{
76 //Perform Login & Authentication using above url address
77 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
78 rpc.login(user, pass);
79
80 //Log out action. Returns true if user successfully logged out
81 loggedOut=rpc.logout();
82 System.out.println(loggedOut);
83 }
84 catch(XmlRpcException e){
85 System.out.println("invalid username/password was specified or communication problem");
86 }
87 }
88 }
89 {{/code}}
90
91 = Search documents =
92
93 {{code language="java"}}
94 import java.net.MalformedURLException;
95 import java.util.List;
96 import org.apache.xmlrpc.XmlRpcException;
97 import org.codehaus.swizzle.confluence.SearchResult;
98 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
99
100 public class XWikiXMLRPCSearch {
101
102 public static void main(String[] args) throws MalformedURLException, XmlRpcException {
103 //Replace the url with your xwiki server address
104 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
105
106 //Replace user & pass with desired xwiki username & password
107 String user = "Admin";
108 String pass = "admin";
109
110 //Perform Login & Authentication using above url address
111 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
112 rpc.login(user, pass);
113
114 //Replace string to search in the rpc.search() method below
115 //Specify the number of results to be returned in rpc.search() as 5
116 List<SearchResult> result = rpc.search("text I want to search", 5);
117
118
119 //Print obtained results to console if result is not empty
120 if (result.size() != 0) {
121 for (int i = 0; i < result.size(); i++) {
122 System.out.println(result.get(i).getTitle());
123 }
124 } //Print standard message if result is empty
125 else {
126 System.out.println("No Results To Display");
127 }
128 }
129 }
130 {{/code}}
131
132 = Space: Get A List Of Spaces =
133
134 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.
135
136 {{code language="java"}}
137
138 import java.net.MalformedURLException;
139 import java.util.List;
140 import org.apache.xmlrpc.XmlRpcException;
141 import org.codehaus.swizzle.confluence.SpaceSummary;
142 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
143 public class SpaceList {
144
145 public static void main(String[] args) throws MalformedURLException {
146 //URL of the xwiki instance
147 String url="http://localhost:8080/xwiki/xmlrpc/confluence";
148
149 //Replace user & pass with desired xwiki username & password
150 String user="Admin";
151 String pass="admin";
152
153 //Perform Login & Authentication using above url address
154 try{
155 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
156 rpc.login(user, pass);
157
158 List<SpaceSummary> spaceList = rpc.getSpaces();
159 System.out.println("Total Number of Spaces: " +spaceList.size());
160 for(int i=0;i<spaceList.size();i++){
161 System.out.println(spaceList.get(i).getKey());
162 }
163 rpc.logout();
164 }
165 catch(XmlRpcException e){
166 System.out.println("invalid username/password was specified or communication problem");
167 }
168 }
169 }
170 {{/code}}
171
172 = Space: Create A Space =
173
174 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
175
176 {{code language="java"}}
177 import java.net.MalformedURLException;
178 import org.apache.xmlrpc.XmlRpcException;
179 import org.codehaus.swizzle.confluence.Space;
180 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
181
182 public class CreateSpace {
183
184 public static void main(String[] args) throws MalformedURLException {
185 //URL of the xwiki instance
186 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
187
188 //Replace user & pass with desired xwiki username & password
189 String user = "Admin";
190 String pass = "admin";
191
192 //Perform Login & Authentication using above url address
193 try {
194 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
195 rpc.login(user, pass);
196
197 //Create a Space object which holds the Key, Homepage, Name & description.
198 //Key is visible name for the space
199 Space space = new Space();
200 space.setKey("XMLRPC");
201 space.setHomepage("xmlrpc.WebHome");
202 space.setName("xmlrpc");
203 space.setDescription("Demo Space Created To Test XMLRPC");
204
205 //One simple method adds the space created to your xwiki instance
206 rpc.addSpace(space);
207
208 rpc.logout();
209 } catch (XmlRpcException e) {
210 System.out.println("invalid username/password was specified or communication problem");
211 }
212 }
213 }
214 {{/code}}
215
216 = Space: Delete A Space =
217
218 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.
219
220 {{warning}}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.
221 {{/warning}}
222
223 {{code language="java"}}
224
225 import java.net.MalformedURLException;
226 import org.apache.xmlrpc.XmlRpcException;
227 import org.codehaus.swizzle.confluence.Space;
228 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
229
230 public class DeleteSpace {
231
232 public static void main(String[] args) throws MalformedURLException {
233 //URL of the xwiki instance
234 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
235
236 //Replace user & pass with desired xwiki username & password
237 String user = "Admin";
238 String pass = "admin";
239
240 //Perform Login & Authentication using above url address
241 try {
242 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
243 rpc.login(user, pass);
244
245 //Delete the space which is reference by "XMLRPC" key
246 rpc.removeSpace("XMLRPC");
247
248 rpc.logout();
249 } catch (XmlRpcException e) {
250 System.out.println("invalid username/password was specified or communication problem");
251 }
252 }
253 }
254 {{/code}}
255
256 = Page: Retrieve/Search A Page =
257
258 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.
259 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.
260 Since, this topic is an important one, all the page retrieval/search examples [[can be found here>>XMLRPCJavaExamples2]]
261
262 = Page: Create A Page =
263
264 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.
265 At the very least, you would require the following three parameters to create a Page:
266
267 1. Space - The Space where the Page is to be stored
268 1. Title - The title for the Page
269 1. Content - The content to be displayed inside the Page
270
271 {{warning}}
272 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.
273 {{/warning}}
274
275 In our example below, we would use
276
277 |=Parameter|=Value
278 |Space|demo code
279 |title|New Page
280 |Content|New Page Created
281 {{info}}This is XMLRPC Test{{/info}}
282
283 {{code language="java"}}
284 import java.net.MalformedURLException;
285 import org.apache.xmlrpc.XmlRpcException;
286 import org.codehaus.swizzle.confluence.Page;
287 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
288
289 public class CreatePage {
290
291 public static void main(String[] args) throws MalformedURLException, XmlRpcException {
292
293 //URL of the xwiki instance
294 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
295
296 //Replace user & pass with desired xwiki username & password
297 String user = "Admin";
298 String pass = "admin";
299
300
301 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
302 try {
303
304 //Perform Login & Authentication
305 rpc.login(user, pass);
306
307 //Create a Page object & set it's three important attributes viz. Space, Title, Content
308 //Observe how the \\\\ has been used to create a new line in the final wiki Page
309 //Also, XWiki syntax can be passed as it is. Here, we passed the info macro
310 //The info macro would get rendered an info box in the Page
311 Page page = new Page();
312 page.setSpace("demo code");
313 page.setTitle("New Page");
314 page.setContent("New Page Created \\\\ {{info}}This is XMLRPC Test{{/info}}");
315
316 //Also set the parent Page to "demo code.WebHome" so that the "New Page" we created is not
317 //an orphan Page
318 page.setParentId("demo code.WebHome");
319
320
321 //Store the page object into XWiki
322 rpc.storePage(page);
323
324
325 } catch (XmlRpcException e) {
326 System.out.println("invalid username/password was specified or communication problem or ");
327 System.out.println(e);
328 } finally {
329 rpc.logout();
330 }
331 }
332 }
333
334 {{/code}}
335
336
337 = Page: Get Page History =
338
339 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.
340
341 {{info}}
342 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.
343 {{/info}}
344
345 {{code language="java"}}
346 import java.net.MalformedURLException;
347 import java.util.List;
348 import org.apache.xmlrpc.XmlRpcException;
349 import org.xwiki.xmlrpc.XWikiXmlRpcClient;
350 import org.xwiki.xmlrpc.model.XWikiPageHistorySummary;
351
352
353 public class PageHistory {
354
355 public static void main(String[] args) throws MalformedURLException, XmlRpcException {
356
357 //URL of the xwiki instance
358 String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
359
360 //Replace user & pass with desired xwiki username & password
361 String user = "Admin";
362 String pass = "admin";
363
364
365 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
366 try {
367
368 //Perform Login & Authentication
369 rpc.login(user, pass);
370
371 //Create a XWikiPageHistorySummary object to hold all the revisions/history
372 //of the Main.WebHome Page
373 List<XWikiPageHistorySummary> hist = rpc.getPageHistory("Main.WebHome");
374
375 //Iterate through all the available versions for Main.WebHome
376 for(XWikiPageHistorySummary xphs:hist){
377
378 //Print the fully qualified name of the Page i.e. SpaceName.PageName
379 //In our example this would be Main.WebHome
380 System.out.println(xphs.getBasePageId());
381
382 //Print the historical page ID or name of the Main.WebHome page
383 //This would print something like:
384 //Main.WebHome?minorVersion=1&language=&version=2
385 //It means page=Main.WebHome, version=2, minor version=1 & language=default
386 System.out.println(xphs.getId());
387
388 //Printing the version of Main.WebHome in the majorversion.minorversion format
389 System.out.println("Version: "+xphs.getVersion()+"."+xphs.getMinorVersion());
390
391 //Date when the Page was last modified
392 System.out.println(xphs.getModified());
393
394 //User who last modified the Page
395 System.out.println(xphs.getModifier());
396
397 //Just a seperator between various versions of the document when printing
398 //to console
399 System.out.println("------------------------------");
400 }
401
402 } catch (XmlRpcException e) {
403 System.out.println("invalid username/password was specified or communication problem or ");
404 System.out.println(e);
405 } finally {
406 rpc.logout();
407 }
408 }
409 }
410 {{/code}}

Get Connected