-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
93 additions
and
124 deletions.
There are no files selected for viewing
9 changes: 0 additions & 9 deletions
9
src/main/kotlin/plus/maa/backend/common/annotation/JsonSchema.kt
This file was deleted.
Oops, something went wrong.
86 changes: 0 additions & 86 deletions
86
src/main/kotlin/plus/maa/backend/common/aop/JsonSchemaAop.kt
This file was deleted.
Oops, something went wrong.
6 changes: 3 additions & 3 deletions
6
src/main/kotlin/plus/maa/backend/config/accesslimit/AccessLimitConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
package plus.maa.backend.config.accesslimit | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.data.redis.core.StringRedisTemplate | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer | ||
import plus.maa.backend.service.DataTransferService | ||
|
||
@Configuration | ||
class AccessLimitConfig( | ||
private val stringRedisTemplate: StringRedisTemplate, | ||
private val objectMapper: ObjectMapper, | ||
private val dataTransferService: DataTransferService, | ||
) : WebMvcConfigurer { | ||
override fun addInterceptors(registry: InterceptorRegistry) { | ||
registry.addInterceptor(AccessLimitInterceptor(stringRedisTemplate, objectMapper)) | ||
registry.addInterceptor(AccessLimitInterceptor(stringRedisTemplate, dataTransferService)) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/plus/maa/backend/config/validation/JsonSchemaMatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package plus.maa.backend.config.validation | ||
|
||
import jakarta.validation.Constraint | ||
import jakarta.validation.Payload | ||
import kotlin.reflect.KClass | ||
|
||
@MustBeDocumented | ||
@Constraint(validatedBy = [JsonSchemaMatchValidator::class]) | ||
@Target(AnnotationTarget.FIELD) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class JsonSchemaMatch( | ||
val message: String = "Not Satisfying Json Schema", | ||
val schema: String, | ||
val groups: Array<KClass<*>> = [], | ||
val payload: Array<KClass<out Payload>> = [], | ||
) |
43 changes: 43 additions & 0 deletions
43
src/main/kotlin/plus/maa/backend/config/validation/JsonSchemaMatchValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package plus.maa.backend.config.validation | ||
|
||
import jakarta.validation.ConstraintValidator | ||
import jakarta.validation.ConstraintValidatorContext | ||
import org.everit.json.schema.Schema | ||
import org.everit.json.schema.loader.SchemaLoader | ||
import org.json.JSONObject | ||
import org.json.JSONTokener | ||
import org.springframework.core.io.ClassPathResource | ||
|
||
class JsonSchemaMatchValidator : ConstraintValidator<JsonSchemaMatch, String> { | ||
private lateinit var schema: String | ||
override fun initialize(constraintAnnotation: JsonSchemaMatch) { | ||
super.initialize(constraintAnnotation) | ||
schema = constraintAnnotation.schema | ||
} | ||
|
||
override fun isValid(text: String?, ctx: ConstraintValidatorContext): Boolean { | ||
try { | ||
if (text == null) return true | ||
val validator = validators[schema] ?: return false | ||
validator.validate(text.let(::JSONObject)) | ||
return true | ||
} catch (e: Exception) { | ||
ctx.disableDefaultConstraintViolation() | ||
ctx.buildConstraintViolationWithTemplate(e.message).addConstraintViolation() | ||
return false | ||
} | ||
} | ||
|
||
companion object { | ||
const val COPILOT_SCHEMA_JSON = "static/templates/maa-copilot-schema.json" | ||
val validators = mapOf( | ||
loadSchema(COPILOT_SCHEMA_JSON), | ||
) | ||
|
||
@Suppress("SameParameterValue") | ||
private fun loadSchema(path: String): Pair<String, Schema> { | ||
val schema = ClassPathResource(path).inputStream.let(::JSONTokener).let(::JSONObject).let(SchemaLoader::load) | ||
return path to schema | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/plus/maa/backend/config/validation/RatingType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package plus.maa.backend.config.validation | ||
|
||
import jakarta.validation.Constraint | ||
import jakarta.validation.Payload | ||
import jakarta.validation.ReportAsSingleViolation | ||
import jakarta.validation.constraints.Pattern | ||
import kotlin.reflect.KClass | ||
|
||
@MustBeDocumented | ||
@Target(AnnotationTarget.FIELD) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@Constraint(validatedBy = []) | ||
@ReportAsSingleViolation | ||
@Pattern(regexp = "Like|Dislike|None") | ||
annotation class RatingType( | ||
val message: String = "The rating must be one of Like, Dislike, None", | ||
val groups: Array<KClass<*>> = [], | ||
val payload: Array<KClass<out Payload>> = [], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/kotlin/plus/maa/backend/controller/request/copilot/CopilotCUDRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
package plus.maa.backend.controller.request.copilot | ||
|
||
import plus.maa.backend.config.validation.JsonSchemaMatch | ||
import plus.maa.backend.config.validation.JsonSchemaMatchValidator | ||
|
||
data class CopilotCUDRequest( | ||
@JsonSchemaMatch(schema = JsonSchemaMatchValidator.COPILOT_SCHEMA_JSON) | ||
val content: String? = null, | ||
val id: Long? = null, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
src/main/resources/static/templates/maa-rating-schema.json
This file was deleted.
Oops, something went wrong.