<commit_before>package io.zhudy.uia.web

import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.http.codec.*
import org.springframework.http.codec.json.Jackson2JsonDecoder
import org.springframework.http.codec.json.Jackson2JsonEncoder
import org.springframework.http.codec.multipart.MultipartHttpMessageReader
import org.springframework.http.codec.multipart.MultipartHttpMessageWriter
import org.springframework.http.codec.multipart.SynchronossPartHttpMessageReader
import org.springframework.web.reactive.function.server.HandlerStrategies
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.result.view.ViewResolver
import java.util.*
import java.util.function.Consumer
import java.util.function.Function
import java.util.function.Supplier


/**
 * @author <NAME> (<EMAIL>)
 */
class SimpleHandlerStrategiesBuilder(objectMapper: ObjectMapper) : HandlerStrategies.Builder {

    val codecConfigurer = ServerCodecConfigurer.create()!!
    val viewResolvers = ArrayList<ViewResolver>()
    var localeResolver: Function<ServerRequest, Optional<Locale>>? = null

    init {
        codecConfigurer.registerDefaults(false)
        codecConfigurer.customCodecs().reader(FormHttpMessageReader())

        codecConfigurer.customCodecs().encoder(Jackson2JsonEncoder(objectMapper))
        codecConfigurer.customCodecs().decoder(Jackson2JsonDecoder(objectMapper))

        // 文件上传
        val partReader = SynchronossPartHttpMessageReader()
        codecConfigurer.customCodecs().reader(partReader)
        codecConfigurer.customCodecs().reader(MultipartHttpMessageReader(partReader))
        codecConfigurer.customCodecs().writer(MultipartHttpMessageWriter())
    }

    override fun viewResolver(viewResolver: ViewResolver): HandlerStrategies.Builder {
        viewResolvers.add(viewResolver)
        return this
    }

    override fun defaultCodecs(consumer: Consumer<ServerCodecConfigurer.ServerDefaultCodecsConfigurer>): HandlerStrategies.Builder {
        consumer.accept(this.codecConfigurer.defaultCodecs())
        return this
    }

    override fun customCodecs(consumer: Consumer<CodecConfigurer.CustomCodecsConfigurer>): HandlerStrategies.Builder {
        consumer.accept(this.codecConfigurer.customCodecs())
        return this
    }

    override fun localeResolver(localeResolver: Function<ServerRequest, Optional<Locale>>): HandlerStrategies.Builder {
        this.localeResolver = localeResolver
        return this
    }

    override fun build(): HandlerStrategies {
        return MyHandlerStrategies(codecConfigurer.readers, codecConfigurer.writers, viewResolvers, localeResolver)
    }

    class MyHandlerStrategies(
            val messageReaders: List<HttpMessageReader<*>>,
            val messageWriters: List<HttpMessageWriter<*>>,
            val viewResolvers: List<ViewResolver>,
            val localeResolver: Function<ServerRequest, Optional<Locale>>?
    ) : HandlerStrategies {

        override fun viewResolvers() = Supplier {<commit_msg>修改返回规则<commit_after>package io.zhudy.uia.web

import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.http.codec.*
import org.springframework.http.codec.json.Jackson2JsonDecoder
import org.springframework.http.codec.json.Jackson2JsonEncoder
import org.springframework.web.reactive.function.server.HandlerStrategies
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.result.view.ViewResolver
import java.util.*
import java.util.function.Consumer
import java.util.function.Function
import java.util.function.Supplier


/**
 * @author <NAME> (<EMAIL>)
 */
class SimpleHandlerStrategiesBuilder(objectMapper: ObjectMapper) : HandlerStrategies.Builder {

    val codecConfigurer = ServerCodecConfigurer.create()!!
    val viewResolvers = ArrayList<ViewResolver>()
    var localeResolver: Function<ServerRequest, Optional<Locale>>? = null

    init {
        codecConfigurer.registerDefaults(false)
        codecConfigurer.customCodecs().reader(FormHttpMessageReader())

        codecConfigurer.customCodecs().encoder(Jackson2JsonEncoder(objectMapper))
        codecConfigurer.customCodecs().decoder(Jackson2JsonDecoder(objectMapper))

        // 文件上传
        // val partReader = SynchronossPartHttpMessageReader()
        // codecConfigurer.customCodecs().reader(partReader)
        // codecConfigurer.customCodecs().reader(MultipartHttpMessageReader(partReader))
        // codecConfigurer.customCodecs().writer(MultipartHttpMessageWriter())
    }

    override fun viewResolver(viewResolver: ViewResolver): HandlerStrategies.Builder {
        viewResolvers.add(viewResolver)
        return this
    }

    override fun defaultCodecs(consumer: Consumer<ServerCodecConfigurer.ServerDefaultCodecsConfigurer>): HandlerStrategies.Builder {
        consumer.accept(this.codecConfigurer.defaultCodecs())
        return this
    }

    override fun customCodecs(consumer: Consumer<CodecConfigurer.CustomCodecsConfigurer>): HandlerStrategies.Builder {
        consumer.accept(this.codecConfigurer.customCodecs())
        return this
    }

    override fun localeResolver(localeResolver: Function<ServerRequest, Optional<Locale>>): HandlerStrategies.Builder {
        this.localeResolver = localeResolver
        return this
    }

    override fun build(): HandlerStrategies {
        return SimpleHandlerStrategies(codecConfigurer.readers, codecConfigurer.writers, viewResolvers, localeResolver)
    }

    class SimpleHandlerStrategies(
            val messageReaders: List<HttpMessageReader<*>>,
            val messageWriters: List<HttpMessageWriter<*>>,
            val viewResolvers: List<ViewResolver>,
            val localeResolver: Function<ServerRequest, Optional<Locale>>?
    ) : HandlerStrategies {

        override fun viewResolvers() = Supplier {
{'fraction_non_alphanumeric': 0.08959537572254335, 'fraction_numerical': 0.0013600816048962938, 'mean_word_length': 4.55, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5662227', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1514, 'n_words': 240}