XML-RPC Java Examples
Requirements
The following libraries should be added to your application classpath:
Library | Source |
---|---|
commons-logging-1.1.1.jar | WEB-INF/lib folder of XWiki 2.1.1 |
ws-commons-util-1.0.2.jar | WEB-INF/lib folder of XWiki 2.1.1 |
xmlrpc-common-3.1.jar | WEB-INF/lib folder of XWiki 2.1.1 |
xmlrpc-client-3.1.jar | WEB-INF/lib folder of XWiki 2.1.1 |
xwiki-core-2.1.1.jar | WEB-INF/lib folder of XWiki 2.1.1 |
swizzle-confluence-1.2-20080419-xwiki.jar | WEB-INF/lib folder of XWiki 2.1.1 |
xwiki-core-xmlrpc-client-2.1.1.jar | For XWiki 2.1.1 the jar is available here |
xwiki-core-xmlrpc-model-2.1.1.jar | WEB-INF/lib folder of XWiki 2.1.1 |
Authentication: Login Example
Try Providing an incorrect username & password. The application will throw an exception
For correct username & password, the application will compile & run successfully.
import org.apache.xmlrpc.XmlRpcException;
import org.xwiki.xmlrpc.XWikiXmlRpcClient;
public class XmlRpcLogin {
public static void main(String[] args) throws MalformedURLException{
//URL of the xwiki instance
String url="http://localhost:8080/xwiki/xmlrpc/confluence";
//Replace user & pass with desired xwiki username & password
String user="Admin";
String pass="admin";
//Perform Login & Authentication using above url address
try{
XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
rpc.login(user, pass);
}
catch(XmlRpcException e){
System.out.println("invalid username/password was specified or communication problem");
}
}
}
Authentication: Logout Example
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.
import java.net.MalformedURLException;
import org.apache.xmlrpc.XmlRpcException;
import org.xwiki.xmlrpc.XWikiXmlRpcClient;
public class XmlRpcLogout {
public static void main(String[] args) throws MalformedURLException{
//Variable to test whether user logged out successfully. Default set to false
boolean loggedOut=false;
//URL of the xwiki instance
String url="http://localhost:8080/xwiki/xmlrpc/confluence";
//Replace user & pass with desired xwiki username & password
String user="Admin";
String pass="admin";
try{
//Perform Login & Authentication using above url address
XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
rpc.login(user, pass);
//Log out action. Returns true if user successfully logged out
loggedOut=rpc.logout();
System.out.println(loggedOut);
}
catch(XmlRpcException e){
System.out.println("invalid username/password was specified or communication problem");
}
}
}
Space: Get A List Of Spaces
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.
import java.net.MalformedURLException;
import java.util.List;
import org.apache.xmlrpc.XmlRpcException;
import org.codehaus.swizzle.confluence.SpaceSummary;
import org.xwiki.xmlrpc.XWikiXmlRpcClient;
public class SpaceList {
public static void main(String[] args) throws MalformedURLException {
//URL of the xwiki instance
String url="http://localhost:8080/xwiki/xmlrpc/confluence";
//Replace user & pass with desired xwiki username & password
String user="Admin";
String pass="admin";
//Perform Login & Authentication using above url address
try{
XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
rpc.login(user, pass);
List<SpaceSummary> spaceList = rpc.getSpaces();
System.out.println("Total Number of Spaces: " +spaceList.size());
for(int i=0;i<spaceList.size();i++){
System.out.println(spaceList.get(i).getKey());
}
rpc.logout();
}
catch(XmlRpcException e){
System.out.println("invalid username/password was specified or communication problem");
}
}
}
Space: Create A Space
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
import org.apache.xmlrpc.XmlRpcException;
import org.codehaus.swizzle.confluence.Space;
import org.xwiki.xmlrpc.XWikiXmlRpcClient;
public class CreateSpace {
public static void main(String[] args) throws MalformedURLException {
//URL of the xwiki instance
String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
//Replace user & pass with desired xwiki username & password
String user = "Admin";
String pass = "admin";
//Perform Login & Authentication using above url address
try {
XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
rpc.login(user, pass);
//Create a Space object which holds the Key, Homepage, Name & description.
//Key is visible name for the space
Space space = new Space();
space.setKey("XMLRPC");
space.setHomepage("xmlrpc.WebHome");
space.setName("xmlrpc");
space.setDescription("Demo Space Created To Test XMLRPC");
//One simple method adds the space created to your xwiki instance
rpc.addSpace(space);
rpc.logout();
} catch (XmlRpcException e) {
System.out.println("invalid username/password was specified or communication problem");
}
}
}
Space: Delete A Space
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.
import java.net.MalformedURLException;
import org.apache.xmlrpc.XmlRpcException;
import org.codehaus.swizzle.confluence.Space;
import org.xwiki.xmlrpc.XWikiXmlRpcClient;
public class DeleteSpace {
public static void main(String[] args) throws MalformedURLException {
//URL of the xwiki instance
String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
//Replace user & pass with desired xwiki username & password
String user = "Admin";
String pass = "admin";
//Perform Login & Authentication using above url address
try {
XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
rpc.login(user, pass);
//Delete the space which is reference by "XMLRPC" key
rpc.removeSpace("XMLRPC");
rpc.logout();
} catch (XmlRpcException e) {
System.out.println("invalid username/password was specified or communication problem");
}
}
}
Page: Retrieve/Search A Page
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.
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.
Since, this topic is an important one, all the page retrieval/search examples can be found here
Page: Create A Page
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.
At the very least, you would require the following three parameters to create a Page:
- Space - The Space where the Page is to be stored
- Title - The title for the Page
- Content - The content to be displayed inside the Page
In our example below, we would use
Parameter | Value |
---|---|
Space | demo code |
title | New Page |
Content | New Page Created |
import org.apache.xmlrpc.XmlRpcException;
import org.codehaus.swizzle.confluence.Page;
import org.xwiki.xmlrpc.XWikiXmlRpcClient;
public class CreatePage {
public static void main(String[] args) throws MalformedURLException, XmlRpcException {
//URL of the xwiki instance
String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
//Replace user & pass with desired xwiki username & password
String user = "Admin";
String pass = "admin";
XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
try {
//Perform Login & Authentication
rpc.login(user, pass);
//Create a Page object & set it's three important attributes viz. Space, Title, Content
//Observe how the \\\\ has been used to create a new line in the final wiki Page
//Also, XWiki syntax can be passed as it is. Here, we passed the info macro
//The info macro would get rendered an info box in the Page
Page page = new Page();
page.setSpace("demo code");
page.setTitle("New Page");
page.setContent("New Page Created \\\\ {{info}}This is XMLRPC Test{{/info}}");
//Also set the parent Page to "demo code.WebHome" so that the "New Page" we created is not
//an orphan Page
page.setParentId("demo code.WebHome");
//Store the page object into XWiki
rpc.storePage(page);
} catch (XmlRpcException e) {
System.out.println("invalid username/password was specified or communication problem or ");
System.out.println(e);
} finally {
rpc.logout();
}
}
}
Page: Get Page History
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.
import java.util.List;
import org.apache.xmlrpc.XmlRpcException;
import org.xwiki.xmlrpc.XWikiXmlRpcClient;
import org.xwiki.xmlrpc.model.XWikiPageHistorySummary;
public class PageHistory {
public static void main(String[] args) throws MalformedURLException, XmlRpcException {
//URL of the xwiki instance
String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
//Replace user & pass with desired xwiki username & password
String user = "Admin";
String pass = "admin";
XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
try {
//Perform Login & Authentication
rpc.login(user, pass);
//Create a XWikiPageHistorySummary object to hold all the revisions/history
//of the Main.WebHome Page
List<XWikiPageHistorySummary> hist = rpc.getPageHistory("Main.WebHome");
//Iterate through all the available versions for Main.WebHome
for(XWikiPageHistorySummary xphs:hist){
//Print the fully qualified name of the Page i.e. SpaceName.PageName
//In our example this would be Main.WebHome
System.out.println(xphs.getBasePageId());
//Print the historical page ID or name of the Main.WebHome page
//This would print something like:
//Main.WebHome?minorVersion=1&language=&version=2
//It means page=Main.WebHome, version=2, minor version=1 & language=default
System.out.println(xphs.getId());
//Printing the version of Main.WebHome in the majorversion.minorversion format
System.out.println("Version: "+xphs.getVersion()+"."+xphs.getMinorVersion());
//Date when the Page was last modified
System.out.println(xphs.getModified());
//User who last modified the Page
System.out.println(xphs.getModifier());
//Just a seperator between various versions of the document when printing
//to console
System.out.println("------------------------------");
}
} catch (XmlRpcException e) {
System.out.println("invalid username/password was specified or communication problem or ");
System.out.println(e);
} finally {
rpc.logout();
}
}
}
Page: Update A Page
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.
import java.net.MalformedURLException;
import org.apache.xmlrpc.XmlRpcException;
import org.codehaus.swizzle.confluence.Page;
import org.xwiki.xmlrpc.XWikiXmlRpcClient;
public class UpdatePage {
public static void main(String[] args) throws MalformedURLException, XmlRpcException {
//URL of the xwiki instance
String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
//Replace user & pass with desired xwiki username & password
String user = "Admin";
String pass = "admin";
XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
try {
//Perform Login & Authentication
rpc.login(user, pass);
//Create a Page object to hold our Document information
Page page = new Page();
//Fetch the required page. In our example, the page is in Space "demo code"
//and the Page is "Update Page"
page=rpc.getPage("demo code.Update Page");
//Fetch the content of the page & store it in a string for temporary storage
//This is the present content of the Page
String presentContent=page.getContent();
//Create a string that will hold the new content that is to be added to the Page
String newContent="\\\\Some new content added";
//Set the content of the page as: present content + new content
//However, this page is not yet stored to XWiki. It only resides in your application
page.setContent(presentContent+newContent);
//Finally, store the "updated" Page to XWiki
rpc.storePage(page);
//Just to make sure everything saved all right, fetch the content again for the Page
System.out.println(page.getContent());
} catch (XmlRpcException e) {
System.out.println("invalid username/password was specified or communication problem or ");
System.out.println(e);
} finally {
rpc.logout();
}
}
}