From 4e307377b24703c3b2ee9b4cc370799c64409416 Mon Sep 17 00:00:00 2001 From: erickgonzalez Date: Wed, 12 Jun 2024 18:18:45 -0600 Subject: [PATCH] #28089 include in 23.01.18 --- .../dotcms/filters/VanityUrlFilterTest.java | 7 +++-- .../vanityurl/business/VanityUrlAPIImpl.java | 3 +++ .../java/com/dotmarketing/util/URLUtils.java | 27 ++++++++++++------- hotfix_tracking.md | 1 + 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/dotCMS/src/integration-test/java/com/dotcms/filters/VanityUrlFilterTest.java b/dotCMS/src/integration-test/java/com/dotcms/filters/VanityUrlFilterTest.java index 684f59c3b229..94b8c32bfb1b 100644 --- a/dotCMS/src/integration-test/java/com/dotcms/filters/VanityUrlFilterTest.java +++ b/dotCMS/src/integration-test/java/com/dotcms/filters/VanityUrlFilterTest.java @@ -238,7 +238,10 @@ public void test_that_vanity_url_filter_handles_redirects() throws Exception { forwardTo, action, order, defaultLanguage.getId()); filtersUtil.publishVanityUrl(contentlet1); - final String testURI = baseURI + "/test redirect 301"; + final String resource = "/test redirect 301".replaceAll(" ", "%20"); + final String queryWithFragment = "?param1=value 1¶m2=value 2#test-fragment" + .replaceAll(" ", "+"); + final String testURI = baseURI + resource + queryWithFragment; final HttpServletRequest request = new MockHttpRequestIntegrationTest(defaultHost.getHostname(), testURI).request(); final HttpServletResponse response = new MockHttpStatusResponse(new MockHeaderResponse(new MockHttpResponse().response()).response()).response(); @@ -247,7 +250,7 @@ public void test_that_vanity_url_filter_handles_redirects() throws Exception { filter.doFilter(request, response, null); - final String expectedLocation = forwardBaseURI + "/test%20redirect%20301"; + final String expectedLocation = forwardBaseURI + resource + queryWithFragment; Assert.assertEquals(expectedLocation, response.getHeader("Location")); Assert.assertEquals(301,response.getStatus()); Assert.assertNotNull(response.getHeader("X-DOT-VanityUrl")); diff --git a/dotCMS/src/main/java/com/dotcms/vanityurl/business/VanityUrlAPIImpl.java b/dotCMS/src/main/java/com/dotcms/vanityurl/business/VanityUrlAPIImpl.java index ca2cacbc4a5b..5138a2499303 100644 --- a/dotCMS/src/main/java/com/dotcms/vanityurl/business/VanityUrlAPIImpl.java +++ b/dotCMS/src/main/java/com/dotcms/vanityurl/business/VanityUrlAPIImpl.java @@ -429,6 +429,9 @@ private String encodeRedirectURL(final String uri) { } } } + if (UtilMethods.isSet(urlToEncode.getFragment())) { + uriBuilder.setFragment(urlToEncode.getFragment()); + } return uriBuilder.build().toASCIIString(); } catch (URISyntaxException e) { throw new RuntimeException(e); diff --git a/dotCMS/src/main/java/com/dotmarketing/util/URLUtils.java b/dotCMS/src/main/java/com/dotmarketing/util/URLUtils.java index 40674ccb7d04..04cbad89a57c 100644 --- a/dotCMS/src/main/java/com/dotmarketing/util/URLUtils.java +++ b/dotCMS/src/main/java/com/dotmarketing/util/URLUtils.java @@ -12,16 +12,21 @@ public class URLUtils { + private URLUtils() { + throw new IllegalStateException("Utility class"); + } + public static class ParsedURL { private String protocol; private String host; private int port; - private String URI; + private String uri; private String path; private String resource; private String queryString; private Map parameters; + private String fragment; public String getProtocol() { return protocol; @@ -66,15 +71,22 @@ public void setParameters(Map parameters) { this.parameters = parameters; } public void setURI(String uRI) { - URI = uRI; + uri = uRI; } public String getURI() { - return URI; + return uri; + } + public String getFragment() { + return fragment; + } + public void setFragment(String fragment) { + this.fragment = fragment; } - } + } - private static Pattern regexPattern = Pattern.compile("((\\w+)://([^/\\p{Cntrl}:]+)(?::([0-9]+))?)?(((?:/[^/\\p{Cntrl}]+)*)(?:/([^/\\p{Cntrl}?]+)?))?\\??(.*)?"); + private static final Pattern regexPattern = Pattern.compile( + "((\\w+)://([^/\\p{Cntrl}:]+)(?::(\\d+))?)?(((?:/[^/\\p{Cntrl}]+)*)(?:/([^/\\p{Cntrl}?#]+)?))?\\??([^#]*)?(?:#(.*))?"); public static ParsedURL parseURL(String url) throws IllegalArgumentException { @@ -91,6 +103,7 @@ public static ParsedURL parseURL(String url) throws IllegalArgumentException { parsedUrl.setPath(matcher.group(6)); parsedUrl.setResource(matcher.group(7)); parsedUrl.setQueryString(matcher.group(8)); + parsedUrl.setFragment(matcher.group(9)); Map> parameters = new HashMap>(); String[] queryStringSplitted = parsedUrl.queryString.split("&"); @@ -125,8 +138,4 @@ public static ParsedURL parseURL(String url) throws IllegalArgumentException { return parsedUrl; } - - public static String addSlashIfNeeded (final String uri) { - return (uri.length() > 0 && '/' == uri.charAt(0)) ? uri : ("/" + uri); - } } diff --git a/hotfix_tracking.md b/hotfix_tracking.md index 9a3ef18b2e86..df643c3a5ba2 100644 --- a/hotfix_tracking.md +++ b/hotfix_tracking.md @@ -250,3 +250,4 @@ This maintenance release includes the following code fixes: **Release-23.01.18** 195. https://github.com/dotCMS/core/issues/24908 : Lost ability to select system host #24908 +196. https://github.com/dotCMS/core/issues/28089 : Pound char not decoded when using Rules or Vanity URLs #28089