Using Query Pipelines with SolrJ

Fusion Pipelines can be used in conjunction with a SolrJ client, allowing you to use the power of Fusion pipelines with an existing Solr installation.

Authentication with SolrJ

Fusion user authentication and authorization is carried out by the Fusion UI service. For details on how Fusion handles authentication and authorization, please see Access Control.

When using SolrJ, however, there are two approaches that can be used: basic authentication and passing credentials in the URL. Once the authentication has occurred, the roles that have been assigned to the user provide the authorization.

Basic Authentication

The Basic authentication approach looks very similar to the session-based approach. However, some classes are changed.

    HttpClient client = HttpClientBuilder.create().useSystemProperties()
        .addInterceptorLast(new PreEmptiveBasicAuthenticator(user, password))
        .build();
    HttpSolrServer server = new HttpSolrServer(url, client);
   // ...
  public static class PreEmptiveBasicAuthenticator implements HttpRequestInterceptor {
    private final UsernamePasswordCredentials credentials;
    public PreEmptiveBasicAuthenticator(String user, String pass) {
      credentials = new UsernamePasswordCredentials(user, pass);
    }
    public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
      request.addHeader(BasicScheme.authenticate(credentials,"US-ASCII",false));
    }
  }

URL Credentials

The URL credential approach provides the ability to pass the authentication properties in the URL, as in this example:

    String url = "http://user:pass@localhost:8764/api/apollo/solr/demo";
    HttpSolrServer server = new HttpSolrServer(url);

Example

The example below demonstrates the use of query profiles and query pipelines for querying a collection named 'test' with a query pipeline named 'default', using the basic authentication approach.

package com.lucidworks.apollo.testQueryPipeline;

import org.apache.http.HttpRequestInterceptor;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.ModifiableSolrParams;
public class TestQPSolrJ {
    public static void main(String[] args) throws SolrServerException {
        /**
         * Request URL points to Fusion UI which uses authentication proxy.
         * Example uses basic authentication
         * URL is Apollo query-pipelines endpoint:
         * http://{hostname}:{port_name}/api/apollo/query-pipelines/{pipeline_name}/collections/{collection_name}
         */
        String url = "http://localhost:8764/api/apollo/query-pipelines/default/collections/demo";
        String user = "admin";
        String password = "password123";
        HttpClient client = HttpClientBuilder.create().useSystemProperties()
            .addInterceptorLast(new PreEmptiveBasicAuthenticator(user, password))
            .build();
        HttpSolrServer solrServer = new HttpSolrServer(url, client);
        solrServer.setParser(new XMLResponseParser());
        ModifiableSolrParams solrParams = new ModifiableSolrParams();
        solrParams.add("q", "*:*");
        QueryResponse out = solrServer.query(solrParams);
        System.out.println("QTime is " + out.getQTime());
        System.out.println("RH is " + out.getResponseHeader().toString());
    }
    public static class PreEmptiveBasicAuthenticator implements HttpRequestInterceptor {
        private final UsernamePasswordCredentials credentials;
        public PreEmptiveBasicAuthenticator(String user, String pass) {
            credentials = new UsernamePasswordCredentials(user, pass);
        }
        public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
            request.addHeader(BasicScheme.authenticate(credentials,"US-ASCII",false));
        }
    }

}

For more information about SolrJ, see the Apache Solr Reference Guide section Using SolrJ.