[ 
https://issues.apache.org/jira/browse/SLING-7509?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16382542#comment-16382542
 ] 

ASF GitHub Bot commented on SLING-7509:
---------------------------------------

volteanu commented on a change in pull request #5: SLING-7509 - Add QueryClient
URL: 
https://github.com/apache/sling-org-apache-sling-testing-clients/pull/5#discussion_r171674340
 
 

 ##########
 File path: 
src/main/java/org/apache/sling/testing/clients/query/servlet/QueryServlet.java
 ##########
 @@ -0,0 +1,168 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package org.apache.sling.testing.clients.query.servlet;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.osgi.service.component.annotations.Component;
+
+import javax.jcr.Session;
+import javax.jcr.query.*;
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import java.io.IOException;
+import java.util.Date;
+
+import static 
org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_METHODS;
+import static 
org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_PATHS;
+
+@Component(
+        name = QueryServlet.SERVLET_NAME,
+        service = {Servlet.class},
+        property = {
+                SLING_SERVLET_PATHS + "=" + QueryServlet.SERVLET_PATH,
+                SLING_SERVLET_METHODS + "=GET"
+        }
+)
+public class QueryServlet extends SlingSafeMethodsServlet {
+    private static final long serialVersionUID = 1L;
+
+    public static final String SERVLET_PATH = "/system/testing/query";
+    public static final String SERVLET_NAME = "Sling Testing Clients Query 
Servlet";
+
+    @Override
+    protected void doGet(SlingHttpServletRequest request, 
SlingHttpServletResponse response)
+            throws ServletException, IOException {
+
+        response.setContentType("application/json");
+        response.setCharacterEncoding("UTF-8");
+
+        try {
+            final QueryManager qm = 
request.getResourceResolver().adaptTo(Session.class)
+                    .getWorkspace().getQueryManager();
+
+            long before = 0;
+            long after = 0;
+            long total = 0;
+
+            String query = request.getParameter("query");
+            String type = request.getParameter("type");
+
+            // default for showResults is true, unless parameter is matching 
exactly "false"
+            boolean showResults = 
!("false".equalsIgnoreCase(request.getParameter("showresults")));
+            // default for explainQuery is false, unless parameter is present 
and is not matching "false"
+            String explainParam = request.getParameter("explain");
+            boolean explainQuery = (explainParam != null) && 
!("false".equalsIgnoreCase(explainParam));
+
+            boolean tidy = false;
+            for (String selector : 
request.getRequestPathInfo().getSelectors()) {
+                if ("tidy".equals(selector)) {
+                    tidy = true;
+                }
+            }
+
+            if ((query == null) || query.equals("") || (type == null) || 
type.equals("")) {
+                response.sendError(400, "Parameters query and type are 
required"); // invalid request
+                return;
+            }
+
+            // prepare
+            if (explainQuery) {
+                query = "explain " + query;
+            }
+
+            Query q = qm.createQuery(query, type);
+
+            // execute
+            before = new Date().getTime();
+            QueryResult result = q.execute();
+            after = new Date().getTime();
+
+            // collect results
+            String firstSelector = null;
+            if (result.getSelectorNames().length > 1) {
+                firstSelector = result.getSelectorNames()[0];
+                try {
+                    String[] columnNames = result.getColumnNames();
+                    if (columnNames.length > 0) {
+                        String firstColumnName = columnNames[0];
+                        int firstDot = firstColumnName.indexOf('.');
+                        if (firstDot > 0) {
+                            firstSelector = firstColumnName.substring(0, 
firstDot);
+                        }
+                    }
+                } catch (Exception ignored) {
+                }
+            }
+
+            ObjectMapper mapper = new ObjectMapper();
+            ObjectNode responseJson = mapper.createObjectNode();
+
+            if (explainQuery) {
+                responseJson.put("plan", 
result.getRows().nextRow().getValue("plan").getString());
+            } else if (showResults) {
+                ArrayNode results = mapper.createArrayNode();
+
+                RowIterator rows = result.getRows();
+                while (rows.hasNext()) {
+                    Row row = rows.nextRow();
+                    String rowPath = (firstSelector != null) ? 
row.getPath(firstSelector) : row.getPath();
+                    String rowType = (firstSelector != null)
+                            ? 
row.getNode(firstSelector).getPrimaryNodeType().getName()
+                            : row.getNode().getPrimaryNodeType().getName();
+
+                    ObjectNode rowJson = mapper.createObjectNode();
+                    rowJson.put("path", rowPath);
+                    rowJson.put("type", rowType);
+                    results.add(rowJson);
+
+                    total++;
+                }
+
+                responseJson.set("results", results);
+            } else {
+                // only count results
+                RowIterator rows = result.getRows();
+                while (rows.hasNext()) {
+                    rows.nextRow();
+                    total++;
+                }
+            }
+
+            responseJson.put("total", total);
+            responseJson.put("time", after - before);
+
+            if (tidy) {
+                
response.getWriter().write(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(responseJson));
+            } else {
+                response.getWriter().write(responseJson.toString());
+            }
+
+        } catch (InvalidQueryException e) {
+            // Consider InvalidQueryException as an invalid request instead of 
sending 500 server error
+            response.sendError(400, e.getMessage());
+            e.printStackTrace(response.getWriter());
 
 Review comment:
   Sending the error to the client is more important than logging it 
server-side. The stacktrace is there for debugging, but as you said, it doesn't 
hurt for testing.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Add QueryClient
> ---------------
>
>                 Key: SLING-7509
>                 URL: https://issues.apache.org/jira/browse/SLING-7509
>             Project: Sling
>          Issue Type: New Feature
>          Components: Apache Sling Testing Clients
>            Reporter: Valentin Olteanu
>            Priority: Major
>
> Currently, there is no way to run queries in sling using the clients. This is 
> needed in several tests to search content and assert the effects of a feature.
>  
> The solution proposed in 
> [https://github.com/apache/sling-org-apache-sling-testing-clients/pull/5] 
>  * installs a custom query servlet
>  * runs the query in any of the supported format
>  * returns results as json



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to