From 8e95979729d8914d6738efe5011a0e3ca31b2cbc Mon Sep 17 00:00:00 2001 From: Rick Herrick <jrherrick@wustl.edu> Date: Sun, 14 Aug 2016 23:21:41 -0500 Subject: [PATCH] XNAT-4382 Added /plugins XAPI function to return plugin registry. --- .../rest/settings/XnatPluginAdminApi.java | 58 +++++++++++++++++++ .../org/nrg/xnat/services/XnatAppInfo.java | 27 +++++++-- 2 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/nrg/xapi/rest/settings/XnatPluginAdminApi.java diff --git a/src/main/java/org/nrg/xapi/rest/settings/XnatPluginAdminApi.java b/src/main/java/org/nrg/xapi/rest/settings/XnatPluginAdminApi.java new file mode 100644 index 00000000..bff17199 --- /dev/null +++ b/src/main/java/org/nrg/xapi/rest/settings/XnatPluginAdminApi.java @@ -0,0 +1,58 @@ +package org.nrg.xapi.rest.settings; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import org.nrg.framework.annotations.XapiRestController; +import org.nrg.xdat.rest.AbstractXapiRestController; +import org.nrg.xdat.security.services.RoleHolder; +import org.nrg.xdat.security.services.UserManagementServiceI; +import org.nrg.xnat.services.XnatAppInfo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import java.io.IOException; +import java.util.Map; +import java.util.Properties; + +@Api(description = "XNAT Plugin Admin API") +@XapiRestController +@RequestMapping(value = "/plugins") +public class XnatPluginAdminApi extends AbstractXapiRestController { + @Autowired + public XnatPluginAdminApi(final UserManagementServiceI userManagementService, final RoleHolder roleHolder, final XnatAppInfo appInfo) { + super(userManagementService, roleHolder); + _appInfo = appInfo; + } + + @ApiOperation(value = "Returns a list of all of the installed XNAT plugins.", notes = "The maps returned from this call include all of the properties specified in the plugin's property file.", response = String.class, responseContainer = "Map") + @ApiResponses({@ApiResponse(code = 200, message = "XNAT plugin properties successfully retrieved."), + @ApiResponse(code = 401, message = "Must be authenticated to access the XNAT REST API."), + @ApiResponse(code = 500, message = "Unexpected error")}) + @RequestMapping(produces = {MediaType.APPLICATION_JSON_VALUE}, method = {RequestMethod.GET}) + public ResponseEntity<Map<String, Properties>> getAllDataTypeSchemas() throws IOException { + return new ResponseEntity<>(_appInfo.getPluginProperties(), HttpStatus.OK); + } + + @ApiOperation(value = "Returns the requested XNAT plugin properties.", notes = "The maps returned from this call include all of the properties specified in the plugin's property file.", response = Properties.class) + @ApiResponses({@ApiResponse(code = 200, message = "XNAT plugin properties successfully retrieved."), + @ApiResponse(code = 401, message = "Must be authenticated to access the XNAT REST API."), + @ApiResponse(code = 404, message = "The requested resource wasn't found."), + @ApiResponse(code = 500, message = "Unexpected error")}) + @RequestMapping(value = "{plugin}", produces = {MediaType.APPLICATION_JSON_VALUE}, method = {RequestMethod.GET}) + public ResponseEntity<Properties> getRequestedDataTypeSchema(@PathVariable("plugin") final String plugin) throws IOException { + final Map<String, Properties> plugins = _appInfo.getPluginProperties(); + if (!plugins.containsKey(plugin)) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + return new ResponseEntity<>(plugins.get(plugin), HttpStatus.OK); + } + + private final XnatAppInfo _appInfo; +} \ No newline at end of file diff --git a/src/main/java/org/nrg/xnat/services/XnatAppInfo.java b/src/main/java/org/nrg/xnat/services/XnatAppInfo.java index 45c6d15f..72c82c41 100644 --- a/src/main/java/org/nrg/xnat/services/XnatAppInfo.java +++ b/src/main/java/org/nrg/xnat/services/XnatAppInfo.java @@ -1,7 +1,11 @@ package org.nrg.xnat.services; +import org.nrg.framework.utilities.BasicXnatResourceLocator; +import org.python.google.common.collect.ImmutableMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; @@ -59,6 +63,10 @@ public class XnatAppInfo { } } } + for (final Resource resource : BasicXnatResourceLocator.getResources("classpath*:META-INF/xnat/**/*-plugin.properties")) { + final Properties properties = PropertiesLoaderUtils.loadProperties(resource); + _plugins.put(properties.getProperty("name"), properties); + } _template = template; } @@ -208,15 +216,26 @@ public class XnatAppInfo { return buffer.toString(); } + /** + * Returns the properties for all of the installed and active plugins in the deployed XNAT server. + * + * @return A map of all of the plugins installed on the server. + */ + public Map<String, Properties> getPluginProperties() throws IOException { + return ImmutableMap.copyOf(_plugins); + } + private static final Logger _log = LoggerFactory.getLogger(XnatAppInfo.class); private static final List<String> PRIMARY_MANIFEST_ATTRIBUTES = Arrays.asList("Build-Number", "Build-Date", "Implementation-Version", "Implementation-Sha"); private final JdbcTemplate _template; - private final Date _startTime = new Date(); - private final Properties _properties = new Properties(); - private final Map<String, Map<String, String>> _attributes = new HashMap<>(); - private boolean _initialized = false; + private final Date _startTime = new Date(); + private final Properties _properties = new Properties(); + private final Map<String, Map<String, String>> _attributes = new HashMap<>(); + private final Map<String, Properties> _plugins = new HashMap<>(); + + private boolean _initialized = false; } -- GitLab