refactor: relocate ResourceNotFoundController

This commit is contained in:
Gauthier Roebroeck 2023-09-11 17:45:45 +08:00
parent de6c17a7f9
commit 2d404d3197
3 changed files with 22 additions and 20 deletions

View file

@ -1,15 +1,8 @@
package org.gotson.komga.infrastructure.web
import jakarta.servlet.http.HttpServletRequest
import org.springframework.context.annotation.Configuration
import org.springframework.http.CacheControl
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Component
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.method.support.HandlerMethodArgumentResolver
import org.springframework.web.server.ResponseStatusException
import org.springframework.web.servlet.NoHandlerFoundException
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
@ -90,15 +83,3 @@ class WebMvcConfiguration : WebMvcConfigurer {
resolvers.add(DelimitedPairHandlerMethodArgumentResolver())
}
}
@Component
@ControllerAdvice
class ResourceNotFoundController {
val apis = listOf("/api", "/opds", "/sse")
@ExceptionHandler(NoHandlerFoundException::class)
fun notFound(request: HttpServletRequest): String {
if (apis.any { request.requestURI.startsWith(it, true) }) throw ResponseStatusException(HttpStatus.NOT_FOUND)
return "forward:/"
}
}

View file

@ -0,0 +1,21 @@
package org.gotson.komga.interfaces.mvc
import jakarta.servlet.http.HttpServletRequest
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Component
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.server.ResponseStatusException
import org.springframework.web.servlet.NoHandlerFoundException
@Component
@ControllerAdvice
class ResourceNotFoundController {
val apis = listOf("/api", "/opds", "/sse")
@ExceptionHandler(NoHandlerFoundException::class)
fun notFound(request: HttpServletRequest): String {
if (apis.any { request.requestURI.startsWith(it, true) }) throw ResponseStatusException(HttpStatus.NOT_FOUND)
return "forward:/"
}
}

View file

@ -1,4 +1,4 @@
package org.gotson.komga.infrastructure.web
package org.gotson.komga.interfaces.mvc
import org.gotson.komga.interfaces.api.rest.WithMockCustomUser
import org.junit.jupiter.api.Test