Skip to content

Commit 7c4993c

Browse files
Allow request mappings to handle a request based on host (fixes mkopylec#121)
1 parent f6311c9 commit 7c4993c

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

charon-spring-webmvc/src/main/java/com/github/mkopylec/charon/configuration/RequestMappingConfiguration.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
public class RequestMappingConfiguration implements Valid {
2020

2121
private String name;
22+
private Pattern hostRegex;
2223
private Pattern pathRegex;
2324
private RestTemplateConfiguration restTemplateConfiguration;
2425
private List<RequestForwardingInterceptor> requestForwardingInterceptors;
@@ -27,6 +28,7 @@ public class RequestMappingConfiguration implements Valid {
2728

2829
RequestMappingConfiguration(String name) {
2930
this.name = name;
31+
hostRegex = compile(".*");
3032
pathRegex = compile("/.*");
3133
requestForwardingInterceptors = new ArrayList<>();
3234
unsetRequestForwardingInterceptors = new ArrayList<>();
@@ -41,6 +43,14 @@ public String getName() {
4143
return name;
4244
}
4345

46+
public Pattern getHostRegex() {
47+
return hostRegex;
48+
}
49+
50+
void setHostRegex(String hostRegex) {
51+
this.hostRegex = compile(hostRegex);
52+
}
53+
4454
public Pattern getPathRegex() {
4555
return pathRegex;
4656
}

charon-spring-webmvc/src/main/java/com/github/mkopylec/charon/configuration/RequestMappingConfigurer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public static RequestMappingConfigurer requestMapping(String name) {
1414
return new RequestMappingConfigurer(name);
1515
}
1616

17+
public RequestMappingConfigurer hostRegex(String hostRegex) {
18+
configuredObject.setHostRegex(hostRegex);
19+
return this;
20+
}
21+
1722
public RequestMappingConfigurer pathRegex(String pathRegex) {
1823
configuredObject.setPathRegex(pathRegex);
1924
return this;

charon-spring-webmvc/src/main/java/com/github/mkopylec/charon/forwarding/RequestMappingResolver.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ class RequestMappingResolver {
2323
}
2424

2525
RequestMappingConfiguration resolveRequestMapping(HttpServletRequest request) {
26+
final String serverName = request.getServerName();
2627
String requestURI = request.getRequestURI();
2728
List<RequestMappingConfiguration> configurations = requestMappingConfigurations.stream()
28-
.filter(configuration -> configuration.getPathRegex().matcher(requestURI).matches())
29+
.filter(configuration ->
30+
configuration.getHostRegex().matcher(serverName).matches() &&
31+
configuration.getPathRegex().matcher(requestURI).matches())
2932
.collect(toList());
3033
if (configurations.isEmpty()) {
3134
log.debug("No request mapping matches {} incoming request", requestURI);

0 commit comments

Comments
 (0)