1
1
/*
2
- * Copyright (C) 2011-2024 Thomas Akehurst
2
+ * Copyright (C) 2011-2025 Thomas Akehurst
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@ public class RequestPattern implements NamedValueMatcher<Request> {
46
46
private final String scheme ;
47
47
private final StringValuePattern host ;
48
48
private final Integer port ;
49
+ private final StringValuePattern clientIp ;
49
50
private final UrlPattern url ;
50
51
private final RequestMethod method ;
51
52
private final Map <String , MultiValuePattern > headers ;
@@ -66,6 +67,7 @@ public RequestPattern(
66
67
final String scheme ,
67
68
final StringValuePattern host ,
68
69
final Integer port ,
70
+ final StringValuePattern clientIp ,
69
71
final UrlPattern url ,
70
72
final RequestMethod method ,
71
73
final Map <String , MultiValuePattern > headers ,
@@ -81,6 +83,7 @@ public RequestPattern(
81
83
this .scheme = scheme ;
82
84
this .host = host ;
83
85
this .port = port ;
86
+ this .clientIp = clientIp ;
84
87
this .url = getFirstNonNull (url , UrlPattern .ANY );
85
88
this .method = getFirstNonNull (method , RequestMethod .ANY );
86
89
this .headers = headers ;
@@ -103,6 +106,7 @@ public MatchResult match(Request request) {
103
106
requestPartMatchResults .add (weight (schemeMatches (request ), 3.0 ));
104
107
requestPartMatchResults .add (weight (hostMatches (request ), 10.0 ));
105
108
requestPartMatchResults .add (weight (portMatches (request ), 10.0 ));
109
+ requestPartMatchResults .add (weight (clientIpMatches (request ), 3.0 ));
106
110
requestPartMatchResults .add (
107
111
weight (RequestPattern .this .url .match (request .getUrl ()), 10.0 ));
108
112
requestPartMatchResults .add (
@@ -146,6 +150,7 @@ public RequestPattern(
146
150
@ JsonProperty ("host" ) StringValuePattern host ,
147
151
@ JsonProperty ("port" ) Integer port ,
148
152
@ JsonProperty ("url" ) String url ,
153
+ @ JsonProperty ("clientIp" ) StringValuePattern clientIp ,
149
154
@ JsonProperty ("urlPattern" ) String urlPattern ,
150
155
@ JsonProperty ("urlPath" ) String urlPath ,
151
156
@ JsonProperty ("urlPathPattern" ) String urlPathPattern ,
@@ -165,6 +170,7 @@ public RequestPattern(
165
170
scheme ,
166
171
host ,
167
172
port ,
173
+ clientIp ,
168
174
UrlPattern .fromOneOf (url , urlPattern , urlPath , urlPathPattern , urlPathTemplate ),
169
175
method ,
170
176
headers ,
@@ -184,6 +190,7 @@ public RequestPattern(
184
190
null ,
185
191
null ,
186
192
null ,
193
+ null ,
187
194
anyUrl (),
188
195
RequestMethod .ANY ,
189
196
null ,
@@ -202,6 +209,7 @@ public RequestPattern(ValueMatcher<Request> customMatcher) {
202
209
null ,
203
210
null ,
204
211
null ,
212
+ null ,
205
213
UrlPattern .ANY ,
206
214
RequestMethod .ANY ,
207
215
null ,
@@ -221,6 +229,7 @@ public RequestPattern(CustomMatcherDefinition customMatcherDefinition) {
221
229
null ,
222
230
null ,
223
231
null ,
232
+ null ,
224
233
UrlPattern .ANY ,
225
234
RequestMethod .ANY ,
226
235
null ,
@@ -299,6 +308,10 @@ private MatchResult portMatches(final Request request) {
299
308
return port != null ? MatchResult .of (request .getPort () == port ) : MatchResult .exactMatch ();
300
309
}
301
310
311
+ private MatchResult clientIpMatches (final Request request ) {
312
+ return clientIp != null ? clientIp .match (request .getClientIp ()) : MatchResult .exactMatch ();
313
+ }
314
+
302
315
private MatchResult allHeadersMatchResult (final Request request ) {
303
316
Map <String , MultiValuePattern > combinedHeaders = combineBasicAuthAndOtherHeaders ();
304
317
@@ -427,6 +440,10 @@ public Integer getPort() {
427
440
return port ;
428
441
}
429
442
443
+ public StringValuePattern getClientIp () {
444
+ return clientIp ;
445
+ }
446
+
430
447
public String getUrl () {
431
448
return urlPatternOrNull (UrlPattern .class , false );
432
449
}
0 commit comments