02-25-2016 09:34 AM - edited 03-01-2019 06:40 AM
Task Name | Example on REST API integration |
Description | |
Prerequisites | Minimum UCSD version: 5.4.0.2 |
Category | Custom task |
Components | |
User Inputs | |
User Output |
Instructions for Regular Workflow Use:
Instructions for Regular Workflow Use:
Thank you Ryan Criss.
The Custom Task:
The Document:
The Code Example:
importPackage(com.cloupia.lib.util);
importPackage(org.apache.commons.httpclient);
importPackage(org.apache.commons.httpclient.cookie);
importPackage(org.apache.commons.httpclient.methods);
importPackage(org.apache.commons.httpclient.auth);
importPackage(org.apache.commons.httpclient.protocol);
//----------------------------------------------------------------------------------------
//
// Author: Ryan Criss (rcriss@cisco.com)
//
// Workflow Task Name: Open Library REST Example
//
// Version: 1.0
//
// Description:
// This custom task retrieves book details from the Open Library database when given an ISBN-10
//
// Inputs:
// ISBN10: The ISBN 10 of the book
//
// Outputs:
// None
//
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
// ### FUNCTIONS ###
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//
// Created By: Ryan Criss (rcriss@cisco.com)
//
// Function Name: openlibraryRetrieveBookNameByIsbn()
//
// Version: 1.0
//
// Description: Retrieves the book name given an ISBN10
//
// Inputs:
// isbn10: The ISBN-10 of the book
//
// Outputs:
// bookName: The book name
//
//----------------------------------------------------------------------------------------
function openlibraryRetrieveBookNameByIsbn10(isbn10) {
logger.addInfo("function openlibraryRetrieveBookNameByIsbn10");
// Setup HTTPS Connection
var httpClient = new HttpClient();
httpClient.getHostConfiguration().setHost("openlibrary.org", 443, "https");
httpClient.getHostConfiguration().setProxy("proxy.test.com", 80);
httpMethod = new GetMethod("/query.json?type=/type/edition&isbn_10="+isbn10+"&*=&limit=1");
// If basic authorization is required, you could add an authorization header
// httpMethod.addRequestHeader("Authorization", "Basic Y9laY346UjL7GmFAdSE=");
// If an authorization cookie is required, you can add a cookie
// httpMethod.addRequestHeader("Cookie", "cookiestring");
// Execute HTTPS and Return Results
httpClient.executeMethod(httpMethod);
statusCode = httpMethod.getStatusCode();
response = httpMethod.getResponseBodyAsString();
httpMethod.releaseConnection();
// Check Response Code and Parse Results
if (statusCode == 200){
logger.addInfo("Response received: "+response);
var jsonResponse = JSON.getJsonElement(response, null);
// Make sure the query found a matching book.
if (jsonResponse.size() == 0){
logger.addError("Request Failed. The book search did not find any books with that ISBN-10.");
ctxt.setFailed("Open Library book search failed.");
ctxt.exit();
}
// Retrieve the Title from the JSON object
var bookName = jsonResponse.get(0).get("title").getAsString();
logger.addInfo("Book Name: " + bookName);
return bookName;
} else {
// HTTP Request Failed
logger.addError("Request Failed. HTTP response code: "+statusCode);
logger.addError("Response = " + response);
ctxt.setFailed("Open Library request failed.");
ctxt.exit();
}
} //function openlibraryRetrieveBookNameByIsbn10
//----------------------------------------------------------------------------------------
//
// Created By: Ryan Criss (rcriss@cisco.com)
//
// Function Name: openlibraryRetrieveBookDetailsByIsbn()
//
// Version: 1.0
//
// Description: Retrieves the book details given an ISBN10
//
// Inputs:
// isbn10: The ISBN-10 of the book
//
// Outputs:
// bookDetails: The book details as an object with individual details stored as properties
//
//----------------------------------------------------------------------------------------
function openlibraryRetrieveBookDetailsByIsbn10(isbn10) {
logger.addInfo("function openlibraryRetrieveBookDetailsByIsbn10");
// Setup HTTPS Connection
var httpClient = new HttpClient();
httpClient.getHostConfiguration().setHost("openlibrary.org", 443, "https");
httpClient.getHostConfiguration().setProxy("proxy.test.com", 80);
httpMethod = new GetMethod("/query.json?type=/type/edition&isbn_10="+isbn10+"&*=&limit=1");
// If basic authorization is required, you could add an authorization header
// httpMethod.addRequestHeader("Authorization", "Basic Y936glaY346UjhL7Gm3FAdSE=");
// If an authorization cookie is required, you can add a cookie
// httpMethod.addRequestHeader("Cookie", "cookiestring");
// Execute HTTPS and Return Results
httpClient.executeMethod(httpMethod);
statusCode = httpMethod.getStatusCode();
response = httpMethod.getResponseBodyAsString();
httpMethod.releaseConnection();
// Check Response Code and Parse Results
if (statusCode == 200){
logger.addInfo("Response received: "+response);
var jsonResponse = JSON.getJsonElement(response, null);
// Make sure the query found a matching book.
if (jsonResponse.size() == 0){
logger.addError("Request Failed. The book search did not find any books with that ISBN-10.");
ctxt.setFailed("Open Library book search failed.");
ctxt.exit();
}
// Retrieve the Details from the JSON object
var bookDetails = {};
// Book Title
bookDetails.title = jsonResponse.get(0).get("title").getAsString();
logger.addInfo("Book Title: " + bookDetails.title);
// Book Description
bookDetails.description = jsonResponse.get(0).get("description").getAsString();
logger.addInfo("Book Description: " + bookDetails.description);
// Book Genres - Might be multiple genres, so will create an array
bookDetails.genres = [];
for (var i = 0; i < jsonResponse.get(0).get("genres").size(); i++){
bookDetails.genres.push(jsonResponse.get(0).get("genres").get(i).getAsString());
}
logger.addInfo("Book Genres: " + bookDetails.genres.toString());
// Book Publishers - Might be multiple publishers, so will create an array
bookDetails.publishers = [];
for (var i = 0; i < jsonResponse.get(0).get("publishers").size(); i++){
bookDetails.publishers.push(jsonResponse.get(0).get("publishers").get(i).getAsString());
}
logger.addInfo("Book Publishers: " + bookDetails.publishers.toString());
// Author Keys - Might be multiple author keys, so will create an array
bookDetails.authorKeys = [];
for (var i = 0; i < jsonResponse.get(0).get("authors").size(); i++){
bookDetails.authorKeys.push(jsonResponse.get(0).get("authors").get(i).get("key").getAsString());
}
logger.addInfo("Author Keys: " + bookDetails.authorKeys.toString());
// Return the bookDetails Object
return bookDetails;
} else {
// HTTP Request Failed
logger.addError("Request Failed. HTTP response code: "+statusCode);
logger.addError("Response = " + response);
ctxt.setFailed("Open Library request failed.");
ctxt.exit();
}
} //function openlibraryRetrieveBookDetailsByIsbn10
//----------------------------------------------------------------------------------------
//
// Created By: Ryan Criss (rcriss@cisco.com)
//
// Function Name: openlibraryRetrieveAuthorNameByAuthorKey()
//
// Version: 1.0
//
// Description: Retrieves the author name given an author key
//
// Inputs:
// authorKey: The author key
//
// Outputs:
// authorName: The author name
//
//----------------------------------------------------------------------------------------
function openlibraryRetrieveAuthorNameByAuthorKey(authorKey) {
logger.addInfo("function openlibraryRetrieveAuthorNameByAuthorKey");
// Setup HTTPS Connection
var httpClient = new HttpClient();
httpClient.getHostConfiguration().setHost("openlibrary.org", 443, "https");
httpClient.getHostConfiguration().setProxy("proxy.test.com", 80);
httpMethod = new GetMethod(authorKey+".json");
// If basic authorization is required, you could add an authorization header
// httpMethod.addRequestHeader("Authorization", "Basic Y9laY346UjL7GmFAdSE=");
// If an authorization cookie is required, you can add a cookie
// httpMethod.addRequestHeader("Cookie", "cookiestring");
// Execute HTTPS and Return Results
httpClient.executeMethod(httpMethod);
statusCode = httpMethod.getStatusCode();
response = httpMethod.getResponseBodyAsString();
httpMethod.releaseConnection();
// Check Response Code and Parse Results
if (statusCode == 200){
logger.addInfo("Response received: "+response);
var jsonResponse = JSON.getJsonElement(response, null);
// Retrieve the author name from the JSON object
var authorName = jsonResponse.get("name").getAsString();
logger.addInfo("Author Name: " + authorName);
return authorName;
} else {
// HTTP Request Failed
logger.addError("Request Failed. HTTP response code: "+statusCode);
logger.addError("Response = " + response);
ctxt.setFailed("Open Library request failed.");
ctxt.exit();
}
} //function openlibraryRetrieveAuthorNameByAuthorKey
//----------------------------------------------------------------------------------------
// ### MAIN PROGRAM ###
//----------------------------------------------------------------------------------------
// Workflow Inputs Mappings
var isbn10 = input.isbn10;
// Retrieve Book Name By ISBN-10 (Example to show retrieving and returning a single item)
var bookName = openlibraryRetrieveBookNameByIsbn10(isbn10);
// Retrieve Book Details By ISBN-10 (Retrieves the bookDetails object with the details stored in properties)
var bookDetails = openlibraryRetrieveBookDetailsByIsbn10(isbn10);
// Retrieve Author Names by Author Keys (Because the above method only returned the author key - not the name)
var authorNames = [];
for (var i = 0; i < bookDetails.authorKeys.length; i++){
authorNames.push(openlibraryRetrieveAuthorNameByAuthorKey(bookDetails.authorKeys[i]));
}
// Pass Outputs
output.bookName = bookName;
output.bookTitle = bookDetails.title;
output.bookDescription = bookDetails.description;
output.bookGenres = bookDetails.genres.toString();
output.bookPublishers = bookDetails.publishers.toString();
output.bookAuthors = authorNames.toString();
I added the following function:
function authenticateUser(user,password)
{
var token = user +":" + password;
var hash = Base64.encode(token);
return "Basic " + hash;
}
So that I can base 64 encode the input.. Director tells me that both these functions don't exist
boa and
Base64.encode
am I missing something
try this
importPackage(java.util);
importPackage(java.lang);
importPackage(java.io);
var x=input.mask;
logger.addInfo("My Netmask = "+x);
var base64encodedString = Base64.getEncoder().encodeToString(x.getBytes("utf-8"));
logger.addInfo("Base64 Encoded String: " + base64encodedString);
That worked.
function authenticateUser(user,password)
{
var token = user +":" + password;
var hash = Base64.getEncoder().encodeToString(token.getBytes("utf-8"));
return hash
}
Good..
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: