diff --git a/src/main/java/org/nrg/xapi/rest/notifications/NotificationsApi.java b/src/main/java/org/nrg/xapi/rest/notifications/NotificationsApi.java index 5f489efb5e1f6aaea459a291ac514bc59170049a..5cd6813a7c1d871deeb6b95645a1c3a8c631a040 100644 --- a/src/main/java/org/nrg/xapi/rest/notifications/NotificationsApi.java +++ b/src/main/java/org/nrg/xapi/rest/notifications/NotificationsApi.java @@ -18,6 +18,8 @@ import org.nrg.xapi.exceptions.InitializationException; import org.nrg.xdat.XDAT; import org.nrg.xdat.preferences.NotificationsPreferences; import org.nrg.xdat.rest.AbstractXnatRestApi; +import org.nrg.xnat.services.XnatAppInfo; +import org.nrg.xnat.utils.XnatHttpUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -29,6 +31,7 @@ import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.web.bind.annotation.*; import javax.inject.Inject; +import javax.servlet.http.HttpServletRequest; import java.util.*; @Api(description = "XNAT Notifications management API") @@ -49,6 +52,30 @@ public class NotificationsApi extends AbstractXnatRestApi { + "remove existing properties by setting the property with an empty value. This will " + "modify the existing server configuration. You can completely replace the configuration " + "by calling the POST version of this method."; + @ApiOperation(value = "Returns the full map of site configuration properties.", notes = "Complex objects may be returned as encapsulated JSON strings.", response = String.class, responseContainer = "Map") + @ApiResponses({@ApiResponse(code = 200, message = "Site configuration properties successfully retrieved."), @ApiResponse(code = 401, message = "Must be authenticated to access the XNAT REST API."), @ApiResponse(code = 403, message = "Not authorized to set site configuration properties."), @ApiResponse(code = 500, message = "Unexpected error")}) + @RequestMapping(produces = {MediaType.APPLICATION_JSON_VALUE}, method = {RequestMethod.GET}) + public ResponseEntity<Map<String, Object>> getAllSiteConfigProperties(final HttpServletRequest request) { + final HttpStatus status = isPermitted(); + if (status != null) { + return new ResponseEntity<>(status); + } + final String username = getSessionUser().getUsername(); + if (_log.isDebugEnabled()) { + _log.debug("User " + username + " requested the site configuration."); + } + + final Map<String, Object> preferences = _notificationsPrefs.getPreferenceMap(); + + if (!_appInfo.isInitialized()) { + if (_log.isInfoEnabled()) { + _log.info("The site is being initialized by user {}. Setting default values from context.", username); + } + preferences.put("siteUrl", XnatHttpUtils.getServerRoot(request)); + } + + return new ResponseEntity<>(preferences, HttpStatus.OK); + } @ApiOperation(value = "Sets a map of notifications properties.", notes = "Sets the notifications properties specified in the map.", response = Void.class) @ApiResponses({@ApiResponse(code = 200, message = "Notifications properties successfully set."), @ApiResponse(code = 401, message = "Must be authenticated to access the XNAT REST API."), @ApiResponse(code = 403, message = "Not authorized to set notifications properties."), @ApiResponse(code = 500, message = "Unexpected error")}) @@ -113,6 +140,19 @@ public class NotificationsApi extends AbstractXnatRestApi { } } + String host = _notificationsPrefs.getHostname(); + int port = _notificationsPrefs.getPort(); + String protocol = _notificationsPrefs.getProtocol(); + String username = _notificationsPrefs.getUsername(); + String password = _notificationsPrefs.getPassword(); + + logConfigurationSubmit(host,port,protocol,username,password, properties); + + setHost(host, false); + setPort(port); + setProtocol(protocol); + setUsername(username); + setPassword(password); final Properties javaMailProperties = new Properties(); if (properties != null) { @@ -123,7 +163,6 @@ public class NotificationsApi extends AbstractXnatRestApi { } } } - logConfigurationSubmit(_javaMailSender.getHost(),_javaMailSender.getPort(),_javaMailSender.getUsername(),_javaMailSender.getPassword(),_javaMailSender.getProtocol(), properties); _javaMailSender.setJavaMailProperties(javaMailProperties); setSmtp(); @@ -700,4 +739,8 @@ public class NotificationsApi extends AbstractXnatRestApi { @Inject private NotificationService _notificationService; + + @Autowired + @Lazy + private XnatAppInfo _appInfo; } diff --git a/src/main/java/org/nrg/xnat/configuration/ApplicationConfig.java b/src/main/java/org/nrg/xnat/configuration/ApplicationConfig.java index b926c09aba669ca6241bee92db4fc2bc642a0be8..8c458859cfec40bc484e70e605ed0fd9796b03be 100644 --- a/src/main/java/org/nrg/xnat/configuration/ApplicationConfig.java +++ b/src/main/java/org/nrg/xnat/configuration/ApplicationConfig.java @@ -5,6 +5,7 @@ import org.nrg.config.exceptions.SiteConfigurationException; import org.nrg.framework.services.ContextService; import org.nrg.xdat.XDAT; import org.nrg.xdat.preferences.InitializerSiteConfiguration; +import org.nrg.xdat.preferences.NotificationsPreferences; import org.nrg.xdat.preferences.SiteConfigPreferences; import org.nrg.xdat.security.*; import org.nrg.xdat.security.services.UserManagementServiceI; @@ -51,6 +52,11 @@ public class ApplicationConfig { return new SiteConfigPreferences(); } + @Bean + public NotificationsPreferences notificationsPreferences() { + return new NotificationsPreferences(); + } + @Bean public PETTracerUtils petTracerUtils() throws Exception { return new PETTracerUtils(); diff --git a/src/main/resources/META-INF/xnat/spawner/site-admin-elements.yaml b/src/main/resources/META-INF/xnat/spawner/site-admin-elements.yaml index 36f43919906d807734a9f4ac3a956a40f9b8a550..16fffd4561d771fe2c09ea53cd87de05d9033a30 100644 --- a/src/main/resources/META-INF/xnat/spawner/site-admin-elements.yaml +++ b/src/main/resources/META-INF/xnat/spawner/site-admin-elements.yaml @@ -448,8 +448,8 @@ emailServerSettings: method: POST action: /xapi/notifications/batchMail contentType: json - load: ?? XNAT.data.notifications.batchMail - refresh: /xapi/notifications/batchMail + load: ?? XNAT.data.notifications + refresh: /xapi/notifications name: emailServerSettings label: Mail Server Settings contents: @@ -521,7 +521,7 @@ notifications: id: helpContactInfo name: notifications.helpContactInfo label: "Help Contact Info" - value: "!? XNAT.data.siteConfig['notifications.helpContactInfo'] || XNAT.data.siteConfig.adminEmail" + value: "!? XNAT.data.notifications['notifications.helpContactInfo'] || XNAT.data.siteConfig.adminEmail" emailMessageSubhead: kind: panel.subhead @@ -589,28 +589,28 @@ notifications: name: notifications.emailRecipientErrorMessages label: "Error Messages" description: "What email address should receive error emails" - value: "!? XNAT.data.siteConfig['notifications.emailRecipientErrorMessages'] || XNAT.data.siteConfig.adminEmail" + value: "!? XNAT.data.notifications['notifications.emailRecipientErrorMessages'] || XNAT.data.siteConfig.adminEmail" emailRecipientIssueReports: kind: panel.input.email id: emailRecipientIssueReports name: notifications.emailRecipientIssueReports label: "Issue Reports" description: "What email address should receive issue reports" - value: "!? XNAT.data.siteConfig['notifications.emailRecipientIssueReports'] || XNAT.data.siteConfig.adminEmail" + value: "!? XNAT.data.notifications['notifications.emailRecipientIssueReports'] || XNAT.data.siteConfig.adminEmail" emailRecipientNewUserAlert: kind: panel.input.email id: emailRecipientNewUserAlert name: notifications.emailRecipientNewUserAlert label: "New User Alert" description: "What email address should receive New User Registration emails" - value: "!? XNAT.data.siteConfig['notifications.emailRecipientNewUserAlert'] || XNAT.data.siteConfig.adminEmail" + value: "!? XNAT.data.notifications['notifications.emailRecipientNewUserAlert'] || XNAT.data.siteConfig.adminEmail" emailRecipientUpdate: kind: panel.input.email id: emailRecipientUpdate name: notifications.emailRecipientUpdate label: "Updates" description: "What email address should receive update emails" - value: "!? XNAT.data.siteConfig['notifications.emailRecipientUpdate'] || XNAT.data.siteConfig.adminEmail" + value: "!? XNAT.data.notifications['notifications.emailRecipientUpdate'] || XNAT.data.siteConfig.adminEmail" otherSubhead: kind: panel.subhead diff --git a/src/main/webapp/page/admin/content.jsp b/src/main/webapp/page/admin/content.jsp index 897cbbd8fcb56045ca28ce9d670cd6394f948615..17d49aa4184a80f95f3c0194256be42e55c74522 100755 --- a/src/main/webapp/page/admin/content.jsp +++ b/src/main/webapp/page/admin/content.jsp @@ -38,12 +38,13 @@ </div> <c:import url="/xapi/siteConfig" var="siteConfig"/> - + <c:import url="/xapi/notifications" var="notifications"/> <script> (function(){ XNAT.data = extend({}, XNAT.data, { - siteConfig: ${siteConfig} + siteConfig: ${siteConfig}, + notifications: ${notifications} }); // get rid of the 'targetSource' property delete XNAT.data.siteConfig.targetSource; diff --git a/src/main/webapp/page/admin/site-config/content.jsp b/src/main/webapp/page/admin/site-config/content.jsp index 9103383d7f304d3b10f1e109c1b6e92bcd9a2513..ec4e63e16d539a9017f6d15e6a6e36e54f9e64ec 100644 --- a/src/main/webapp/page/admin/site-config/content.jsp +++ b/src/main/webapp/page/admin/site-config/content.jsp @@ -17,10 +17,12 @@ <pg:restricted msg="${_msg}"> <c:import url="/xapi/siteConfig" var="siteConfig"/> + <c:import url="/xapi/notifications" var="notifications"/> <script> XNAT.data = extend({}, XNAT.data, { - siteConfig: ${siteConfig} + siteConfig: ${siteConfig}, + notifications: ${notifications} }); // get rid of the 'targetSource' property delete XNAT.data.siteConfig.targetSource;