Skip to content

Instantly share code, notes, and snippets.

@randyphoa
Last active July 21, 2023 08:06
Show Gist options
  • Save randyphoa/2629b53208ceed4b15fdadb38637a561 to your computer and use it in GitHub Desktop.
Save randyphoa/2629b53208ceed4b15fdadb38637a561 to your computer and use it in GitHub Desktop.
Sample OpenPages Custom Workflow Action
package com.randyphoa;
import java.io.File;
import java.util.List;
import org.apache.log4j.Logger;
import com.ibm.openpages.api.resource.IStringField;
import com.ibm.openpages.api.resource.IGRCObject;
import com.ibm.openpages.api.workflow.actions.AbstractCustomAction;
import com.ibm.openpages.api.workflow.actions.IWFCustomProperty;
import com.ibm.openpages.api.workflow.actions.IWFOperationContext;
import org.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.BasicResponseHandler;
public class TestCustomAction extends AbstractCustomAction {
private Logger logger = Logger.getLogger(this.getClass());
private static String getToken() throws Exception {
String payload = "{\"username\":\"admin\",\"api_key\":\"xxx\"}";
StringEntity entity = new StringEntity(payload, ContentType.APPLICATION_JSON);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost("https://cpd-cpd.itzroks-550003aw18-5sggq6-6ccd7f378ae819553d37d5f2ee142bd6-0000.jp-tok.containers.appdomain.cloud/icp4d-api/v1/authorize");
request.setEntity(entity);
HttpResponse response = httpClient.execute(request);
String responseString = new BasicResponseHandler().handleResponse(response);
JSONObject obj = new JSONObject(responseString);
String token = obj.getString("token");
return token;
}
public TestCustomAction(IWFOperationContext context, List<IWFCustomProperty> properties) {
super(context, properties);
}
@Override
protected void process() throws Exception {
IGRCObject object = getContext().getResource();
IStringField containerId = (IStringField) object.getField("IntegrationWKC:Container ID");
System.out.println("Project ID: " + containerId.getValue());
IStringField modelId = (IStringField) object.getField("Integration:External ID");
System.out.println("Model ID: " + modelId.getValue());
String token = getToken();
String payload = "{\"input_data\": [{\"values\": [\" + modelId.getValue() +\"]}]}";
StringEntity entity = new StringEntity(payload, ContentType.APPLICATION_JSON);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost("https://cpd-cpd.itzroks-550003aw18-5sggq6-6ccd7f378ae819553d37d5f2ee142bd6-0000.jp-tok.containers.appdomain.cloud/ml/v4/deployments/03469168-2c50-49b3-bbab-96020ab2b2c4/predictions?version=2022-12-14");
request.setEntity(entity);
request.addHeader("Content-Type", "application/json; charset=UTF-8");
request.addHeader("Authorization", "Bearer " + token);
HttpResponse response = httpClient.execute(request);
System.out.println(response.getStatusLine().getStatusCode());
String responseString = new BasicResponseHandler().handleResponse(response);
System.out.println(responseString);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment