All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.nemerosa.ontrack.boot.ui.SettingsController.kt Maven / Gradle / Ivy

There is a newer version: 4.4.5
Show newest version
package net.nemerosa.ontrack.boot.ui

import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.databind.JsonNode
import net.nemerosa.ontrack.json.ObjectMapperFactory
import net.nemerosa.ontrack.model.Ack
import net.nemerosa.ontrack.model.form.DescribedForm
import net.nemerosa.ontrack.model.security.GlobalSettings
import net.nemerosa.ontrack.model.security.SecurityService
import net.nemerosa.ontrack.model.settings.SettingsManager
import net.nemerosa.ontrack.model.settings.SettingsManagerNotFoundException
import net.nemerosa.ontrack.model.settings.SettingsValidationException
import net.nemerosa.ontrack.ui.controller.AbstractResourceController
import net.nemerosa.ontrack.ui.resource.Resource
import net.nemerosa.ontrack.ui.resource.Resources
import org.apache.commons.lang3.StringUtils
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.on

/**
 * Global settings management.
 */
@RestController
@RequestMapping("/rest/settings")
class SettingsController(
        private val securityService: SecurityService,
        private val settingsManagers: Collection>
) : AbstractResourceController() {

    private val objectMapper = ObjectMapperFactory.create()

    /**
     * List of forms to configure.
     */
    @GetMapping("")
    fun configuration(): Resources {
        securityService.checkGlobalFunction(GlobalSettings::class.java)
        val forms = settingsManagers
                .sortedBy { it.title }
                .map { this.getSettingsForm(it) }
        // OK
        return Resources.of(
                forms,
                uri(on(javaClass).configuration())
        )
    }

    /**
     * Gets settings
     */
    @GetMapping("/{type:.*}")
    fun  getSettings(@PathVariable type: String): Resource {
        securityService.checkGlobalFunction(GlobalSettings::class.java)
        @Suppress("UNCHECKED_CAST") val settings: T? = settingsManagers
                .filter { candidate ->
                    StringUtils.equals(
                            type,
                            getSettingsManagerName(candidate)
                    )
                }
                .map { it.settings }
                .firstOrNull() as T?
        return if (settings != null) {
            Resource.of(
                    settings,
                    uri(on(javaClass).getSettings(type))
            )
        } else {
            throw SettingsManagerNotFoundException(type)
        }
    }


    /**
     * Security
     */
    @PutMapping("/{type:.*}")
    @ResponseStatus(HttpStatus.ACCEPTED)
    fun  updateSettings(@PathVariable type: String, @RequestBody settingsNode: JsonNode?): Ack {
        securityService.checkGlobalFunction(GlobalSettings::class.java)
        // Gets the settings manager by type
        @Suppress("UNCHECKED_CAST")
        val settingsManager: SettingsManager = (settingsManagers
                .firstOrNull { candidate ->
                    StringUtils.equals(
                            type,
                            getSettingsManagerName(candidate)
                    )
                }
                ?: throw  SettingsManagerNotFoundException(type))
                as SettingsManager
        // Parsing
        val settings: T
        try {
            settings = objectMapper.treeToValue(settingsNode!!, settingsManager.settingsClass)
        } catch (e: JsonProcessingException) {
            throw SettingsValidationException(e)
        }

        // Saves the settings
        settingsManager.saveSettings(settings)
        // OK
        return Ack.OK
    }

    private fun getSettingsManagerName(settingsManager: SettingsManager<*>): String {
        return settingsManager.id
    }

    private fun  getSettingsForm(settingsManager: SettingsManager): DescribedForm {
        return DescribedForm.create(
                getSettingsManagerName(settingsManager),
                settingsManager.settingsForm
        )
                .title(settingsManager.title)
                .uri(uri(on(javaClass).updateSettings(getSettingsManagerName(settingsManager), null)))
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy