Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to check if space exist ? Java / Confluence

R K February 12, 2013

Hello,

I'm developing my first plugin for Confluence (all in Java). Purpose is to accept REST connections from LDAP, create users and permit them access to spaces.

For that I want to check if a space exists (before granting access to it). How may I do that?

Following the API documentation I created the following code, however any method executed on DefaultSpaceManager fails with a NullPpointerException. Is there anything I'm missing to initialise? Or what else could be wrong?

 

import com.atlassian.confluence.spaces.DefaultSpaceManager;
import com.atlassian.confluence.spaces.Space;
import com.atlassian.confluence.spaces.SpaceManager;
SpaceManager rktest = new DefaultSpaceManager();
List<Space> spaces = rktest.getAllSpaces();

EDIT: That code gets executed without prior login to Confluence - if that must happen prior to trying to access spaces please advise.

 

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

4 votes
Answer accepted
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2013

Why don't you use com.atlassian.confluence.spaces.SpaceManager#getSpace? From the javadoc:

/**
     * Retrieve a space by its key. The space can be of any type.
     *
     * @param spaceKey the space key of the space to retrieve
     * @return the space, or null if no space exists with that key
     */

R K February 12, 2013
SpaceManager rktest2 = new SpaceManager();
rktest2.getSpace(name);

build fails with "error: SpaceManager is abstract; cannot be instantiated"

...and

SpaceManager rktest = new DefaultSpaceManager();
String x = rktest.getSpace("rktest").getName();

fails when called, error message is: "java.lang.NullPointerException at com.atlassian.confluence.spaces.DefaultSpaceManager.getSpace(DefaultSpaceManager.java:117)"

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2013

You need to get the SpaceManager through dependency injection.

R K February 13, 2013

Thanks for the hint. Added that using the following code snippet at the beginning of the class:

private SpaceManager spaceManager;
	
public void setSpaceManager(SpaceManager spaceManager) {
	this.spaceManager = spaceManager;
}

Now I'm able to use the following method insides the class, which meets my initial requirement: compare the name of a spaceName with a list of spaces and their names, to see if spaceName is among the list entries:

private boolean spaceExists(String spaceName) {
	for (Space s : spaceManager.getAllSpaces()) {
		if (s.getName().equals(spaceName))
			return true;
	}		
	return false;
}

New people of the future, I cordially greet you and hope my posting this hard-earned code snippet helps you save time in your efforts to make our world a better place.

Ruth_Kusterer October 16, 2014

Thanks for sharing this solution, R.K.

0 votes
R K February 12, 2013

Have further attempted:

List<Space> permittedSpaces = rktest.getAllSpaces(SpacesQuery.newQuery().forUser(new DefaultUser("adminusername")).build());

gives this exception:

java.lang.NullPointerException
at com.atlassian.confluence.spaces.DefaultSpaceManager$1.getElements(DefaultSpaceManager.java:654)
at com.atlassian.confluence.core.DefaultListBuilder.getPage(DefaultListBuilder.java:47)
at com.atlassian.confluence.spaces.DefaultSpaceManager.getAllSpaces(DefaultSpaceManager.java:665)

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 12, 2013

Key: a unique string that identifies the space. Immutable, cannot be changed, relied on throughout the system as a unique id

Name: a string that displays a label for the space, can be edited not only by system admins, but space admins.

0 votes
R K February 12, 2013

by the way - I attempted to extract a list of all spaces so I could loop over them and see if the name requested is among them .. if there is an easier way to do it, please advice - ideally I could to "Boolean SpaceManager.spaceExists(String name)" but according to the documentation there is no such thing ... and getSpace() accepts the key of a space, not the name ... I wonder what the difference between key and name might be?

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events