Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 3177 - Automate creation of vanity URLs for each country & lang… #3179

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Issue 3177 - Automate creation of vanity URLs for each country & lang…
…uage
Yassine EL OUARDI committed Sep 6, 2023

Verified

This commit was signed with the committer’s verified signature.
commit 5a60a6402328503df1c9e14881aace2f60353a85
Original file line number Diff line number Diff line change
@@ -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;

@@ -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);
}