-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support additional context paths (#5156)
* Support additional context paths - for the primary deployment, for redirecting, and for secondary static content
- Loading branch information
1 parent
5fbecac
commit 1ba37bb
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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("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; | ||
} | ||
|
||
@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); | ||
} | ||
} | ||
} |