Skip to content

Commit 5f585be

Browse files
Merge pull request #14 from icerockdev/task/WD-321
WD-321 Add custom request/response configuration
2 parents 1dbf74b + 946f9aa commit 5f585be

File tree

9 files changed

+56
-19
lines changed

9 files changed

+56
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repositories {
1212
}
1313

1414
// Append dependency
15-
implementation("com.icerockdev:web-utils:0.3.0")
15+
implementation("com.icerockdev:web-utils:0.3.1")
1616
````
1717

1818
## Library usage

sample/collection.http

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1+
GET http://127.0.0.1:8080/
2+
Accept: text/plain
3+
4+
###
5+
6+
GET http://127.0.0.1:8080/object
7+
Accept: application/json
8+
9+
###
10+
111
POST http://127.0.0.1:8080/object
12+
Accept: application/json
213
Content-Type: application/json
314

415
{
@@ -7,22 +18,28 @@ Content-Type: application/json
718
}
819

920
###
21+
22+
GET http://127.0.0.1:8080/custom-object
23+
Accept: application/json
24+
25+
###
26+
1027
GET http://127.0.0.1:8080/exception
11-
accept: application/json
28+
Accept: application/json
1229

1330
###
1431

1532
GET http://127.0.0.1:8080/handled
16-
accept: application/json
33+
Accept: application/json
1734

1835
###
1936

2037
GET http://127.0.0.1:8080/handled2
21-
accept: application/json
38+
Accept: application/json
2239

2340
###
2441

2542
GET http://127.0.0.1:8080/get?age=10&TestValue=20&email=a@a.com&test=10&test=20&test&tmp=1&tmp=2&tmp=3
26-
accept: application/json
43+
Accept: application/json
2744

28-
###
45+
###

sample/src/main/kotlin/com/icerockdev/sample/server.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.icerockdev.exception.ValidationException
1212
import com.icerockdev.util.QueryParser
1313
import com.icerockdev.util.receiveQuery
1414
import com.icerockdev.webserver.*
15+
import com.icerockdev.webserver.log.LoggingConfiguration
1516
import com.icerockdev.webserver.log.JsonDataLogger
1617
import com.icerockdev.webserver.log.JsonSecret
1718
import com.icerockdev.webserver.log.jsonLogger
@@ -41,6 +42,8 @@ fun Application.main() {
4142
}
4243
install(JsonDataLogger) {
4344
mapperConfiguration = getObjectMapper()
45+
loggingConfiguration =
46+
LoggingConfiguration(responseTypes = listOf(AbstractResponse::class, CustomResponse::class))
4447
}
4548
install(CallId, getCallConfiguration())
4649
install(ContentNegotiation) {
@@ -72,6 +75,10 @@ fun Application.main() {
7275
}
7376
call.respond(TestResponse2(200, request.email, request.password))
7477
}
78+
79+
get("/custom-object") {
80+
call.respond(CustomResponse(200, "Custom message", listOf(1, 2, 3)))
81+
}
7582
}
7683

7784
get("/exception") {
@@ -104,6 +111,8 @@ class TestResponse(status: Int, message: String) :
104111
val data = message
105112
}
106113

114+
class CustomResponse(var status: Int, var message: String, var list: List<Int>)
115+
107116
data class QueryValues(
108117
private val email: String = "",
109118
private val age: Int?,

web-utils/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ apply(plugin = "java")
1313
apply(plugin = "kotlin")
1414

1515
group = "com.icerockdev"
16-
version = "0.3.0"
16+
version = "0.3.1"
1717

1818
val sourcesJar by tasks.registering(Jar::class) {
1919
classifier = "sources"

web-utils/src/main/kotlin/com/icerockdev/api/AbstractResponse.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ abstract class AbstractResponse(
1111
var message: String = "",
1212
var timestamp: Long = DateTimeUtil.getTimestamp(),
1313
var success: Boolean = false
14-
) {
15-
}
14+
)

web-utils/src/main/kotlin/com/icerockdev/api/ErrorResponse.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@ class ErrorResponse() : ResponseList() {
2828
setValidationParams(errorList)
2929
totalCount = errorList.count()
3030
}
31-
3231
}

web-utils/src/main/kotlin/com/icerockdev/api/Response.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ open class Response(
1212
message: String = "",
1313
timestamp: Long = DateTimeUtil.getTimestamp(),
1414
isSuccess: Boolean = true
15-
) : AbstractResponse(status, message, timestamp, isSuccess) {
16-
}
15+
) : AbstractResponse(status, message, timestamp, isSuccess)

web-utils/src/main/kotlin/com/icerockdev/webserver/log/JsonDataLogger.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector
1616
import com.fasterxml.jackson.databind.jsontype.TypeSerializer
1717
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer
1818
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
19-
import com.icerockdev.api.AbstractResponse
20-
import com.icerockdev.api.Request
2119
import com.icerockdev.webserver.Constants
2220
import com.icerockdev.webserver.Environment
2321
import io.ktor.application.*
@@ -36,6 +34,7 @@ class JsonDataLogger(configure: Configuration.() -> Unit) {
3634

3735
private val mapper: ObjectMapper
3836
private val configuration: Configuration
37+
3938
/**
4039
* Configuration type for [JsonDataLogger] feature
4140
*/
@@ -46,6 +45,7 @@ class JsonDataLogger(configure: Configuration.() -> Unit) {
4645
var responseStatusCodeName: String = Constants.LOG_FIELD_STATUS_CODE
4746
var appEnvName: String = Constants.LOG_FIELD_ENV
4847
var systemEnvKey: String = "env"
48+
var loggingConfiguration: LoggingConfiguration = LoggingConfiguration()
4949
}
5050

5151
init {
@@ -78,17 +78,21 @@ class JsonDataLogger(configure: Configuration.() -> Unit) {
7878
// response data intercept
7979
pipeline.sendPipeline.insertPhaseBefore(ApplicationSendPipeline.Render, LoggingPhase)
8080
pipeline.sendPipeline.intercept(LoggingPhase) { subject ->
81-
if (subject is AbstractResponse) {
82-
MDC.put(configuration.responseBodyName, mapper.writeValueAsString(subject))
81+
configuration.loggingConfiguration.responseTypes.forEach { type ->
82+
if (type.isInstance(subject)) {
83+
MDC.put(configuration.responseBodyName, mapper.writeValueAsString(subject))
84+
}
8385
}
8486
}
8587

8688
// Received data intercept
8789
pipeline.receivePipeline.insertPhaseBefore(ApplicationReceivePipeline.After, LoggingPhase)
8890
pipeline.receivePipeline.intercept(LoggingPhase) { request ->
8991
val requestValue = request.value
90-
if (requestValue is Request) {
91-
MDC.put(configuration.requestBodyName, mapper.writeValueAsString(requestValue))
92+
configuration.loggingConfiguration.requestTypes.forEach { type ->
93+
if (type.isInstance(requestValue)) {
94+
MDC.put(configuration.requestBodyName, mapper.writeValueAsString(requestValue))
95+
}
9296
}
9397
}
9498

@@ -157,4 +161,4 @@ class SecretSerializer : StdScalarSerializer<Any?>(String::class.java, false) {
157161
override fun serialize(value: Any?, gen: JsonGenerator?, provider: SerializerProvider?) {
158162
gen!!.writeString("****")
159163
}
160-
}
164+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.icerockdev.webserver.log
2+
3+
import com.icerockdev.api.AbstractResponse
4+
import com.icerockdev.api.Request
5+
import kotlin.reflect.KClass
6+
7+
class LoggingConfiguration(
8+
val requestTypes: List<KClass<*>> = listOf(Request::class),
9+
val responseTypes: List<KClass<*>> = listOf(AbstractResponse::class)
10+
)

0 commit comments

Comments
 (0)