2020import javax .annotation .Nonnull ;
2121import javax .servlet .Servlet ;
2222import javax .servlet .ServletException ;
23- import javax .servlet .http .HttpServletResponse ;
2423
2524import org .apache .commons .collections4 .CollectionUtils ;
2625import 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