Skip to content

Commit

Permalink
Issue 3177 - Automate creation of vanity URLs for each country & lang…
Browse files Browse the repository at this point in the history
…uage
  • Loading branch information
Yassine EL OUARDI committed Sep 6, 2023
1 parent 4be29a1 commit 5a60a64
Showing 1 changed file with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@
package com.adobe.acs.commons.redirectmaps.impl;

import java.io.IOException;
import java.util.List;
import java.util.*;

import javax.servlet.ServletException;

import org.apache.commons.collections.CollectionUtils;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.graalvm.util.CollectionsUtil;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -36,31 +40,51 @@
* Servlet for adding a line into the redirect map text file
*/
@SlingServlet(methods = { "POST" }, resourceTypes = {
"acs-commons/components/utilities/redirectmappage" }, selectors = {
"addentry" }, extensions = { "json" }, metatype = false)
"acs-commons/components/utilities/redirectmappage" }, selectors = {
"addentry" }, extensions = { "json" }, metatype = false)
public class AddEntryServlet extends SlingAllMethodsServlet {

private static final long serialVersionUID = -1704915461516132101L;
private static final Logger log = LoggerFactory.getLogger(AddEntryServlet.class);

public static final String ETC_ACS_COMMONS_LISTS_COUNTRIES_JCR_CONTENT_LIST = "/etc/acs-commons/lists/countries/jcr:content/list";

// Disable this feature on AEM as a Cloud Service
@Reference(target="(distribution=classic)")
transient RequireAem requireAem;

Map<String, String> countries;

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
throws ServletException, IOException {
log.trace("doPost");

String source = request.getParameter("source");
String target = request.getParameter("target");
log.debug("Adding entry with {} {}", source, target);

Resource resource = request.getResourceResolver().getResource(ETC_ACS_COMMONS_LISTS_COUNTRIES_JCR_CONTENT_LIST);
if (Objects.nonNull(resource)) {
countries = new HashMap<>();
@NotNull
Iterable<Resource> children = resource.getChildren();
for (Resource childResource : children) {
String title = childResource.getValueMap().get("jcr:title", String.class);
String nodeValue = childResource.getValueMap().get("value", String.class);
countries.put(nodeValue, title);
}
}
List<String> lines = RedirectEntriesUtils.readEntries(request);
if(CollectionUtils.isNotEmpty(lines)){
for (Map.Entry<String,String> country : countries.entrySet()) {
String genericSource = country.getKey()+":" +source;
String genericTarget= "/"+country.getKey()+"/"+country.getValue()+target;
lines.add(genericSource + " " + genericTarget);
}
}else{
lines.add(source + " " + target);
}

lines.add(source + " " + target);
log.debug("Added entry...");

RedirectEntriesUtils.updateRedirectMap(request, lines);
RedirectEntriesUtils.writeEntriesToResponse(request, response, "Added entry " + source + " " + target);
}
Expand Down

0 comments on commit 5a60a64

Please sign in to comment.