Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support additional context paths #5156

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions api/src/org/labkey/api/ApiModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions api/src/org/labkey/api/view/RedirectorServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, IntelliJ had helpfully collapsed this section so I hadn't noticed it. I started with a redirector servlet that I wrote many years ago but I think the source code got lost in the SVN->GitHub migration. I still had the binaries in email though.


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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this error message use the property name to make it easier to track down where the error is?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

}
_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, "");
labkey-susanh marked this conversation as resolved.
Show resolved Hide resolved

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);
}
}
}