Making Asynchronous Requests - Lucidworks Fusion JavaScript Stage - Capture Club
kmcowan 7 May, 2018 0

Making Asynchronous Requests — Lucidworks Fusion JavaScript Stage

There are many ways you can make an asynchronous request in a JavaScript stage.

  1. Using the Apache HTTP Client
  2. Using the native Java URL or URI classes
  3. Using the native Java HttpConnection class
  4. Using the Jsoup built-in connect() method

Examples of each


1) Apache HTTP Client

function(doc) {
   var BufferedReader = java.io.BufferedReader;
var InputStreamReader = java.io.InputStreamReader;
var userAgent = org.apache.http.HttpHeaders.USER_AGENT;
var HttpResponse = org.apache.http.HttpResponse;
var HttpClient = org.apache.http.client.HttpClient;
var HttpGet = org.apache.http.client.methods.HttpGet;
var HttpClientBuilder = org.apache.http.impl.client.HttpClientBuilder;
var StringBuffer = java.lang.StringBuffer;
var String = java.lang.String;
var e = java.lang.Exception;
result = new StringBuffer();
        try {
            var url = new String( "http://www.google.com/search?q=httpClient");
            var client = HttpClientBuilder.create().build();
            var request = new HttpGet(url);
             // add request header
            request.addHeader("User-Agent", userAgent);
            var response = client.execute(request);
            logger.info("RESPONSE Code : " + response.getStatusLine().getStatusCode());
            var rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
             result = new StringBuffer();
            var line = "";
            while ((line = rd.readLine()) !== null) {
                result.append(line);
            }
        } catch (e) {
           logger.error(e);
        }
       logger.info(result);
   return doc;
}

function (url) {
    var e = java.lang.Exception;
    var URL = java.net.URL;
    var BufferedReader = java.io.BufferedReader;
    var InputStreamReader = java.io.InputStreamReader;
    var String = java.lang.String;
    var result = new String();
    try {
        var targetUrl = new URL(url);
        var ins = new BufferedReader(new InputStreamReader(targetUrl.openStream()));
        var inputLine = "";
        while ((inputLine = ins.readLine()) !== null) {
            //logger.info(inputLine);
            result += inputLine;
        }
        ins.close();
        logger.info("Return RESULT: " + result);
    } catch (e) {
        logger.error(e);
    }
    return result;
}

3) Using Java native HttpConnection

function (docurl) {
    var HttpURLConnection = java.net.HttpURLConnection;
    var URL = java.net.URL;
    var conn = java.net.HttpURLConnection;
    var e = java.lang.Exception;
    var len = 0;
    try {
        var url = new URL(docurl);
        conn = url.openConnection();
        conn.setRequestMethod("HEAD");
        conn.getInputStream();
        len = conn.getContentLength();
    } catch (e) {
        len = -1;
    } finally {
        if(conn.disconnect){
        conn.disconnect();
        }
    }
   return len;
};

4) JSoup Built-in

function (doc) {
        var doclist = java.util.ArrayList;
        var Jsoup = org.jsoup.Jsoup;
        var jdoc = org.jsoup.nodes.Document;
        var ex = java.lang.Exception;
 try {
           jdoc = Jsoup.connect("http://example.com/").get();
         var title = jdoc.title();
        } catch (ex) {
            logger.error(ex);
        }

Generally, these methods are internchangeable. However, you’ll always want to consider your end goal and any particular constaints before makeing a decision. For example, it’s always best to try and use the native Java classes rather than third-parties, but sometimes the HttpClient just makes more sense. If you’re planning to extract with JSoup, then you would like choose that route. It all depends on what you’re trying to accomplish.

Leave a Reply