Sterling Managed File Transfer

Sterling Managed File Transfer

Come for answers, stay for best practices. All we're missing is you.

 View Only

SFG_B2Bi REST API Example Clients

By Tanvi Kakodkar posted Mon February 10, 2020 02:56 PM

  
Originally updated on December 16, 2016 by Drew Myers

Introduction

The B2Bi REST APIs allow users to programmatically Create, Read, Update, and Delete resources in B2Bi. Two examples of client programs that interact with the REST APIs in two different programming languages, javascript (Node.js) and Java, are given below.

You can find the source for both of these programs at the bottom of this article, or here:

https://github.com/drew-myers-ibm/sfg-api-client

Both these examples use APIs related to the mailbox functionality in B2Bi to list the contents of a mailbox, upload a file into that mailbox, and then list the contents again to show that the file has been uploaded.

Few other steps when using these example programs as a starting point for a new project would be to enable the clients to connect to the REST APIs using HTTPS, store the password used to authenticate the client user in a secure way, as well as build more REST calls to interact with other B2Bi entities that are not included here.

Output of the Programs

Node.js:

 
Java:

Source of the Programs:
Node.js: 

var Client = require('node-rest-client').Client;

//Define URI constants
//TODO: Customize these
var address = "http://hostname.com";
var port = "1234"
var apiUri = "/B2BAPIs/svc/"
var baseUri = address + ":" + port + apiUri;

//These are the mailbox you'll be adding into. These need to match
//TODO: customize these
var mailboxPath = "/YXt43Lo";
var mailboxId = "1410";

//Setup HTTP auth info, B2Bi/SFG user with "APIUser" permission
//TODO: Customize these, please eventually do something smarter
// than this with the password
var auth_info = {user:"admin", password:"password"};

//Create client instance with auth info
client = new Client(auth_info);

//Define required headers
var headers = {
	"Content-Type": "application/json",
	"Accept": "application/json"
};

//Argument set with only required headers
var basicArgs = {
	"headers": headers
};

//Define arguments for document create
var createDocArgs = {
	"headers": headers,
	data:{
		"payload": "SGVsbG8gV29ybGQh"  //Hello World! Base 64 encoded
	}
};


//Register this method since we will call it twice
client.registerMethod("listRootMailbox", baseUri + "mailboxcontents/?parentMailboxId=" + mailboxId, "GET");

//Setup over, start HTTP action here
//Lots of callback action happening here because we need to work with results
//If this was any more complex would have been smart to use js promises

//List mailbox contents
client.methods.listRootMailbox(basicArgs, function(data, response) {
	//Print list
	console.log("LIST:");
	console.log(data); //Library pre-parses list result for some reason

	//Create document
	client.post(baseUri + "documents/", createDocArgs, function(data, response) {
	
		//Parse out id of created document
		var docCreateResult = JSON.parse(data);
		var splitLocationArray = docCreateResult.Location.split("/");
		var id = splitLocationArray[splitLocationArray.length-1];
		console.log("\n\nCreated document: " + id);
	
		//Define arguments for message create
		//Have to do this here because we need the id we just recieved
		var createMessageArgs = {
			"headers": headers,
			data:{
				"documentId": id,
				"extractableCount": 1,
				"mailboxPath": mailboxPath,
				"name": "demo_file.txt"
			}
		}
	
		//Create message
		client.post(baseUri + "mailboxmessages/", createMessageArgs, function(data, response) {
			
			//Parse out id of created message
			var messageCreateResult = JSON.parse(data);
			var splitLocationArray = messageCreateResult.Location.split("/");
			var id = splitLocationArray[splitLocationArray.length-1];
			console.log("\n\nCreated message: " + id);
	
			//List again to show message has been uploaded
			client.methods.listRootMailbox(basicArgs, function(data, response) {
				console.log("\n\nLIST:");
				console.log(data); //Library pre-parses list result for some reason
			});
		});
	});
});

 

 

Java:
 

package com.ibm.b2b.api.mailbox.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONException;
import org.json.JSONObject;


public class ApiClient {

	//Hostname and port
	//TODO: edit this to specify the location of your API server
	private static final String HTTP_BASE = "http://";
	private static final String HTTPS_BASE = "https://";
	private static final String HOSTNAME = "hostname";
	private static final int PORT = 1234;
	private static final String MBX_EXT = "/B2BAPIs/svc/";
	private static final String API_URI_BASE = HTTP_BASE + HOSTNAME + ":" + PORT + MBX_EXT;

	//API URI's
	private static final String DOCUMENT_URI = "documents/";
	private static final String MAILBOX_URI = "mailboxes/";
	private static final String MESSAGE_URI = "mailboxmessages/";
	private static final String MAILBOX_CONTENTS_URI = "mailboxcontents/?parentMailboxId=";

	//Headers
	private static final String CONTENT_TYPE = "Content-Type";
	private static final String ACCEPT = "Accept";
	private static final String APPLICATION_JSON = "application/json";

	//Auth
	//TODO: edit these to specify a user created in B2Bi that has the "API User" permission
	private static final String USERNAME = "admin";
	//Eventually you'll want to do something smarter with the password
	private static final String PASSWORD = "password";

	//TODO: set this to a mailbox you want to operate on
	//Hardcoded mailbox ID and path for demo purposes
	//These 2 must match
	private static final String MBX_LIST_URI = "1410";
	private static final String MBX_PATH = "/YXt43Lo";


	public static void main(String[] args) throws IOException, JSONException {

		//
		// List mailbox contents
		//
		CloseableHttpResponse response = null;
		try {
			//Create and make request
			HttpRequestBase request = createRequest("GET", MAILBOX_CONTENTS_URI+MBX_LIST_URI, null);
			response = executeRequest(request);

			//Print body of response to system.out
			printResponseFromEntity(response);

		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		} finally { //Must close response
			response.close();
		}


		//
		// Create Document
		//
		//

		//This will store the id of the document we're going to create
		String documentId = null;

		//Create JSON request body
		JSONObject docJson = new JSONObject();
		docJson.put("payload", "SGVsbG8gV29ybGQh"); //Hello World! Base 64 encoded
		String docJsonString = docJson.toString();

		try {
			//Create and make request
			HttpRequestBase request = createRequest("POST", DOCUMENT_URI, docJsonString);
			response = executeRequest(request);
			
			//Parse created document id out of response JSON
			HttpEntity entity = response.getEntity();
			JSONObject json = new JSONObject(getResponseString(entity));
			
			//Get created Location element from JSON
			String jsonLocation = (String) json.get("Location");
			
			//Split the location and grab the last entry, the document id
			String[] jsonLocationSplit = jsonLocation.split("/");
			documentId = jsonLocationSplit[jsonLocationSplit.length-1];

			System.out.println("Created document id: " + documentId);
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		} finally { //Must close response
			response.close();
		}

		//
		// Create Message
		//
		//Create JSON request body
		JSONObject msgJson = new JSONObject();
		msgJson.put("documentId", documentId);
		msgJson.put("extractableAlways", true);
		msgJson.put("mailboxPath", ApiClient.MBX_PATH);
		msgJson.put("name", "demoFile.txt");
		String msgJsonString = msgJson.toString();

		try {
			//Create and make request
			HttpRequestBase request = createRequest("POST", MESSAGE_URI, msgJsonString);
			response = executeRequest(request);

			printResponseFromEntity(response);	
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		} finally { //Must close response
			response.close();
		}


		//
		// List mailbox contents, again
		//
		try {
			//Create and make request
			HttpRequestBase request = createRequest("GET", MAILBOX_CONTENTS_URI+MBX_LIST_URI, null);
			response = executeRequest(request);

			//Print body of response to system.out
			printResponseFromEntity(response);

		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		} finally { //Must close response
			response.close();
		}
	}


	/**
	 * Creates an HTTP request of the provided type, using the provided URI with the body containing the provided JSON
	 * 
	 * @param requestType - GET, POST, PUT, or DELETE
	 * @param URI - Full URI to make request to, eg; http://test.com/api/someid123
	 * @param jsonString - JSON string to be sent in the request body
	 * @return - The request object that has not yet been executed
	 * @throws UnsupportedEncodingException - if the JSON body uses characters not in the default HTTP charset
	 */
	private static HttpRequestBase createRequest(String requestType, String URI, String jsonString) throws UnsupportedEncodingException {
		HttpRequestBase httpRequest = null;
		switch(requestType.toUpperCase()) {
		case "GET": 
			httpRequest = new HttpGet(API_URI_BASE + URI);
			break;
		case "POST": 
			httpRequest = new HttpPost(API_URI_BASE + URI);
			((HttpPost) httpRequest).setEntity(new StringEntity(jsonString));
			break;
		case "PUT": 
			httpRequest = new HttpPut(API_URI_BASE + URI);
			((HttpPut) httpRequest).setEntity(new StringEntity(jsonString));
			break;
		case "DELETE": 
			httpRequest = new HttpDelete(API_URI_BASE + URI);
			break;	
		}
		httpRequest.setHeader(CONTENT_TYPE, APPLICATION_JSON);
		httpRequest.setHeader(ACCEPT, APPLICATION_JSON);
		return httpRequest;
	}


	/**
	 * Executes the provided request and returns a response object
	 * 
	 * @param request - HTTP request to be executed
	 * @return - Response to the executed request
	 * @throws ClientProtocolException - if there is an HTTP protocol error during the request execution
	 * @throws IOException - if there is an error at the connection level during request execution
	 */
	private static CloseableHttpResponse executeRequest(HttpRequestBase request) throws ClientProtocolException, IOException {
		//Setup HTTP auth
		CredentialsProvider credsProvider = new BasicCredentialsProvider();
		credsProvider.setCredentials(new AuthScope(HOSTNAME, PORT),new UsernamePasswordCredentials(USERNAME, PASSWORD));
		CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();

		return httpclient.execute(request);
	}


	/**
	 * Prints the body of the response object to System.out
	 * 
	 * @param response - Response to be printed from
	 * @throws UnsupportedOperationException - if the response body cannot be streamed correctly
	 * @throws IOException - if there is an error streaming the response body
	 */
	private static void printResponseFromEntity(CloseableHttpResponse response) throws UnsupportedOperationException, IOException {
		System.out.println(response.getStatusLine());
		HttpEntity entity = response.getEntity();
		System.out.println(getResponseString(entity));
	}


	/**
	 * Streams the content of the provided entity into a String and returns that String
	 * 
	 * @param entity - Entity to take content from
	 * @return - String containing the content of the entity
	 * @throws UnsupportedOperationException - if the entity content cannot be streamed to a string
	 * @throws IOException - if there is an error streaming the entity content
	 */
	private static String getResponseString(HttpEntity entity) throws UnsupportedOperationException, IOException {
		InputStream responseIn = entity.getContent();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		IOUtils.copy(responseIn, out);
		String result = out.toString();

		//Close streams
		out.flush();
		out.close();
		responseIn.close();

		//Return
		return result;
	}
}

 

 



#IBMSterlingB2BIntegratorandIBMSterlingFileGatewayDevelopers
#DataExchange
0 comments
51 views

Permalink