Skip to content

Commit 3315ba1

Browse files
committed
Ref #15718: allow to customize exchange
1 parent 88ba30f commit 3315ba1

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

src/main/java/eu/openanalytics/containerproxy/util/ProxyMappingManager.java

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,6 @@
2020
*/
2121
package eu.openanalytics.containerproxy.util;
2222

23-
import java.io.IOException;
24-
import java.lang.reflect.Field;
25-
import java.net.URI;
26-
import java.nio.ByteBuffer;
27-
import java.util.HashMap;
28-
import java.util.Map;
29-
import java.util.Map.Entry;
30-
import java.util.concurrent.TimeUnit;
31-
32-
import javax.inject.Inject;
33-
import javax.servlet.ServletException;
34-
import javax.servlet.http.HttpServletRequest;
35-
import javax.servlet.http.HttpServletResponse;
36-
37-
import org.springframework.context.annotation.Lazy;
38-
import org.springframework.stereotype.Component;
39-
4023
import eu.openanalytics.containerproxy.service.hearbeat.HeartbeatService;
4124
import io.undertow.server.HttpHandler;
4225
import io.undertow.server.HttpServerExchange;
@@ -49,6 +32,22 @@
4932
import io.undertow.servlet.handlers.ServletRequestContext;
5033
import io.undertow.util.AttachmentKey;
5134
import io.undertow.util.PathMatcher;
35+
import org.springframework.context.annotation.Lazy;
36+
import org.springframework.stereotype.Component;
37+
38+
import javax.inject.Inject;
39+
import javax.servlet.ServletException;
40+
import javax.servlet.http.HttpServletRequest;
41+
import javax.servlet.http.HttpServletResponse;
42+
import java.io.IOException;
43+
import java.lang.reflect.Field;
44+
import java.net.URI;
45+
import java.nio.ByteBuffer;
46+
import java.util.HashMap;
47+
import java.util.Map;
48+
import java.util.Map.Entry;
49+
import java.util.concurrent.TimeUnit;
50+
import java.util.function.Consumer;
5251

5352
/**
5453
* This component keeps track of which proxy mappings (i.e. URL endpoints) are currently registered,
@@ -82,7 +81,7 @@ public synchronized void addMapping(String proxyId, String mapping, URI target)
8281
if (mappings.containsKey(mapping)) {
8382
return;
8483
}
85-
84+
8685
LoadBalancingProxyClient proxyClient = new LoadBalancingProxyClient() {
8786
@Override
8887
public void getConnection(ProxyTarget target, HttpServerExchange exchange, ProxyCallback<ProxyConnection> callback, long timeout, TimeUnit timeUnit) {
@@ -132,17 +131,24 @@ public String getProxyId(String mapping) {
132131
* @throws ServletException If the dispatch fails for any other reason.
133132
*/
134133
public void dispatchAsync(String mapping, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
134+
dispatchAsync(mapping, request, response, null);
135+
}
136+
137+
public void dispatchAsync(String mapping, HttpServletRequest request, HttpServletResponse response, Consumer<HttpServerExchange> exchangeCustomizer) throws IOException, ServletException {
135138
HttpServerExchange exchange = ServletRequestContext.current().getExchange();
136139
exchange.putAttachment(ATTACHMENT_KEY_DISPATCHER, this);
137-
140+
138141
String queryString = request.getQueryString();
139142
queryString = (queryString == null) ? "" : "?" + queryString;
140143
String targetPath = PROXY_INTERNAL_ENDPOINT + "/" + mapping + queryString;
141-
144+
145+
if (exchangeCustomizer != null) {
146+
exchangeCustomizer.accept(exchange);
147+
}
142148
request.startAsync();
143149
request.getRequestDispatcher(targetPath).forward(request, response);
144150
}
145-
151+
146152
private static class ProxyPathHandler extends PathHandler {
147153

148154
public ProxyPathHandler(HttpHandler defaultHandler) {

0 commit comments

Comments
 (0)