public ReverseProxyMapDSL map(String uriPrefix) { return new ReverseProxyMapDSL(uriPrefix); }
public T reuseConnections(boolean reuseConnections) { this.reuseConnections = reuseConnections; return me(); }
public ReverseProxyMapDSL to(List<String> upstreams) { this.upstreams = ProxyUpstream.refine(upstreams); return this; }
private ReverseProxy createReverseProxy() { List<ProxyUpstream> proxyUpstreams = U.list(); U.notNull(upstreams, "proxy upstreams"); for (String upstream : upstreams) { proxyUpstreams.add(new ProxyUpstream(upstream)); } LoadBalancer balancer = loadBalancer != null ? loadBalancer : new RoundRobinLoadBalancer(); ProxyMapping mapping = new ProxyMapping(uriPrefix, balancer, proxyUpstreams); return new ReverseProxy(mapping); }
private void addExtraRequestHeaders(Req req, Map<String, String> headers) { String clientIpAddress = req.clientIpAddress(); if (setXUsernameHeader()) headers.put("X-Username", U.safe(Current.username())); if (setXRolesHeader()) headers.put("X-Roles", U.join(", ", Current.roles())); if (setXClientIPHeader()) headers.put("X-Client-IP", clientIpAddress); if (setXRealIPHeader()) headers.put("X-Real-IP", req.realIpAddress()); if (setXForwardedForHeader()) { String forwardedFor = headers.get("X-Forwarded-For"); if (U.notEmpty(forwardedFor)) { forwardedFor += ", " + clientIpAddress; } else { forwardedFor = clientIpAddress; } headers.put("X-Forwarded-For", forwardedFor); } }
private void process(final Req req, final Resp resp, final ProxyMapping mapping, final int attempts, final long since) { final String targetUrl = mapping.getTargetUrl(req); Map<String, String> headers = U.map(req.headers()); headers.remove("transfer-encoding"); headers.remove("content-length"); addExtraRequestHeaders(req, headers); HttpClient client = getOrCreateClient(); client.req() .verb(req.verb()) .url(targetUrl) .headers(headers) .cookies(req.cookies()) .body(req.body()) .raw(true) .execute((result, error) -> { if (error == null) { resp.code(result.code()); resp.body(result.bodyBytes()); // process the response headers SimpleHttpResp proxyResp = new SimpleHttpResp(); HttpUtils.proxyResponseHeaders(result.headers(), proxyResp); if (proxyResp.contentType != null) resp.contentType(proxyResp.contentType); if (proxyResp.headers != null) resp.headers().putAll(proxyResp.headers); if (proxyResp.cookies != null) resp.cookies().putAll(proxyResp.cookies); resp.done(); } else { handleError(error, req, resp, mapping, attempts, since); } }); }
@Override protected HttpClient createClient() { return HTTP.client() .reuseConnections(reuseConnections()) .keepCookies(false) .maxConnTotal(maxConnections()) .maxConnPerRoute(maxConnectionsPerRoute()); }
private void handleError(Throwable error, final Req req, final Resp resp, final ProxyMapping mapping, final int attempts, final long since) { if (error instanceof ConnectException || error instanceof IOException) { if (HttpUtils.isGetReq(req) && !U.timedOut(since, timeout())) { Jobs.after(retryDelay()).milliseconds(() -> process(req, resp, mapping, attempts + 1, since)); } else { HttpIO.INSTANCE.errorAndDone(req, U.rte("Couldn't connect to the upstream!", error), LogLevel.DEBUG); } } else { HttpIO.INSTANCE.errorAndDone(req, error, LogLevel.ERROR); } }
public static synchronized ReverseProxyMapDSL proxy(String uriPrefix) { return new ReverseProxyDSL().map(uriPrefix); }
@Override public Object execute(final Req req, final Resp resp) { ProxyMapping mapping = findMapping(req); if (mapping == null) return null; // not found! req.async(); process(req, resp, mapping, 1, U.time()); return req; }
public String getTargetUrl(Req req) { ProxyUpstream upstream = loadBalancer.pickUpstream(req, upstreams); String trimmed = prefix.equals("/") ? req.uri() : Str.triml(req.uri(), prefix); return upstream.url() + trimmed; }
public ReverseProxyMapDSL to(String... upstreams) { return to(U.list(upstreams)); }
/** * Use proxy(uriPrefix) instead */ @Deprecated public static synchronized ReverseProxyDSL proxy() { return new ReverseProxyDSL(); }
public ReverseProxy add() { return addTo(On.setup()); }
public ReverseProxy addTo(Setup setup) { Log.info("!Reverse proxy mapping", "!uriPrefix", uriPrefix, "!upstreams", upstreams); ReverseProxy proxy = createReverseProxy(); U.must(uriPrefix.startsWith("/"), "The URI prefix must start with '/'"); String path = uriPrefix.equals("/") ? "/*" : uriPrefix + "/*"; OnRoute route = setup.any(path); if (roles != null) route.roles(roles); if (cacheTTL != null) route.cacheTTL(cacheTTL); if (cacheCapacity != null) route.cacheCapacity(cacheCapacity); route.serve(proxy); return proxy; }
@Override public HttpClient call() throws Exception { return createClient(); } });
private ReverseProxy createReverseProxy() { List<ProxyUpstream> proxyUpstreams = U.list(); U.notNull(upstreams, "proxy upstreams"); for (String upstream : upstreams) { proxyUpstreams.add(new ProxyUpstream(upstream)); } LoadBalancer balancer = loadBalancer != null ? loadBalancer : new RoundRobinLoadBalancer(); ProxyMapping mapping = new ProxyMapping(uriPrefix, balancer, proxyUpstreams); return new ReverseProxy(mapping); }
public T client(HttpClient client) { this.client.setValue(client); return me(); }
public ReverseProxyMapDSL to(List<String> upstreams) { this.upstreams = ProxyUpstream.refine(upstreams); return this; }
public T reuseConnections(boolean reuseConnections) { this.reuseConnections = reuseConnections; return me(); }