Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expression support to endpoint definition attributes for handling endpoints in the HTTP connector. #2238

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9f36f1d
add expression for suspend initial duration attribute without create …
chathuranga-jayanath-99 Oct 3, 2024
261b0f4
add expression for suspend maximum duration attribute without set sus…
chathuranga-jayanath-99 Oct 4, 2024
704a8da
add expression for suspend progression factor
chathuranga-jayanath-99 Oct 4, 2024
26ff0b4
add expression for retries on timeout before suspend without set time…
chathuranga-jayanath-99 Oct 4, 2024
64aab6c
add expression for retry duration on timeout
chathuranga-jayanath-99 Oct 4, 2024
1ba5ad2
add tests to check added expressions
chathuranga-jayanath-99 Oct 5, 2024
ea622f3
add expression for timeout action
chathuranga-jayanath-99 Oct 5, 2024
9e371ab
fix expression check pattern
chathuranga-jayanath-99 Oct 5, 2024
7178623
add expression for suspend error codes without setSuspendStateProperties
chathuranga-jayanath-99 Oct 7, 2024
67e5592
add expression for timeout error codes without setTimeoutStateProperties
chathuranga-jayanath-99 Oct 7, 2024
65fd4d2
add tests for error codes
chathuranga-jayanath-99 Oct 7, 2024
e19e5d4
parse message context for createJsonRepresentation
chathuranga-jayanath-99 Oct 8, 2024
81b9899
handle timeout action value "never"
chathuranga-jayanath-99 Nov 25, 2024
e27203b
remove log warns where unnecessary
chathuranga-jayanath-99 Nov 25, 2024
2a5cb16
add new tests handling value not defined scenarios
chathuranga-jayanath-99 Nov 25, 2024
5d0d62f
Merge branch 'refs/heads/add-expression' into integrate-new-expressions
chathuranga-jayanath-99 Nov 25, 2024
1b00df2
remove handling timeout action value "never"
chathuranga-jayanath-99 Nov 26, 2024
2df1a58
add synapse expression support
chathuranga-jayanath-99 Nov 26, 2024
3c782b1
Revert "parse message context for createJsonRepresentation"
chathuranga-jayanath-99 Nov 28, 2024
4a99054
introduce value to handle endpoint definition attributes
chathuranga-jayanath-99 Nov 28, 2024
4383df8
add 'never' as default value of timeoutAction
chathuranga-jayanath-99 Dec 6, 2024
80a2d64
change delimiter ", " to ","
chathuranga-jayanath-99 Dec 6, 2024
b1367aa
add tests to check endpoint definition
chathuranga-jayanath-99 Dec 7, 2024
9d29be9
address reviewed changes
chathuranga-jayanath-99 Dec 8, 2024
c88988f
add serializer tests
chathuranga-jayanath-99 Dec 8, 2024
68cf96d
add method overload for onSuccess, onFault, onTimeout
chathuranga-jayanath-99 Dec 9, 2024
213b9ec
update print endpoint address
chathuranga-jayanath-99 Dec 9, 2024
e79f395
address reviewed changes
chathuranga-jayanath-99 Dec 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static void setSpanTags(SpanWrapper spanWrapper, MessageContext synCtx) {
}
if (openStatisticsLog.getEndpoint() != null) {
span.setAttribute(TelemetryConstants.ENDPOINT_ATTRIBUTE_KEY,
String.valueOf(openStatisticsLog.getEndpoint().getJsonRepresentation()));
String.valueOf(openStatisticsLog.getEndpoint().getJsonRepresentation(synCtx)));
}
if (synCtx.getProperty(CorrelationConstants.CORRELATION_ID) != null) {
span.setAttribute(TelemetryConstants.CORRELATION_ID_ATTRIBUTE_KEY,
Expand Down
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,35 +161,34 @@ 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)) {
chathuranga-jayanath-99 marked this conversation as resolved.
Show resolved Hide resolved
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");
}
}
}

OMElement action = timeout.getFirstChildWithName(
new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "responseAction"));
if (action != null && action.getText() != null) {
String actionString = action.getText();
if ("discard".equalsIgnoreCase(actionString.trim())) {
definition.setTimeoutAction(SynapseConstants.DISCARD);
} else if ("fault".equalsIgnoreCase(actionString.trim())) {
definition.setTimeoutAction(SynapseConstants.DISCARD_AND_FAULT);
String actionString = action.getText().trim();
if (isExpression(actionString)) {
definition.setDynamicTimeoutAction(valueFactory.createTextValue(action));
} else {
handleException("Invalid timeout action, action : "
+ actionString + " is not supported");
if ("discard".equalsIgnoreCase(actionString)) {
definition.setTimeoutAction(SynapseConstants.DISCARD);
} else if ("fault".equalsIgnoreCase(actionString)) {
definition.setTimeoutAction(SynapseConstants.DISCARD_AND_FAULT);
} else {
handleException("Invalid timeout action, action : "
+ actionString + " is not supported");
}
}
}
}
Expand All @@ -202,14 +203,19 @@ public EndpointDefinition createDefinition(OMElement elem) {
SynapseConstants.SYNAPSE_NAMESPACE,
XMLConfigConstants.ERROR_CODES));
if (timeoutCodes != null && timeoutCodes.getText() != null) {
StringTokenizer st = new StringTokenizer(timeoutCodes.getText().trim(), ", ");
while (st.hasMoreTokens()) {
String s = st.nextToken();
try {
definition.addTimeoutErrorCode(Integer.parseInt(s));
} catch (NumberFormatException e) {
handleException("The timeout error codes should be specified " +
"as valid numbers separated by commas : " + timeoutCodes.getText(), e);
String trimmedTimeoutCodes = timeoutCodes.getText().trim();
if (isExpression(trimmedTimeoutCodes)) {
definition.setDynamicTimeoutErrorCodes(valueFactory.createTextValue(timeoutCodes));
} else {
StringTokenizer st = new StringTokenizer(timeoutCodes.getText().trim(), ", ");
chathuranga-jayanath-99 marked this conversation as resolved.
Show resolved Hide resolved
while (st.hasMoreTokens()) {
String s = st.nextToken();
try {
definition.addTimeoutErrorCode(Integer.parseInt(s));
} catch (NumberFormatException e) {
handleException("The timeout error codes should be specified " +
"as valid numbers separated by commas : " + timeoutCodes.getText(), e);
}
}
}
}
Expand All @@ -219,8 +225,13 @@ public EndpointDefinition createDefinition(OMElement elem) {
XMLConfigConstants.RETRIES_BEFORE_SUSPENSION));
if (retriesBeforeSuspend != null && retriesBeforeSuspend.getText() != null) {
try {
definition.setRetriesOnTimeoutBeforeSuspend(
Integer.parseInt(retriesBeforeSuspend.getText().trim()));
String trimmedRetriesBeforeSuspend = retriesBeforeSuspend.getText().trim();
if (isExpression(trimmedRetriesBeforeSuspend)) {
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);
Expand All @@ -232,8 +243,13 @@ public EndpointDefinition createDefinition(OMElement elem) {
XMLConfigConstants.RETRY_DELAY));
if (retryDelay != null && retryDelay.getText() != null) {
try {
definition.setRetryDurationOnTimeout(
Integer.parseInt(retryDelay.getText().trim()));
String trimmedRetryDelay = retryDelay.getText().trim();
if (isExpression(trimmedRetryDelay)) {
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);
Expand Down Expand Up @@ -267,15 +283,19 @@ public EndpointDefinition createDefinition(OMElement elem) {
SynapseConstants.SYNAPSE_NAMESPACE,
XMLConfigConstants.ERROR_CODES));
if (suspendCodes != null && suspendCodes.getText() != null) {

StringTokenizer st = new StringTokenizer(suspendCodes.getText().trim(), ", ");
while (st.hasMoreTokens()) {
String s = st.nextToken();
try {
definition.addSuspendErrorCode(Integer.parseInt(s));
} catch (NumberFormatException e) {
handleException("The suspend error codes should be specified " +
"as valid numbers separated by commas : " + suspendCodes.getText(), e);
String trimmedSuspendCodes = suspendCodes.getText().trim();
if (isExpression(trimmedSuspendCodes)) {
definition.setDynamicSuspendErrorCodes(valueFactory.createTextValue(suspendCodes));
} else {
StringTokenizer st = new StringTokenizer(trimmedSuspendCodes, ", ");
while (st.hasMoreTokens()) {
String s = st.nextToken();
try {
definition.addSuspendErrorCode(Integer.parseInt(s));
} catch (NumberFormatException e) {
handleException("The suspend error codes should be specified " +
"as valid numbers separated by commas : " + suspendCodes.getText(), e);
}
}
}
}
Expand All @@ -285,8 +305,13 @@ public EndpointDefinition createDefinition(OMElement elem) {
XMLConfigConstants.SUSPEND_INITIAL_DURATION));
if (initialDuration != null && initialDuration.getText() != null) {
try {
definition.setInitialSuspendDuration(
Integer.parseInt(initialDuration.getText().trim()));
String initialDurationTrimmed = initialDuration.getText().trim();
if (isExpression(initialDurationTrimmed)) {
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);
Expand All @@ -298,8 +323,13 @@ public EndpointDefinition createDefinition(OMElement elem) {
XMLConfigConstants.SUSPEND_PROGRESSION_FACTOR));
if (progressionFactor != null && progressionFactor.getText() != null) {
try {
definition.setSuspendProgressionFactor(
Float.parseFloat(progressionFactor.getText().trim()));
String trimmedProgressionFactor = progressionFactor.getText().trim();
if (isExpression(trimmedProgressionFactor)) {
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);
Expand All @@ -311,8 +341,13 @@ public EndpointDefinition createDefinition(OMElement elem) {
XMLConfigConstants.SUSPEND_MAXIMUM_DURATION));
if (maximumDuration != null && maximumDuration.getText() != null) {
try {
definition.setSuspendMaximumDuration(
Long.parseLong(maximumDuration.getText().trim()));
String trimmedMaximumDuration = maximumDuration.getText().trim();
if (isExpression(trimmedMaximumDuration)) {
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);
Expand Down Expand Up @@ -368,6 +403,14 @@ public EndpointDefinition createDefinition(OMElement elem) {
return definition;
}

private boolean isExpression(String expressionStr) {
Pattern pattern = Pattern.compile("\\{.*\\}");
if (pattern.matcher(expressionStr).matches()) {
chathuranga-jayanath-99 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
return false;
}

protected static void handleException(String msg) {
log.error(msg);
throw new SynapseException(msg);
Expand Down
Loading
Loading