Sterling Managed File Transfer

Sterling Managed File Transfer

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

 View Only

‘Box’ Integration with IBM B2B Sterling Integrator

By Tanvi Kakodkar posted Thu February 13, 2020 01:54 AM

  
Originally updated on December 6, 2016 by PhilipJRoss

Box’ Integration with IBM B2B Sterling Integrator
By: Philip Ross
 

Setting Up Your Project

You will need the Box SDK for Java for this example to work. If you do not currently have the SDK please download it here: http://opensource.box.com/box-java-sdk/

Create a Box developer account (https://developer.box.com/) if you do not already have one.

Here you will need to create you first Box application:

Give your application a name and click ‘Create Application’.

Under ‘OAuth2 Parameters’ make sure to set the redirect_uri. https://localhost will work for this demo. Also note your client_id and client_secret. These will be used for authentication shortly.
 

Authenticate with Box

For this example, we will be using ‘Manual Authentication’ as document here: https://github.com/box/box-java-sdk/blob/master/doc/authentication.md

We already have the client_id and client_secret. As such we still need an access token and refresh token for this example. To acquire these, you can start by accessing the following link:

https://account.box.com/api/oauth2/authorize?response_type=code&client_id=MY-CLIENT-ID&state=authenticated

and replacing ‘MY-CLIENT-ID’ with your own id. Once there, you can grant your Box application access to the Box account of your choice.

After giving access, you will be sent to your redirect_uri. In it you will see an authentication code. Make sure to copy this authentication code as if is only valid for 30 seconds. Using cURL you can execute the following command:

curl https://api.box.com/oauth2/token -d 'grant_type=authorization_code&code=AUTH-CODE&client_id=MY-CLIENT-ID&client_secret=MY-CLIENT-SECRET' -X POST
 

making sure to fill in the various fields with your own values.

Upon successful execution of this command, you will receive an access token and a refresh token. Make sure to copy these down. Note that when we get around to actually using these, the Box APIs will take care of automatically refreshing them as necessary.

For now store create a file called ‘config.properties’ and store them there:

clientId=

clientSecret=

accessToken=

refreshToken=


 

Create Box Client

List Box Files

Put a Box File

Get a Box File

Delete a Box File


 

Complete Java Implementation:

package com.sterlingcommerce.woodstock.services.integration;
 

import com.sterlingcommerce.woodstock.services.IService;

import com.sterlingcommerce.woodstock.workflow.WorkFlowContext;

import com.sterlingcommerce.woodstock.workflow.WorkFlowException;

import com.sterlingcommerce.woodstock.util.frame.log.LogService;

import com.sterlingcommerce.woodstock.util.frame.log.Logger;

import com.sterlingcommerce.woodstock.workflow.Document;

import com.sterlingcommerce.woodstock.workflow.DocumentOutputStream;

import com.box.sdk.*;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.Properties;

import java.io.InputStream;

public class BoxClientService implements IService {


 

private Document primaryDoc = null;

protected static Logger log;

static {

log = LogService.getLogger("integrationservices");

}

private static boolean isOK = true;


 

public WorkFlowContext processData(WorkFlowContext wfc) throws WorkFlowException {


 

isOK = true;

Properties props = new Properties();

BoxAPIConnection api = null;

try {

FileInputStream in = new FileInputStream("/ais_local/share/pjross/config.properties");

props.load(in);


 

wfc.harnessRegister();

String action = wfc.getParm("operation"); //PUT, LIST, GET, DELETE

String local_path = wfc.getParm("local_path"); //Path to read file for uploading

String client_id = "yourClientId";

String client_secret = "yourClientSecret";

String access_token = props.getProperty("accessToken");

String refresh_token = props.getProperty("refreshToken");

String boxFileName = wfc.getParm("boxFileName");


 

log.log("BoxClientService.processData(): action:" + action);

log.log("BoxClientService.processData(): local_path:" + local_path);

log.log("BoxClientService.processData(): client_id:" + client_id);

log.log("BoxClientService.processData(): client_secret:" + client_secret);

log.log("BoxClientService.processData(): access_token:" + access_token);

log.log("BoxClientService.processData(): refresh_token:" + refresh_token);


 

//Create a connection to Box

api = new BoxAPIConnection(client_id, client_secret, access_token, refresh_token);


 

if("list".equalsIgnoreCase(action)) {

log.log("Usage: Box list");

listBoxFiles(api);

} else if("put".equalsIgnoreCase(action)) {

log.log("Usage: Box put");

InputStream docAsStream = null;


 

if ( wfc.getPrimaryDocument() != null ){

docAsStream = wfc.getPrimaryDocument().getBodyInputStream();

putBoxFile(docAsStream, api, local_path);

} else if (local_path != null && !"".equalsIgnoreCase(local_path)) {// put file from FS to Box

File document = new File( local_path );

docAsStream = new FileInputStream(document);

putBoxFile(docAsStream, api, wfc.getPrimaryDocument().getDocumentName());

}

if(docAsStream != null)

docAsStream.close();

} else if("get".equalsIgnoreCase(action)) {

log.log("Usage: Box get");

BoxFile file = new BoxFile(api, boxFileName);


 

Document primaryDocument = wfc.newDocument();

DocumentOutputStream out = primaryDocument.getOutputStream();

file.download(out);

wfc.putPrimaryDocument(primaryDocument);


 

if (out!=null)

out.close();

} else if("delete".equalsIgnoreCase(action)) {

log.log("Usage: delete");

deleteBoxFile(api, boxFileName);

}

log.log("Done!");

in.close();

} catch (Exception e) {

e.printStackTrace();

isOK = false;

log.log(e.getMessage());

log.logException(e);

wfc.setBasicStatus(1);

throw new WorkFlowException(e.toString());

} finally {

System.out.println(api.getAccessToken());

System.out.println(api.getAccessToken());

FileOutputStream out = null;

try {

out = new FileOutputStream("/ais_local/share/pjross/config.properties");

props.setProperty("accessToken", api.getAccessToken());

props.setProperty("refreshToken", api.getRefreshToken());

props.store(out, null);

out.close();

} catch (Exception e) {

e.printStackTrace();

}

wfc.unregisterThread();

if (isOK) {

wfc.setBasicStatusSuccess();

} else {

wfc.setBasicStatusError();

}

}

return wfc;

}


 

public void listBoxFiles(BoxAPIConnection api) {

BoxFolder rootFolder = BoxFolder.getRootFolder(api);

for (BoxItem.Info itemInfo : rootFolder) {

System.out.format("[%s] %s\n", itemInfo.getID(), itemInfo.getName());

}

}


 

public void putBoxFile(InputStream docAsStream, BoxAPIConnection api, String documentName) {

BoxFolder rootFolder = BoxFolder.getRootFolder(api);

rootFolder.uploadFile(docAsStream, documentName);

}


 

public void deleteBoxFile(BoxAPIConnection api, String boxFileName) {

if(boxFileName != null) {

BoxFile file = new BoxFile(api, boxFileName);

file.delete();

log.log("BoxClientMain.deleteBoxFile(): fileName:"+boxFileName);

}

}

}


Business Processes:


Service Implementation – serviceinstances.xml

Service Definitions – servicedefs/IntegrationServices.xml.in

Service Configuration in B2B UI


Installation of all dependent client jars

The following jars need to be in the dynamicclasspath.cfg

To do this go to ./install/bin and run:

./install3rdParty.sh box 1_0 –j <path-to-jar>/<required-box-jar>


Installing Box Client Service jar

Download the attached Box Client jar to test the sample example.

Go to ./install/bin

Run:

./InstallService.sh <path-to-jar>/integrationservices_1010000.jar


 
Execute CRUD operation

Execute the above BP to perform the desired action. Logging info should go to integrationservices.log under <INSTALL_DIR>/logs folder.

Issues Encountered

As of this article, errors were still be encountered during the execution of the business process that remain unresolved. The seemed to be related to a network issue. The following error appeared in the wf log whenever the business process was run:

ERRORDTL [1467389676980]com.box.sdk.BoxAPIException: Couldn't connect to the Box API due to a network error.

at com.box.sdk.BoxAPIRequest.writeBody(BoxAPIRequest.java:320)

at com.box.sdk.BoxAPIRequest.trySend(BoxAPIRequest.java:389)

at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:209)

at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:184)

at com.box.sdk.BoxAPIConnection.refresh(BoxAPIConnection.java:464)

at com.box.sdk.BoxAPIConnection.getAccessToken(BoxAPIConnection.java:266)

at com.sterlingcommerce.woodstock.services.integration.BoxClientService.processData(BoxClientService.java:110)

at com.sterlingcommerce.woodstock.workflow.activity.engine.ActivityEngineHelper.invokeService(ActivityEngineHelper.java:1818)

at com.sterlingcommerce.woodstock.workflow.activity.engine.ActivityEngineHelper.nextMainLogic(ActivityEngineHelper.java:631)

at com.sterlingcommerce.woodstock.workflow.activity.engine.ActivityEngineHelper.next(ActivityEngineHelper.java:362)

at com.sterlingcommerce.woodstock.workflow.queue.WorkFlowQueueListener.doWork(WorkFlowQueueListener.java:459)

at com.sterlingcommerce.woodstock.workflow.queue.WorkFlowQueueListener.run(WorkFlowQueueListener.java:240)

at com.sterlingcommerce.woodstock.workflow.queue.WorkFlowQueueListener.onMessage(WorkFlowQueueListener.java:197)

at com.sterlingcommerce.woodstock.workflow.queue.WorkFlowQueueListener.onMessage(WorkFlowQueueListener.java:184)

at com.sterlingcommerce.woodstock.workflow.queue.wfTransporter.run(wfTransporter.java:444)

at com.sterlingcommerce.woodstock.workflow.queue.BasicExecutor$Worker.run(BasicExecutor.java:508)

at java.lang.Thread.run(Thread.java:795)

Caused by: javax.net.ssl.SSLHandshakeException: com.ibm.jsse2.util.j: PKIX path building failed: java.security.cert.CertPathBuilderException: PKIXCertPathBuilderImpl could not build a valid CertPath.; internal cause is:

java.security.cert.CertPathValidatorException: The certificate issued by CN=GeoTrust Global CA, O=GeoTrust Inc., C=US is not trusted; internal cause is:

java.security.cert.CertPathValidatorException: Certificate chaining error

at com.ibm.jsse2.j.a(j.java:42)

at com.ibm.jsse2.qc.a(qc.java:116)

at com.ibm.jsse2.ab.a(ab.java:413)

at com.ibm.jsse2.ab.a(ab.java:255)

at com.ibm.jsse2.bb.a(bb.java:42)

at com.ibm.jsse2.bb.a(bb.java:614)

at com.ibm.jsse2.ab.s(ab.java:373)

at com.ibm.jsse2.ab.a(ab.java:140)

at com.ibm.jsse2.qc.a(qc.java:701)

at com.ibm.jsse2.qc.h(qc.java:453)

at com.ibm.jsse2.qc.a(qc.java:625)

at com.ibm.jsse2.qc.startHandshake(qc.java:113)

at com.ibm.net.ssl.www2.protocol.https.c.afterConnect(c.java:188)

at com.ibm.net.ssl.www2.protocol.https.d.connect(d.java:9)

at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1103)

at com.ibm.net.ssl.www2.protocol.https.b.getOutputStream(b.java:84)

at com.box.sdk.BoxAPIRequest.writeBody(BoxAPIRequest.java:309)

... 16 more


 

Assuming this error could be resolved, the business process should run as expected as similar code outside of IBM B2B Sterling Integrator (and on the same server) was able to run as expected. Due to time constraints, this article is being published despite the above error still persisting. Any feedback regarding the error (or if others do not encounter the error) is much appreciated.


#IBMSterlingB2BIntegratorandIBMSterlingFileGatewayDevelopers
#DataExchange
0 comments
32 views

Permalink