From 16462198f67514a1524ceea9e1bfc67c6760815b Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sun, 8 Oct 2023 15:00:22 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20JsonOrFormBind=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../usbharu/hideout/config/JsonOrFormBind.kt | 6 ++ .../config/JsonOrFormModelMethodProcessor.kt | 55 +++++++++++++++++++ .../usbharu/hideout/config/MvcConfigurer.kt | 30 ++++++++++ 3 files changed, 91 insertions(+) create mode 100644 src/main/kotlin/dev/usbharu/hideout/config/JsonOrFormBind.kt create mode 100644 src/main/kotlin/dev/usbharu/hideout/config/JsonOrFormModelMethodProcessor.kt create mode 100644 src/main/kotlin/dev/usbharu/hideout/config/MvcConfigurer.kt diff --git a/src/main/kotlin/dev/usbharu/hideout/config/JsonOrFormBind.kt b/src/main/kotlin/dev/usbharu/hideout/config/JsonOrFormBind.kt new file mode 100644 index 00000000..10460b20 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/config/JsonOrFormBind.kt @@ -0,0 +1,6 @@ +package dev.usbharu.hideout.config + +@MustBeDocumented +@Retention(AnnotationRetention.RUNTIME) +@Target(AnnotationTarget.VALUE_PARAMETER) +annotation class JsonOrFormBind() diff --git a/src/main/kotlin/dev/usbharu/hideout/config/JsonOrFormModelMethodProcessor.kt b/src/main/kotlin/dev/usbharu/hideout/config/JsonOrFormModelMethodProcessor.kt new file mode 100644 index 00000000..4a87eb50 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/config/JsonOrFormModelMethodProcessor.kt @@ -0,0 +1,55 @@ +package dev.usbharu.hideout.config + +import org.slf4j.LoggerFactory +import org.springframework.core.MethodParameter +import org.springframework.web.bind.support.WebDataBinderFactory +import org.springframework.web.context.request.NativeWebRequest +import org.springframework.web.method.annotation.ModelAttributeMethodProcessor +import org.springframework.web.method.support.HandlerMethodArgumentResolver +import org.springframework.web.method.support.ModelAndViewContainer +import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor + +class JsonOrFormModelMethodProcessor( + private val modelAttributeMethodProcessor: ModelAttributeMethodProcessor, + private val requestResponseBodyMethodProcessor: RequestResponseBodyMethodProcessor +) : HandlerMethodArgumentResolver { + override fun supportsParameter(parameter: MethodParameter): Boolean { + return parameter.hasParameterAnnotation(JsonOrFormBind::class.java) + } + + private val isJsonRegex = Regex("application/((\\w*)\\+)?json") + + override fun resolveArgument( + parameter: MethodParameter, + mavContainer: ModelAndViewContainer?, + webRequest: NativeWebRequest, + binderFactory: WebDataBinderFactory? + ): Any? { + + val contentType = webRequest.getHeader("Content-Type").orEmpty() + logger.trace("ContentType is {}", contentType) + if (contentType.contains(isJsonRegex)) { + logger.trace("Determine content type as json.") + return requestResponseBodyMethodProcessor.resolveArgument( + parameter, + mavContainer, + webRequest, + binderFactory + ) + } + + return try { + modelAttributeMethodProcessor.resolveArgument(parameter, mavContainer, webRequest, binderFactory) + } catch (e: Exception) { + try { + requestResponseBodyMethodProcessor.resolveArgument(parameter, mavContainer, webRequest, binderFactory) + } catch (e: Exception) { + logger.warn("Failed to bind request", e) + } + } + } + + companion object { + val logger = LoggerFactory.getLogger(JsonOrFormModelMethodProcessor::class.java) + } +} diff --git a/src/main/kotlin/dev/usbharu/hideout/config/MvcConfigurer.kt b/src/main/kotlin/dev/usbharu/hideout/config/MvcConfigurer.kt new file mode 100644 index 00000000..6e14270c --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/config/MvcConfigurer.kt @@ -0,0 +1,30 @@ +package dev.usbharu.hideout.config + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.http.converter.HttpMessageConverter +import org.springframework.web.method.support.HandlerMethodArgumentResolver +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer +import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor +import org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor + +@Configuration +class MvcConfigurer(private val jsonOrFormModelMethodProcessor: JsonOrFormModelMethodProcessor) : WebMvcConfigurer { + override fun addArgumentResolvers(resolvers: MutableList) { + resolvers.add(jsonOrFormModelMethodProcessor) + } +} + + +@Configuration +class JsonOrFormModelMethodProcessorConfig { + @Bean + fun jsonOrFormModelMethodProcessor(converter: List>): JsonOrFormModelMethodProcessor { + return JsonOrFormModelMethodProcessor( + ServletModelAttributeMethodProcessor(true), + RequestResponseBodyMethodProcessor( + converter + ) + ) + } +}