Skip to content

Commit d8e5cf3

Browse files
author
Yassine EL OUARDI
committed
Automate creation of vanity URLs for each country & language
1 parent 980d6f3 commit d8e5cf3

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

bundle/src/main/java/com/adobe/acs/commons/redirectmaps/impl/AddEntryServlet.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222

2323
import javax.servlet.ServletException;
2424

25+
import com.drew.lang.annotations.NotNull;
2526
import org.apache.commons.collections.CollectionUtils;
2627
import org.apache.felix.scr.annotations.Reference;
2728
import org.apache.felix.scr.annotations.sling.SlingServlet;
2829
import org.apache.sling.api.SlingHttpServletRequest;
2930
import org.apache.sling.api.SlingHttpServletResponse;
3031
import org.apache.sling.api.resource.Resource;
32+
import org.apache.sling.api.resource.ValueMap;
3133
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
3234
import org.slf4j.Logger;
3335
import org.slf4j.LoggerFactory;
@@ -63,15 +65,21 @@ protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse
6365
Resource resource = request.getResourceResolver().getResource(ETC_ACS_COMMONS_LISTS_COUNTRIES_JCR_CONTENT_LIST);
6466
if (Objects.nonNull(resource)) {
6567
countries = new HashMap<>();
68+
@NotNull
6669
Iterable<Resource> children = resource.getChildren();
67-
for (Resource childResource : children) {
68-
String title = childResource.getValueMap().get("jcr:title", String.class);
69-
String nodeValue = childResource.getValueMap().get("value", String.class);
70-
countries.put(nodeValue, title);
70+
if(children != null){
71+
for (Resource childResource : children) {
72+
ValueMap valueMap = childResource.getValueMap();
73+
if (valueMap != null) {
74+
String title = valueMap.get("jcr:title", String.class);
75+
String nodeValue = valueMap.get("value", String.class);
76+
countries.put(nodeValue, title);
77+
}
78+
}
7179
}
7280
}
7381
List<String> lines = RedirectEntriesUtils.readEntries(request);
74-
if(CollectionUtils.isNotEmpty(lines)){
82+
if(countries != null && !countries.isEmpty()){
7583
for (Map.Entry<String,String> country : countries.entrySet()) {
7684
String genericSource = country.getKey()+":" +source;
7785
String genericTarget= "/"+country.getKey()+"/"+country.getValue()+target;

bundle/src/test/java/com/adobe/acs/commons/redirectmaps/impl/TestServlets.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertFalse;
2222
import static org.junit.Assert.assertTrue;
23-
import static org.mockito.Mockito.doReturn;
24-
import static org.mockito.Mockito.mock;
23+
import static org.mockito.Mockito.*;
2524

2625
import java.io.IOException;
2726
import java.io.InputStream;
@@ -41,12 +40,14 @@
4140
import org.apache.sling.api.SlingHttpServletResponse;
4241
import org.apache.sling.api.resource.ModifiableValueMap;
4342
import org.apache.sling.api.resource.Resource;
43+
import org.apache.sling.api.resource.ResourceResolver;
4444
import org.apache.sling.commons.testing.sling.MockResourceResolver;
4545
import org.apache.tika.io.IOUtils;
4646
import org.junit.Before;
4747
import org.junit.Test;
4848
import org.mockito.InjectMocks;
4949
import org.mockito.Mock;
50+
import org.mockito.Mockito;
5051
import org.mockito.MockitoAnnotations;
5152
import org.slf4j.Logger;
5253
import org.slf4j.LoggerFactory;
@@ -76,6 +77,10 @@ public class TestServlets {
7677
@Mock
7778
private Resource mockMapContentResource;
7879

80+
@Mock
81+
private ResourceResolver resourceResolver;
82+
83+
7984
private Map<String, String> value = new HashMap<>();
8085

8186
private ModifiableValueMap mvm = new ModifiableValueMap() {
@@ -292,7 +297,6 @@ public void testAddEntryServlet() throws ServletException, IOException {
292297
log.info("testAddEntryServlet");
293298
AddEntryServlet servlet = new AddEntryServlet();
294299
servlet.doPost(mockSlingRequest, mockSlingResponse);
295-
296300
log.info("REDIRECT MAP:\n" + value.get(JcrConstants.JCR_DATA));
297301
assertTrue(value.get(JcrConstants.JCR_DATA).contains("/source /target"));
298302
log.info("Test successful!");

0 commit comments

Comments
 (0)