Technical Blog Post
Abstract
REST API Proof Of Concept - Automate Sterling FileGateway Community and Trading Partner creation
Body
I am going to present a REST client, written using Apache HTTP Client, that trigger SB2Bi/SFG REST API calls to create Community and Trading Partner. As I have noticed users trying to create these SFG resources through direct Database SQL inserts and/or XAPI service calls but often used to end up in errors or lead to inconsistent partner accounts. I am targeting this article for those customers and hoping this would help all such customers in simplifying resource creation with less of efforts.
While REST APIs introduced very first time in Sterling Integrator and Sterling FileGateway through 5.2.6.1 fixpack, these capabilities were expanded in recently released 5.2.6.2 fixpack.
Product Documentation : Introduction to using B2Bi REST APIs
API additions in SB2BI 5.2.6.2 / SFG 2.2.6.2
1. Community Services - Services to create, read, update, and delete Communities in Sterling File Gateway.
2. Partner Group Services - Services to create, read, update, and delete partner groups.
3. Virtual Root Services - Services to create, read, and delete virtual root mailboxes.
4. Document Services - More APIs added.
5. Trading Partner APIs - More APIs added.
Full list of B2B REST APIs available in SB2Bi/SFG
As we all aware typical REST APIs need input parameters as JSON object and this request object varies from one API to other. I would obtain structure of respective JSON object from B2BAPIs UI. (http://host:LIBERTY_HTTP_PORT/B2BAPIs/svc).
Screen below, just for reference, shows JSON request for Create Community POST API. As you see, for this Community, I checked all check boxes except 2 and respective JSON attributes shown true vs false. I would use this kind of JSON body while invoking REST API call in my REST client class.
Following is REST Client interacts with B2B REST APIs creating Community and Partner inside Community. I put inline documentation within code to make it self explanatory.
//CommunityPartnerAPIs.java import java.io.IOException; import net.sf.json.JSONObject; import org.apache.http.HttpEntity; import com.ibm.misc.BASE64Encoder; public class CommunityPartnerAPIs { public static void main(String[] args) throws Exception{ /* JSON attributes for Community. Please refer to screen-shot captured from B2BAPIs UI System.out.println("commJsonString looks like this : " + commJsonString); String retPhrase = response.getStatusLine().getReasonPhrase(); //Parse API response to derive community location return retPhrase; //Create JSON request body. Obtained JSON structure for Create Trading Partner from B2BAPIs UI String partnerJsonString = partnerJson.toString(); try { String retPhrase = response.getStatusLine().getReasonPhrase(); String responseString = parseResponseToString(entity); public static HttpRequestBase prepareAPIRequest(String type, String URL, String credentials, String jsonString) throws UnsupportedEncodingException, URISyntaxException { public static String parseResponseToString(HttpEntity entity) throws IOException{ } |
I needed 9 jars to compile and execute this class. While 6 of them part of SB2Bi install, other 3 needs to downloaded from Java and Apache web-sites.
- sb2bi_install/jar/commons_beanutils/1_6_1/commons-beanutils.jar
- sb2bi_install/commons_codec/1_3/commons-codec-1.3.jar
- sb2bi_install/jar/commons_logging/1_0_4/commons-logging.jar
- sb2bi_install/jar/commons_lang/2_1/commons-lang-2.1.jar
- sb2bi_install/jar/ezmorph/1_0_4/ezmorph-1.0.4.jar
- sb2bi_installinstall/jar/commons_collections/3_2_2/commons-collections-3.2.2.jar
- httpclient-4.5.1.jar (http://archive.apache.org/dist/httpcomponents/)
- httpcore-4.4.3.jar (http://archive.apache.org/dist/httpcomponents/)
- json-lib-2.3-jdk15.jar (http://www.java2s.com/Code/Jar/j/Downloadjsonlib23jdk15jar.htm)
-- Compile
sb2bi_install/jdk/bin/javac -classpath commons-beanutils.jar:commons-codec-1.3.jar:commons-logging.jar:commons-lang-2.1.jar:ezmorph-1.0.4.jar:commons-collections-3.2.2.jar:httpclient-4.5.1.jar:httpcore-4.4.3.jar:json-lib-2.3-jdk15.jar CommunityPartnerAPIs.java
-- Execute
sb2bi_install/jdk/bin/java -classpath .:commons-beanutils.jar:commons-codec-1.3.jar:commons-logging.jar:commons-lang-2.1.jar:ezmorph-1.0.4.jar:commons-collections-3.2.2.jar:httpclient-4.5.1.jar:httpcore-4.4.3.jar:json-lib-2.3-jdk15.jar CommunityPartnerAPIs
--Output from class execution
You should see both Community and Partner in filegateway dashboard as below.
It is just for POC purpose and is limited to POST (Create) operation. But similar client is possible for any SB2Bi/SFG REST API and any supported operation such as /POST/PUT/DELETE/GET.
Thanks for reading through. You may post your questions, comments, feedback under comments section below.
UID
ibm11121469