TIPS & TRICKS

Java SDK and Same-Origin Policy

We all know about the limitations at the browser level that prevents any communication between 2 different domains, aka Same-Origin Policy. The examples below show how you can use the Java SDK to work around this policy by communicating with Splunk server (splunkd) in your server side code. This way your client side code gets the data served on a silver plate from the server and can focus on building awesome UIs using css, js etc.

Sample Java code for the method in your server side class that can be invoked directly from the JSP:

public String getSearchResults(String query, String outputMode) {
    // instantiate Service and connect
    Args args = new Args();
    args.add("username", "admin");
    args.add("password", "changeme");
    args.add("host", "localhost");
    args.add("port", 8089);
    Service service = Service.connect(args);
    		
    // specify the format for output_mode (json, xml, csv, json_cols or json_rows)
    Args outputArgs = new Args();
    outputArgs.put("output_mode", outputMode);
    
    // fetch search results into an InputStream and convert it to a String
    InputStream stream = service.oneshot(query, outputArgs);
    StringBuilder sb = new StringBuilder();
    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
    String line = null;
    try {
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
    } catch (IOException e) {
        // perform appropriate exception handling
        e.printStackTrace();
    }
    		
    return sb.toString();
}

 

 

Sample code for your JSP using Google’s JavaScript Charting library (Read more)

var drawChart = function(rows) {  
  var data = google.visualization.arrayToDataTable(rows);

  // Create and draw the visualization.
  new google.visualization.PieChart(document.getElementById('google-container')).draw(data, {title:"HTTP Status Codes on Splunk"});
};

// The javascript 'results' variable is set using a JSP scriptlet that runs the server side code using the Java SDK ...
<% String searchQuery = "search index=_internal sourcetype=splunk* | head 1000 |  chart count over status"; %>
results = <%= MyServerSideClass.getSearchResults(searchQuery, "json_rows") %>;

// Format the out to comply with what Google Charts expects
var rows = results.rows.slice();
var fields = results.fields.slice();

for(var i = 0; i < rows.length; i++) {
  var row = rows[i];
  for(var j = 0; j < row.length; j++) {
    if (j == 1) row[j] = parseInt(row[j]);
  }
}

// Add the headers in and draw
rows.unshift(fields);
drawChart(rows);

Sample Output:
Sample Chart output using Google JS Chart library

 

 

Sample code for your JSP using Splunk’s JavasScript charting library (download here)

// The javascript 'results' variable is set through a JSP scriptlet that invokles the server side code mentioned above
<% String searchQuery = "search index=_internal | head 1000 | stats count(host), count(source) by sourcetype"; %>
results = <%= MyServerSideClass.getSearchResults(searchQuery, "json_cols") %>;

var chart = null; 
var chartToken = splunkjs.UI.loadCharting("../splunkjs.lib/client/splunk.ui.charting.js", function() {
// Once we have the charting code, create a chart and update it.
    chart = new splunkjs.UI.Charting.Chart($("#chartContainerDivId"), splunkjs.UI.Charting.ChartType.COLUMN, false);
});

splunkjs.UI.ready(chartToken, function() {
 chart.setData(results, {
 "chart.stackMode": "stacked"
});
chart.draw();
});

Sample Output:
Sample Chart output using Splunk JS SDK

 

 

Want to learn more about the Splunk Developer Platform and using the SDKs? Join us for the Developing on Splunk sessions on .conf!

----------------------------------------------------
Thanks!
Neeraj Luthra

Splunk
Posted by

Splunk

Join the Discussion