From b92b2c52e0bbe3d28e52d5f2bd6b6a103caf2785 Mon Sep 17 00:00:00 2001 From: labkey-jeckels Date: Tue, 23 Jan 2024 16:24:18 -0800 Subject: [PATCH 1/2] Support additional context paths - for the primary deployment, for redirecting, and for secondary static content --- api/src/org/labkey/api/ApiModule.java | 9 ++++ .../labkey/api/view/RedirectorServlet.java | 51 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 api/src/org/labkey/api/view/RedirectorServlet.java diff --git a/api/src/org/labkey/api/ApiModule.java b/api/src/org/labkey/api/ApiModule.java index 3d39111a8e0..74b47e795be 100644 --- a/api/src/org/labkey/api/ApiModule.java +++ b/api/src/org/labkey/api/ApiModule.java @@ -109,6 +109,7 @@ import org.labkey.api.view.JspTemplate; import org.labkey.api.view.LabKeyKaptchaServlet; import org.labkey.api.view.Portal; +import org.labkey.api.view.RedirectorServlet; import org.labkey.api.view.ViewServlet; import org.labkey.api.view.WebPartFactory; import org.labkey.api.webdav.WebdavResolverImpl; @@ -197,6 +198,14 @@ public void registerServlets(ServletContext servletCtx) // http://host/labkey/filecontent/proj/dir/sendFile.view?file=test.html servletCtx.addServlet("FileServlet", new FileServlet()). addMapping("/files/*"); + + String legacyContextPath = servletCtx.getInitParameter("legacyContextPath"); + if (legacyContextPath != null) + { + ServletRegistration.Dynamic redirectorDynamic = servletCtx.addServlet("RedirectorServlet", new RedirectorServlet(legacyContextPath)); + redirectorDynamic.addMapping(legacyContextPath + "/*"); + redirectorDynamic.setMultipartConfig(new MultipartConfigElement(SpringActionController.getTempUploadDir().getPath())); + } } @Override diff --git a/api/src/org/labkey/api/view/RedirectorServlet.java b/api/src/org/labkey/api/view/RedirectorServlet.java new file mode 100644 index 00000000000..3f6fba57a75 --- /dev/null +++ b/api/src/org/labkey/api/view/RedirectorServlet.java @@ -0,0 +1,51 @@ +// +// Source code recreated from a .class file by IntelliJ IDEA +// (powered by FernFlower decompiler) +// + +package org.labkey.api.view; + +import java.io.IOException; + +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +/** Simple redirector to redirect and forward from legacy context paths to the root context */ +public class RedirectorServlet extends HttpServlet +{ + private final String _legacyContextPath; + + public RedirectorServlet(String legacyContextPath) + { + if (!legacyContextPath.startsWith("/") || legacyContextPath.length() < 2) + { + throw new IllegalArgumentException("Invalid legacy context path: " + legacyContextPath); + } + _legacyContextPath = legacyContextPath; + } + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + + if ("get".equalsIgnoreCase(request.getMethod())) + { + // Send a redirect to let the client know there's a new preferred URL + String originalUrl = request.getRequestURL().toString(); + String redirectUrl = originalUrl.replaceFirst(_legacyContextPath, ""); + + response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); + response.setHeader("Location", redirectUrl); + } + else + { + // For non-GETs, use a forward so that we don't lose POST parameters, etc + String originalUri = request.getRequestURI(); + String forwardUri = originalUri.replaceFirst(_legacyContextPath, ""); + + getServletContext().getRequestDispatcher(forwardUri).forward(request, response); + } + } +} From d83536a1c21a7a61dea1e79e35cda6db1ad85d8e Mon Sep 17 00:00:00 2001 From: labkey-jeckels Date: Wed, 24 Jan 2024 10:23:28 -0800 Subject: [PATCH 2/2] Code review suggestions --- api/src/org/labkey/api/view/RedirectorServlet.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/api/src/org/labkey/api/view/RedirectorServlet.java b/api/src/org/labkey/api/view/RedirectorServlet.java index 3f6fba57a75..d30f2b7c7cb 100644 --- a/api/src/org/labkey/api/view/RedirectorServlet.java +++ b/api/src/org/labkey/api/view/RedirectorServlet.java @@ -1,8 +1,3 @@ -// -// Source code recreated from a .class file by IntelliJ IDEA -// (powered by FernFlower decompiler) -// - package org.labkey.api.view; import java.io.IOException; @@ -21,7 +16,7 @@ public RedirectorServlet(String legacyContextPath) { if (!legacyContextPath.startsWith("/") || legacyContextPath.length() < 2) { - throw new IllegalArgumentException("Invalid legacy context path: " + legacyContextPath); + throw new IllegalArgumentException("Legacy context path must start with / and cannot be the root context path. Invalid path: " + legacyContextPath + ", specified via context.legacyContextPath in application.properties"); } _legacyContextPath = legacyContextPath; } @@ -29,7 +24,6 @@ public RedirectorServlet(String legacyContextPath) @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - if ("get".equalsIgnoreCase(request.getMethod())) { // Send a redirect to let the client know there's a new preferred URL