/
CustomHeader.kt
50 lines (42 loc) · 1.67 KB
/
CustomHeader.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package io.ktor.samples.feature
import io.ktor.application.*
import io.ktor.util.pipeline.*
import io.ktor.response.*
import io.ktor.util.*
/**
* Adds custom header.
*/
class CustomHeader(configuration: Configuration) {
// get an immutable snapshot of a configuration values
private val name = configuration.headerName
private val value = configuration.headerValue
// Feature configuration class
class Configuration {
// mutable properties with default values so user can modify it
var headerName = "Custom"
var headerValue = "Value"
}
// Body of the feature
private fun intercept(context: PipelineContext<Unit, ApplicationCall>) {
// Add custom header to the response
context.call.response.header(name, value)
}
/**
* Installable feature for [CustomHeader].
*/
companion object Feature : ApplicationFeature<ApplicationCallPipeline, CustomHeader.Configuration, CustomHeader> {
override val key = AttributeKey<CustomHeader>("CustomHeader")
override fun install(pipeline: ApplicationCallPipeline, configure: Configuration.() -> Unit): CustomHeader {
// Call user code to configure a feature
val configuration = Configuration().apply(configure)
// Create a feature instance
val feature = CustomHeader(configuration)
// Install an interceptor that will be run on each call and call feature instance
pipeline.intercept(ApplicationCallPipeline.Call) {
feature.intercept(this)
}
// Return a feature instance so that client code can use it
return feature
}
}
}