public ProxyHandler build() { return new ProxyHandler(this); } }
/** * Returns a handler that can act as a load balancing reverse proxy. * * @param proxyClient The proxy client to use to connect to the remote server * @return The proxy handler */ public static ProxyHandler proxyHandler(ProxyClient proxyClient) { return ProxyHandler.builder().setProxyClient(proxyClient).build(); }
@Override public void stop(StopContext stopContext) { ProxyHandler proxyHandler = (ProxyHandler) (this.proxyHandler.getValue() instanceof GlobalRequestControllerHandler ? ((GlobalRequestControllerHandler)this.proxyHandler.getValue()).getNext() : this.proxyHandler.getValue()); final LoadBalancingProxyClient client = (LoadBalancingProxyClient) proxyHandler.getProxyClient(); try { client.removeHost(getUri()); } catch (URISyntaxException e) { throw new RuntimeException(e); //impossible } }
final HeaderMap inboundRequestHeaders = exchange.getRequestHeaders(); final HeaderMap outboundRequestHeaders = request.getRequestHeaders(); copyHeaders(outboundRequestHeaders, inboundRequestHeaders);
@Override public void failed(IOException e) { handleFailure(exchange, proxyClientHandler, idempotentPredicate, e); } });
final HeaderMap outboundResponseHeaders = exchange.getResponseHeaders(); exchange.setStatusCode(response.getResponseCode()); copyHeaders(outboundResponseHeaders, inboundResponseHeaders);
@Override public void failed(IOException e) { handleFailure(exchange, proxyClientHandler, idempotentPredicate, e); } }
@Override public HttpHandler wrap(HttpHandler handler) { final LoadBalancingProxyClient loadBalancingProxyClient = new LoadBalancingProxyClient(); for (URI url : uris) { loadBalancingProxyClient.addHost(url); } return new ProxyHandler(loadBalancingProxyClient, -1, handler, rewriteHostHeader, false); } }
/** * Returns a handler that can act as a load balancing reverse proxy. * * @param proxyClient The proxy client to use to connect to the remote server * @param next The next handler to invoke if the proxy client does not know how to proxy the request * @return The proxy handler */ public static ProxyHandler proxyHandler(ProxyClient proxyClient, HttpHandler next) { return ProxyHandler.builder().setProxyClient(proxyClient).setNext(next).build(); }
final HeaderMap inboundRequestHeaders = exchange.getRequestHeaders(); final HeaderMap outboundRequestHeaders = request.getRequestHeaders(); copyHeaders(outboundRequestHeaders, inboundRequestHeaders);
@Override public void handleEvent(final StreamSinkChannel channel) { HeaderMap trailers = source.getAttachment(HttpAttachments.REQUEST_TRAILERS); if (trailers != null) { target.putAttachment(HttpAttachments.RESPONSE_TRAILERS, trailers); } try { channel.shutdownWrites(); if (!channel.flush()) { channel.getWriteSetter().set(ChannelListeners.flushingChannelListener(new ChannelListener<StreamSinkChannel>() { @Override public void handleEvent(StreamSinkChannel channel) { channel.suspendWrites(); channel.getWriteSetter().set(null); } }, ChannelListeners.closingChannelExceptionHandler())); channel.resumeWrites(); } else { channel.getWriteSetter().set(null); channel.shutdownWrites(); } } catch (IOException e) { handleFailure(exchange, proxyClientHandler, idempotentPredicate, e); } catch (Exception e) { handleFailure(exchange, proxyClientHandler, idempotentPredicate, new IOException(e)); } } }
@Override public void start(StartContext startContext) throws StartException { //todo: this is a bit of a hack, as the proxy handler may be wrapped by a request controller handler for graceful shutdown ProxyHandler proxyHandler = (ProxyHandler) (this.proxyHandler.getValue() instanceof GlobalRequestControllerHandler ? ((GlobalRequestControllerHandler)this.proxyHandler.getValue()).getNext() : this.proxyHandler.getValue()); final LoadBalancingProxyClient client = (LoadBalancingProxyClient) proxyHandler.getProxyClient(); try { SSLContext sslContext = this.sslContext.getOptionalValue(); if (sslContext == null) { SecurityRealm securityRealm = this.securityRealm.getOptionalValue(); if (securityRealm != null) { sslContext = securityRealm.getSSLContext(); } } if (sslContext == null) { client.addHost(getUri(), instanceId, null, OptionMap.create(UndertowOptions.ENABLE_HTTP2, enableHttp2)); } else { OptionMap.Builder builder = OptionMap.builder(); builder.set(Options.USE_DIRECT_BUFFERS, true); OptionMap combined = builder.getMap(); XnioSsl xnioSsl = new UndertowXnioSsl(Xnio.getInstance(), combined, sslContext); client.addHost(getUri(), instanceId, xnioSsl, OptionMap.create(UndertowOptions.ENABLE_HTTP2, enableHttp2)); } } catch (URISyntaxException e) { throw new StartException(e); } }
ProxyHandler handler = new ProxyHandler(lb, maxTime, ResponseCodeHandler.HANDLE_404, false, false, maxRetries); return handler;
/** * Returns a handler that can act as a load balancing reverse proxy. * * @param proxyClient The proxy client to use to connect to the remote server * @param maxRequestTime The maximum amount of time a request can be in progress before it is forcibly closed * @param next The next handler to invoke if the proxy client does not know how to proxy the request * @return The proxy handler */ public static ProxyHandler proxyHandler(ProxyClient proxyClient, int maxRequestTime, HttpHandler next) { return ProxyHandler.builder().setProxyClient(proxyClient).setNext(next).setMaxRequestTime(maxRequestTime).build(); } /**
final HeaderMap inboundRequestHeaders = exchange.getRequestHeaders(); final HeaderMap outboundRequestHeaders = request.getRequestHeaders(); copyHeaders(outboundRequestHeaders, inboundRequestHeaders);
@Override public void failed(IOException e) { handleFailure(exchange, proxyClientHandler, idempotentPredicate, e); } });
proxyClient = (LoadBalancingProxyClient) proxy.getProxyClient(); } else { proxyClient = (LoadBalancingProxyClient) ((ProxyHandler) proxyHandler).getProxyClient();
public ProxyHandler build() { return new ProxyHandler(this); } }
/** * Get the handler proxying the requests. * * @return the proxy handler */ public HttpHandler createProxyHandler() { return ProxyHandler.builder() .setProxyClient(container.getProxyClient()) .setMaxRequestTime(maxRequestTime) .setMaxConnectionRetries(maxRetries) .setReuseXForwarded(reuseXForwarded) .build(); }
final HeaderMap inboundRequestHeaders = exchange.getRequestHeaders(); final HeaderMap outboundRequestHeaders = request.getRequestHeaders(); copyHeaders(outboundRequestHeaders, inboundRequestHeaders);