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 aMuHandlerthat 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 ReverseProxyBuilderaddProxyCompleteListener(ProxyCompleteListener proxyCompleteListener)Registers a proxy completion listener.ReverseProxybuild()Creates a new ReverseProxy which is a MuHandler.static java.net.http.HttpClient.BuildercreateHttpClientBuilder(boolean trustAll)Creates a new HTTP Client builder that is suitable for use in mu reverse proxy.ReverseProxyBuilderdiscardClientForwardedHeaders(boolean discardClientForwardedHeaders)If true, then anyForwardedorX-Forwarded-*headers that are sent from the client to this reverse proxy will be dropped (defaults to false).ReverseProxyBuilderproxyHostHeader(boolean sendHostToTarget)Specifies whether or not to send the originalHostheader to the target server.static ReverseProxyBuilderreverseProxy()Creates and returns a new instance of a reverse proxy builder.ReverseProxyBuildersendLegacyForwardedHeaders(boolean sendLegacyForwardedHeaders)Murp always sendsForwardedheaders, however by default does not send the non-standardX-Forwarded-*headers.ReverseProxyBuilderwithHttpClient(java.net.http.HttpClient httpClient)Specifies the JDK HTTP client to use to make the request to the target server.ReverseProxyBuilderwithRequestInterceptor(RequestInterceptor requestInterceptor)Adds an interceptor to the point where a request to the target server has been prepared, but not sent.ReverseProxyBuilderwithResponseInterceptor(ResponseInterceptor responseInterceptor)Adds an interceptor to the point where a response to the client has been prepared, but not sent.ReverseProxyBuilderwithTotalTimeout(long totalTimeoutInMillis)Sets the total request timeout in millis for a proxied request.ReverseProxyBuilderwithTotalTimeout(long totalTimeout, java.util.concurrent.TimeUnit unit)Sets the total request timeout in millis for a proxied request.ReverseProxyBuilderwithUriMapper(UriMapper uriMapper)Required value.ReverseProxyBuilderwithViaName(java.lang.String viaName)The name to add as theViaheader, which defaults toprivate.
-
-
-
Method Detail
-
withViaName
public ReverseProxyBuilder withViaName(java.lang.String viaName)
The name to add as theViaheader, which defaults toprivate.- Parameters:
viaName- The name to add to theViaheader.- 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 sendsForwardedheaders, however by default does not send the non-standardX-Forwarded-*headers. Set this totrueto enable these legacy headers for older clients that rely on them.- Parameters:
sendLegacyForwardedHeaders-trueto forward headers such asX-Forwarded-Host; otherwisefalse- Returns:
- This builder
-
proxyHostHeader
public ReverseProxyBuilder proxyHostHeader(boolean sendHostToTarget)
Specifies whether or not to send the original
Hostheader to the target server.Reverse proxies are generally supposed to forward the original
Hostheader 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) theHostrequest header will be sent to the target; iffalsethen the host header will be based on the target's URL.- Returns:
- This builder
-
discardClientForwardedHeaders
public ReverseProxyBuilder discardClientForwardedHeaders(boolean discardClientForwardedHeaders)
If true, then anyForwardedorX-Forwarded-*headers that are sent from the client to this reverse proxy will be dropped (defaults to false). Set this totrueif you do not trust the client.- Parameters:
discardClientForwardedHeaders-trueto 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.
-
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:
buildin interfaceio.muserver.MuHandlerBuilder<ReverseProxy>
-
-