Trying to use conflunce spaces api but getting a compilation error
Asked by Ibrahiem Mohammad:

Hi,

I'm writing a REST API method to get the space permissions of a given space. I did some research and found the com.atlassian.confluence.spaces package : <https://docs.atlassian.com/ConfluenceServer/javadoc/6.12.2/>

I want to use this to retrieve a list of space permissions based on the space key. This is what I have so far:

Java File

```
package com.herzum.confluence.subnavigationmenu.rest;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
import com.atlassian.confluence.user.ConfluenceUser;
import com.atlassian.confluence.user.UserAccessor;
import com.atlassian.confluence.user.persistence.dao.compatibility.FindUserHelper;
import com.atlassian.sal.api.component.ComponentLocator;
import com.atlassian.user.EntityException;
import com.atlassian.user.User;
import com.atlassian.user.UserManager;
import com.atlassian.confluence.spaces;

@Path("/api")
public class SpacePermissionsResource {
 
 private static final Logger log = LoggerFactory.getLogger(SpacePermissionsResource.class);
 
 @Inject
 private UserManager userManager;
 
 @Inject
 private UserAccessor userAccessor;

 @Inject
 private Space space;

 public SpacePermissionsResource() {}

 @GET
 @Path("/getSpacePermissions")
 @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 public Response getSpacePermissions(@QueryParam("spaceKey") String spaceKey) throws EntityException {

 log.debug("[getSpacePermissions] - Start Method");

 space = new Space(spaceKey);
 String name = space.getName();

 return Response.ok(name).build();

 }

}
```

POM

```
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

 <modelVersion>4.0.0</modelVersion>
 <groupId>com.herzum.confluence.macro</groupId>
 <artifactId>subnavigation-menu</artifactId>
 <version>1.0.53</version>

 <organization>
 <name>Herzum S.R.L.</name>
 <url>http://www.herzum.com/</url>
 </organization>

 <name>SubNavigationMenu Macro</name>
 <description>This is the Subnavigation Menu plugin for Atlassian Confluence.</description>
 <packaging>atlassian-plugin</packaging>

 <dependencies>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.10</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>com.atlassian.confluence</groupId>
 <artifactId>confluence</artifactId>
 <version>${confluence.version}</version>
 <scope>provided</scope>
 </dependency>

 <dependency>
 <groupId>com.atlassian.plugin</groupId>
 <artifactId>atlassian-spring-scanner-annotation</artifactId>
 <version>${atlassian.spring.scanner.version}</version>
 <scope>compile</scope>
 </dependency>

 <dependency>
 <groupId>com.atlassian.plugin</groupId>
 <artifactId>atlassian-spring-scanner-runtime</artifactId>
 <version>${atlassian.spring.scanner.version}</version>
 <scope>runtime</scope>
 </dependency>
 <dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>servlet-api</artifactId>
 <version>2.4</version>
 <scope>provided</scope>
 </dependency>

 <dependency>
 <groupId>javax.inject</groupId>
 <artifactId>javax.inject</artifactId>
 <version>1</version>
 <scope>provided</scope>
 </dependency>

 <!-- WIRED TEST RUNNER DEPENDENCIES -->
 <dependency>
 <groupId>com.atlassian.plugins</groupId>
 <artifactId>atlassian-plugins-osgi-testrunner</artifactId>
 <version>${plugin.testrunner.version}</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>javax.ws.rs</groupId>
 <artifactId>jsr311-api</artifactId>
 <version>1.1.1</version>
 <scope>provided</scope>
 </dependency>
 <dependency>
 <groupId>com.google.code.gson</groupId>
 <artifactId>gson</artifactId>
 <version>2.2.2-atlassian-1</version>
 </dependency>
 </dependencies>

 <build>
 <plugins>
 <plugin>
 <groupId>com.atlassian.maven.plugins</groupId>
 <artifactId>maven-confluence-plugin</artifactId>
 <version>${amps.version}</version>
 <extensions>true</extensions>
 <configuration>
 <productVersion>${confluence.version}</productVersion>
 <productDataVersion>${confluence.data.version}</productDataVersion>
 <enableQuickReload>true</enableQuickReload>
 <enableFastdev>false</enableFastdev>

 <!-- See here for an explanation of default instructions: -->
 <!-- https://developer.atlassian.com/docs/advanced-topics/configuration-of-instructions-in-atlassian-plugins -->
 <instructions>
 <Atlassian-Plugin-Key>${atlassian.plugin.key}</Atlassian-Plugin-Key>

 <!-- Add package to export here -->
 <Export-Package>
 com.atlassian.tutorial.api,
 </Export-Package>

 <!-- Add package import here -->
 <Import-Package>
 org.springframework.osgi.*;resolution:="optional",
 org.eclipse.gemini.blueprint.*;resolution:="optional",
 *
 </Import-Package>

 <!-- Ensure plugin is spring powered -->
 <Spring-Context>*</Spring-Context>
 </instructions>
 </configuration>
 </plugin>

 <plugin>
 <groupId>com.atlassian.plugin</groupId>
 <artifactId>atlassian-spring-scanner-maven-plugin</artifactId>
 <version>${atlassian.spring.scanner.version}</version>
 <executions>
 <execution>
 <goals>
 <goal>atlassian-spring-scanner</goal>
 </goals>
 <phase>process-classes</phase>
 </execution>
 </executions>
 <configuration>
 <scannedDependencies>
 <dependency>
 <groupId>com.atlassian.plugin</groupId>
 <artifactId>atlassian-spring-scanner-external-jar</artifactId>
 </dependency>
 </scannedDependencies>
 <verbose>false</verbose>
 </configuration>
 </plugin>
 </plugins>
 </build>

 <properties>
 <confluence.version>6.7.1</confluence.version>
 <confluence.data.version>6.7.1</confluence.data.version>
 <amps.version>6.3.15</amps.version>
 <plugin.testrunner.version>1.2.3</plugin.testrunner.version>
 <atlassian.spring.scanner.version>1.2.13</atlassian.spring.scanner.version>
 <!-- This key is used to keep the consistency between the key in atlassian-plugin.xml 
 and the key to generate bundle. -->
 <atlassian.plugin.key>${project.groupId}.${project.artifactId}</atlassian.plugin.key>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <maven.compiler.source>1.8</maven.compiler.source>
 <maven.compiler.target>1.8</maven.compiler.target>
 </properties>

</project>
```

This is the error I am getting:

\[INFO\] BUILD FAILURE  
\[INFO\] ------------------------------------------------------------------------  
\[INFO\] Total time: 20.255 s  
\[INFO\] Finished at: 2018-11-27T11:45:09-06:00  
\[INFO\] Final Memory: 71M/335M  
\[INFO\] ------------------------------------------------------------------------  
\[ERROR\] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0-jboss-1:compile (default-compile) on project subnavigation-menu: Compilation failure: Compilation failure:  
\[ERROR\] /C:/Users/imohamma/FXMacros/subnavgiation-menu-dev/src/main/java/com/herzum/confluence/subnavigationmenu/rest/SpacePermissionsResource.java:\[27,32\] cannot find symbol  
\[ERROR\] symbol: class spaces  
\[ERROR\] location: package com.atlassian.confluence  
\[ERROR\] /C:/Users/imohamma/FXMacros/subnavgiation-menu-dev/src/main/java/com/herzum/confluence/subnavigationmenu/rest/SpacePermissionsResource.java:\[41,17\] cannot find symbol  
\[ERROR\] symbol: class Space  
\[ERROR\] location: class com.herzum.confluence.subnavigationmenu.rest.SpacePermissionsResource

Can someone please tell if I am missing a package or doing something incorrectly in the java code?


Answers:
author: Santwana Sarangi {Appfire}
body: Hi [@Ibrahiem Mohammad](/t5/user/viewprofilepage/user-id/1710032),

To fetch the space using spaceKey, you can use the SpaceManager class. Please find the docs below

<https://docs.atlassian.com/atlassian-confluence/6.6.0/index.html?com/atlassian/confluence/spaces/SpaceManager.html>

Inject the SpaceManager class in your class and use getSpace(String spacekey) method to fetch the respective space, like this

```
@Path("/api")
public class SpacePermissionsResource {
 
 private static final Logger log = LoggerFactory.getLogger(SpacePermissionsResource.class);
 
 @Inject
 private UserManager userManager;
 
 @Inject
 private UserAccessor userAccessor;

 @Inject
 private SpaceManager spaceManager;

 public SpacePermissionsResource() {}

 @GET
 @Path("/getSpacePermissions")
 @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 public Response getSpacePermissions(@QueryParam("spaceKey") String spaceKey) throws EntityException {

 log.debug("[getSpacePermissions] - Start Method");

 Space space = spaceManager.getSpace(spaceKey);
 String name = space.getName();

 return Response.ok(name).build();

 }

}
```

Hope this helps!

Thanks,

Santwana


Tags: app

confluence-app

confluence-development

confluence-server

maven

server