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

io.javalin.openapi.plugin.redoc.ReDocHandler.kt Maven / Gradle / Ivy

There is a newer version: 6.4.0
Show newest version
package io.javalin.openapi.plugin.redoc

import io.javalin.http.Context
import io.javalin.http.Handler

/**
 * Based on https://github.com/tipsy/javalin/blob/master/javalin-openapi/src/main/java/io/javalin/plugin/openapi/ui/ReDocRenderer.kt by @chsfleury
 */
class ReDocHandler(
    private val title: String,
    private val documentationPath: String,
    private val version: String,
    private val routingPath: String,
    private val basePath: String?
) : Handler {

    override fun handle(context: Context) {
        context
            .html(createReDocUI())
            .res().characterEncoding = "UTF-8"
    }

    private fun createReDocUI(): String {
        val rootPath = (basePath ?: "") + routingPath
        val publicBasePath = "$rootPath/webjars/redoc/$version".removedDoubledPathOperators()
        val publicDocumentationPath = (rootPath + documentationPath).removedDoubledPathOperators()

        return """
        |
        |
        |  
        |    $title
        |    
        |    
        |    
        |    
        |    
        |    
        |  
        |  
        |  
        |  
        |  
        |  
        |
        |""".trimMargin()
    }

    private val multiplePathOperatorsRegex = Regex("/+")

    private fun String.removedDoubledPathOperators(): String =
        replace(multiplePathOperatorsRegex, "/")

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy