@Override public Object getParamValue(Req req) { return req.response(); }
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); } }
@Override public AuthResponse execute(Req req, Resp resp) { AuthResponse auth = new AuthResponse(); String username = req.data("username"); String password = req.data("password"); auth.success = resp.login(username, password); auth.token = auth.success ? Tokens.serialize(req.token()) : ""; return auth; }
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); } }); }
public static boolean isGetReq(Req req) { return req.verb().equals(GET); }
"/pods/http:" + podName + ":" + String.valueOf(podJolokiaPort.get()) + "/proxy/jolokia/" + jolokiaPath + "?" + req.query(); Request request; switch (req.verb()) { case "GET": request = new Request.Builder().url(url).build(); break; case "POST": request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse(req.contentType().toString()), req.body())).build(); break; case "PUT": request = new Request.Builder().url(url).put(RequestBody.create(MediaType.parse(req.contentType().toString()), req.body())).build(); break; case "DELETE": request = new Request.Builder().url(url).delete(RequestBody.create(MediaType.parse(req.contentType().toString()), req.body())).build(); break; case "HEAD": break; default: logger.info("Unsupported Verb {}", req.verb()); return Optional.empty();
@SuppressWarnings("unchecked") public Object postProcessResult(Req req, Object result) { if (result instanceof HttpStatus) { return result; } else if (result instanceof Req) { U.must(req == result, "Unknown request instance was received as result!"); return reqToStatus(req, result); } else if (result instanceof Resp) { U.must(req != null && req.response() == result, "Unknown response instance was received as result!"); return reqToStatus(req, result); } else if (result == null || result instanceof NotFound) { // not found return HttpStatus.NOT_FOUND; } else if ((result instanceof Future<?>) || (result instanceof org.rapidoid.concurrent.Future<?>)) { // async if (req != null) { req.async(); } return HttpStatus.ASYNC; } else if (result instanceof Results) { return ((Results) result).all(); // fetch while still inside tx (potentially) } else { return result; } }
@Override public Object execute(Req req) throws Exception { String code = req.param("code"); String state = req.param("state"); state = state.substring(1); U.must(stateCheck.isValidState(state, secret, req.sessionId()), "Invalid OAuth state!"); user.oauthId = String.valueOf(auth.get("id")); req.response().authorize(user); return req.response().redirect("/"); String error = req.param("error"); if (error != null) { Log.warn("OAuth error", "error", error);
@Override public Map<String, Serializable> token() { return req().token(); }
private static boolean isCurl(Req req) { return req.header("User-Agent", "").toLowerCase().startsWith("curl/"); }
@Override public Object execute(Req req) throws Exception { String scope = req.param("scope"); PeerDiscoveryInfo info = new PeerDiscoveryInfo(req.clientIpAddress(), req.realIpAddress()); state.clients.get(scope).add(info); return NiceResponse.ok(req, "Successfully registered for discovery"); }
public static String inferRealIpAddress(Req req) { // TODO if CloudFlare is detected, use req.header("cf-connecting-ip")? return req.clientIpAddress(); }
@Override public String uri() { return req().uri(); }