diff --git a/SimpleProxyServlet.java b/SimpleProxyServlet.java index 6dae354..57b31eb 100644 --- a/SimpleProxyServlet.java +++ b/SimpleProxyServlet.java @@ -127,7 +127,11 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws String headerName = entry.getKey(); if (headerName != null && !headerName.equalsIgnoreCase("Transfer-Encoding")) { for (String value : entry.getValue()) { - resp.addHeader(headerName, value); + if (headerName.equalsIgnoreCase("Location") && (responseCode >= 300 && responseCode < 400)) { + resp.addHeader(headerName, rewriteUrl(value, req)); + } else { + resp.addHeader(headerName, value); + } } } } @@ -154,4 +158,33 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws } } + private String rewriteUrl(String locationHeader, HttpServletRequest request) { + if (locationHeader == null) { + return null; + } + + try { + URL originalUrl = new URL(locationHeader); + String proxyHost = request.getServerName(); + int proxyPort = request.getServerPort(); + String proxyScheme = request.getScheme(); + + // Use -1 for default port + if ((proxyScheme.equals("http") && proxyPort == 80) || (proxyScheme.equals("https") && proxyPort == 443)) { + proxyPort = -1; + } + + + // Build the rewritten URL using the proxy's host and port + URL rewrittenUrl = new URL(proxyScheme, proxyHost, proxyPort, originalUrl.getFile()); + log("Rewriting redirect Location header to: " + rewrittenUrl.toString()); + return rewrittenUrl.toString(); + + } catch (MalformedURLException e) { + // If the location is not a valid URL (e.g., a relative path), just return it as is + log("Location header is not a valid URL, passing through: " + locationHeader); + return locationHeader; + } + } + } diff --git a/apex/WEB-INF/web.xml b/apex/WEB-INF/web.xml index a772458..2f925ec 100644 --- a/apex/WEB-INF/web.xml +++ b/apex/WEB-INF/web.xml @@ -32,15 +32,15 @@ mapping.3.type - contains + prefix mapping.3.path - apex/wwv_flow.ajax? + /apex/wwv_flow.ajax mapping.3.url - ${baseUrl}/apex/wwv_flow.ajax? + ${baseUrl}/apex/wwv_flow.ajax mapping.4.type