Skip to content
Snippets Groups Projects
Commit f4e4687a authored by Rick Herrick's avatar Rick Herrick
Browse files

XNAT-4349 Cleaned up dependency issues that blocked REST implementation.

parent e62ce882
No related branches found
No related tags found
No related merge requests found
...@@ -70,7 +70,7 @@ public class EventHandlerApi extends AbstractXapiRestController { ...@@ -70,7 +70,7 @@ public class EventHandlerApi extends AbstractXapiRestController {
* *
* @return the response entity * @return the response entity
*/ */
@ApiOperation(value = "Get list of event classes.", notes = "Returns a list of classes implementing AutomationEventI.", response = List.class) @ApiOperation(value = "Get list of event classes.", notes = "Returns a list of classes implementing AutomationEventI.", response = EventClassInfo.class, responseContainer = "List")
@ApiResponses({@ApiResponse(code = 200, message = "An array of class names"), @ApiResponse(code = 500, message = "Unexpected error")}) @ApiResponses({@ApiResponse(code = 200, message = "An array of class names"), @ApiResponse(code = 500, message = "Unexpected error")})
@RequestMapping(value = {"/projects/{project_id}/eventHandlers/automationEventClasses"}, produces = {MediaType.APPLICATION_JSON_VALUE}, method = RequestMethod.GET) @RequestMapping(value = {"/projects/{project_id}/eventHandlers/automationEventClasses"}, produces = {MediaType.APPLICATION_JSON_VALUE}, method = RequestMethod.GET)
@ResponseBody @ResponseBody
...@@ -92,7 +92,7 @@ public class EventHandlerApi extends AbstractXapiRestController { ...@@ -92,7 +92,7 @@ public class EventHandlerApi extends AbstractXapiRestController {
* *
* @return the response entity * @return the response entity
*/ */
@ApiOperation(value = "Get list of event classes.", notes = "Returns a list of classes implementing AutomationEventI.", response = List.class) @ApiOperation(value = "Get list of event classes.", notes = "Returns a list of classes implementing AutomationEventI.", response = EventClassInfo.class, responseContainer = "List")
@ApiResponses({@ApiResponse(code = 200, message = "An array of class names"), @ApiResponse(code = 500, message = "Unexpected error")}) @ApiResponses({@ApiResponse(code = 200, message = "An array of class names"), @ApiResponse(code = 500, message = "Unexpected error")})
@RequestMapping(value = {"/eventHandlers/automationEventClasses"}, produces = {MediaType.APPLICATION_JSON_VALUE}, method = RequestMethod.GET) @RequestMapping(value = {"/eventHandlers/automationEventClasses"}, produces = {MediaType.APPLICATION_JSON_VALUE}, method = RequestMethod.GET)
@ResponseBody @ResponseBody
...@@ -109,6 +109,33 @@ public class EventHandlerApi extends AbstractXapiRestController { ...@@ -109,6 +109,33 @@ public class EventHandlerApi extends AbstractXapiRestController {
} }
} }
/**
* Checks if is permitted.
*
* @param projectId the project ID
*
* @return the http status
*/
// TODO: Migrate this to the abstract superclass. Can't right now because XDAT doesn't know about XnatProjectdata, etc.
protected HttpStatus canEditProject(String projectId) {
final UserI sessionUser = getSessionUser();
if ((sessionUser instanceof XDATUser)) {
if (projectId != null) {
final XnatProjectdata project = AutoXnatProjectdata.getXnatProjectdatasById(projectId, sessionUser, false);
try {
return Permissions.canEdit(sessionUser, project) ? null : HttpStatus.FORBIDDEN;
} catch (Exception e) {
_log.error("Error checking read status for project", e);
return HttpStatus.INTERNAL_SERVER_ERROR;
}
} else {
return isPermitted() == null ? null : HttpStatus.FORBIDDEN;
}
}
_log.error("Error checking read status for project");
return HttpStatus.INTERNAL_SERVER_ERROR;
}
/** /**
* Gets the event info list. * Gets the event info list.
* *
...@@ -251,33 +278,6 @@ public class EventHandlerApi extends AbstractXapiRestController { ...@@ -251,33 +278,6 @@ public class EventHandlerApi extends AbstractXapiRestController {
return classList; return classList;
} }
/**
* Checks if is permitted.
*
* @param projectId the project ID
*
* @return the http status
*/
// TODO: Migrate this to the abstract superclass. Can't right now because XDAT doesn't know about XnatProjectdata, etc.
protected HttpStatus canEditProject(String projectId) {
final UserI sessionUser = getSessionUser();
if ((sessionUser instanceof XDATUser)) {
if (projectId != null) {
final XnatProjectdata project = AutoXnatProjectdata.getXnatProjectdatasById(projectId, sessionUser, false);
try {
return Permissions.canEdit(sessionUser, project) ? null : HttpStatus.FORBIDDEN;
} catch (Exception e) {
_log.error("Error checking read status for project", e);
return HttpStatus.INTERNAL_SERVER_ERROR;
}
} else {
return isPermitted() == null ? null : HttpStatus.FORBIDDEN;
}
}
_log.error("Error checking read status for project");
return HttpStatus.INTERNAL_SERVER_ERROR;
}
/** /**
* The Constant _log. * The Constant _log.
*/ */
......
package org.nrg.xnat.configuration; package org.nrg.xnat.configuration;
import org.nrg.framework.annotations.XapiRestController; import org.nrg.framework.annotations.XapiRestController;
import org.nrg.xnat.services.XnatAppInfo;
import org.nrg.xnat.spawner.configuration.SpawnerConfig; import org.nrg.xnat.spawner.configuration.SpawnerConfig;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -22,10 +23,13 @@ import org.springframework.web.servlet.view.JstlView; ...@@ -22,10 +23,13 @@ import org.springframework.web.servlet.view.JstlView;
import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo; import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.Locale;
@Configuration @Configuration
@EnableWebMvc @EnableWebMvc
@EnableSwagger2 @EnableSwagger2
...@@ -65,15 +69,25 @@ public class WebConfig extends WebMvcConfigurerAdapter { ...@@ -65,15 +69,25 @@ public class WebConfig extends WebMvcConfigurerAdapter {
} }
@Bean @Bean
public Docket api() { public Docket api(final XnatAppInfo info, final MessageSource messageSource) {
_log.debug("Initializing the Swagger Docket object"); _log.debug("Initializing the Swagger Docket object");
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withClassAnnotation(XapiRestController.class)).paths(PathSelectors.any()).build().apiInfo(apiInfo()).pathMapping("/xapi"); return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withClassAnnotation(XapiRestController.class)).paths(PathSelectors.any()).build().apiInfo(apiInfo(info, messageSource)).pathMapping("/xapi");
}
private ApiInfo apiInfo(final XnatAppInfo info, final MessageSource messageSource) {
return new ApiInfo(getMessage(messageSource, "apiInfo.title"),
getMessage(messageSource, "apiInfo.description"),
info.getVersion(),
getMessage(messageSource, "apiInfo.termsOfServiceUrl"),
new Contact(getMessage(messageSource, "apiInfo.contactName"),
getMessage(messageSource, "apiInfo.contactUrl"),
getMessage(messageSource, "apiInfo.contactEmail")),
getMessage(messageSource, "apiInfo.license"),
getMessage(messageSource, "apiInfo.licenseUrl"));
} }
private ApiInfo apiInfo() { private String getMessage(final MessageSource messageSource, final String messageId) {
// TODO: The deprecated class is ApiInfo(). There's no documentation on how to use the replacement Contact class instead, so migrate this when that's explained. return messageSource.getMessage(messageId, null, Locale.getDefault());
//noinspection deprecation
return new ApiInfo("XNAT REST API", "The XNAT REST API (XAPI) functions provide access to XNAT internal functions for remote clients.", "1.7.0", "http://www.xnat.org", "info@xnat.org", "Simplified 2-Clause BSD", "API license URL");
} }
private static final Logger _log = LoggerFactory.getLogger(WebConfig.class); private static final Logger _log = LoggerFactory.getLogger(WebConfig.class);
......
...@@ -17,19 +17,19 @@ import java.util.jar.Manifest; ...@@ -17,19 +17,19 @@ import java.util.jar.Manifest;
@Component @Component
public class XnatAppInfo { public class XnatAppInfo {
public static final int MILLISECONDS_IN_A_DAY = (24 * 60 * 60 * 1000); private static final int MILLISECONDS_IN_A_DAY = (24 * 60 * 60 * 1000);
public static final int MILLISECONDS_IN_AN_HOUR = (60 * 60 * 1000); private static final int MILLISECONDS_IN_AN_HOUR = (60 * 60 * 1000);
public static final int MILLISECONDS_IN_A_MINUTE = (60 * 1000); private static final int MILLISECONDS_IN_A_MINUTE = (60 * 1000);
public static final DecimalFormat SECONDS_FORMAT = new DecimalFormat("##.000"); private static final DecimalFormat SECONDS_FORMAT = new DecimalFormat("##.000");
public static final String DAYS = "days"; private static final String DAYS = "days";
public static final String HOURS = "hours"; private static final String HOURS = "hours";
public static final String MINUTES = "minutes"; private static final String MINUTES = "minutes";
public static final String SECONDS = "seconds"; private static final String SECONDS = "seconds";
@Inject @Inject
public XnatAppInfo(final ServletContext context, final JdbcTemplate template) throws IOException { public XnatAppInfo(final ServletContext context, final JdbcTemplate template) throws IOException {
try (final InputStream input = context.getResourceAsStream("/META-INF/MANIFEST.MF")) { try (final InputStream input = context.getResourceAsStream("/META-INF/MANIFEST.MF")) {
final Manifest manifest = new Manifest(input); final Manifest manifest = new Manifest(input);
final Attributes attributes = manifest.getMainAttributes(); final Attributes attributes = manifest.getMainAttributes();
_properties.setProperty("buildNumber", attributes.getValue("Build-Number")); _properties.setProperty("buildNumber", attributes.getValue("Build-Number"));
_properties.setProperty("buildDate", attributes.getValue("Build-Date")); _properties.setProperty("buildDate", attributes.getValue("Build-Date"));
...@@ -84,14 +84,14 @@ public class XnatAppInfo { ...@@ -84,14 +84,14 @@ public class XnatAppInfo {
/** /**
* Returns the primary XNAT system properties extracted from the installed application's manifest file. These * Returns the primary XNAT system properties extracted from the installed application's manifest file. These
* properties are guaranteed to include the following: * properties are guaranteed to include the following:
* * <p>
* <ul> * <ul>
* <li>version</li> * <li>version</li>
* <li>buildNumber</li> * <li>buildNumber</li>
* <li>buildDate</li> * <li>buildDate</li>
* <li>commit</li> * <li>commit</li>
* </ul> * </ul>
* * <p>
* There may be other properties available in the system properties and even more available through the {@link * There may be other properties available in the system properties and even more available through the {@link
* #getSystemAttributes()} method. * #getSystemAttributes()} method.
* *
...@@ -101,6 +101,42 @@ public class XnatAppInfo { ...@@ -101,6 +101,42 @@ public class XnatAppInfo {
return (Properties) _properties.clone(); return (Properties) _properties.clone();
} }
/**
* Gets the version of the application.
*
* @return The version of the application.
*/
public String getVersion() {
return _properties.getProperty("version");
}
/**
* Gets the build number of the application.
*
* @return The build number of the application.
*/
public String getBuildNumber() {
return _properties.getProperty("buildNumber");
}
/**
* Gets the date the application was built.
*
* @return The date the application was built.
*/
public String getBuildDate() {
return _properties.getProperty("buildDate");
}
/**
* Gets the commit number in the source repository from which the application was built.
*
* @return The commit number of the application.
*/
public String getCommit() {
return _properties.getProperty("commit");
}
/** /**
* Returns extended XNAT system attributes. * Returns extended XNAT system attributes.
* *
...@@ -128,12 +164,12 @@ public class XnatAppInfo { ...@@ -128,12 +164,12 @@ public class XnatAppInfo {
* @return A map of values indicating the system uptime. * @return A map of values indicating the system uptime.
*/ */
public Map<String, String> getUptime() { public Map<String, String> getUptime() {
final long diff = new Date().getTime() - _startTime.getTime(); final long diff = new Date().getTime() - _startTime.getTime();
final int days = (int) (diff / MILLISECONDS_IN_A_DAY); final int days = (int) (diff / MILLISECONDS_IN_A_DAY);
final long daysRemainder = diff % MILLISECONDS_IN_A_DAY; final long daysRemainder = diff % MILLISECONDS_IN_A_DAY;
final int hours = (int) (daysRemainder / MILLISECONDS_IN_AN_HOUR); final int hours = (int) (daysRemainder / MILLISECONDS_IN_AN_HOUR);
final long hoursRemainder = daysRemainder % MILLISECONDS_IN_AN_HOUR; final long hoursRemainder = daysRemainder % MILLISECONDS_IN_AN_HOUR;
final int minutes = (int) (hoursRemainder / MILLISECONDS_IN_A_MINUTE); final int minutes = (int) (hoursRemainder / MILLISECONDS_IN_A_MINUTE);
final long minutesRemainder = hoursRemainder % MILLISECONDS_IN_A_MINUTE; final long minutesRemainder = hoursRemainder % MILLISECONDS_IN_A_MINUTE;
final Map<String, String> uptime = new HashMap<>(); final Map<String, String> uptime = new HashMap<>();
...@@ -158,7 +194,7 @@ public class XnatAppInfo { ...@@ -158,7 +194,7 @@ public class XnatAppInfo {
*/ */
public String getFormattedUptime() { public String getFormattedUptime() {
final Map<String, String> uptime = getUptime(); final Map<String, String> uptime = getUptime();
final StringBuilder buffer = new StringBuilder(); final StringBuilder buffer = new StringBuilder();
if (uptime.containsKey(DAYS)) { if (uptime.containsKey(DAYS)) {
buffer.append(uptime.get(DAYS)).append(" days, "); buffer.append(uptime.get(DAYS)).append(" days, ");
} }
......
apiInfo.title=XNAT REST API
apiInfo.description=The XNAT REST API (XAPI) functions provide access to XNAT internal functions for remote clients.
apiInfo.termsOfServiceUrl=http://www.xnat.org/download
apiInfo.contactName=XNAT
apiInfo.contactUrl=http://www.xnat.org
apiInfo.contactEmail=info@xnat.org
apiInfo.license=Simplified 2-Clause BSD
apiInfo.licenseUrl=https://opensource.org/licenses/BSD-2-Clause
providerManager.providerNotFound=No authentication provider found for {0} providerManager.providerNotFound=No authentication provider found for {0}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment