From 681e0272296813a0fdad1b70900029c3f7c70f21 Mon Sep 17 00:00:00 2001 From: Adam Rauch Date: Sat, 19 Jul 2025 11:52:23 -0700 Subject: [PATCH] Update redirect() and eliminate HttpRedirectView --- .../org/labkey/api/action/FormViewAction.java | 2 +- .../api/action/SimpleRedirectAction.java | 4 +- api/src/org/labkey/api/util/URLHelper.java | 2 +- .../labkey/api/view/HttpPostRedirectView.java | 5 -- .../org/labkey/api/view/HttpRedirectView.java | 49 ------------------- api/src/org/labkey/api/view/HttpView.java | 17 +++---- api/src/org/labkey/api/view/Portal.java | 8 +-- .../filters/ContentSecurityPolicyFilter.java | 6 --- .../labkey/core/portal/ProjectController.java | 4 +- .../TestSecondaryController.java | 2 +- .../query/reports/ReportsController.java | 7 +-- 11 files changed, 17 insertions(+), 89 deletions(-) delete mode 100644 api/src/org/labkey/api/view/HttpRedirectView.java diff --git a/api/src/org/labkey/api/action/FormViewAction.java b/api/src/org/labkey/api/action/FormViewAction.java index fa34304a341..f464857d80d 100644 --- a/api/src/org/labkey/api/action/FormViewAction.java +++ b/api/src/org/labkey/api/action/FormViewAction.java @@ -95,7 +95,7 @@ public ModelAndView handleRequest(FORM form, BindException errors) throws Except { URLHelper url = getSuccessURL(form); if (null != url) - return HttpView.redirect(url); + return HttpView.redirect(url, false); try (Timing ignored = MiniProfiler.step("createView")) { ModelAndView successView = getSuccessView(form); diff --git a/api/src/org/labkey/api/action/SimpleRedirectAction.java b/api/src/org/labkey/api/action/SimpleRedirectAction.java index 4334799a0ab..e3fc053736d 100644 --- a/api/src/org/labkey/api/action/SimpleRedirectAction.java +++ b/api/src/org/labkey/api/action/SimpleRedirectAction.java @@ -23,8 +23,8 @@ import org.springframework.web.servlet.ModelAndView; /** - * Base class for actions that don't want to render their own page, - * but instead just bounce the user to another URL via a HTTP response code that redirects the browser + * Base class for actions that don't want to render their own page, but instead just bounce the user to another URL via + * an HTTP response code that redirects the browser */ public abstract class SimpleRedirectAction
extends SimpleViewAction { diff --git a/api/src/org/labkey/api/util/URLHelper.java b/api/src/org/labkey/api/util/URLHelper.java index ed62bc0ace3..f766917f996 100644 --- a/api/src/org/labkey/api/util/URLHelper.java +++ b/api/src/org/labkey/api/util/URLHelper.java @@ -915,7 +915,7 @@ public boolean isConfiguredExternalHost() return false; } - // Issue 35896 - Disallow external redirects to URLs not on the allow list + // Issue 35896 - Disallow external redirects to URLs not on the allowlist public boolean isAllowableHost() { String host = StringUtils.trimToNull(this.getHost()); diff --git a/api/src/org/labkey/api/view/HttpPostRedirectView.java b/api/src/org/labkey/api/view/HttpPostRedirectView.java index 09752a88c70..a95403cb20d 100644 --- a/api/src/org/labkey/api/view/HttpPostRedirectView.java +++ b/api/src/org/labkey/api/view/HttpPostRedirectView.java @@ -24,13 +24,8 @@ import java.util.Map; /** - * User: kevink - * Date: 11/8/12 - * * Renders an HTML form that will POST inputs to a URL. * Set the PageConfig template to Template.None before rendering the view. - * - * @see HttpRedirectView */ public class HttpPostRedirectView extends HttpView { diff --git a/api/src/org/labkey/api/view/HttpRedirectView.java b/api/src/org/labkey/api/view/HttpRedirectView.java deleted file mode 100644 index a030b8b9e58..00000000000 --- a/api/src/org/labkey/api/view/HttpRedirectView.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2011-2018 LabKey Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.labkey.api.view; - -import org.springframework.web.servlet.View; -import org.springframework.web.servlet.view.RedirectView; - -import java.io.PrintWriter; - -/** - * A view that simple redirects the browser to some other URL. - * @see HttpPostRedirectView - * User: matthewb - * Date: 2011-11-09 - */ -public class HttpRedirectView extends HttpView -{ - final String _url; - - public HttpRedirectView(String url) - { - _url = url; - } - - @Override - public View getView() - { - return new RedirectView(_url, false); - } - - @Override - protected void renderInternal(Object model, PrintWriter out) - { - throw new RedirectException(_url); - } -} diff --git a/api/src/org/labkey/api/view/HttpView.java b/api/src/org/labkey/api/view/HttpView.java index f2915816074..0c7aee94c04 100644 --- a/api/src/org/labkey/api/view/HttpView.java +++ b/api/src/org/labkey/api/view/HttpView.java @@ -53,10 +53,9 @@ /** - * BEWARE: Our shorts are showing a bit here. Our primary HTML components (aka views) - * are not spring Views. Architecturally they act like ModelAndView. We may want to - * fix this in the future, but we're moving forward with this conceptual co-mingling - * for now. + * BEWARE: Our shorts are showing a bit here. Our primary HTML components (aka views) are not spring Views. + * Architecturally, they act like ModelAndView. We may want to fix this in the future, but we're moving forward with + * this conceptual co-mingling for now. */ public abstract class HttpView extends DefaultModelAndView implements View, HasViewContext { @@ -595,20 +594,20 @@ protected void renderInternal(Object model, PrintWriter out) throws IOException public static HttpView redirect(URLHelper url, boolean allowAbsoluteUrl) { - return new HttpRedirectView((!allowAbsoluteUrl || url.isLocalUri(getRootContext())) ? url.getLocalURIString() : url.getURIString()); + String redirectUrl = (!allowAbsoluteUrl || url.isLocalUri(getRootContext())) ? url.getLocalURIString() : url.getURIString(); + return redirect(redirectUrl); } - public static HttpView redirect(URLHelper url) + public static HttpView redirect(ActionURL url) { - return new HttpRedirectView(url.getLocalURIString()); + return redirect(url.getLocalURIString()); } public static HttpView redirect(String url) { - return new HttpRedirectView(url); + throw new RedirectException(url); } - /** * Pulls out the context's URL for redirecting. This fetches * the original URL before any redirects, in case internally diff --git a/api/src/org/labkey/api/view/Portal.java b/api/src/org/labkey/api/view/Portal.java index 3d5fd35b5f7..794acd30508 100644 --- a/api/src/org/labkey/api/view/Portal.java +++ b/api/src/org/labkey/api/view/Portal.java @@ -1584,18 +1584,12 @@ public static MultiValuedMap getPartsByLocation(Collection getViewMap() { if (null == _viewMap) diff --git a/api/src/org/labkey/filters/ContentSecurityPolicyFilter.java b/api/src/org/labkey/filters/ContentSecurityPolicyFilter.java index 8e96eaf7774..d55438013ea 100644 --- a/api/src/org/labkey/filters/ContentSecurityPolicyFilter.java +++ b/api/src/org/labkey/filters/ContentSecurityPolicyFilter.java @@ -244,12 +244,6 @@ public static String getScriptNonceHeader(HttpServletRequest request) private static final SecureRandom rand = new SecureRandom(); - @Deprecated // Keep around to ease the transition to the new method signature - public static void registerAllowedSources(Directive directive, String key, String... allowedSources) - { - registerAllowedSources(key, directive, allowedSources); - } - public static void registerAllowedSources(String key, Directive directive, String... allowedSources) { synchronized (SUBSTITUTION_LOCK) diff --git a/core/src/org/labkey/core/portal/ProjectController.java b/core/src/org/labkey/core/portal/ProjectController.java index ba306ca044c..ff8155a00e9 100644 --- a/core/src/org/labkey/core/portal/ProjectController.java +++ b/core/src/org/labkey/core/portal/ProjectController.java @@ -635,7 +635,7 @@ public ModelAndView getView(AddWebPartForm form, boolean reshow, BindException e { URLHelper successURL = getSuccessURL(form); if (null != successURL) - return HttpView.redirect(successURL); + return HttpView.redirect(successURL, false); return HttpView.redirect(getContainer().getStartURL(getUser())); } @@ -915,7 +915,7 @@ public ModelAndView getView(CustomizePortletForm customizePortletForm, boolean r handlePost(customizePortletForm, errors); URLHelper successURL = getSuccessURL(customizePortletForm); if (null != successURL) - return HttpView.redirect(successURL); + return HttpView.redirect(successURL, false); return HttpView.redirect(getContainer().getStartURL(getUser())); } diff --git a/devtools/src/org/labkey/devtools/authentication/TestSecondaryController.java b/devtools/src/org/labkey/devtools/authentication/TestSecondaryController.java index bbe5fc28c68..798a47e73ed 100644 --- a/devtools/src/org/labkey/devtools/authentication/TestSecondaryController.java +++ b/devtools/src/org/labkey/devtools/authentication/TestSecondaryController.java @@ -109,7 +109,7 @@ public void validateCommand(TestSecondaryForm form, Errors errors) public ModelAndView getView(TestSecondaryForm form, boolean reshow, BindException errors) { if (!getUser().isGuest()) - return HttpView.redirect(AuthenticationManager.getAfterLoginURL(getContainer(), null, getUser())); + return HttpView.redirect(AuthenticationManager.getAfterLoginURL(getContainer(), null, getUser()), false); PrimaryAuthenticationResult result = AuthenticationManager.getPrimaryAuthenticationResult(getViewContext().getSession()); diff --git a/query/src/org/labkey/query/reports/ReportsController.java b/query/src/org/labkey/query/reports/ReportsController.java index 535ed8f462d..86e2330abbe 100644 --- a/query/src/org/labkey/query/reports/ReportsController.java +++ b/query/src/org/labkey/query/reports/ReportsController.java @@ -144,7 +144,6 @@ import org.labkey.api.util.URLHelper; import org.labkey.api.view.ActionURL; import org.labkey.api.view.HtmlView; -import org.labkey.api.view.HttpRedirectView; import org.labkey.api.view.HttpView; import org.labkey.api.view.JspView; import org.labkey.api.view.NavTree; @@ -203,10 +202,6 @@ import static org.labkey.api.util.DOM.SPAN; import static org.labkey.api.util.DOM.cl; -/** - * User: Karl Lum - * Date: Apr 19, 2007 - */ public class ReportsController extends SpringActionController { private static final Logger _log = LogManager.getLogger(ReportsController.class); @@ -1027,7 +1022,7 @@ public ModelAndView getView(ReportDesignBean form, BindException errors) thro return new HtmlView(SPAN(cl("labkey-error"), e.getMessage(), ". Unable to create report.")); } - if (!isPrint() && !(reportView instanceof HttpRedirectView) && DiscussionService.get() != null) + if (!isPrint() && DiscussionService.get() != null) { DiscussionService service = DiscussionService.get(); String title = "Discuss report - " + _report.getDescriptor().getReportName();