Skip to content

Commit

Permalink
Update all responses to use HTML based error responses so that Granit…
Browse files Browse the repository at this point in the history
…e UI shows them in the dialogs
  • Loading branch information
Roy Teeuwen committed Aug 3, 2024
1 parent 3e77134 commit 4a5e724
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.apache.sling.servlets.post.HtmlResponse;
import org.jetbrains.annotations.NotNull;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand All @@ -18,6 +19,9 @@
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;

@Component(service = Servlet.class)
@SlingServletResourceTypes(
Expand All @@ -41,8 +45,9 @@ protected void doPost(SlingHttpServletRequest request, @NotNull SlingHttpServlet
String[] languages = request.getParameterValues("language");

if (StringUtils.isEmpty(name) || StringUtils.isEmpty(path) || languages == null) {
LOG.warn("Invalid parameters to create dictionary, 'dictionary={}', 'path={}', 'languages={}', 'basename={}'", name, path, languages, basename);
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST, String.format("Invalid parameters to create dictionary, 'dictionary=%s', 'path=%s', 'languages=%s', 'basename=%s'", name, path, String.join(",", Arrays.asList(Optional.ofNullable(languages).orElse(new String[0]))), basename));
htmlResponse.send(response, true);
} else {
final ResourceResolver resourceResolver = request.getResourceResolver();
Resource resource = resourceResolver.getResource(path);
Expand All @@ -52,12 +57,14 @@ protected void doPost(SlingHttpServletRequest request, @NotNull SlingHttpServlet
LOG.debug("Create dictionary '{}'", name);
dictionaryService.createDictionary(resource, name, languages, basename);
} catch (PersistenceException e) {
LOG.error("Unable to create dictionary", e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, String.format("Unable to create dictionary: %s", e.getMessage()));
htmlResponse.send(response, true);
}
} else {
LOG.warn("Unable to get resource for path '{}", path);
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST, String.format("Unable to get resource for path '%s", path));
htmlResponse.send(response, true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.apache.sling.servlets.post.HtmlResponse;
import org.jetbrains.annotations.NotNull;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand All @@ -22,9 +23,7 @@
import java.util.HashMap;
import java.util.Map;

import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_KEY;
import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGE;
import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGEENTRY;
import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.*;
import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;

@Component(service = Servlet.class)
Expand All @@ -40,37 +39,52 @@ public class CreateLabelServlet extends SlingAllMethodsServlet {
@Reference
private transient DictionaryService dictionaryService;

// @Reference
// private XSSAPI xssapi;

@Override
protected void doPost(SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws IOException {
String key = request.getParameter("key");
String dictionary = request.getParameter("dictionary");

if (StringUtils.isEmpty(key) || StringUtils.isEmpty(dictionary)) {
LOG.warn("Key and dictionary parameters are required");
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST, String.format("Invalid parameters to create language, 'key=%s', 'dictionary=%s', ", key, dictionary));
htmlResponse.send(response, true);
} else {
ResourceResolver resourceResolver = request.getResourceResolver();
Resource resource = resourceResolver.getResource(dictionary);
Resource dictionaryResource = resourceResolver.getResource(dictionary);
try {
if (resource != null) {
for (String language : dictionaryService.getLanguages(resource)) {
if (dictionaryResource != null) {
for (String language : dictionaryService.getLanguages(dictionaryResource)) {
// javasecurity:S5145
LOG.debug("Create label on path '{}/{}'", dictionary, key);
String message = request.getParameter(language);
addMessage(resourceResolver, resource, language, key, message);
if (!labelExists(resourceResolver, dictionaryResource, language, key)) {
addMessage(resourceResolver, dictionaryResource, language, key, message);
} else {
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST, String.format("Can not create label %s, label already exists", key));
htmlResponse.send(response, true);
}
}
} else {
// javasecurity:S5145
LOG.warn("Unable to get dictionary '{}'", dictionary);
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST, String.format("Unable to get dictionary '%s'", dictionary));
htmlResponse.send(response, true);
}
} catch (PersistenceException e) {
LOG.error("Unable to create key '{}' on dictionary '{}'", key, dictionary);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, String.format("Unable to create key '%s' on dictionary '%s'", key, dictionary));
htmlResponse.send(response, true);
}
}
}

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

private void addMessage(ResourceResolver resourceResolver, Resource dictionary, String language, String key, String message) throws PersistenceException {
Resource resource = dictionary.getChild(language);

Expand All @@ -79,7 +93,7 @@ private void addMessage(ResourceResolver resourceResolver, Resource dictionary,
Map<String, Object> properties = new HashMap<>();
properties.put(JCR_PRIMARYTYPE, SLING_MESSAGEENTRY);
properties.put(SLING_KEY, key);
if (!message.isBlank()){
if (!message.isBlank()) {
properties.put(SLING_MESSAGE, message);
}
resourceResolver.create(resource, JcrUtil.createValidName(key), properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.apache.sling.servlets.post.HtmlResponse;
import org.jetbrains.annotations.NotNull;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand Down Expand Up @@ -46,16 +47,19 @@ protected void doPost(@NotNull SlingHttpServletRequest request, @NotNull SlingHt
LOG.debug("Adding language '{}' to dictionary '{}'", language, dictionary);
dictionaryService.addLanguage(resource, language, basename);
} else {
LOG.warn("Unable to get dictionary '{}'", dictionary);
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST, String.format("Unable to get dictionary for path '%s", dictionary));
htmlResponse.send(response, true);
}
} catch (PersistenceException e) {
LOG.error("Unable to add language '{}' to dictionary '{}'", language, dictionary);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, String.format("Unable to add language '%s' to dictionary '%s': %s", language, dictionary, e.getMessage()));
htmlResponse.send(response, true);
}
} else {
LOG.warn("Dictionary and language parameter are required to add a language to a dictionary");
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST, "Dictionary and language parameter are required to add a language to a dictionary");
htmlResponse.send(response, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.apache.sling.servlets.post.HtmlResponse;
import org.jetbrains.annotations.NotNull;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand Down Expand Up @@ -50,17 +51,20 @@ protected void doPost(SlingHttpServletRequest request, @NotNull SlingHttpServlet
deactivateAndDelete(resourceResolver, resource);
resourceResolver.commit();
} else {
LOG.warn("Dictionary '{}' not found to delete", dictionary);
response.sendError(HttpServletResponse.SC_NOT_FOUND);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_NOT_FOUND, "Dictionary not found");
htmlResponse.send(response, true);
}
} catch (PersistenceException | ReplicationException e) {
LOG.error("Error deleting item: {}", dictionary, e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, String.format("Unable to delete dictionary '%s': %s", dictionary, e.getMessage()));
htmlResponse.send(response, true);
}
}
} else {
LOG.warn("Dictionary parameter can not be empty");
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST, "Dictionary parameter can not be empty");
htmlResponse.send(response, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.apache.sling.servlets.post.HtmlResponse;
import org.jetbrains.annotations.NotNull;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand Down Expand Up @@ -40,8 +41,9 @@ protected void doPost(SlingHttpServletRequest request, @NotNull SlingHttpServlet
String labels = request.getParameter(LABELS_PARAM);

if (StringUtils.isEmpty(labels)) {
LOG.warn("Labels parameters are required");
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST, "Labels parameters are required");
htmlResponse.send(response, true);
} else {
for (String label : labels.split(",")) {
ResourceResolver resourceResolver = request.getResourceResolver();
Expand All @@ -53,12 +55,14 @@ protected void doPost(SlingHttpServletRequest request, @NotNull SlingHttpServlet
deactivateAndDelete(resourceResolver, resource);
} else {
// javasecurity:S5145
LOG.warn("Unable to get label '{}'", labels);
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST, String.format("Unable to get label '%s'", labels));
htmlResponse.send(response, true);
}
} catch (PersistenceException | ReplicationException e) {
LOG.error("Unable to delete labels '{}'", labels);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, String.format("Unable to delete labels '%s': %s", labels, e.getMessage()));
htmlResponse.send(response, true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.apache.sling.servlets.post.HtmlResponse;
import org.jetbrains.annotations.NotNull;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand Down Expand Up @@ -53,16 +54,19 @@ public void doPost(SlingHttpServletRequest request, @NotNull SlingHttpServletRes
deactivateAndDelete(resourceResolver, resource);
resourceResolver.commit();
} else {
LOG.warn("Unable to find dictionary '{}'", path);
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST, String.format("Unable to get dictionary '%s'", dictionary));
htmlResponse.send(response, true);
}
} catch (PersistenceException | ReplicationException e) {
LOG.error("Unable to delete language '{}' from dictionary '{}'", language, dictionary);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, String.format("Unable to delete language '%s': %s", language, e.getMessage()));
htmlResponse.send(response, true);
}
} else {
LOG.warn("Language and dictionary parameter are required");
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST, "Language and dictionary parameter can not be empty");
htmlResponse.send(response, true);
}
}

Expand Down
Loading

0 comments on commit 4a5e724

Please sign in to comment.