Skip to content

Commit

Permalink
add synapse expression support
Browse files Browse the repository at this point in the history
  • Loading branch information
chathuranga-jayanath-99 committed Nov 26, 2024
1 parent 1b00df2 commit 2df1a58
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.synapse.SynapseConstants;
import org.apache.synapse.SynapseException;
import org.apache.synapse.aspects.AspectConfiguration;
import org.apache.synapse.config.xml.ValueFactory;
import org.apache.synapse.config.xml.XMLConfigConstants;
import org.apache.synapse.endpoints.EndpointDefinition;
import org.apache.synapse.util.xpath.SynapseXPath;
Expand All @@ -46,6 +47,7 @@ public class EndpointDefinitionFactory implements DefinitionFactory{
*/
public EndpointDefinition createDefinition(OMElement elem) {
EndpointDefinition definition = new EndpointDefinition();
ValueFactory valueFactory = new ValueFactory();

OMAttribute optimize
= elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "optimize"));
Expand Down Expand Up @@ -159,20 +161,15 @@ public EndpointDefinition createDefinition(OMElement elem) {
String d = duration.getText();
if (d != null) {
try {
Pattern pattern = Pattern.compile("\\{.*\\}");
if (pattern.matcher(d).matches()) {
d = d.trim().substring(1, d.length() - 1);
SynapseXPath xpath = new SynapseXPath(d);
definition.setDynamicTimeoutExpression(xpath);
if (isExpression(d)) {
definition.setDynamicTimeoutExpression(valueFactory.createTextValue(duration));
} else {
long timeoutMilliSeconds = Long.parseLong(d.trim());
definition.setTimeoutDuration(timeoutMilliSeconds);
}
} catch (NumberFormatException e) {
handleException("Endpoint timeout duration expected as a " +
"number but was not a number");
} catch (JaxenException e) {
handleException("Couldn't assign dynamic endpoint timeout as Synapse expression");
}
}
}
Expand All @@ -182,13 +179,7 @@ public EndpointDefinition createDefinition(OMElement elem) {
if (action != null && action.getText() != null) {
String actionString = action.getText().trim();
if (isExpression(actionString)) {
try {
String expressionStr = actionString.substring(1, actionString.length() - 1);
SynapseXPath expressionXPath = new SynapseXPath(expressionStr);
definition.setDynamicTimeoutAction(expressionXPath);
} catch (JaxenException e) {
handleException("Couldn't assign dynamic timeout action as Synapse expression");
}
definition.setDynamicTimeoutAction(valueFactory.createTextValue(action));
} else {
if ("discard".equalsIgnoreCase(actionString)) {
definition.setTimeoutAction(SynapseConstants.DISCARD);
Expand All @@ -214,13 +205,7 @@ public EndpointDefinition createDefinition(OMElement elem) {
if (timeoutCodes != null && timeoutCodes.getText() != null) {
String trimmedTimeoutCodes = timeoutCodes.getText().trim();
if (isExpression(trimmedTimeoutCodes)) {
try {
String expressionStr = trimmedTimeoutCodes.substring(1, trimmedTimeoutCodes.length() - 1);
SynapseXPath expressionXPath = new SynapseXPath(expressionStr);
definition.setDynamicTimeoutErrorCodes(expressionXPath);
} catch (JaxenException e) {
handleException("Couldn't assign dynamic timeout error codes as Synapse expression");
}
definition.setDynamicTimeoutErrorCodes(valueFactory.createTextValue(timeoutCodes));
} else {
StringTokenizer st = new StringTokenizer(timeoutCodes.getText().trim(), ", ");
while (st.hasMoreTokens()) {
Expand All @@ -242,18 +227,14 @@ public EndpointDefinition createDefinition(OMElement elem) {
try {
String trimmedRetriesBeforeSuspend = retriesBeforeSuspend.getText().trim();
if (isExpression(trimmedRetriesBeforeSuspend)) {
String expressionStr = trimmedRetriesBeforeSuspend.substring(1, trimmedRetriesBeforeSuspend.length() - 1);
SynapseXPath expressionXPath = new SynapseXPath(expressionStr);
definition.setDynamicRetriesOnTimeoutBeforeSuspend(expressionXPath);
definition.setDynamicRetriesOnTimeoutBeforeSuspend(valueFactory.createTextValue(retriesBeforeSuspend));
} else {
definition.setRetriesOnTimeoutBeforeSuspend(
Integer.parseInt(trimmedRetriesBeforeSuspend));
}
} catch (NumberFormatException e) {
handleException("The retries before suspend [for timeouts] should be " +
"specified as a valid number : " + retriesBeforeSuspend.getText(), e);
} catch (JaxenException e) {
handleException("Couldn't assign dynamic retries before suspend [for timeouts] as Synapse expression");
}
}

Expand All @@ -264,18 +245,14 @@ public EndpointDefinition createDefinition(OMElement elem) {
try {
String trimmedRetryDelay = retryDelay.getText().trim();
if (isExpression(trimmedRetryDelay)) {
String expressionStr = trimmedRetryDelay.substring(1, trimmedRetryDelay.length() - 1);
SynapseXPath expressionXPath = new SynapseXPath(expressionStr);
definition.setDynamicRetryDurationOnTimeout(expressionXPath);
definition.setDynamicRetryDurationOnTimeout(valueFactory.createTextValue(retryDelay));
} else {
definition.setRetryDurationOnTimeout(
Integer.parseInt(trimmedRetryDelay));
}
} catch (NumberFormatException e) {
handleException("The retry delay for timeouts should be specified " +
"as a valid number : " + retryDelay.getText(), e);
} catch (JaxenException e) {
handleException("Couldn't assign dynamic retry delay for timeouts as Synapse expression");
}
}
}
Expand Down Expand Up @@ -308,13 +285,7 @@ public EndpointDefinition createDefinition(OMElement elem) {
if (suspendCodes != null && suspendCodes.getText() != null) {
String trimmedSuspendCodes = suspendCodes.getText().trim();
if (isExpression(trimmedSuspendCodes)) {
try {
String expressionStr = trimmedSuspendCodes.substring(1, trimmedSuspendCodes.length() - 1);
SynapseXPath expressionXPath = new SynapseXPath(expressionStr);
definition.setDynamicSuspendErrorCodes(expressionXPath);
} catch (JaxenException e) {
handleException("Couldn't assign dynamic suspend error codes as Synapse expression");
}
definition.setDynamicSuspendErrorCodes(valueFactory.createTextValue(suspendCodes));
} else {
StringTokenizer st = new StringTokenizer(trimmedSuspendCodes, ", ");
while (st.hasMoreTokens()) {
Expand All @@ -336,18 +307,14 @@ public EndpointDefinition createDefinition(OMElement elem) {
try {
String initialDurationTrimmed = initialDuration.getText().trim();
if (isExpression(initialDurationTrimmed)) {
String expressionStr = initialDurationTrimmed.substring(1, initialDurationTrimmed.length() - 1);
SynapseXPath expressionXPath = new SynapseXPath(expressionStr);
definition.setDynamicInitialSuspendDuration(expressionXPath);
definition.setDynamicInitialSuspendDuration(valueFactory.createTextValue(initialDuration));
} else {
definition.setInitialSuspendDuration(
Integer.parseInt(initialDurationTrimmed));
}
} catch (NumberFormatException e) {
handleException("The initial suspend duration should be specified " +
"as a valid number : " + initialDuration.getText(), e);
} catch (JaxenException e) {
handleException("Couldn't assign dynamic initial suspend duration as Synapse expression");
}
}

Expand All @@ -358,18 +325,14 @@ public EndpointDefinition createDefinition(OMElement elem) {
try {
String trimmedProgressionFactor = progressionFactor.getText().trim();
if (isExpression(trimmedProgressionFactor)) {
String expressionStr = trimmedProgressionFactor.substring(1, trimmedProgressionFactor.length() - 1);
SynapseXPath expressionXPath = new SynapseXPath(expressionStr);
definition.setDynamicSuspendProgressionFactor(expressionXPath);
definition.setDynamicSuspendProgressionFactor(valueFactory.createTextValue(progressionFactor));
} else {
definition.setSuspendProgressionFactor(
Float.parseFloat(trimmedProgressionFactor));
}
} catch (NumberFormatException e) {
handleException("The suspend duration progression factor should be specified " +
"as a valid float : " + progressionFactor.getText(), e);
} catch (JaxenException e) {
handleException("Couldn't assign dynamic suspend duration progression factor as Synapse expression");
}
}

Expand All @@ -380,18 +343,14 @@ public EndpointDefinition createDefinition(OMElement elem) {
try {
String trimmedMaximumDuration = maximumDuration.getText().trim();
if (isExpression(trimmedMaximumDuration)) {
String expressionStr = trimmedMaximumDuration.substring(1, trimmedMaximumDuration.length() - 1);
SynapseXPath expressionXPath = new SynapseXPath(expressionStr);
definition.setDynamicSuspendMaximumDuration(expressionXPath);
definition.setDynamicSuspendMaximumDuration(valueFactory.createTextValue(maximumDuration));
} else {
definition.setSuspendMaximumDuration(
Long.parseLong(maximumDuration.getText().trim()));
}
} catch (NumberFormatException e) {
handleException("The maximum suspend duration should be specified " +
"as a valid number : " + maximumDuration.getText(), e);
} catch (JaxenException e) {
handleException("Couldn't assign dynamic maximum suspend duration as Synapse expression");
}
}
}
Expand Down
Loading

0 comments on commit 2df1a58

Please sign in to comment.