In this tutorial we are going to build a SOAP client for the popular Confluence Wiki in about five minutes. The client is going to receive rendered HTML Markup from a specified Confluence Page.
Prerequisites
-
A running Confluence Installation with SOAP API enabled – if you don’t already have one take a look at this article or if you’ve got the Atlassian Plugin SDK installed .. start a standalone instance using atlas-run-standalone ..
-
Maven – never go without it ;)
-
Five minutes of your life time ..
Project Setup / Dependencies
-
Create a new Maven project:
mvn archetype:generate ...
-
Edit the pom.xml and add the dependencies for the axistools plugin – I’m using an older version of axis here because of some Confluence SOAP dialect in some versions ;)
<dependencies> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>javax.xml</groupId> <artifactId>jaxrpc-api</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery</artifactId> <version>0.4</version> </dependency> <dependency> <groupId>javax.xml.soap</groupId> <artifactId>saaj-api</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>axis</groupId> <artifactId>axis-wsdl4j</artifactId> <version>1.5.1</version> </dependency> </dependencies>
Service Stub and SOAP Client
Now that we’ve got everything we need, we’re ready to create our soap client.
-
Start your Confluence instance, ensure it is running ..
-
Change to the Confluence administration console, click “General Configuration” > “Feature Settings” > “Remote API (XML-RPC & SOAP)” and activate this feature
-
Now you should be able to view the WSDL for Confluence – point your browser to: http://localhost:8080/rpc/soap-axis/confluenceservice-v1?wsdl (newer Confluence versions use: http://localhost:8080/rpc/soap-axis/confluenceservice-v2?wsdl)
-
Create a directory named wsdl in src/main in the project directory
-
Save the wsdl file in the created wsdl directory – the axistool plugin searches for wsdl files at this location
-
Run the following command to create the skeleton files:
mvn axistools:wsdl2java
-
Now we got all we need in target/generated-classes
-
We create a new package named com.hascode.confluence.rpc
-
In this____package we create a new class named PageReader:
package com.hascode.confluence.rpc; import javax.xml.rpc.ServiceException; import localhost.confluence.rpc.soap_axis.confluenceservice_v2.ConfluenceSoapService; import localhost.confluence.rpc.soap_axis.confluenceservice_v2.ConfluenceSoapServiceServiceLocator; import com.atlassian.confluence.rpc.AuthenticationFailedException; import com.atlassian.confluence.rpc.RemoteException; import com.atlassian.confluence.rpc.soap.beans.RemotePage; public class PageReader { public static void main(final String[] args) throws AuthenticationFailedException, RemoteException, java.rmi.RemoteException, ServiceException { final ConfluenceSoapService service; ConfluenceSoapServiceServiceLocator serviceLocator = new ConfluenceSoapServiceServiceLocator(); service = serviceLocator.getConfluenceserviceV2(); // insert your account data here String token = service.login("theuser", "thepassword"); // we are going to fetch a page from the pre-installed Demonstration // Space RemotePage page = service.getPage(token, "ds", "Tutorial"); String renderedOutput = service.renderContent(token, "ds", page.getId(), page.getContent()); System.out.println(renderedOutput); } }
-
Now run the application and enjoy the rendered HTML content of the page “Tutorial” in the “Demonstration Space” ;)
Tutorial Sources
Please feel free to download the tutorial sources from my GitHub repository, fork it there or clone it using Git:
git clone https://github.com/hascode/confluence-soap-client-tutorial.git
Alternatives
There’s also a very nice Maven plugin using wsimport if you dont’t like the axis stuff … in this case, take a look at the jaxws-maven-plugin – there’s also a nice blog article from the Sun guys regarding this plugin.
*update*I just wrote an article about the jaxws-maven plugin – enjoy ! :)
update If you want to know how to write a SOAP component plugin for Confluence – take a look at this article of mine