Skip to content

Commit

Permalink
Add variable support for Freemarker template in Payload factory
Browse files Browse the repository at this point in the history
  • Loading branch information
SanojPunchihewa committed Dec 3, 2024
1 parent a460488 commit af4911f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private Constants() {
public static final String CTX_PROPERTY_INJECTING_NAME = "ctx";
public static final String AXIS2_PROPERTY_INJECTING_NAME = "axis2";
public static final String TRANSPORT_PROPERTY_INJECTING_NAME = "trp";
public static final String VARIABLE_INJECTING_NAME = "var";
public static final String JSON_TYPE = "json";
public static final String XML_TYPE = "xml";
public static final String TEXT_TYPE = "text";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import static org.apache.synapse.mediators.transform.pfutils.Constants.PAYLOAD_INJECTING_NAME;
import static org.apache.synapse.mediators.transform.pfutils.Constants.TEXT_PAYLOAD_TYPE;
import static org.apache.synapse.mediators.transform.pfutils.Constants.TRANSPORT_PROPERTY_INJECTING_NAME;
import static org.apache.synapse.mediators.transform.pfutils.Constants.VARIABLE_INJECTING_NAME;
import static org.apache.synapse.mediators.transform.pfutils.Constants.XML_PAYLOAD_TYPE;
import static org.apache.synapse.util.PayloadHelper.TEXTELT;
import static org.apache.synapse.util.PayloadHelper.getXMLPayload;
Expand All @@ -88,6 +89,7 @@ public class FreeMarkerTemplateProcessor extends TemplateProcessor {
private boolean usingPropertyAxis2;
private boolean usingPropertyTransport;
private boolean usingArgs;
private boolean usingVariables;

private static final Log log = LogFactory.getLog(FreeMarkerTemplateProcessor.class);
private boolean templateLoaded = false;
Expand Down Expand Up @@ -218,6 +220,7 @@ private void findRequiredInjections(String templateString) {
usingPropertyCtx = templateString.contains(CTX_PROPERTY_INJECTING_NAME);
usingPropertyAxis2 = templateString.contains(AXIS2_PROPERTY_INJECTING_NAME);
usingPropertyTransport = templateString.contains(TRANSPORT_PROPERTY_INJECTING_NAME);
usingVariables = templateString.contains(VARIABLE_INJECTING_NAME);
}

/**
Expand Down Expand Up @@ -400,6 +403,7 @@ private void injectProperties(MessageContext synCtx, Map<String, Object> data) {
injectCtxProperties(synCtx, data);
injectAxis2Properties(synCtx, data);
injectTransportProperties(synCtx, data);
injectVariables(synCtx, data);
}

private void injectCtxProperties(MessageContext synCtx, Map<String, Object> data) {
Expand All @@ -420,6 +424,21 @@ private void injectCtxProperties(MessageContext synCtx, Map<String, Object> data
}
}

private void injectVariables(MessageContext synCtx, Map<String, Object> data) {

if (usingVariables) {
Map<String, String> variables = new HashMap<>();
for (Object o : synCtx.getVariableKeySet()) {
String varName = (String) o;
Object variable = synCtx.getVariable(varName);
if (variable != null) {
variables.put(varName, variable.toString());
}
}
data.put(VARIABLE_INJECTING_NAME, variables);
}
}

private void injectAxis2Properties(MessageContext synCtx, Map<String, Object> data) {

if (usingPropertyAxis2) {
Expand Down

0 comments on commit af4911f

Please sign in to comment.