Created
July 3, 2020 05:55
-
-
Save yudhiwidyatama/76ccb144b4e83e0942bcfd149decbbad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintStream; | |
import java.io.UnsupportedEncodingException; | |
import java.net.InetSocketAddress; | |
import java.net.Proxy; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.net.URLEncoder; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.logging.Logger; | |
import java.util.logging.FileHandler; | |
import org.joget.plugin.base.PluginManager; | |
import org.joget.workflow.model.WorkflowActivity; | |
import org.joget.workflow.model.service.*; | |
import org.json.simple.JSONObject; | |
import org.json.simple.JSONArray; | |
import org.json.simple.parser.JSONParser; | |
String buildWebQuery(java.util.Map parameters) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
for (Object oentry : parameters.entrySet()) { | |
Map.Entry entry = (Map.Entry)oentry; | |
String key = URLEncoder.encode((String)entry.getKey(), "UTF-8"); | |
String value = URLEncoder.encode(entry.getValue().toString(), "UTF-8"); | |
sb.append(key).append("=").append(value).append("&"); | |
} | |
return sb.toString().substring(0, sb.length() - 1); | |
} | |
String callRestfulWebService(String address, Map parameters) { | |
String proxy = null; | |
int port = 0; | |
Proxy proxyObject = null; | |
if (isNotBlank(proxy) && port > 0) { | |
InetSocketAddress proxyAddress = new InetSocketAddress(proxy, port); | |
proxyObject = new Proxy(Proxy.Type.HTTP, proxyAddress); | |
} | |
String response = ""; | |
String query = buildWebQuery(parameters); | |
// More info: http://xml.nig.ac.jp/tutorial/rest/index.html | |
URL url = new URL(address); | |
// make post mode connection | |
URLConnection urlc = null; | |
if (proxyObject == null) { | |
urlc = url.openConnection(); | |
} else { | |
urlc = url.openConnection(proxyObject); | |
} | |
urlc.setDoOutput(true); | |
urlc.setAllowUserInteraction(false); | |
// send query | |
PrintStream ps = new PrintStream(urlc.getOutputStream()); | |
ps.print(query); | |
ps.close(); | |
// retrieve result | |
BufferedReader br = new BufferedReader(new InputStreamReader(urlc.getInputStream(), "UTF-8")); | |
StringBuilder sb = new StringBuilder(); | |
String line; | |
while ((line = br.readLine()) != null) { | |
sb.append(line); | |
sb.append("\n"); | |
} | |
br.close(); | |
response = sb.toString(); | |
return response; | |
} | |
boolean isNotBlank(String proxy) { | |
if (proxy==null) return false; | |
if (proxy.equals("")) return false; | |
return true; | |
} | |
Logger logger = Logger.getLogger("verificator"); | |
WorkflowManager workflowManager = (WorkflowManager) pluginManager.getBean("workflowManager"); | |
WorkflowActivity activity = workflowActivity; | |
Map m = new HashMap(); | |
Map emptyMap = new HashMap(); | |
emptyMap.put("t",System.currentTimeMillis()); | |
m.put("r", "site/getVerificator"); | |
String ba_id = workflowManager.getProcessVariable(activity.getProcessId(),"ba_id"); | |
String id_company = workflowManager.getProcessVariable(activity.getProcessId(),"id_company"); | |
String ba_ubis = workflowManager.getProcessVariable(activity.getProcessId(),"ba_ubis"); | |
logger.warning("ba_id = "+ ba_id + " ba_ubis = " + ba_ubis + " proc " + activity.getProcessId() ); | |
m.put("ba_id",ba_id); | |
m.put("id_company",id_company); | |
m.put("ba_ubis",ba_ubis); | |
String restUrl = "http://finestjoget-finest.apps.osh.telkom.co.id/index.php"; | |
String getQuery = ""; | |
int rCounter = 0; | |
for (;rCounter < 3; rCounter++){ | |
try { | |
getQuery = buildWebQuery(m); | |
String result = callRestfulWebService(restUrl+"?" + getQuery,emptyMap); | |
JSONParser jp = new JSONParser(); | |
Object pr = jp.parse(result); | |
if (pr instanceof JSONArray) | |
{ | |
JSONArray parsedObj = (JSONArray) pr; | |
arraySize = parsedObj.size(); | |
ArrayList aList = new ArrayList(); | |
int i; | |
for (i=0; i<arraySize;i++) | |
{ | |
aList.add(userid = parsedObj.get(i)); | |
logger.warning(""+ userid + " as verificator satu " ); | |
} | |
if (aList.size()>0) return aList; | |
}else | |
logger.warning("Service return value is not one JSON Object"); | |
} catch (Exception e1) { | |
e1.printStackTrace(); | |
} | |
Thread.sleep(1000); | |
} | |
logger.warning("verificator satu not found for " + activity.getProcessId()); | |
dList = new ArrayList(); | |
dList.add("finestVerificatorEmpty"); | |
return dList; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment