Skip to content

Commit

Permalink
Support PATCH method, execute returns generic Object type
Browse files Browse the repository at this point in the history
1. Support PATCH method
2. execute returns generic Object type for wider use case instead of Map type to use with record iterator process tool
  • Loading branch information
hugoatjoget committed Dec 6, 2022
1 parent cec9503 commit 1dfa693
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 32 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>org.joget.marketplace</groupId>
<artifactId>enhanced-json-tool</artifactId>
<packaging>bundle</packaging>
<version>7.0.2</version>
<version>7.0.3</version>
<name>enhanced-json-tool</name>
<url>http://www.joget.org</url>
<build>
Expand Down
108 changes: 78 additions & 30 deletions src/main/java/org/joget/marketplace/EnhancedJsonTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.joget.apps.app.service.AppUtil;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.client.methods.HttpPatch;
import org.joget.apps.app.service.CustomURLDataSource;
import org.joget.apps.form.model.Element;
import org.joget.apps.form.model.Form;
Expand All @@ -61,20 +62,18 @@
import org.springframework.context.ApplicationContext;

public class EnhancedJsonTool extends DefaultApplicationPlugin {

//Support i18n
private final static String MESSAGE_PATH = "messages/enhancedJsonTool";

public String getName() {
return "Enhanced Json Tool";
return AppPluginUtil.getMessage("app.enhancedjsontool.pluginLabel", getClassName(), MESSAGE_PATH);
}

public String getDescription() {
return AppPluginUtil.getMessage("app.enhancedjsontool.pluginDesc", getClassName(), MESSAGE_PATH);
}

public String getVersion() {
return "7.0.2";
return "7.0.3";
}

public String getLabel() {
Expand Down Expand Up @@ -105,13 +104,12 @@ public Object execute(Map properties) {
HttpRequestBase request = null;

String jsonResponse = "";
Map object = null;
Map jsonResponseObject = null;
Object jsonResponseObjectRaw = null;

try {
client = HttpClients.createDefault();

jsonUrl = WorkflowUtil.processVariable(jsonUrl, "", wfAssignment);

jsonUrl = StringUtil.encodeUrlParam(jsonUrl);

if ("true".equalsIgnoreCase(getPropertyString("debugMode"))) {
Expand Down Expand Up @@ -247,7 +245,7 @@ public Object execute(Map properties) {
HttpEntity entity = builder.build();
((HttpPost) request).setEntity(entity);
}
}else if ("put".equalsIgnoreCase(getPropertyString("requestType"))) {
} else if ("put".equalsIgnoreCase(getPropertyString("requestType"))) {
request = new HttpPut(jsonUrl);

if ("jsonPayload".equals(getPropertyString("postMethod"))) {
Expand Down Expand Up @@ -287,7 +285,47 @@ public Object execute(Map properties) {
}
((HttpPut) request).setEntity(new UrlEncodedFormEntity(urlParameters, "UTF-8"));
}
}else {
} else if ("patch".equalsIgnoreCase(getPropertyString("requestType"))) {
request = new HttpPatch(jsonUrl);

if ("jsonPayload".equals(getPropertyString("postMethod"))) {
JSONObject obj = new JSONObject();
Object[] paramsValues = (Object[]) properties.get("params");
for (Object o : paramsValues) {
Map mapping = (HashMap) o;
String name = mapping.get("name").toString();
String value = mapping.get("value").toString();
obj.accumulate(name, WorkflowUtil.processVariable(value, "", wfAssignment));
}

StringEntity requestEntity = new StringEntity(obj.toString(4), "UTF-8");
((HttpPatch) request).setEntity(requestEntity);
request.setHeader("Content-type", "application/json");
if ("true".equalsIgnoreCase(getPropertyString("debugMode"))) {
LogUtil.info(EnhancedJsonTool.class.getName(), "JSON Payload : " + obj.toString(4));
}
} else if ("custom".equals(getPropertyString("postMethod"))) {
StringEntity requestEntity = new StringEntity(getPropertyString("customPayload"), "UTF-8");
((HttpPatch) request).setEntity(requestEntity);
request.setHeader("Content-type", "application/json");
if ("true".equalsIgnoreCase(getPropertyString("debugMode"))) {
LogUtil.info(EnhancedJsonTool.class.getName(), "Custom JSON Payload : " + getPropertyString("customPayload"));
}
} else {
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
Object[] paramsValues = (Object[]) properties.get("params");
for (Object o : paramsValues) {
Map mapping = (HashMap) o;
String name = mapping.get("name").toString();
String value = mapping.get("value").toString();
urlParameters.add(new BasicNameValuePair(name, WorkflowUtil.processVariable(value, "", wfAssignment)));
if ("true".equalsIgnoreCase(getPropertyString("debugMode"))) {
LogUtil.info(EnhancedJsonTool.class.getName(), "Adding param " + name + " : " + value);
}
}
((HttpPatch) request).setEntity(new UrlEncodedFormEntity(urlParameters, "UTF-8"));
}
} else {
request = new HttpGet(jsonUrl);
}

Expand Down Expand Up @@ -329,32 +367,43 @@ public Object execute(Map properties) {
if ("true".equalsIgnoreCase(getPropertyString("debugMode"))) {
LogUtil.info(EnhancedJsonTool.class.getName(), jsonResponseFormatted);
}
object = PropertyUtil.getProperties(new JSONObject(jsonResponseFormatted));
jsonResponseObject = PropertyUtil.getProperties(new JSONObject(jsonResponseFormatted));

//Added ability to format response via bean shell in configuration
if ("true".equalsIgnoreCase(getPropertyString("enableFormatResponse"))) {
properties.put("data", object);
properties.put("data", jsonResponseObject);

String script = (String) properties.get("script");

Map<String, String> replaceMap = new HashMap<String, String>();
replaceMap.put("\n", "\\\\n");

script = WorkflowUtil.processVariable(script, "", wfAssignment, "", replaceMap);

object = (Map) executeScript(script, properties);
jsonResponseObjectRaw = executeScript(script, properties);
}

String formDefId = (String) properties.get("formDefId");
if (formDefId != null && formDefId.trim().length() > 0) {
if(jsonResponseObjectRaw != null){
jsonResponseObject = (Map) jsonResponseObjectRaw;
}
storeToForm(wfAssignment, properties, jsonResponseObject);
}

Object[] wfVariableMapping = (Object[]) properties.get("wfVariableMapping");
if (wfVariableMapping != null && wfVariableMapping.length > 0) {
if(jsonResponseObjectRaw != null){
jsonResponseObject = (Map) jsonResponseObjectRaw;
}
storeToWorkflowVariable(wfAssignment, properties, jsonResponseObject);
}

storeToForm(wfAssignment, properties, object);
storeToWorkflowVariable(wfAssignment, properties, object);

}
}else{
//assume binary

//attempt to get filename
String fileName = "";
try{
try {
Header header = response.getFirstHeader("Content-Disposition");
HeaderElement[] helelms = header.getElements();
if (helelms.length > 0) {
Expand All @@ -364,16 +413,16 @@ public Object execute(Map properties) {
fileName = nmv.getValue();
}
}
}catch(Exception ex){
} catch(Exception ex){
LogUtil.info(getClass().getName(), "Cannot get file name automatically");
}

if(fileName.isEmpty()){
if (fileName.isEmpty()){
String[] n = request.getURI().getPath().split("/");
fileName = n[n.length-1];
}

if(fileName.isEmpty()){
if (fileName.isEmpty()){
fileName = "downloaded";
}

Expand Down Expand Up @@ -416,34 +465,33 @@ public Object execute(Map properties) {
while((inByte = is.read()) != -1){
fos.write(inByte);
}
}catch(Exception ex){
} catch(Exception ex){
LogUtil.error(getClass().getName(), ex, "Cannot save file");
}finally{
} finally{
fos.close();
}
}
}

if( !getPropertyString("responseStatusWorkflowVariable").isEmpty() ){
if ( !getPropertyString("responseStatusWorkflowVariable").isEmpty() ){
workflowManager.activityVariable(wfAssignment.getActivityId(), getPropertyString("saveStatusToWorkflowVariable"), response.getStatusLine().getStatusCode());
}

if( !getPropertyString("responseStatusFormDefId").isEmpty() ){
if ( !getPropertyString("responseStatusFormDefId").isEmpty() ){
storeStatusToForm(wfAssignment, properties, String.valueOf(response.getStatusLine().getStatusCode()), jsonResponse );
}

return object;
return jsonResponseObjectRaw;

} catch (Exception ex) {
LogUtil.error(getClass().getName(), ex, "");

if( !getPropertyString("saveStatusToWorkflowVariable").isEmpty() ){
if ( !getPropertyString("saveStatusToWorkflowVariable").isEmpty() ){
workflowManager.activityVariable(wfAssignment.getActivityId(), getPropertyString("saveStatusToWorkflowVariable"), ex.toString());
}
if( !getPropertyString("responseStatusFormDefId").isEmpty() ){
if ( !getPropertyString("responseStatusFormDefId").isEmpty() ){
storeStatusToForm(wfAssignment, properties, ex.toString() + " - " + ex.getMessage(), jsonResponse);
}

} finally {
try {
if (request != null) {
Expand Down Expand Up @@ -521,7 +569,7 @@ protected void storeToForm(WorkflowAssignment wfAssignment, Map properties, Map
rowSet.add(getRow(wfAssignment, null, null, fieldMapping, object));
}

if (rowSet.size() > 0) {
if (!rowSet.isEmpty()) {
appService.storeFormData(appDef.getId(), appDef.getVersion().toString(), formDefId, rowSet, null);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages/enhancedJsonTool.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ app.jsontool.responseStatusIdField=Response Field ID
app.jsontool.responseStatusIdField.desc=Fill in to use as foreign key to main record. Leave it blank to save form record ID into column "ID". When it is blank and there are multiple logs, logs will be overwritten and only the last entry will be visible.
app.jsontool.responseStatusStatusField=Response Status Field
app.jsontool.requestType.put=PUT
app.jsontool.requestType.patch=PATCH
app.jsontool.payloadType=Payload Type
app.jsontool.value=Value
app.jsontool.responseStatusResponseDataField=Response Data Field
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/properties/enhancedJsonTool.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
{
"value":"put",
"label":"@@app.jsontool.requestType.put@@"
},
{
"value":"patch",
"label":"@@app.jsontool.requestType.patch@@"
}
]
},
Expand All @@ -48,7 +52,7 @@
}
],
"control_field":"requestType",
"control_value":"post|put",
"control_value":"post|put|patch",
"control_use_regex":"true"
},
{
Expand Down

0 comments on commit 1dfa693

Please sign in to comment.