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

com.trib3.server.swagger.SwaggerInitializer.kt Maven / Gradle / Ivy

There is a newer version: 3.1.5187
Show newest version
package com.trib3.server.swagger

import com.trib3.server.config.TribeApplicationConfig
import io.swagger.v3.jaxrs2.integration.JaxrsApplicationScanner
import io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder
import io.swagger.v3.jaxrs2.integration.OpenApiServlet
import io.swagger.v3.oas.integration.SwaggerConfiguration
import io.swagger.v3.oas.integration.api.OpenApiContext
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.servers.Server
import javax.inject.Inject
import javax.ws.rs.core.Application

// JaxrsOpenApiContextBuilder needs to be subclassed in order to instantiate with a 
private class SwaggerContextBuilder : JaxrsOpenApiContextBuilder()

// TODO: find a better place for this interface
interface JaxrsAppProcessor {
    fun process(application: Application)
}

class SwaggerInitializer
@Inject constructor(val appConfig: TribeApplicationConfig) : JaxrsAppProcessor {
    override fun process(application: Application) {
        // this is tricky, but we want to document the jersey exposed APIs
        // from within the admin servlet, and the swagger libraries don't
        // make things easy.  Fake out the servlet context -> swagger context
        // with what ServletConfigContextUtils.getContextIdFromServletConfig
        // will generate for the actual servlet, then add the swagger servlet
        val ctxId = OpenApiContext.OPENAPI_CONTEXT_ID_PREFIX + "servlet." + OpenApiServlet::class.simpleName
        SwaggerContextBuilder()
            .openApiConfiguration(
                SwaggerConfiguration()
                    .openAPI(
                        OpenAPI().servers(
                            listOf(
                                Server().url("https://${appConfig.corsDomains[0]}/app"),
                                Server().url("http://${appConfig.corsDomains[0]}/app"),
                                Server().url("http://${appConfig.corsDomains[0]}:${appConfig.appPort}/app")
                            )
                        )
                    )
                    .scannerClass(JaxrsApplicationScanner::class.qualifiedName)
            )
            .application(application)
            .ctxId(ctxId)
            .buildContext(true)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy