Groovy Scripts
Use a scripting App to interact with Compliance for Confluence.
Get the Classification Level of a page
Get the current classification of a page from it’s Page ID:
import com.atlassian.confluence.api.service.content.ContentPropertyService
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.api.model.content.Content;
import com.atlassian.confluence.api.model.content.JsonContentProperty;
import com.atlassian.confluence.api.model.content.id.ContentId;
import com.atlassian.confluence.api.service.content.ContentPropertyService;
import com.atlassian.confluence.api.service.content.ContentService;
import com.atlassian.confluence.json.json.JsonString;
import com.atlassian.confluence.api.model.Expansion;
import groovy.json.JsonSlurper
ContentPropertyService contentPropertyService = ComponentLocator.getComponent(ContentPropertyService);
try {
// Create a Finder
ContentPropertyService.ContentPropertyFinder finder = contentPropertyService.find(new Expansion("metadata"));
// Find the Pages Classification Property
JsonContentProperty property = finder
.withContentId(ContentId.of(65611L)) // The Page ID
.withPropertyKey("classification") // The fixed Content Property Name
.fetchOrNull(); // If there isn't one, return null.
if(property != null) {
// We have a valid property
String propertyAsString = property.getValue().getValue();
// Convert to an object
JsonSlurper slurper = new JsonSlurper();
def propertyAsJson = slurper.parseText(propertyAsString);
def index = propertyAsJson["name"]["index"];
// We can do something useful with the level index here.
// 0 is the first level, 1 is the second level and so on...
if(index == 0) {
return "Level is Highly Restricted (according to the default levels)";
} else if(index == 1) {
return "Level is Restricted (according to the default levels)";
} else if(index == 2) {
return "Level is Internal (according to the default levels)";
} else if(index == 3) {
return "Level is Public (according to the default levels)";
}
} else {
// Page is not classified yet
return;
}
} catch(Exception e) {
// The script failed for some reason
return e.getMessage();
}