Skip to content

Commit 68f04c1

Browse files
committed
refactor: allows script engines to customise request object.
1 parent d2cb3e0 commit 68f04c1

File tree

16 files changed

+406
-181
lines changed

16 files changed

+406
-181
lines changed

core/api/src/main/java/io/gatehill/imposter/script/ExecutionContext.kt

Lines changed: 1 addition & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,13 @@
4242
*/
4343
package io.gatehill.imposter.script
4444

45-
import io.gatehill.imposter.util.CollectionUtil
46-
import java.util.function.Supplier
47-
4845
/**
4946
* Wrapper for context variables available during script execution.
5047
*
5148
* @author Pete Cornish
5249
*/
5350
class ExecutionContext(
54-
request: Request
51+
request: ScriptRequest
5552
) : HashMap<String, Any>() {
5653

5754
init {
@@ -73,115 +70,4 @@ class ExecutionContext(
7370

7471
return super.get(key)
7572
}
76-
77-
interface Request {
78-
val path: String
79-
val method: String
80-
val uri: String
81-
val headers: Map<String, String>
82-
83-
/**
84-
* @return the request path parameters
85-
*/
86-
val pathParams: Map<String, String>
87-
88-
/**
89-
* @return the request query parameters
90-
*/
91-
val queryParams: Map<String, String>
92-
93-
/**
94-
* @return the request form parameters
95-
*/
96-
val formParams: Map<String, String>
97-
98-
/**
99-
* @return the request body
100-
*/
101-
val body: String?
102-
103-
/**
104-
* @return the [headers] map, but with all keys in lowercase
105-
*/
106-
val normalisedHeaders: Map<String, String>
107-
108-
/**
109-
* Legacy property removed.
110-
*/
111-
@get:Deprecated("Use queryParams instead.", ReplaceWith("queryParams"))
112-
val params: Map<String, String>
113-
}
114-
115-
/**
116-
* Representation of the request, supporting lazily-initialised collections for params and headers.
117-
*/
118-
class RequestImpl(
119-
override val path: String,
120-
override val method: String,
121-
override val uri: String,
122-
private val headersSupplier: Supplier<Map<String, String>>,
123-
private val pathParamsSupplier: Supplier<Map<String, String>>,
124-
private val queryParamsSupplier: Supplier<Map<String, String>>,
125-
private val formParamsSupplier: Supplier<Map<String, String>>,
126-
private val bodySupplier: Supplier<String?>,
127-
) : Request {
128-
override val headers: Map<String, String> by lazy {
129-
headersSupplier.get()
130-
}
131-
132-
/**
133-
* @return the request path parameters
134-
*/
135-
override val pathParams: Map<String, String> get() {
136-
return pathParamsSupplier.get()
137-
}
138-
139-
/**
140-
* @return the request query parameters
141-
*/
142-
override val queryParams: Map<String, String> by lazy {
143-
queryParamsSupplier.get()
144-
}
145-
146-
/**
147-
* @return the request form parameters
148-
*/
149-
override val formParams: Map<String, String> by lazy {
150-
formParamsSupplier.get()
151-
}
152-
153-
/**
154-
* @return the request body
155-
*/
156-
override val body: String? by lazy {
157-
bodySupplier.get()
158-
}
159-
160-
/**
161-
* @return the [headers] map, but with all keys in lowercase
162-
*/
163-
override val normalisedHeaders: Map<String, String>
164-
get() = CollectionUtil.convertKeysToLowerCase(headers)
165-
166-
/**
167-
* Legacy property removed.
168-
*/
169-
@get:Deprecated("Use queryParams instead.", ReplaceWith("queryParams"))
170-
override val params: Map<String, String>
171-
get() = throw UnsupportedOperationException(
172-
"Error: the deprecated 'context.request.params' property was removed. Use 'context.request.queryParams' or 'context.request.pathParams' instead."
173-
)
174-
175-
override fun toString(): String {
176-
return "Request{" +
177-
"path='" + path + '\'' +
178-
", method='" + method + '\'' +
179-
", uri='" + uri + '\'' +
180-
", pathParams=" + pathParams +
181-
", queryParams=" + queryParams +
182-
", headers=" + headers +
183-
", body=<" + (body?.let { "${it.length} bytes" } ?: "null") + '>' +
184-
'}'
185-
}
186-
}
18773
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2024.
3+
*
4+
* This file is part of Imposter.
5+
*
6+
* "Commons Clause" License Condition v1.0
7+
*
8+
* The Software is provided to you by the Licensor under the License, as
9+
* defined below, subject to the following condition.
10+
*
11+
* Without limiting other conditions in the License, the grant of rights
12+
* under the License will not include, and the License does not grant to
13+
* you, the right to Sell the Software.
14+
*
15+
* For purposes of the foregoing, "Sell" means practicing any or all of
16+
* the rights granted to you under the License to provide to third parties,
17+
* for a fee or other consideration (including without limitation fees for
18+
* hosting or consulting/support services related to the Software), a
19+
* product or service whose value derives, entirely or substantially, from
20+
* the functionality of the Software. Any license notice or attribution
21+
* required by the License must also include this Commons Clause License
22+
* Condition notice.
23+
*
24+
* Software: Imposter
25+
*
26+
* License: GNU Lesser General Public License version 3
27+
*
28+
* Licensor: Peter Cornish
29+
*
30+
* Imposter is free software: you can redistribute it and/or modify
31+
* it under the terms of the GNU Lesser General Public License as published by
32+
* the Free Software Foundation, either version 3 of the License, or
33+
* (at your option) any later version.
34+
*
35+
* Imposter is distributed in the hope that it will be useful,
36+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
37+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38+
* GNU Lesser General Public License for more details.
39+
*
40+
* You should have received a copy of the GNU Lesser General Public License
41+
* along with Imposter. If not, see <https://www.gnu.org/licenses/>.
42+
*/
43+
44+
package io.gatehill.imposter.script
45+
46+
interface ScriptRequest {
47+
val path: String
48+
val method: String
49+
val uri: String
50+
val headers: Map<String, String>
51+
52+
/**
53+
* @return the request path parameters
54+
*/
55+
val pathParams: Map<String, String>
56+
57+
/**
58+
* @return the request query parameters
59+
*/
60+
val queryParams: Map<String, String>
61+
62+
/**
63+
* @return the request form parameters
64+
*/
65+
val formParams: Map<String, String>
66+
67+
/**
68+
* @return the request body
69+
*/
70+
val body: String?
71+
72+
/**
73+
* @return the [headers] map, but with all keys in lowercase
74+
*/
75+
val normalisedHeaders: Map<String, String>
76+
77+
/**
78+
* Legacy property removed.
79+
*/
80+
@get:Deprecated("Use queryParams instead.", ReplaceWith("queryParams"))
81+
val params: Map<String, String>
82+
}

core/api/src/main/java/io/gatehill/imposter/service/ScriptService.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,17 @@
4242
*/
4343
package io.gatehill.imposter.service
4444

45+
import io.gatehill.imposter.http.HttpRequest
4546
import io.gatehill.imposter.script.ReadWriteResponseBehaviour
4647
import io.gatehill.imposter.script.RuntimeContext
48+
import io.gatehill.imposter.script.ScriptRequest
4749

4850
/**
4951
* @author Pete Cornish
5052
*/
5153
interface ScriptService {
54+
val requestBuilder: ScriptRequestBuilder
55+
5256
fun initScript(script: ScriptSource) {
5357
// no op
5458
}
@@ -67,4 +71,7 @@ interface ScriptService {
6771

6872
fun evalInlineScript(scriptId: String, scriptCode: String, runtimeContext: RuntimeContext): Boolean =
6973
throw NotImplementedError()
74+
7075
}
76+
77+
typealias ScriptRequestBuilder = (request: HttpRequest) -> ScriptRequest

0 commit comments

Comments
 (0)