Implementation:Apache Spark ProxyUtils
| Knowledge Sources | |
|---|---|
| Domains | Security, YARN, Web_UI |
| Last Updated | 2026-02-08 22:00 GMT |
Overview
Utility class providing HTTP proxy helper methods for redirect handling, error responses, and request validation in the YARN web proxy.
Description
ProxyUtils is a public utility class copied from Hadoop 3.4.0's `org.apache.hadoop.yarn.server.webproxy.ProxyUtils` and modified to use Jakarta Servlet APIs. It provides three static utility methods: `sendRedirect` for generating HTTP 302 redirects with an HTML body, `notFound` for generating HTTP 404 responses, and `rejectNonHttpRequests` for validating that incoming servlet requests are HTTP requests. The redirect method generates a user-friendly HTML page with a clickable link to the target URL using Hadoop's Hamlet HTML builder.
Usage
Use this class when implementing or extending YARN web proxy functionality in Spark. It is used internally by `AmIpFilter` to handle redirects and request validation. Application code generally does not call this directly.
Code Reference
Source Location
- Repository: Apache_Spark
- File: resource-managers/yarn/.../ProxyUtils.java
- Lines: 1-126
Signature
public class ProxyUtils {
public static final String E_HTTP_HTTPS_ONLY =
"This filter only works for HTTP/HTTPS";
public static final String LOCATION = "Location";
public static void sendRedirect(HttpServletRequest request,
HttpServletResponse response, String target) throws IOException { ... }
public static void notFound(HttpServletResponse resp, String message)
throws IOException { ... }
public static void rejectNonHttpRequests(ServletRequest req)
throws ServletException { ... }
}
Import
import org.apache.spark.deploy.yarn.ProxyUtils;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| request | HttpServletRequest | Yes (sendRedirect) | The original HTTP request |
| response | HttpServletResponse | Yes | The HTTP response to write to |
| target | String | Yes (sendRedirect) | Target URL for redirection |
| message | String | Yes (notFound) | Message to display on 404 page |
| req | ServletRequest | Yes (rejectNonHttp) | Request to validate |
Outputs
| Name | Type | Description |
|---|---|---|
| HTTP 302 response | HttpServletResponse | Redirect with Location header and HTML body |
| HTTP 404 response | HttpServletResponse | Not found with HTML message |
| ServletException | Exception | Thrown if request is not HTTP |
Usage Examples
Redirect Usage
// Inside a servlet filter or handler:
ProxyUtils.rejectNonHttpRequests(req);
HttpServletRequest httpReq = (HttpServletRequest) req;
HttpServletResponse httpResp = (HttpServletResponse) resp;
// Redirect to the proxy URL
ProxyUtils.sendRedirect(httpReq, httpResp,
"http://rm.example.com:8088/proxy/application_123");