Skip to content

Commit

Permalink
Merge pull request #569 from exadel-inc/feature/eak-558-2
Browse files Browse the repository at this point in the history
[EAK-558] Reverted to use of OptionSource/OptionProvider as Lists JSON renderer
  • Loading branch information
smiakchilo authored Feb 24, 2025
2 parents 49f175e + 89a3f70 commit d24b247
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 215 deletions.
4 changes: 0 additions & 4 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import javax.annotation.Nonnull;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.sling.api.SlingHttpServletRequest;
Expand Down Expand Up @@ -73,28 +72,28 @@ public class OptionProviderServlet extends SlingSafeMethodsServlet {
* @param response {@code SlingHttpServletResponse} instance
*/
@Override
protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws ServletException, IOException {
protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response)
throws ServletException, IOException {

List<Resource> options = optionProvider.getOptions(request);

if (CollectionUtils.isEmpty(options) && isJsonOutput(request)) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
if (!isJsonOutput(request)) {
DataSource ds = new SimpleDataSource(options.iterator());
request.setAttribute(DataSource.class.getName(), ds);
return;
}

if (isJsonOutput(request)) {
response.setContentType(CoreConstants.CONTENT_TYPE_JSON);
response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
try {
response.getWriter().print(getJsonOutput(options));
} catch (JSONException | NullPointerException e) {
throw new ServletException(e);
}
response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
response.setContentType(CoreConstants.CONTENT_TYPE_JSON);
if (CollectionUtils.isEmpty(options)) {
response.getWriter().write(CoreConstants.ARRAY_OPENING + CoreConstants.ARRAY_CLOSING);
return;
}

DataSource ds = new SimpleDataSource(options.iterator());
request.setAttribute(DataSource.class.getName(), ds);
try {
response.getWriter().print(getJsonOutput(options));
} catch (JSONException | NullPointerException e) {
throw new ServletException(e);
}
}

/**
Expand All @@ -103,6 +102,9 @@ protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHtt
* @return True or false
*/
private static boolean isJsonOutput(SlingHttpServletRequest request) {
if (QUERY_VALUE_JSON.equalsIgnoreCase(request.getRequestPathInfo().getExtension())) {
return true;
}
RequestParameter jsonParameter = request.getRequestParameter(QUERY_KEY_OUTPUT);
if (jsonParameter == null) {
return false;
Expand Down

This file was deleted.

4 changes: 3 additions & 1 deletion docs/content/authoring-tools/etoolbox-lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,6 @@ public class MyComponent {

## Retrieving Lists' content via HTTP

You website's authoring logic may require fetching the content of a list via HTTP - e.g., with an AJAX request. EToolbox provides a servlet that delivers a flat array of items in JSON format. The data will be stripped of all the "system" properties like _jcr:created_, etc. Request it with the `.list.` selector and `.json` extension. Example: `/content/path/to/your/lists/sampleList.list.json`.
Your website's authoring logic may require fetching the content of a list via HTTP — e.g., with an AJAX request. This can be achieved with the OptionSource / [OptionProvider](../dev-tools/option-provider.md) servlet. Make a GET request to `/apps/etoolbox-authoring-kit/datasources/option-provider.json?path=<path_to_EToolbox_list>`.

By default, the servlet returns a JSON array of objects with `text` and `value` properties matching the same-named attributes of list items. You will probably want to modify the mapping. To say, a typical EToolbox List makes use of `jcr:title` and `value` attributes. Add `&textMember=jcr:title` to expose _jcr:title_. Similarly, you can alter the `valueMember` parameter, add some `attributeMembers`, etc. Please remember that if an item doesn't have a non-blank mapping for both _text_ and _value_, it won't be displayed.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,6 @@
<version>3.0.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
Expand Down

0 comments on commit d24b247

Please sign in to comment.