Call Confluence REST API from within Java plugin

Hi,

I’m working on a plugin which I ultimately want to release for both server and cloud. Additionally, one of the features I require is not yet stable in Confluence server and all of the classes are part of the experimental Java API. For that reason, I am considering accessing it not directly through Java but through its REST API. Given that I’ll have to use REST when developing the cloud addon either way, this would allow me to reuse a lot of my code.

I am able to send generic requests using Jersey, but I’m wondering if there is some sane way to call the REST API from within the server whilst using the permissions of a given client. In theory, my plugin will react to events raised by updating pages. It will then do some magic and call one of the native REST APIs of Confluence, but when doing so it needs to impersonate the user, such that permissions are guaranteed to be in check.

I’d appreciate any help.
Best regards,
Christopher

Are you able through javascript or java?

I’ve never heard anyone instatiating a REST class in order to call the class’ methods, if that’s what you mean. You can of course using the jersey client to call the REST methods

Hi,

I am thinking of doing something similar within our instance of confluence ( to access remote external API within our network), how did you end up implementing your solution ?

Hello,
first of all add the required dependencies in your pom.xml:

   <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-client</artifactId>
      <version>1.8-atlassian-11</version>
   </dependency>
   <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-json</artifactId>
      <version>1.8-atlassian-11</version>
   </dependency>
   <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-core</artifactId>
      <version>1.8-atlassian-11</version>
   </dependency>

I know for fact that these versions work. Feel free to play with versions.
Then, in your java classes, you can do something like

Client client=Client.create();
WebResource webResource = client.resource("URL");
ClientResponse response = webResource.accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
if (response.getStatus() == 200) {
//Do something with the response.getEntity(YourClass.class);
}
1 Like