Wiki source code of XML-RPC Java Example - Retrieve/Search Pages
Last modified by Thomas Mortagne on 2023/10/10 14:35
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}} | ||
2 | |||
3 | This page is dedicated to providing examples of various methods available to retrieve/search pages through the XML-RPC Client Side Proxy for Java. | ||
4 | |||
5 | = Retrieve Page By Fully Qualified Name = | ||
6 | |||
7 | This is more of a page retrieval than a page search since it is assumed that we already know the fully qualified name of the page (Fully Qualified Name = Space Name.Page Name eg. Main.WebHome) | ||
8 | |||
9 | {{code language="java"}} | ||
10 | import java.net.MalformedURLException; | ||
11 | import org.apache.xmlrpc.XmlRpcException; | ||
12 | import org.xwiki.xmlrpc.XWikiXmlRpcClient; | ||
13 | import org.xwiki.xmlrpc.model.XWikiPage; | ||
14 | |||
15 | public class RetrieveByName | ||
16 | { | ||
17 | public static void main(String[] args) throws MalformedURLException{ | ||
18 | |||
19 | //URL of the xwiki instance | ||
20 | String url="http://localhost:8080/xwiki/xmlrpc/confluence"; | ||
21 | |||
22 | //Replace user & pass with desired xwiki username & password | ||
23 | String user="Admin"; | ||
24 | String pass="admin"; | ||
25 | |||
26 | //Perform Login & Authentication using above url address | ||
27 | try{ | ||
28 | XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url); | ||
29 | rpc.login(user, pass); | ||
30 | |||
31 | //Pass the fully qualified name eg. Main.WebHome | ||
32 | XWikiPage page= rpc.getPage("Main.WebHome"); | ||
33 | |||
34 | //Display the URL of the page at Main.WebHome | ||
35 | System.out.println(page.getUrl()); | ||
36 | |||
37 | rpc.logout(); | ||
38 | } | ||
39 | catch(XmlRpcException e){ | ||
40 | System.out.println("invalid username/password was specified or communication problem or "); | ||
41 | System.out.println(e); | ||
42 | } | ||
43 | } | ||
44 | } | ||
45 | {{/code}} | ||
46 | |||
47 | = Retrieve Page By Major Version = | ||
48 | |||
49 | We can retrieve a page by it's fully qualified name and also any major version of that page. Here we are attempting to retrieve major version 2.1 of the page Main.WebHome. | ||
50 | {{warning}} | ||
51 | If the version 2.1 of the page Main.WebHome does not exist you will be greeted with an error such as: | ||
52 | Failed to invoke method getPage in class com.xpn.xwiki.xmlrpc.XWikiXmlRpcApiImpl: Error number 3205 in 3: Version 2.1 does not exist while reading document Main.WebHome | ||
53 | {{/warning}} | ||
54 | |||
55 | {{code language="java"}} | ||
56 | import java.net.MalformedURLException; | ||
57 | import org.apache.xmlrpc.XmlRpcException; | ||
58 | import org.xwiki.xmlrpc.XWikiXmlRpcClient; | ||
59 | import org.xwiki.xmlrpc.model.XWikiPage; | ||
60 | |||
61 | public class RetrieveByName | ||
62 | { | ||
63 | public static void main(String[] args) throws MalformedURLException{ | ||
64 | |||
65 | //URL of the xwiki instance | ||
66 | String url="http://localhost:8080/xwiki/xmlrpc/confluence"; | ||
67 | |||
68 | //Replace user & pass with desired xwiki username & password | ||
69 | String user="Admin"; | ||
70 | String pass="admin"; | ||
71 | |||
72 | //Perform Login & Authentication using above url address | ||
73 | try{ | ||
74 | XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url); | ||
75 | rpc.login(user, pass); | ||
76 | |||
77 | //Pass the fully qualified name eg. Main.WebHome | ||
78 | XWikiPage page= rpc.getPage("Main.WebHome",2); | ||
79 | |||
80 | //Display the URL of the page at Main.WebHome | ||
81 | System.out.println(page.getContent()); | ||
82 | |||
83 | rpc.logout(); | ||
84 | } | ||
85 | catch(XmlRpcException e){ | ||
86 | System.out.println("invalid username/password was specified or communication problem or "); | ||
87 | System.out.println(e); | ||
88 | } | ||
89 | } | ||
90 | } | ||
91 | {{/code}} | ||
92 | |||
93 | = Retrieve Page By Major & Minor Version = | ||
94 | |||
95 | Carefully observe the rpc.getPage() method in this example. The getPage() is an overloaded method and in this case accepts the fully qualified page name followed by the major version followed by the minor version. | ||
96 | |||
97 | {{code language="java"}} | ||
98 | import java.net.MalformedURLException; | ||
99 | import org.apache.xmlrpc.XmlRpcException; | ||
100 | import org.xwiki.xmlrpc.XWikiXmlRpcClient; | ||
101 | import org.xwiki.xmlrpc.model.XWikiPage; | ||
102 | |||
103 | public class RetrieveByName | ||
104 | { | ||
105 | public static void main(String[] args) throws MalformedURLException{ | ||
106 | |||
107 | //URL of the xwiki instance | ||
108 | String url="http://localhost:8080/xwiki/xmlrpc/confluence"; | ||
109 | |||
110 | //Replace user & pass with desired xwiki username & password | ||
111 | String user="Admin"; | ||
112 | String pass="admin"; | ||
113 | |||
114 | //Perform Login & Authentication using above url address | ||
115 | try{ | ||
116 | XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url); | ||
117 | rpc.login(user, pass); | ||
118 | |||
119 | //Pass the fully qualified name eg. Main.WebHome | ||
120 | XWikiPage page= rpc.getPage("Main.WebHome",3,1); | ||
121 | |||
122 | //Display the URL of the page at Main.WebHome | ||
123 | System.out.println(page.getContent()); | ||
124 | |||
125 | rpc.logout(); | ||
126 | } | ||
127 | catch(XmlRpcException e){ | ||
128 | System.out.println("invalid username/password was specified or communication problem or "); | ||
129 | System.out.println(e); | ||
130 | } | ||
131 | } | ||
132 | } | ||
133 | {{/code}} | ||
134 | |||
135 | = Retrieve Page By Language = | ||
136 | |||
137 | In a multilingual wiki you can lookup a page by it's language. The same overloaded method rpc.getPage() is called here. Note the "de" in rpc.getPage(). "de" is used to find the German Version of this page. You could use any of the 21 languages supported by XWiki as listed [[here>>http://platform.xwiki.org/xwiki/bin/view/Features/I18N]] provided you've turned on multilingual support for your XWiki instance and also created pages in a particular language. | ||
138 | |||
139 | {{code language="java"}} | ||
140 | package search; | ||
141 | |||
142 | import java.net.MalformedURLException; | ||
143 | import org.apache.xmlrpc.XmlRpcException; | ||
144 | import org.xwiki.xmlrpc.XWikiXmlRpcClient; | ||
145 | import org.xwiki.xmlrpc.model.XWikiPage; | ||
146 | |||
147 | public class RetrieveByName | ||
148 | { | ||
149 | public static void main(String[] args) throws MalformedURLException{ | ||
150 | |||
151 | //URL of the xwiki instance | ||
152 | String url="http://localhost:8080/xwiki/xmlrpc/confluence"; | ||
153 | |||
154 | //Replace user & pass with desired xwiki username & password | ||
155 | String user="Admin"; | ||
156 | String pass="admin"; | ||
157 | |||
158 | //Perform Login & Authentication using above url address | ||
159 | try{ | ||
160 | XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url); | ||
161 | rpc.login(user, pass); | ||
162 | |||
163 | //Pass the fully qualified name eg. Main.WebHome | ||
164 | XWikiPage page= rpc.getPage("Main.WebHome","de"); | ||
165 | |||
166 | //Display the URL of the page at Main.WebHome | ||
167 | System.out.println(page.getContent()); | ||
168 | |||
169 | rpc.logout(); | ||
170 | } | ||
171 | catch(XmlRpcException e){ | ||
172 | System.out.println("invalid username/password was specified or communication problem or "); | ||
173 | System.out.println(e); | ||
174 | } | ||
175 | } | ||
176 | } | ||
177 | {{/code}} | ||
178 | |||
179 | = Retrieve All Pages For A Space = | ||
180 | |||
181 | Let's move on to build a small application that would help you list all the pages in your wiki instance. Let's say, you want to build an expandable tree in your java application. The tree node would be the name of the space. Once, you click over it, it would expand to list all the pages in that space. One of the best examples for this would be [[the XEclipse tool>>Extension.XWiki Eclipse]]. Observe the windows explorer/tree style navigation in the various screenshots. | ||
182 | |||
183 | We make use of the rpc.getPages() method in our example. The input is the space name (in our example it is the space "Main"). | ||
184 | |||
185 | {{code language="java"}} | ||
186 | package search; | ||
187 | |||
188 | |||
189 | import java.net.MalformedURLException; | ||
190 | import java.util.List; | ||
191 | import org.apache.xmlrpc.XmlRpcException; | ||
192 | import org.xwiki.xmlrpc.XWikiXmlRpcClient; | ||
193 | import org.xwiki.xmlrpc.model.XWikiPageSummary; | ||
194 | |||
195 | public class PagesInSpace { | ||
196 | |||
197 | public static void main(String[] args) throws MalformedURLException { | ||
198 | |||
199 | //URL of the xwiki instance | ||
200 | String url = "http://localhost:8080/xwiki/xmlrpc/confluence"; | ||
201 | |||
202 | //Replace user & pass with desired xwiki username & password | ||
203 | String user = "Admin"; | ||
204 | String pass = "admin"; | ||
205 | |||
206 | //Perform Login & Authentication using above url address | ||
207 | try { | ||
208 | XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url); | ||
209 | rpc.login(user, pass); | ||
210 | |||
211 | //Pass the "space" name to the getPages method below | ||
212 | List<XWikiPageSummary> result = rpc.getPages("Main"); | ||
213 | |||
214 | //Print name of the Page, Title & absolute URL for the Pages | ||
215 | for (XWikiPageSummary rs : result) { | ||
216 | |||
217 | System.out.println("---" + rs.getId() + "---"); | ||
218 | System.out.println("---" + rs.getTitle() + "---"); | ||
219 | System.out.println("---"+rs.getUrl()+"---"); | ||
220 | |||
221 | } | ||
222 | |||
223 | } catch (XmlRpcException e) { | ||
224 | System.out.println("invalid username/password was specified or communication problem."); | ||
225 | System.out.println(e.getCause()); | ||
226 | } | ||
227 | } | ||
228 | } | ||
229 | |||
230 | {{/code}} | ||
231 | |||
232 | = Search Page In Wiki = | ||
233 | |||
234 | This API could be called a "Search" in the true sense. This is far more than just a simple Page retrieval. We are required to pass keywords for search to the rpc.search() method along with the number of results we want to be returned. | ||
235 | |||
236 | {{info}} | ||
237 | As of XWiki 2.1.1, the results obtained by changing keyword positions will vary. For instance, the results for "a new day" would be different from the results for "day a new". If you would only like to implement the search feature & wish to get more accurate results, you could use [[the XWiki RESTful services>>xwiki:Documentation.UserGuide.Features.XWikiRESTfulAPI]] (especially, the tag search). However, this would require that all your searchable documents be tagged appropriately in the XWiki instance. | ||
238 | {{/info}} | ||
239 | |||
240 | {{info}} | ||
241 | The results obtained are based on the rights & privileges granted to the user whose credential you are passing in the rpc.login() method. | ||
242 | {{/info}} | ||
243 | |||
244 | {{code language="java"}} | ||
245 | |||
246 | import java.net.MalformedURLException; | ||
247 | import java.util.List; | ||
248 | import org.apache.xmlrpc.XmlRpcException; | ||
249 | import org.codehaus.swizzle.confluence.SearchResult; | ||
250 | import org.xwiki.xmlrpc.XWikiXmlRpcClient; | ||
251 | |||
252 | public class XWikiXMLRPCSearch { | ||
253 | |||
254 | public static void main(String[] args) throws MalformedURLException, XmlRpcException { | ||
255 | //Replace the url with your xwiki server address | ||
256 | String url = "http://localhost:8080/xwiki/xmlrpc/confluence"; | ||
257 | |||
258 | //Replace user & pass with desired xwiki username & password | ||
259 | String user = "Admin"; | ||
260 | String pass = "admin"; | ||
261 | |||
262 | //Perform Login & Authentication using above url address | ||
263 | XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url); | ||
264 | rpc.login(user, pass); | ||
265 | |||
266 | //Replace string to search in the rpc.search() method below | ||
267 | //Specify the number of results to be returned in rpc.search() as 5 | ||
268 | List<SearchResult> result = rpc.search("text I want to search", 5); | ||
269 | |||
270 | |||
271 | //Print obtained results to console if result is not empty | ||
272 | if (result.size() != 0) { | ||
273 | for (int i = 0; i < result.size(); i++) { | ||
274 | System.out.println(result.get(i).getTitle()); | ||
275 | } | ||
276 | } //Print standard message if result is empty | ||
277 | else { | ||
278 | System.out.println("No Results To Display"); | ||
279 | } | ||
280 | } | ||
281 | } | ||
282 | |||
283 | {{/code}} |