Overriding the Query Pipeline, Pt II - Capture Club
kmcowan 3 May, 2018 2

Overriding the Query Pipeline, Pt II

In our prior discussion, I showed you how to override the query pipeline in Fusion. So what do we do now? Most often times, the reason people want to override is to manipulate the documents, or perform some sort of transformation on the result. The problem is that in a query pipeline you never actually have a hook to get your hands on the results. To do this, we need to make an asynchronous REST call and run our query, pull back the results and perform our tranformation, and then send them on along.
To accomplish that, we’ll build on our query pipeline override.
In order to make our rest call, we’re going to need to authenticate. To do that, we’ll have to spin up our Apache HttpClient, like so:

function getAuthHttpClient(){
var UsernamePasswordCredentials = org.apache.http.auth.UsernamePasswordCredentials;
        var AuthScope = org.apache.http.auth.AuthScope;
        var BasicCredentialsProvider = org.apache.http.impl.client.BasicCredentialsProvider;
        var HttpClientBuilder = org.apache.http.impl.client.HttpClientBuilder;
        var HttpPost = org.apache.http.client.methods.HttpPost;
        var StringEntity = org.apache.http.entity.StringEntity;
        var pwd = "YOUR_PASSWORD";
        var user = "ADMIN_USER_NAME";
        var fusionUrl = "http://localhost:8764";// base url to Fusion
        var client = null;
        var e = java.lang.Exception;
         // create our auth json to send
        var authJson = "{\"username\":\"" + user + "\", \"password\":\"" + pwd + "\"}";
                var authUrl = fusionUrl + "/api/session?realmName=native";
                var provider = new BasicCredentialsProvider();
                var credentials = new UsernamePasswordCredentials(user, pwd);
                provider.setCredentials(AuthScope.ANY, credentials);
                // spin up our HttpClient
                var client = HttpClientBuilder.create()
                var session = new HttpPost(authUrl);
                var params = new StringEntity(authJson);
                session.addHeader("content-type", "application/json");
                // execute the login request.
                var response = client.execute(session);
        } catch (e){
             logger.error("Error getting httpclient " + e.getLocalizedMessage());
return client;

Specifically, I don’t use the native Java HttpConnection here, because I want to maintain state. The call we make to the Session API┬ásets a cookie in our HttpClient, that will be used for subsequent requests.
With an authenticated client, we can go about making our Solr Query.

 function doHttpQuery(request) {
    var BufferedReader = java.io.BufferedReader;
    var InputStreamReader = java.io.InputStreamReader;
    var HttpGet = org.apache.http.client.methods.HttpGet;
    var e = java.lang.Exception;
    var url = "http://localhost:8764/api/apollo/solr/_test_/select?q=*:*&wt=json&rows=1&fl=id";
    var JSONObject = org.json.JSONObject;
    var json = new JSONObject();
    try {
        var client = getAuthHttpClient();
        json.put("msg1", "got auth client okay");
        var httpget = new HttpGet(url);
        var response = client.execute(httpget);
        json.put("msg2", "execute httpget okay...");
        if (response !== null) {
            var rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            var result = "";
            var line = "";
            while ((line = rd.readLine()) !== null) {
                result += line;
            json.put("result", result);
    } catch (e) {
        json.put("error", "some error occurred...");
    return json;

So here we grab our newly-authenticated HttpClient, and make a SolrQuery of our own choosing, which returns the basic response, which we thus set as “result” in our JSONObject.
Now that we have our documents in had, we can do our massaging and/or transformation and send them on along in the request, as we did in Part I.
Happy Searching!

2 thoughts on “Overriding the Query Pipeline, Pt II

Leave a Reply