From d7ff41d28d8aa0a00f3ed7527378ce69890cca9b Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Sun, 1 Dec 2024 11:36:43 +0100 Subject: [PATCH] #3484 add selector to filter redirects by status code --- .../redirects/servlets/RewriteMapServlet.java | 13 +++-- .../servlets/RewriteMapServletTest.java | 52 ++++++++++++++----- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/bundle/src/main/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServlet.java b/bundle/src/main/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServlet.java index b91c06b84e..37242e3d97 100644 --- a/bundle/src/main/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServlet.java +++ b/bundle/src/main/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServlet.java @@ -1,7 +1,7 @@ /* * ACS AEM Commons * - * Copyright (C) 2013 - 2023 Adobe + * Copyright (C) 2013 - 2024 Adobe * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,6 @@ import com.adobe.acs.commons.redirects.filter.RedirectFilter; import com.adobe.acs.commons.redirects.models.RedirectRule; -import com.google.common.net.MediaType; import org.apache.http.entity.ContentType; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; @@ -56,10 +55,18 @@ protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse r throws ServletException, IOException { response.setContentType(ContentType.TEXT_PLAIN.getMimeType()); + String[] selectors = request.getRequestPathInfo().getSelectors(); + int statusCode = 0; + if(selectors != null && selectors.length > 0) { + statusCode = Integer.parseInt(selectors[0]); + } Collection rules = RedirectFilter.getRules(request.getResource()); PrintWriter out = response.getWriter(); - out.print("# Redirect Map File\n"); + out.printf("# %s Redirects\n", statusCode == 0 ? "All" : "" + statusCode); for (RedirectRule rule : rules) { + if(statusCode != 0 && rule.getStatusCode() != statusCode) { + continue; + } String note = rule.getNote(); if(note != null && !note.isEmpty()) { out.printf("# %s\n", note); diff --git a/bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java b/bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java index 2344c24831..a1eff1a6b7 100755 --- a/bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java +++ b/bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java @@ -1,7 +1,7 @@ /* * ACS AEM Commons * - * Copyright (C) 2013 - 2023 Adobe + * Copyright (C) 2013 - 2024 Adobe * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,12 +18,7 @@ package com.adobe.acs.commons.redirects.servlets; import com.adobe.acs.commons.redirects.RedirectResourceBuilder; -import com.adobe.acs.commons.redirects.filter.RedirectFilter; -import com.adobe.acs.commons.redirects.models.RedirectRule; import org.apache.http.entity.ContentType; -import org.apache.poi.xssf.usermodel.XSSFRow; -import org.apache.poi.xssf.usermodel.XSSFSheet; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.sling.api.resource.PersistenceException; import org.apache.sling.api.resource.Resource; import org.apache.sling.testing.mock.sling.ResourceResolverType; @@ -35,17 +30,10 @@ import org.junit.Test; import javax.servlet.ServletException; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.Calendar; -import java.util.Collection; -import static com.adobe.acs.commons.redirects.Asserts.assertDateEquals; -import static com.adobe.acs.commons.redirects.servlets.ExportRedirectMapServlet.SPREADSHEETML_SHEET; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; /** * @author Yegor Kozlov @@ -97,7 +85,8 @@ public void testGet() throws ServletException, IOException { assertEquals(ContentType.TEXT_PLAIN.getMimeType(), response.getContentType()); String[] lines = response.getOutputAsString().split("\n"); - assertEquals("# Redirect Map File", lines[0]); + assertEquals(4, lines.length); // header + 2 rules + assertEquals("# All Redirects", lines[0]); assertEquals("# note-1", lines[1]); String[] rule1 = lines[2].split(" "); @@ -108,4 +97,39 @@ public void testGet() throws ServletException, IOException { assertEquals("/content/three", rule2[0]); assertEquals("/content/four", rule2[1]); } + + @Test + public void test301Selector() throws ServletException, IOException { + MockSlingHttpServletRequest request = context.request(); + MockSlingHttpServletResponse response = context.response(); + + context.requestPathInfo().setSelectorString("301"); + servlet.doGet(request, response); + + assertEquals(ContentType.TEXT_PLAIN.getMimeType(), response.getContentType()); + String[] lines = response.getOutputAsString().split("\n"); + assertEquals(2, lines.length); // header + 1 rule + assertEquals("# 301 Redirects", lines[0]); + String[] rule1 = lines[1].split(" "); + assertEquals("/content/three", rule1[0]); + assertEquals("/content/four", rule1[1]); + } + + @Test + public void test302Selector() throws ServletException, IOException { + MockSlingHttpServletRequest request = context.request(); + MockSlingHttpServletResponse response = context.response(); + + context.requestPathInfo().setSelectorString("302"); + servlet.doGet(request, response); + + assertEquals(ContentType.TEXT_PLAIN.getMimeType(), response.getContentType()); + String[] lines = response.getOutputAsString().split("\n"); + assertEquals(3, lines.length); // header + notes + 1st rule + assertEquals("# 302 Redirects", lines[0]); + + String[] rule1 = lines[2].split(" "); + assertEquals("/content/one", rule1[0]); + assertEquals("/content/two", rule1[1]); + } } \ No newline at end of file