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

[EAK-558] Reverted to use of OptionSource/OptionProvider as Lists JSON renderer #569

Merged
merged 3 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
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