Authenticating using the Confluence Java API for import program

I need to write a program that will create pages in a target space, under a specific parent, based on existing template, filling in certain “fields” (e.g.: ${description}) based on data from a CSV file.

I am using confluence-rest-client version 5.10.9 even though I am communicating with version 6.15.

I cannot find any (or meaningful) examples of using the Java API. I’m not sure I’m even doing the authentication right. This is a CLI application to be used rarely, to convert old data into new Conf pages, so OAuth or other methods beyond URL, user name and password are overkill.

This is what I’m doing to try and “authenticate”:

AuthenticatedWebResourceProvider client = new AuthenticatedWebResourceProvider(RestClientFactory.newClient(),
getConfluenceCredential().getConfluenceURI(), null);

client.setAuthContext(getConfluenceCredential().getUserName(),
getConfluenceCredential().getUserPassword().toCharArray());

Executor executor = new ThreadPoolExecutor(1,5,1,TimeUnit.MINUTES, new ArrayBlockingQueue(2));

Which “execute” w/o exception.

Then, while looking up the target space:

promiseOptionSpace = spaceService.find()
.withKeys(spaceKey)
.withType(SpaceType.GLOBAL)
.fetchOne();
if ( promiseOptionSpace != null ) result = promiseOptionSpace.get().get();

At runtime I’m getting:

Caused by: java.lang.NullPointerException
at com.atlassian.confluence.rest.client.AbstractRemoteService.newRestWebResource(AbstractRemoteService.java:59)
at com.atlassian.confluence.rest.client.RemoteSpaceServiceImpl.newSpacesRestResource(RemoteSpaceServiceImpl.java:205)
at com.atlassian.confluence.rest.client.RemoteSpaceServiceImpl.newSpacesRestResource(RemoteSpaceServiceImpl.java:213)
at com.atlassian.confluence.rest.client.RemoteSpaceServiceImpl.access$100(RemoteSpaceServiceImpl.java:36)
at com.atlassian.confluence.rest.client.RemoteSpaceServiceImpl$RemoteSpaceFinderImpl.fetchOne(RemoteSpaceServiceImpl.java:141)
at gov.texas.dps.itd.ias.ws.atlassian.confluence.dao.SpaceDAOImpl.getByKey(SpaceDAOImpl.java:50)

What am I doing wrong or forgetting?

Are there no Java/REST API Examples?

Hi Arnei,

I would point out three things before we get into solving this exception:

  1. Can you please use newer version of confluence-rest-client, preferably the same version as in your confluence. The reason I am asking this is because 5.10.9 is very old and is totally different major version from what you are using. We can’t guarantee backward compatibility for the major version change.
  2. Please use AuthenticatedWebResourceProvider.createWithNewClient(baseUrl) for simplicity. I see that you are using (RestClientFactory.newClient(), getConfluenceCredential().getConfluenceURI(), null). The null can cause your baseUrl to be changed to baseUrl + null. Please use the AuthenticatedWebResourceProvider’s factory method or empty string for path.
  3. Are you sure the space you are trying to hist is global, not personal?

Please try these steps and confirm the new behaviour of your app.

Thanks
GG

Thanks, I didn’t see a version beyond 5.10.9 when I was browsing the Artifactory. I plugged in 6.15.6 into the pom and it downloaded. Yes, the space is Global, not Personal. My code now looks like:

try {
client = AuthenticatedWebResourceProvider.createWithNewClient(getConfluenceCredential().getConfluenceURI());

client.setAuthContext(getConfluenceCredential().getUserName(), 
	getConfluenceCredential().getUserPassword().toCharArray());

executor = new ThreadPoolExecutor(1,5,1,TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(2));

connected = true;

} catch(Exception e) {
getLogger().error(“There was a problem creating the ConfluenceRestClient:”);
getLogger().error(e.getMessage());
throw new ConfluenceException(e);
}`

The calls work w/o issue, connected gets set to true. Later on I then instantiate the service:

// client and executor are ancestor objects
spaceService = new RemoteSpaceServiceImpl(getClient(), (ListeningExecutorService) getExecutor());

and I get a class, but when it attempts to work, I still get the error below. Is there another object/class haven’t instantiated? Is the ArrayBlockingQueue appropriate?

Caused by: java.lang.NullPointerException
at com.atlassian.confluence.rest.client.AbstractRemoteService.newRestWebResource(AbstractRemoteService.java:59)
at com.atlassian.confluence.rest.client.RemoteSpaceServiceImpl.newSpacesRestResource(RemoteSpaceServiceImpl.java:217)
at com.atlassian.confluence.rest.client.RemoteSpaceServiceImpl.newSpacesRestResource(RemoteSpaceServiceImpl.java:225)
at com.atlassian.confluence.rest.client.RemoteSpaceServiceImpl.access$100(RemoteSpaceServiceImpl.java:38)
at com.atlassian.confluence.rest.client.RemoteSpaceServiceImpl$RemoteSpaceFinderImpl.fetchOne(RemoteSpaceServiceImpl.java:153)