Class ProxyHandler
- All Implemented Interfaces:
 Handler, Request.Handler, Container, Destroyable, Dumpable, Dumpable.DumpableContainer, LifeCycle, Invocable
- Direct Known Subclasses:
 ProxyHandler.Forward, ProxyHandler.Reverse
A Handler that can be used to implement a forward
proxy ("proxy") or a reverse proxy ("gateway") as defined by
RFC 7230.
This class uses HttpClient to send requests from the proxy to the server.
The HttpClient instance is either
set explicitly, or created implicitly.
To customize the implicit HttpClient instance, applications can
override newHttpClient() and configureHttpClient(HttpClient).
- See Also:
 
- 
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAProxyHandlerthat can be used to implement a forward proxy server.protected static classprotected classstatic classAProxyHandlerthat can be used to implement a reverse proxy.Nested classes/interfaces inherited from class Handler.Abstract
Handler.Abstract.NonBlockingNested classes/interfaces inherited from class AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener, AbstractLifeCycle.StopExceptionNested classes/interfaces inherited from interface Container
Container.InheritedListener, Container.ListenerNested classes/interfaces inherited from interface Dumpable
Dumpable.DumpableContainer, Dumpable.DumpAppendableNested classes/interfaces inherited from interface Handler
Handler.Abstract, Handler.AbstractContainer, Handler.Collection, Handler.Container, Handler.Sequence, Handler.Singleton, Handler.WrapperNested classes/interfaces inherited from interface Invocable
Invocable.Callable, Invocable.InvocationType, Invocable.ReadyTask, Invocable.TaskNested classes/interfaces inherited from interface LifeCycle
LifeCycle.ListenerNested classes/interfaces inherited from interface Request.Handler
Request.Handler.AbortException - 
Field Summary
Fields inherited from interface Invocable
__nonBlocking, NOOP - 
Constructor Summary
Constructors - 
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddForwardedHeader(Request clientToProxyRequest, Request proxyToServerRequest) protected voidaddProxyHeaders(Request clientToProxyRequest, Request proxyToServerRequest) protected voidaddViaHeader(Request clientToProxyRequest, Request proxyToServerRequest) protected voidconfigureHttpClient(HttpClient httpClient) Configures theHttpClientinstance before it is started.protected voidcopyRequestHeaders(Request clientToProxyRequest, Request proxyToServerRequest) protected voiddoStart()Starts the managed lifecycle beans in the order they were added.protected HttpFieldfilterServerToProxyResponseField(HttpField serverToProxyResponseField) Get the proxy-to-serverHostheader value.Get the value to use for theViaheader.booleanhandle(Request clientToProxyRequest, Response proxyToClientResponse, Callback proxyToClientCallback) Invoked to handle the passed HTTP request and response.protected HttpClientCreates a newHttpClientinstance, by default with a thread pool namedproxy-clientand with thedynamic transportconfigured only with HTTP/1.1.protected RequestnewProxyToServerRequest(Request clientToProxyRequest, HttpURI newHttpURI) protected Request.ContentnewProxyToServerRequestContent(Request clientToProxyRequest, Response proxyToClientResponse, Request proxyToServerRequest) protected Response.CompleteListenernewServerToProxyResponseListener(Request clientToProxyRequest, Request proxyToServerRequest, Response proxyToClientResponse, Callback proxyToClientCallback) protected voidonProxyToClientResponseComplete(Request clientToProxyRequest, Request proxyToServerRequest, Response serverToProxyResponse, Response proxyToClientResponse, Callback proxyToClientCallback) protected voidonProxyToClientResponseFailure(Request clientToProxyRequest, Request proxyToServerRequest, Response serverToProxyResponse, Response proxyToClientResponse, Callback proxyToClientCallback, Throwable failure) protected RunnableonServerToProxyResponse100Continue(Request clientToProxyRequest, Request proxyToServerRequest) protected voidonServerToProxyResponse102Processing(Request clientToProxyRequest, Request proxyToServerRequest, HttpFields serverToProxyResponseHeaders, Response proxyToClientResponse) protected voidonServerToProxyResponse103EarlyHints(Request clientToProxyRequest, Request proxyToServerRequest, HttpFields serverToProxyResponseHeaders, Response proxyToClientResponse) protected voidonServerToProxyResponseFailure(Request clientToProxyRequest, Request proxyToServerRequest, Response serverToProxyResponse, Response proxyToClientResponse, Callback proxyToClientCallback, Throwable failure) protected static Stringprotected abstract HttpURIrewriteHttpURI(Request clientToProxyRequest) Rewrites the client-to-proxy request URI to the proxy-to-server request URI.protected voidsendProxyToServerRequest(Request clientToProxyRequest, Request proxyToServerRequest, Response proxyToClientResponse, Callback proxyToClientCallback) voidsetHttpClient(HttpClient httpClient) voidsetProxyToServerHost(String host) Sets the value to use for theHostheader in proxy-to-server requests.voidsetViaHost(String viaHost) Sets the value to use for theViaheader in proxy-to-server requests.Methods inherited from class Handler.Abstract
destroy, doStop, getInvocationType, getServer, setServerMethods inherited from class ContainerLifeCycle
addBean, addBean, addEventListener, addManaged, contains, dump, dump, dump, dumpObjects, dumpStdErr, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, installBean, installBean, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, start, stop, unmanage, updateBean, updateBean, updateBeans, updateBeansMethods inherited from class AbstractLifeCycle
getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, setEventListeners, start, stop, toStringMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Container
getCachedBeans, getEventListenersMethods inherited from interface Dumpable.DumpableContainer
isDumpableMethods inherited from interface LifeCycle
addEventListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeEventListener, start, stop 
- 
Constructor Details
- 
ProxyHandler
public ProxyHandler() 
 - 
 - 
Method Details
- 
getHttpClient
 - 
setHttpClient
 - 
getProxyToServerHost
Get the proxy-to-serverHostheader value.- Returns:
 - the proxy-to-server 
Hostheader value 
 - 
setProxyToServerHost
Sets the value to use for the
Hostheader in proxy-to-server requests.If
null, the client-to-proxy value is used.- Parameters:
 host- the proxy-to-serverHostheader value
 - 
getViaHost
Get the value to use for theViaheader.- Returns:
 - the value to use for the 
Viaheader 
 - 
setViaHost
Sets the value to use for the
Viaheader in proxy-to-server requests.If
null, the local host name is used.- Parameters:
 viaHost- the value to use for theViaheader
 - 
doStart
Description copied from class:ContainerLifeCycleStarts the managed lifecycle beans in the order they were added.- Overrides:
 doStartin classHandler.Abstract- Throws:
 Exception- If there was a problem starting. Will cause a transition to FAILED state
 - 
newHttpClient
Creates a new
HttpClientinstance, by default with a thread pool namedproxy-clientand with thedynamic transportconfigured only with HTTP/1.1.- Returns:
 - a new 
HttpClientinstance 
 - 
configureHttpClient
Configures the
HttpClientinstance before it is started.- Parameters:
 httpClient- theHttpClientinstance to configure
 - 
requestId
 - 
handle
public boolean handle(Request clientToProxyRequest, Response proxyToClientResponse, Callback proxyToClientCallback) Description copied from interface:Request.HandlerInvoked to handle the passed HTTP request and response.
The request is accepted by returning true, then handling must be concluded by completing the passed callback. The handling may be asynchronous, i.e. this method may return true and complete the given callback later, possibly from a different thread. If this method returns false, then the callback must not be invoked and any mutation on the response reversed.
Exceptions thrown by this method may be subsequently handled by an error
Request.Handler, if present, otherwise a default HTTP 500 error is generated and the callback completed while writing the error response.The simplest implementation is:
public boolean handle(Request request, Response response, Callback callback) { callback.succeeded(); return true; }A HelloWorld implementation is:
public boolean handle(Request request, Response response, Callback callback) { response.write(true, ByteBuffer.wrap("Hello World\n".getBytes(StandardCharsets.UTF_8)), callback); return true; }- Parameters:
 clientToProxyRequest- the HTTP request to handleproxyToClientResponse- the HTTP response to handleproxyToClientCallback- the callback to complete when the handling is complete- Returns:
 - True if and only if the request will be handled, a response generated and the callback eventually called. This may occur within the scope of the call to this method, or asynchronously some time later. If false is returned, then this method must not generate a response, nor complete the callback.
 - See Also:
 
 - 
rewriteHttpURI
 - 
newProxyToServerRequest
 - 
copyRequestHeaders
 - 
addProxyHeaders
 - 
addViaHeader
 - 
addForwardedHeader
 - 
newProxyToServerRequestContent
protected Request.Content newProxyToServerRequestContent(Request clientToProxyRequest, Response proxyToClientResponse, Request proxyToServerRequest)  - 
sendProxyToServerRequest
 - 
newServerToProxyResponseListener
protected Response.CompleteListener newServerToProxyResponseListener(Request clientToProxyRequest, Request proxyToServerRequest, Response proxyToClientResponse, Callback proxyToClientCallback)  - 
filterServerToProxyResponseField
 - 
onServerToProxyResponseFailure
 - 
onServerToProxyResponse100Continue
 - 
onServerToProxyResponse102Processing
protected void onServerToProxyResponse102Processing(Request clientToProxyRequest, Request proxyToServerRequest, HttpFields serverToProxyResponseHeaders, Response proxyToClientResponse)  - 
onServerToProxyResponse103EarlyHints
protected void onServerToProxyResponse103EarlyHints(Request clientToProxyRequest, Request proxyToServerRequest, HttpFields serverToProxyResponseHeaders, Response proxyToClientResponse)  - 
onProxyToClientResponseComplete
 - 
onProxyToClientResponseFailure
 
 -