Skip to content

Commit

Permalink
#3484 add selector to filter redirects by status code
Browse files Browse the repository at this point in the history
  • Loading branch information
YegorKozlov committed Dec 1, 2024
1 parent d747a74 commit d7ff41d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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<RedirectRule> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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(" ");
Expand All @@ -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]);
}
}

0 comments on commit d7ff41d

Please sign in to comment.