Making an Authenticated REST Call in a Fusion Pipeline
REST… easy!
There are times you just need to reach outside the box and do something. Sometimes that something would be launching a process, or perhaps making an alternate query, or even calling some third party API, like ESRI. If the REST call you’re making doesn’t require authentication, you can do that with a plain, ole, native HttpConnection. However, if you need to authenticate, you’ll want to use the Apache HttpClient.
To that end, I’ve prepared a brief example of how to authenticate an asynchronous REST call in a Fusion pipeline stage.
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 e = java.lang.Exception; var pwd = "YOUR_PASSWORD"; var user = "ADMIN_USER_NAME"; var fusionUrl = "http://localhost:8764"; var client = null; try { 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); var client = HttpClientBuilder.create() .setDefaultCredentialsProvider(provider) .build(); var session = new HttpPost(authUrl); var params = new StringEntity(authJson); session.addHeader("content-type", "application/json"); session.setEntity(params); var response = client.execute(session); } catch (e) { logger.error("Error getting httpclient " + e.getLocalizedMessage()); } return client; }
Breaking it down…
The first thing we need to do is declare our Java imports. We do that here:
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 e = java.lang.Exception;
This means that we can now use these various Java classes in our JavaScrip stage, which is really pretty nifty. Always keep in mind that you have access to ANY Java class available in the connectors classpath, and you can load your own jars into the context using the load() method, but I digress.
From here we’ll declare our instance variables, to wit:
var pwd = "YOUR_PASSWORD"; var user = "ADMIN_USER_NAME"; var fusionUrl = "http://localhost:8764"; var client = null;
With the basics in hand, we’re ready to instantiate our client and authenticate. In this instance, we’ll be authenticating agsinst the very Fusion instnace in which this stage would be running.
try { var authJson = "{\"username\":\"" + user + "\", \"password\":\"" + pwd + "\"}"; var authUrl = fusionUrl + "/api/session?realmName=native"; var provider = new BasicCredentialsProvider(); // spin up new Credentials using our u/p var credentials = new UsernamePasswordCredentials(user, pwd); // create the credentials provider provider.setCredentials(AuthScope.ANY, credentials); // and now use these two things to spin up our HttpClient var client = HttpClientBuilder.create() .setDefaultCredentialsProvider(provider) .build(); // Now authenticate with Fusion using the Session API var session = new HttpPost(authUrl); var params = new StringEntity(authJson); session.addHeader("content-type", "application/json"); session.setEntity(params); // our session cookie is set automatically in this case. we can now make whatever calls are required to Fusion. var response = client.execute(session); } catch (e) { logger.error("Error getting httpclient " + e.getLocalizedMessage()); }
And that’s all there is to it. Happy Searching! 🙂