From d3f0f99dd3e07f59d0c4e3efab0a2d5756142959 Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Tue, 25 Apr 2023 22:21:31 +0200 Subject: [PATCH] Extract filter lambda method to a concrete class --- .../server/InternalCommunicationModule.java | 45 +++++++++++-------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/core/trino-main/src/main/java/io/trino/server/InternalCommunicationModule.java b/core/trino-main/src/main/java/io/trino/server/InternalCommunicationModule.java index d71bd5dfee5b..a09276a55f10 100644 --- a/core/trino-main/src/main/java/io/trino/server/InternalCommunicationModule.java +++ b/core/trino-main/src/main/java/io/trino/server/InternalCommunicationModule.java @@ -57,10 +57,7 @@ protected void setup(Binder binder) }); configBinder(binder).bindConfigGlobalDefaults(NodeConfig.class, config -> config.setInternalAddressSource(IP_ENCODED_AS_HOSTNAME)); // rewrite discovery client requests to use IP encoded as hostname - newSetBinder(binder, HttpRequestFilter.class, ForDiscoveryClient.class).addBinding() - .toInstance(request -> Request.Builder.fromRequest(request) - .setUri(toIpEncodedAsHostnameUri(request.getUri())) - .build()); + newSetBinder(binder, HttpRequestFilter.class, ForDiscoveryClient.class).addBinding().to(DiscoveryEncodeAddressAsHostname.class); } else { configBinder(binder).bindConfigGlobalDefaults(HttpClientConfig.class, config -> { @@ -77,22 +74,34 @@ protected void setup(Binder binder) httpClientBinder(binder).bindGlobalFilter(InternalAuthenticationManager.class); } - private static URI toIpEncodedAsHostnameUri(URI uri) + private static class DiscoveryEncodeAddressAsHostname + implements HttpRequestFilter { - if (!uri.getScheme().equals("https")) { - return uri; + @Override + public Request filterRequest(Request request) + { + return Request.Builder.fromRequest(request) + .setUri(toIpEncodedAsHostnameUri(request.getUri())) + .build(); } - try { - String host = uri.getHost(); - InetAddress inetAddress = InetAddress.getByName(host); - String addressAsHostname = encodeAddressAsHostname(inetAddress); - return new URI(uri.getScheme(), uri.getUserInfo(), addressAsHostname, uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()); - } - catch (UnknownHostException e) { - throw new UncheckedIOException(e); - } - catch (URISyntaxException e) { - throw new RuntimeException(e); + + private static URI toIpEncodedAsHostnameUri(URI uri) + { + if (!uri.getScheme().equals("https")) { + return uri; + } + try { + String host = uri.getHost(); + InetAddress inetAddress = InetAddress.getByName(host); + String addressAsHostname = encodeAddressAsHostname(inetAddress); + return new URI(uri.getScheme(), uri.getUserInfo(), addressAsHostname, uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()); + } + catch (UnknownHostException e) { + throw new UncheckedIOException(e); + } + catch (URISyntaxException e) { + throw new RuntimeException(e); + } } } }