Skip to content

Commit 5c6fc58

Browse files
committed
[EAK-558] Made OptionProviderServlet return an empty array instead of HTTP 404 if no valid items to display
1 parent 49f175e commit 5c6fc58

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

core/src/main/java/com/exadel/aem/toolkit/core/optionprovider/servlets/OptionProviderServlet.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import javax.annotation.Nonnull;
2121
import javax.servlet.Servlet;
2222
import javax.servlet.ServletException;
23-
import javax.servlet.http.HttpServletResponse;
2423

2524
import org.apache.commons.collections4.CollectionUtils;
2625
import org.apache.sling.api.SlingHttpServletRequest;
@@ -73,28 +72,28 @@ public class OptionProviderServlet extends SlingSafeMethodsServlet {
7372
* @param response {@code SlingHttpServletResponse} instance
7473
*/
7574
@Override
76-
protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws ServletException, IOException {
75+
protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response)
76+
throws ServletException, IOException {
7777

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

80-
if (CollectionUtils.isEmpty(options) && isJsonOutput(request)) {
81-
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
80+
if (!isJsonOutput(request)) {
81+
DataSource ds = new SimpleDataSource(options.iterator());
82+
request.setAttribute(DataSource.class.getName(), ds);
8283
return;
8384
}
8485

85-
if (isJsonOutput(request)) {
86-
response.setContentType(CoreConstants.CONTENT_TYPE_JSON);
87-
response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
88-
try {
89-
response.getWriter().print(getJsonOutput(options));
90-
} catch (JSONException | NullPointerException e) {
91-
throw new ServletException(e);
92-
}
86+
response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
87+
response.setContentType(CoreConstants.CONTENT_TYPE_JSON);
88+
if (CollectionUtils.isEmpty(options)) {
89+
response.getWriter().write(CoreConstants.ARRAY_OPENING + CoreConstants.ARRAY_CLOSING);
9390
return;
9491
}
95-
96-
DataSource ds = new SimpleDataSource(options.iterator());
97-
request.setAttribute(DataSource.class.getName(), ds);
92+
try {
93+
response.getWriter().print(getJsonOutput(options));
94+
} catch (JSONException | NullPointerException e) {
95+
throw new ServletException(e);
96+
}
9897
}
9998

10099
/**
@@ -103,6 +102,9 @@ protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHtt
103102
* @return True or false
104103
*/
105104
private static boolean isJsonOutput(SlingHttpServletRequest request) {
105+
if (QUERY_VALUE_JSON.equalsIgnoreCase(request.getRequestPathInfo().getExtension())) {
106+
return true;
107+
}
106108
RequestParameter jsonParameter = request.getRequestParameter(QUERY_KEY_OUTPUT);
107109
if (jsonParameter == null) {
108110
return false;

0 commit comments

Comments
 (0)