Class QueryClient

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, org.apache.http.client.HttpClient

    public class QueryClient
    extends SlingClient

    Sling client for performing oak queries.

    Uses a custom servlet QueryServlet to execute the query on the server and return the results as a json. If the servlet is not yet present, it automatically installs it and creates the corresponding nodes

    The servlet is exposed under "/system/testing/query".

    The servlet is not automatically uninstalled to avoid too much noise on the instance. The caller should take care of it, if needed, by calling uninstallServlet()

    • Constructor Detail

      • QueryClient

        public QueryClient​(org.apache.http.impl.client.CloseableHttpClient http,
                           SlingClientConfig config)
                    throws ClientException
        Constructor used by adaptTo
        Parameters:
        http - underlying HttpClient
        config - config state
        Throws:
        ClientException - if the client cannot be created
      • QueryClient

        public QueryClient​(java.net.URI url,
                           java.lang.String user,
                           java.lang.String password)
                    throws ClientException
        Convenience constructor
        Parameters:
        url - host url
        user - username
        password - password
        Throws:
        ClientException - if the client cannot be constructed
    • Method Detail

      • doQuery

        public org.codehaus.jackson.JsonNode doQuery​(java.lang.String query,
                                                     QueryClient.QueryType type)
                                              throws ClientException,
                                                     java.lang.InterruptedException
        Executes a query on the server and returns the results as a json
        Parameters:
        query - query to be executed
        type - type of the query
        Returns:
        the results in json as exported by QueryServlet
        Throws:
        ClientException - if the request failed to execute
        java.lang.InterruptedException - to mark that this method blocks
      • doCount

        public long doCount​(java.lang.String query,
                            QueryClient.QueryType type)
                     throws ClientException,
                            java.lang.InterruptedException
        Executes a query on the server and returns only the number of rows in the result
        Parameters:
        query - query to be executed
        type - type of the query
        Returns:
        total results returned by the query
        Throws:
        ClientException - if the request failed to execute
        java.lang.InterruptedException - to mark that this method blocks
      • getPlan

        public java.lang.String getPlan​(java.lang.String query,
                                        QueryClient.QueryType type)
                                 throws ClientException,
                                        java.lang.InterruptedException
        Retrieves the plan of the query. Useful for determining which index is used
        Parameters:
        query - query to be executed
        type - type of the query
        Returns:
        total results returned by the query
        Throws:
        ClientException - if the request failed to execute
        java.lang.InterruptedException - to mark that this method blocks
      • doQuery

        protected org.codehaus.jackson.JsonNode doQuery​(java.lang.String query,
                                                        QueryClient.QueryType type,
                                                        boolean showResults,
                                                        boolean explain)
                                                 throws ClientException,
                                                        java.lang.InterruptedException
        Throws:
        ClientException
        java.lang.InterruptedException
      • installServlet

        public QueryClient installServlet()
                                   throws ClientException,
                                          java.lang.InterruptedException

        Installs the servlet to be able to perform queries.

        By default, methods of this client automatically install the servlet if needed, so there is no need to explicitly call from outside

        Returns:
        this
        Throws:
        ClientException - if the installation fails
        java.lang.InterruptedException - to mark that this method blocks