Package io.muserver.murp
Class ReverseProxyBuilder
- java.lang.Object
-
- io.muserver.murp.ReverseProxyBuilder
-
- All Implemented Interfaces:
io.muserver.MuHandlerBuilder<ReverseProxy>
public class ReverseProxyBuilder extends java.lang.Object implements io.muserver.MuHandlerBuilder<ReverseProxy>
A builder for creating a reverse proxy, which is aMuHandler
that can be added to a Mu Server.- Version:
- 1.0
- Author:
- Daniel Flower
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ReverseProxyBuilder
addProxyCompleteListener(ProxyCompleteListener proxyCompleteListener)
Registers a proxy completion listener.ReverseProxy
build()
Creates a new ReverseProxy which is a MuHandler.static java.net.http.HttpClient.Builder
createHttpClientBuilder(boolean trustAll)
Creates a new HTTP Client builder that is suitable for use in mu reverse proxy.ReverseProxyBuilder
discardClientForwardedHeaders(boolean discardClientForwardedHeaders)
If true, then anyForwarded
orX-Forwarded-*
headers that are sent from the client to this reverse proxy will be dropped (defaults to false).ReverseProxyBuilder
proxyHostHeader(boolean sendHostToTarget)
Specifies whether to send the originalHost
header to the target server.static ReverseProxyBuilder
reverseProxy()
Creates and returns a new instance of a reverse proxy builder.ReverseProxyBuilder
sendLegacyForwardedHeaders(boolean sendLegacyForwardedHeaders)
Murp always sendsForwarded
headers, however by default does not send the non-standardX-Forwarded-*
headers.ReverseProxyBuilder
withHttpClient(java.net.http.HttpClient httpClient)
Specifies the JDK HTTP client to use to make the request to the target server.ReverseProxyBuilder
withProxyListener(ProxyListener proxyListener)
Adds a proxy listener to observe the life cycle of a request, it's useful for debug or metricReverseProxyBuilder
withRequestInterceptor(RequestInterceptor requestInterceptor)
Adds an interceptor to the point where a request to the target server has been prepared, but not sent.ReverseProxyBuilder
withResponseInterceptor(ResponseInterceptor responseInterceptor)
Adds an interceptor to the point where a response to the client has been prepared, but not sent.ReverseProxyBuilder
withTotalTimeout(long totalTimeoutInMillis)
Sets the total request timeout in millis for a proxied request.ReverseProxyBuilder
withTotalTimeout(long totalTimeout, java.util.concurrent.TimeUnit unit)
Sets the total request timeout in millis for a proxied request.ReverseProxyBuilder
withUriMapper(UriMapper uriMapper)
Required value.ReverseProxyBuilder
withViaName(java.lang.String viaName)
The name to add as theVia
header, which defaults toprivate
.
-
-
-
Method Detail
-
withViaName
public ReverseProxyBuilder withViaName(java.lang.String viaName)
The name to add as theVia
header, which defaults toprivate
.- Parameters:
viaName
- The name to add to theVia
header.- Returns:
- This builder
-
withHttpClient
public ReverseProxyBuilder withHttpClient(java.net.http.HttpClient httpClient)
Specifies the JDK HTTP client to use to make the request to the target server. It's recommended you do not set this in order to use the default client that is optimised for reverse proxy usage.- Parameters:
httpClient
- The HTTP client to use, or null to use the default client.- Returns:
- This builder
-
createHttpClientBuilder
public static java.net.http.HttpClient.Builder createHttpClientBuilder(boolean trustAll)
Creates a new HTTP Client builder that is suitable for use in mu reverse proxy.- Parameters:
trustAll
- If true, then any SSL certificate is allowed.- Returns:
- An HTTP Client builder
-
withUriMapper
public ReverseProxyBuilder withUriMapper(UriMapper uriMapper)
Required value. Sets the mapper to use for creating target URIs.If you want to proxy all requests to a single destination, consider using
UriMapper.toDomain(URI)
If the mapper function returns null, then the handler will not proxy the request and the next handler in the chain will be invoked (or a 404 will be returned if there are no further handlers that can handle the request).
- Parameters:
uriMapper
- A mapper that creates a target URI based on a client request.- Returns:
- This builder
-
sendLegacyForwardedHeaders
public ReverseProxyBuilder sendLegacyForwardedHeaders(boolean sendLegacyForwardedHeaders)
Murp always sendsForwarded
headers, however by default does not send the non-standardX-Forwarded-*
headers. Set this totrue
to enable these legacy headers for older clients that rely on them.- Parameters:
sendLegacyForwardedHeaders
-true
to forward headers such asX-Forwarded-Host
; otherwisefalse
- Returns:
- This builder
-
proxyHostHeader
public ReverseProxyBuilder proxyHostHeader(boolean sendHostToTarget)
Specifies whether to send the original
Host
header to the target server.Reverse proxies are generally supposed to forward the original
Host
header to target servers, however there are cases (particularly where you are proxying to HTTPS servers) that the Host needs to match the Host of the SSL certificate (in which case you may see SNI-related errors).- Parameters:
sendHostToTarget
- Iftrue
(which is the default) theHost
request header will be sent to the target; iffalse
then the host header will be based on the target's URL.- Returns:
- This builder
-
discardClientForwardedHeaders
public ReverseProxyBuilder discardClientForwardedHeaders(boolean discardClientForwardedHeaders)
If true, then anyForwarded
orX-Forwarded-*
headers that are sent from the client to this reverse proxy will be dropped (defaults to false). Set this totrue
if you do not trust the client.- Parameters:
discardClientForwardedHeaders
-true
to ignore Forwarded headers from the client; otherwisefalse
- Returns:
- This builder
-
withTotalTimeout
public ReverseProxyBuilder withTotalTimeout(long totalTimeoutInMillis)
Sets the total request timeout in millis for a proxied request. Defaults to 5 minutes.- Parameters:
totalTimeoutInMillis
- The allowed time in milliseconds for a request.- Returns:
- This builder
-
withTotalTimeout
public ReverseProxyBuilder withTotalTimeout(long totalTimeout, java.util.concurrent.TimeUnit unit)
Sets the total request timeout in millis for a proxied request. Defaults to 5 minutes.- Parameters:
totalTimeout
- The allowed time for a request.unit
- The timeout unit.- Returns:
- This builder
-
addProxyCompleteListener
public ReverseProxyBuilder addProxyCompleteListener(ProxyCompleteListener proxyCompleteListener)
Registers a proxy completion listener.- Parameters:
proxyCompleteListener
- A listener to be called when a proxy request is complete- Returns:
- This builder
-
withRequestInterceptor
public ReverseProxyBuilder withRequestInterceptor(RequestInterceptor requestInterceptor)
Adds an interceptor to the point where a request to the target server has been prepared, but not sent. This allows you to change the headers being proxied to the target server.- Parameters:
requestInterceptor
- An interceptor that may change the target request, or null to not have an interceptor.- Returns:
- This builder.
-
withResponseInterceptor
public ReverseProxyBuilder withResponseInterceptor(ResponseInterceptor responseInterceptor)
Adds an interceptor to the point where a response to the client has been prepared, but not sent. This allows you to change the response code or headers being returned to the client.- Parameters:
responseInterceptor
- An interceptor that may change the client response, or null to not have an interceptor.- Returns:
- This builder.
-
withProxyListener
public ReverseProxyBuilder withProxyListener(ProxyListener proxyListener)
Adds a proxy listener to observe the life cycle of a request, it's useful for debug or metric- Parameters:
proxyListener
- proxy listener- Returns:
- This builder.
-
reverseProxy
public static ReverseProxyBuilder reverseProxy()
Creates and returns a new instance of a reverse proxy builder.- Returns:
- A builder
-
build
public ReverseProxy build()
Creates a new ReverseProxy which is a MuHandler. You can pass the resulting handler directly toMuServerBuilder.addHandler(MuHandler)
- Specified by:
build
in interfaceio.muserver.MuHandlerBuilder<ReverseProxy>
-
-