Skip to content

Commit

Permalink
Merge branch 'main' into michielspiritus/main
Browse files Browse the repository at this point in the history
  • Loading branch information
royteeuwen committed Sep 14, 2024
2 parents 0d14101 + b2d148c commit e6a9da8
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package be.orbinson.aem.dictionarytranslator.servlets.action;

import be.orbinson.aem.dictionarytranslator.services.DictionaryService;
import com.day.cq.commons.jcr.JcrUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.PersistenceException;
Expand Down Expand Up @@ -79,7 +79,7 @@ protected void doPost(SlingHttpServletRequest request, @NotNull SlingHttpServlet
}

private boolean labelExists(ResourceResolver resourceResolver, Resource dictionaryResource, String language, String key) {
return resourceResolver.getResource(dictionaryResource.getPath() + "/" + language + "/" + JcrUtil.createValidName(key)) != null;
return resourceResolver.getResource(dictionaryResource.getPath() + "/" + language + "/" + Text.escapeIllegalJcrChars(key)) != null;
}

private void addMessage(ResourceResolver resourceResolver, Resource dictionary, String language, String key, String message) throws PersistenceException {
Expand All @@ -93,7 +93,7 @@ private void addMessage(ResourceResolver resourceResolver, Resource dictionary,
if (!message.isBlank()) {
properties.put(SLING_MESSAGE, message);
}
resourceResolver.create(resource, JcrUtil.createValidName(key), properties);
resourceResolver.create(resource, Text.escapeIllegalJcrChars(key), properties);
LOG.trace("Create label with key '{}' and message '{}' on path '{}'", key, message, path);
resourceResolver.commit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,37 @@
)
public class DeleteLabelServlet extends SlingAllMethodsServlet {
private static final Logger LOG = LoggerFactory.getLogger(DeleteLabelServlet.class);
public static final String LABELS_PARAM = "labels";
public static final String LABEL_PARAM = "item";

@Reference
private transient Replicator replicator;

@Override
protected void doPost(SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws IOException {
String labels = request.getParameter(LABELS_PARAM);
String[] labels = request.getParameterValues(LABEL_PARAM);

if (StringUtils.isEmpty(labels)) {
if (labels == null) {
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST, "Labels parameters are required");
htmlResponse.send(response, true);
} else {
for (String label : labels.split(",")) {
for (String label : labels) {
ResourceResolver resourceResolver = request.getResourceResolver();
Resource resource = resourceResolver.getResource(label);
try {
if (resource != null) {
// javasecurity:S5145
LOG.debug("Delete label on path '{}'", labels);
LOG.debug("Delete label on path '{}'", resource.getPath());
deactivateAndDelete(resourceResolver, resource);
} else {
// javasecurity:S5145
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST, String.format("Unable to get label '%s'", labels));
htmlResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST, String.format("Unable to get label '%s'", label));
htmlResponse.send(response, true);
}
} catch (PersistenceException | ReplicationException e) {
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, String.format("Unable to delete labels '%s': %s", labels, e.getMessage()));
htmlResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, String.format("Unable to delete label '%s': %s", label, e.getMessage()));
htmlResponse.send(response, true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public class ReplicateLabelServlet extends SlingAllMethodsServlet {

@Override
protected void doPost(SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws IOException {
String labels = request.getParameter("labels");
String[] labels = request.getParameterValues("item");

if (StringUtils.isEmpty(labels)) {
LOG.warn("Labels parameters are required");
if (labels == null) {
LOG.warn("At least one item parameter is required");
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
} else {
try {
for (String label : labels.split(",")) {
for (String label : labels) {
//Splitting label into dictionary path and label key
label = label.replace("/mnt/dictionary", "");
int lastIndexOfBackslash = label.lastIndexOf('/');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class UpdateLabelServlet extends SlingAllMethodsServlet {

@Override
protected void doPost(SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws IOException {
String label = request.getParameter("label");
String label = request.getParameter("item"); // only single items are supported

if (StringUtils.isEmpty(label)) {
HtmlResponse htmlResponse = new HtmlResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,46 @@ void doPostWithEmptyMessage() throws ServletException, IOException {
assertNull(properties.get(SLING_MESSAGE));
}

@Test
void doPostCaseSensitiveKeys() throws ServletException, IOException {
context.create().resource("/content/dictionaries/i18n/en", Map.of("jcr:language", "en"));
context.create().resource("/content/dictionaries/i18n/fr", Map.of("jcr:language", "fr"));

context.request().setMethod("POST");
context.request().setParameterMap(Map.of(
"dictionary", "/content/dictionaries/i18n",
"key", "greeting",
"en", "Hello",
"fr", ""
));
servlet.service(context.request(), context.response());
assertEquals(HttpServletResponse.SC_OK, context.response().getStatus());

context.request().setParameterMap(Map.of(
"dictionary", "/content/dictionaries/i18n",
"key", "Greeting",
"en", "Hello2",
"fr", ""
));
servlet.service(context.request(), context.response());

assertEquals(HttpServletResponse.SC_OK, context.response().getStatus());

Resource resource = context.resourceResolver().getResource("/content/dictionaries/i18n/en/greeting");
ValueMap properties = resource.getValueMap();
assertNotNull(resource);
assertEquals(SLING_MESSAGEENTRY, properties.get(JCR_PRIMARYTYPE));
assertEquals("greeting", properties.get(SLING_KEY));
assertEquals("Hello", properties.get(SLING_MESSAGE));

resource = context.resourceResolver().getResource("/content/dictionaries/i18n/en/Greeting");
properties = resource.getValueMap();
assertNotNull(resource);
assertEquals(SLING_MESSAGEENTRY, properties.get(JCR_PRIMARYTYPE));
assertEquals("Greeting", properties.get(SLING_KEY));
assertEquals("Hello2", properties.get(SLING_MESSAGE));
}

@Test
void createLabelThatAlreadyExists() throws ServletException, IOException {
context.create().resource("/content/dictionaries/i18n/en", Map.of("jcr:language", "en"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import java.io.IOException;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -69,7 +67,7 @@ void doPostWithoutParams() throws ServletException, IOException {
void deleteLabelWithNonExistingKey() throws ServletException, IOException {
context.request().setMethod("POST");
context.request().setParameterMap(Map.of(
DeleteLabelServlet.LABELS_PARAM, "/content/dictionaries/i18n/en/apple"
DeleteLabelServlet.LABEL_PARAM, "/content/dictionaries/i18n/en/apple"
));

servlet.service(context.request(), context.response());
Expand All @@ -80,12 +78,12 @@ void deleteLabelWithNonExistingKey() throws ServletException, IOException {
@Test
void deleteExistingLabel() throws ServletException, IOException, ReplicationException {
context.create().resource("/mnt/dictionaries/i18n/appel",
"labelPaths", new String[] {"/content/dictionaries/i18n/appel/fr", "/content/dictionaries/i18n/appel/en"}
"labelPaths", new String[]{"/content/dictionaries/i18n/appel/fr", "/content/dictionaries/i18n/appel/en"}
);
context.create().resource("/content/dictionaries/i18n/peer");
context.request().setMethod("POST");
context.request().setParameterMap(Map.of(
DeleteLabelServlet.LABELS_PARAM, new String[]{"/mnt/dictionaries/i18n/appel"}
DeleteLabelServlet.LABEL_PARAM, new String[]{"/mnt/dictionaries/i18n/appel"}
));

servlet.service(context.request(), context.response());
Expand All @@ -95,7 +93,7 @@ void deleteExistingLabel() throws ServletException, IOException, ReplicationExce
verify(replicator).replicate(any(Session.class), eq(ReplicationActionType.DEACTIVATE), eq("/content/dictionaries/i18n/appel/en"));

assertNotNull(context.resourceResolver().getResource("/content/dictionaries/i18n/peer"));
verify(replicator,times(0)).replicate(any(Session.class), eq(ReplicationActionType.DEACTIVATE), eq("/content/dictionaries/i18n/peer"));
verify(replicator, times(0)).replicate(any(Session.class), eq(ReplicationActionType.DEACTIVATE), eq("/content/dictionaries/i18n/peer"));

assertEquals(HttpServletResponse.SC_OK, context.response().getStatus());
}
Expand All @@ -107,7 +105,7 @@ void deleteMultipleLabels() throws ServletException, IOException {
context.create().resource("/content/dictionaries/i18n/en/framboos");
context.request().setMethod("POST");
context.request().setParameterMap(Map.of(
DeleteLabelServlet.LABELS_PARAM, new String[]{"/content/dictionaries/i18n/en/appel,/content/dictionaries/i18n/en/peer"}
DeleteLabelServlet.LABEL_PARAM, new String[]{"/content/dictionaries/i18n/en/appel", "/content/dictionaries/i18n/en/peer"}
));

servlet.service(context.request(), context.response());
Expand All @@ -123,7 +121,7 @@ void deleteNonExistingLabel() throws ServletException, IOException {
context.create().resource("/content/dictionaries/i18n/en/appel");
context.request().setMethod("POST");
context.request().setParameterMap(Map.of(
DeleteLabelServlet.LABELS_PARAM, new String[]{"/content/dictionaries/i18n/fr/peer"}
DeleteLabelServlet.LABEL_PARAM, new String[]{"/content/dictionaries/i18n/fr/peer"}
));

servlet.service(context.request(), context.response());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
variant="actionBar"
text="Edit">
<data jcr:primaryType="nt:unstructured"
src.uritemplate="/apps/aem-dictionary-translator/content/granite/dialog/dictionary/label/update.html?label={+item}"/>
src.uritemplate="/apps/aem-dictionary-translator/content/granite/dialog/dictionary/label/update.html{?item*}"/>
<granite:rendercondition
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/renderconditions/simple"
Expand All @@ -68,7 +68,7 @@
variant="actionBar"
text="Delete">
<data jcr:primaryType="nt:unstructured"
src.uritemplate="/apps/aem-dictionary-translator/content/granite/dialog/dictionary/label/delete.html?labels={+item}"/>
src.uritemplate="/apps/aem-dictionary-translator/content/granite/dialog/dictionary/label/delete.html{?item*}"/>
<granite:rendercondition
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/renderconditions/simple"
Expand All @@ -84,7 +84,7 @@
variant="actionBar"
text="Publish">
<data jcr:primaryType="nt:unstructured"
src.uritemplate="/apps/aem-dictionary-translator/content/granite/dialog/dictionary/label/publish.html?labels={+item}"/>
src.uritemplate="/apps/aem-dictionary-translator/content/granite/dialog/dictionary/label/publish.html{?item*}"/>
<granite:rendercondition
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/renderconditions/simple"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<form
jcr:primaryType="nt:unstructured"
sling:resourceType="aem-dictionary-translator/servlet/action/delete-label"
action="${requestPathInfo.resourcePath}?labels=${granite:encodeURIComponent(param.labels)}"
action="${requestPathInfo.resourcePath}?${querystring}"
granite:id="delete-label-form"
foundationForm="true"
method="post">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<form
jcr:primaryType="nt:unstructured"
sling:resourceType="aem-dictionary-translator/servlet/action/publish-label"
action="${requestPathInfo.resourcePath}?labels=${granite:encodeURIComponent(param.labels)}"
action="${requestPathInfo.resourcePath}?${querystring}"
granite:id="publish-label-form"
foundationForm="true"
method="post">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
jcr:primaryType="nt:unstructured"
sling:resourceType="aem-dictionary-translator/servlet/action/update-label"
granite:id="update-label-form"
action="${requestPathInfo.resourcePath}?label=${granite:encodeURIComponent(param.label)}"
action="${requestPathInfo.resourcePath}?${querystring}"
foundationForm="true"
method="post"
style="vertical">
Expand Down

0 comments on commit e6a9da8

Please sign in to comment.