diff --git a/pom.xml b/pom.xml index 255537a..d029573 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.joget.marketplace enhanced-json-tool bundle - 7.0.2 + 7.0.3 enhanced-json-tool http://www.joget.org diff --git a/src/main/java/org/joget/marketplace/EnhancedJsonTool.java b/src/main/java/org/joget/marketplace/EnhancedJsonTool.java index 6b80e95..d35e9bb 100644 --- a/src/main/java/org/joget/marketplace/EnhancedJsonTool.java +++ b/src/main/java/org/joget/marketplace/EnhancedJsonTool.java @@ -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; @@ -61,12 +62,10 @@ 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() { @@ -74,7 +73,7 @@ public String getDescription() { } public String getVersion() { - return "7.0.2"; + return "7.0.3"; } public String getLabel() { @@ -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"))) { @@ -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"))) { @@ -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 urlParameters = new ArrayList(); + 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); } @@ -329,11 +367,11 @@ 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"); @@ -341,20 +379,31 @@ public Object execute(Map properties) { 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) { @@ -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"; } @@ -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) { @@ -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); } } diff --git a/src/main/resources/messages/enhancedJsonTool.properties b/src/main/resources/messages/enhancedJsonTool.properties index 6580665..9a9ca39 100644 --- a/src/main/resources/messages/enhancedJsonTool.properties +++ b/src/main/resources/messages/enhancedJsonTool.properties @@ -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 diff --git a/src/main/resources/properties/enhancedJsonTool.json b/src/main/resources/properties/enhancedJsonTool.json index d72405e..71b227b 100644 --- a/src/main/resources/properties/enhancedJsonTool.json +++ b/src/main/resources/properties/enhancedJsonTool.json @@ -25,6 +25,10 @@ { "value":"put", "label":"@@app.jsontool.requestType.put@@" + }, + { + "value":"patch", + "label":"@@app.jsontool.requestType.patch@@" } ] }, @@ -48,7 +52,7 @@ } ], "control_field":"requestType", - "control_value":"post|put", + "control_value":"post|put|patch", "control_use_regex":"true" }, {