public OperationalContext retrieve() {
return urlToContextMapping.get(proxyUrl);
}
};
final HttpFiltersSourceAdapter httpFiltersSource = serviceLoader().onlyOne(HttpFiltersSourceAdapter.class);
final ContextualOperation<HttpRequest, HttpFilters> filterRequest = Contextualizer.contextualize(retriever,
new ContextualOperation<HttpRequest, HttpFilters>() {
@Override
public HttpFilters performInContext(HttpRequest originalRequest) {
return httpFiltersSource.filterRequest(originalRequest);
}
}
);
final InetSocketAddress bindToAddress = new InetSocketAddress(proxyUrl.getHost(), proxyUrl.getPort());
final InetSocketAddress forwardToAddress = new InetSocketAddress(realUrl.getHost(), realUrl.getPort());
return DefaultHttpProxyServer
.bootstrap()
.withAddress(bindToAddress)
.withTransparent(true)
.withChainProxyManager(new ChainedProxyManager() {
@Override
public void lookupChainedProxies(HttpRequest httpRequest, Queue<ChainedProxy> chainedProxies) {
chainedProxies.add(new ChainedProxyAdapter() {
@Override
public InetSocketAddress getChainedProxyAddress() {
return forwardToAddress;
}
});
}
})
.withFiltersSource(new HttpFiltersSourceAdapter() {
@Override
public HttpFilters filterRequest(HttpRequest originalRequest) {
return filterRequest.performInContext(originalRequest);
}
@Override
public int getMaximumRequestBufferSizeInBytes() {
return httpFiltersSource.getMaximumRequestBufferSizeInBytes();
}
@Override
public int getMaximumResponseBufferSizeInBytes() {
return httpFiltersSource.getMaximumResponseBufferSizeInBytes();
}
})
.start();
}