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.
Community moderators have prevented the ability to post new answers.
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 */
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)"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You need to get the SpaceManager through dependency injection.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.