From 9f36f1d750b8bbc198de2d80cc92bda2314323fd Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Fri, 4 Oct 2024 00:08:50 +0530 Subject: [PATCH 01/27] add expression for suspend initial duration attribute without create json representation change --- .../endpoints/EndpointDefinitionFactory.java | 21 ++++++++- .../EndpointDefinitionSerializer.java | 10 +++-- .../synapse/endpoints/EndpointContext.java | 31 +++++++++---- .../synapse/endpoints/EndpointDefinition.java | 44 +++++++++++++++++++ .../synapse/endpoints/HTTPEndpoint.java | 2 +- .../synapse/endpoints/HttpEndpointTest.java | 11 +++++ 6 files changed, 105 insertions(+), 14 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java index ae1e925562..a261505ca9 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java @@ -285,11 +285,20 @@ 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)) { + String expressionStr = initialDurationTrimmed.substring(1, initialDurationTrimmed.length() - 1); + SynapseXPath expressionXPath = new SynapseXPath(expressionStr); + definition.setDynamicInitialSuspendDuration(expressionXPath); + } 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"); } } @@ -368,6 +377,14 @@ public EndpointDefinition createDefinition(OMElement elem) { return definition; } + private boolean isExpression(String expressionStr) { + Pattern pattern = Pattern.compile("\\{.*\\}}"); + if (pattern.matcher(expressionStr).matches()) { + return true; + } + return false; + } + protected static void handleException(String msg) { log.error(msg); throw new SynapseException(msg); diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java index 0c2e7a50f5..4fefc4b4ce 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java @@ -132,7 +132,7 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, } } - if (endpointDefinition.getInitialSuspendDuration() != -1 || + if (endpointDefinition.getInitialSuspendDuration() != -1 || endpointDefinition.isInitialSuspendDurationDynamic() || !endpointDefinition.getSuspendErrorCodes().isEmpty()) { OMElement suspendOnFailure = fac.createOMElement( @@ -148,11 +148,15 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, suspendOnFailure.addChild(errorCodes); } - if (endpointDefinition.getInitialSuspendDuration() != -1) { + if (endpointDefinition.getInitialSuspendDuration() != -1 || endpointDefinition.isInitialSuspendDurationDynamic()) { OMElement initialDuration = fac.createOMElement( org.apache.synapse.config.xml.XMLConfigConstants.SUSPEND_INITIAL_DURATION, SynapseConstants.SYNAPSE_OMNAMESPACE); - initialDuration.setText(Long.toString(endpointDefinition.getInitialSuspendDuration())); + if (!endpointDefinition.isInitialSuspendDurationDynamic()){ + initialDuration.setText(Long.toString(endpointDefinition.getInitialSuspendDuration())); + } else { + initialDuration.setText('{' + endpointDefinition.getDynamicInitialSuspendDuration().getExpression() + '}'); + } suspendOnFailure.addChild(initialDuration); } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java index 0d3a6d80e6..965729d7b9 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java @@ -25,6 +25,7 @@ import org.apache.synapse.SynapseException; import org.apache.synapse.config.SynapsePropertiesLoader; import org.apache.synapse.util.MessageHelper; +import org.apache.synapse.MessageContext; import org.apache.synapse.util.Replicator; import java.util.Calendar; @@ -194,12 +195,16 @@ private void recordStatistics(int state) { } } + private void setState(int state) { + setState(state, null); + } + /** * Update the internal state of the endpoint * * @param state the new state of the endpoint */ - private void setState(int state) { + private void setState(int state, MessageContext messageContext) { recordStatistics(state); @@ -229,7 +234,7 @@ private void setState(int state) { " has been marked for SUSPENSION," + " but no further retries remain. Thus it will be SUSPENDED."); - setState(ST_SUSPENDED); + setState(ST_SUSPENDED, messageContext); } else { Replicator.setAndReplicateState( @@ -246,7 +251,7 @@ private void setState(int state) { break; } case ST_SUSPENDED: { - computeNextRetryTimeForSuspended(); + computeNextRetryTimeForSuspended(messageContext); break; } case ST_OFF: { @@ -294,7 +299,7 @@ private void setState(int state) { + " has been marked for SUSPENSION, " + "but no further retries remain. Thus it will be SUSPENDED."); - setState(ST_SUSPENDED); + setState(ST_SUSPENDED, messageContext); } else { localRemainingRetries = retries - 1; @@ -309,7 +314,7 @@ private void setState(int state) { break; } case ST_SUSPENDED: { - computeNextRetryTimeForSuspended(); + computeNextRetryTimeForSuspended(messageContext); break; } case ST_OFF: { @@ -360,6 +365,15 @@ public void onFault() { setState(ST_SUSPENDED); } + /** + * Endpoint failed processing a message + */ + public void onFault(MessageContext messageContext) { + log.warn("Endpoint : " + endpointName + printEndpointAddress() + + " will be marked SUSPENDED as it failed"); + setState(ST_SUSPENDED, messageContext); + } + /** * Endpoint timeout processing a message */ @@ -374,9 +388,10 @@ public void onTimeout() { /** * Compute the suspension duration according to the geometric series parameters defined */ - private void computeNextRetryTimeForSuspended() { + private void computeNextRetryTimeForSuspended(MessageContext messageContext) { boolean notYetSuspended = true; - long lastSuspendDuration = definition.getInitialSuspendDuration(); + long lastSuspendDuration = definition.getResolvedInitialSuspendDuration(messageContext); + if (isClustered) { Long lastDuration = (Long) cfgCtx.getPropertyNonReplicable(LAST_SUSPEND_DURATION_KEY); if (lastDuration != null) { @@ -389,7 +404,7 @@ private void computeNextRetryTimeForSuspended() { } long nextSuspendDuration = (notYetSuspended ? - definition.getInitialSuspendDuration() : + definition.getResolvedInitialSuspendDuration(messageContext) : (long) (lastSuspendDuration * definition.getSuspendProgressionFactor())); if (nextSuspendDuration > definition.getSuspendMaximumDuration()) { diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java index 3cfe55dcf4..88d6ffcfe6 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java @@ -161,6 +161,10 @@ public class EndpointDefinition implements AspectConfigurable { /** The initial suspend duration when an endpoint is marked inactive */ private long initialSuspendDuration = -1; + /** + * The expression to evaluate dynamic initial suspend duration. + */ + private SynapsePath dynamicInitialSuspendDuration = null; /** The suspend duration ratio for the next duration - this is the geometric series multipler */ private float suspendProgressionFactor = 1; /** This is the maximum duration for which a node will be suspended */ @@ -618,6 +622,46 @@ public void setInitialSuspendDuration(long initialSuspendDuration) { this.initialSuspendDuration = initialSuspendDuration; } + public SynapsePath getDynamicInitialSuspendDuration() { + + return dynamicInitialSuspendDuration; + } + + public void setDynamicInitialSuspendDuration(SynapsePath dynamicInitialSuspendDuration) { + + this.dynamicInitialSuspendDuration = dynamicInitialSuspendDuration; + } + + public boolean isInitialSuspendDurationDynamic() { + if (dynamicInitialSuspendDuration != null) { + return true; + } else { + return false; + } + } + + public long evaluateDynamicInitialSuspendDuration(MessageContext synCtx) { + long result = initialSuspendDuration; + try { + String stringValue = dynamicInitialSuspendDuration.stringValueOf(synCtx); + if (stringValue != null) { + result = Long.parseLong(stringValue.trim()); + } else { + log.warn("Error while evaluating dynamic initial suspend duration."); + } + } catch (NumberFormatException e) { + log.warn("Error while evaluating dynamic initial suspend duration."); + } + return result; + } + + public long getResolvedInitialSuspendDuration(MessageContext messageContext) { + if (isInitialSuspendDurationDynamic()) { + return evaluateDynamicInitialSuspendDuration(messageContext); + } + return initialSuspendDuration; + } + // public int getTraceState() { // return traceState; // } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/HTTPEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/HTTPEndpoint.java index ab5387f98b..ce53631fee 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/HTTPEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/HTTPEndpoint.java @@ -72,7 +72,7 @@ public void onFault(MessageContext synCtx) { if (isTimeout(synCtx)) { getContext().onTimeout(); } else if (isSuspendFault(synCtx)) { - getContext().onFault(); + getContext().onFault(synCtx); } } diff --git a/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java b/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java index edd33d9bed..4134977bfc 100644 --- a/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java +++ b/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java @@ -153,6 +153,17 @@ public void testQueryParamsWithLegacyEncoding() throws AxisFault, XMLStreamExcep } + @Test + public void testSetSuspendOnFailureInitialDuration() throws XMLStreamException, AxisFault { + + HTTPEndpointFactory httpEndpointFactory = new HTTPEndpointFactory(); + OMElement omElement = AXIOMUtil.stringToOM( + "10100010"); + EndpointDefinition ep1 = httpEndpointFactory.createEndpointDefinition(omElement); + Assert.assertEquals(ep1.getRetriesOnTimeoutBeforeSuspend(), 0); + Assert.assertEquals(ep1.getInitialSuspendDuration(), 1000); + } + /** * Create a mock SynapseEnvironment object * From 261b0f4f9c51f0206f0026480797fd17d90dd5f3 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Fri, 4 Oct 2024 09:08:47 +0530 Subject: [PATCH 02/27] add expression for suspend maximum duration attribute without set suspend state properties change --- .../endpoints/EndpointDefinitionFactory.java | 13 ++++++- .../EndpointDefinitionSerializer.java | 10 +++-- .../synapse/endpoints/EndpointContext.java | 4 +- .../synapse/endpoints/EndpointDefinition.java | 38 +++++++++++++++++++ 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java index a261505ca9..d7726fea4d 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java @@ -320,11 +320,20 @@ 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)) { + String expressionStr = trimmedMaximumDuration.substring(1, trimmedMaximumDuration.length() - 1); + SynapseXPath expressionXPath = new SynapseXPath(expressionStr); + definition.setDynamicSuspendMaximumDuration(expressionXPath); + } 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"); } } } diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java index 4fefc4b4ce..576fb3e466 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java @@ -168,12 +168,16 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, suspendOnFailure.addChild(progressionFactor); } - if (endpointDefinition.getSuspendMaximumDuration() != -1 && - endpointDefinition.getSuspendMaximumDuration() != Long.MAX_VALUE) { + if ((endpointDefinition.getSuspendMaximumDuration() != -1 && + endpointDefinition.getSuspendMaximumDuration() != Long.MAX_VALUE) || endpointDefinition.isSuspendMaximumDurationDynamic()) { OMElement suspendMaximum = fac.createOMElement( org.apache.synapse.config.xml.XMLConfigConstants.SUSPEND_MAXIMUM_DURATION, SynapseConstants.SYNAPSE_OMNAMESPACE); - suspendMaximum.setText(Long.toString(endpointDefinition.getSuspendMaximumDuration())); + if (endpointDefinition.isSuspendMaximumDurationDynamic()) { + suspendMaximum.setText('{' + endpointDefinition.getDynamicSuspendMaximumDuration().getExpression() + '}'); + } else { + suspendMaximum.setText(Long.toString(endpointDefinition.getSuspendMaximumDuration())); + } suspendOnFailure.addChild(suspendMaximum); } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java index 965729d7b9..abe9ef408a 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java @@ -407,8 +407,8 @@ private void computeNextRetryTimeForSuspended(MessageContext messageContext) { definition.getResolvedInitialSuspendDuration(messageContext) : (long) (lastSuspendDuration * definition.getSuspendProgressionFactor())); - if (nextSuspendDuration > definition.getSuspendMaximumDuration()) { - nextSuspendDuration = definition.getSuspendMaximumDuration(); + if (nextSuspendDuration > definition.getResolvedSuspendMaximumDuration(messageContext)) { + nextSuspendDuration = definition.getResolvedSuspendMaximumDuration(messageContext); } else if (nextSuspendDuration < 0) { nextSuspendDuration = SynapseConstants.DEFAULT_ENDPOINT_SUSPEND_TIME; } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java index 88d6ffcfe6..1382438c31 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java @@ -169,6 +169,8 @@ public class EndpointDefinition implements AspectConfigurable { private float suspendProgressionFactor = 1; /** This is the maximum duration for which a node will be suspended */ private long suspendMaximumDuration = Long.MAX_VALUE; + /** The expression to maximum duration for which a node will be suspended */ + private SynapsePath dynamicSuspendMaximumDuration = null; /** A list of error codes, which directly puts an endpoint into suspend mode */ private final List suspendErrorCodes = new ArrayList(); @@ -686,6 +688,42 @@ public void setSuspendMaximumDuration(long suspendMaximumDuration) { this.suspendMaximumDuration = suspendMaximumDuration; } + public SynapsePath getDynamicSuspendMaximumDuration() { + + return dynamicSuspendMaximumDuration; + } + + public void setDynamicSuspendMaximumDuration(SynapsePath dynamicSuspendMaximumDuration) { + + this.dynamicSuspendMaximumDuration = dynamicSuspendMaximumDuration; + } + + public boolean isSuspendMaximumDurationDynamic() { + + return dynamicSuspendMaximumDuration != null; + } + + public long evaluateDynamicSuspendMaximumDuration(MessageContext messageContext) { + + long result = suspendMaximumDuration; + try { + String stringValue = dynamicSuspendMaximumDuration.stringValueOf(messageContext); + if (stringValue != null) { + result = Long.parseLong(stringValue); + } + } catch (NumberFormatException e) { + log.warn("Error while evaluating dynamic endpoint timeout expression."); + } + return result; + } + + public long getResolvedSuspendMaximumDuration(MessageContext messageContext) { + if (isSuspendMaximumDurationDynamic()) { + return evaluateDynamicSuspendMaximumDuration(messageContext); + } + return suspendMaximumDuration; + } + public int getRetriesOnTimeoutBeforeSuspend() { return retriesOnTimeoutBeforeSuspend; } From 704a8da9c7bdfe648f4b460d8a70afb11a20e801 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Fri, 4 Oct 2024 10:55:40 +0530 Subject: [PATCH 03/27] add expression for suspend progression factor --- .../endpoints/EndpointDefinitionFactory.java | 13 ++++++- .../EndpointDefinitionSerializer.java | 8 +++- .../synapse/endpoints/EndpointContext.java | 2 +- .../synapse/endpoints/EndpointDefinition.java | 38 +++++++++++++++++++ 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java index d7726fea4d..c96983a3aa 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java @@ -307,11 +307,20 @@ 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)) { + String expressionStr = trimmedProgressionFactor.substring(1, trimmedProgressionFactor.length() - 1); + SynapseXPath expressionXPath = new SynapseXPath(expressionStr); + definition.setDynamicSuspendProgressionFactor(expressionXPath); + } 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"); } } diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java index 576fb3e466..1983c2d470 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java @@ -160,11 +160,15 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, suspendOnFailure.addChild(initialDuration); } - if (endpointDefinition.getSuspendProgressionFactor() != -1) { + if (endpointDefinition.getSuspendProgressionFactor() != -1 || endpointDefinition.isSuspendProgressionFactorDynamic()) { OMElement progressionFactor = fac.createOMElement( org.apache.synapse.config.xml.XMLConfigConstants.SUSPEND_PROGRESSION_FACTOR, SynapseConstants.SYNAPSE_OMNAMESPACE); - progressionFactor.setText(Float.toString(endpointDefinition.getSuspendProgressionFactor())); + if (endpointDefinition.isSuspendProgressionFactorDynamic()) { + progressionFactor.setText('{' + endpointDefinition.getDynamicSuspendProgressionFactor().getExpression() + '}'); + } else { + progressionFactor.setText(Float.toString(endpointDefinition.getSuspendProgressionFactor())); + } suspendOnFailure.addChild(progressionFactor); } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java index abe9ef408a..02c8c15f4b 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java @@ -405,7 +405,7 @@ private void computeNextRetryTimeForSuspended(MessageContext messageContext) { long nextSuspendDuration = (notYetSuspended ? definition.getResolvedInitialSuspendDuration(messageContext) : - (long) (lastSuspendDuration * definition.getSuspendProgressionFactor())); + (long) (lastSuspendDuration * definition.getResolvedSuspendProgressionFactor(messageContext))); if (nextSuspendDuration > definition.getResolvedSuspendMaximumDuration(messageContext)) { nextSuspendDuration = definition.getResolvedSuspendMaximumDuration(messageContext); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java index 1382438c31..f884b992e2 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java @@ -167,6 +167,8 @@ public class EndpointDefinition implements AspectConfigurable { private SynapsePath dynamicInitialSuspendDuration = null; /** The suspend duration ratio for the next duration - this is the geometric series multipler */ private float suspendProgressionFactor = 1; + /** The expression to evaluate dynamic suspend progression factor */ + private SynapsePath dynamicSuspendProgressionFactor = null; /** This is the maximum duration for which a node will be suspended */ private long suspendMaximumDuration = Long.MAX_VALUE; /** The expression to maximum duration for which a node will be suspended */ @@ -680,6 +682,42 @@ public void setSuspendProgressionFactor(float suspendProgressionFactor) { this.suspendProgressionFactor = suspendProgressionFactor; } + public SynapsePath getDynamicSuspendProgressionFactor() { + + return dynamicSuspendProgressionFactor; + } + + public void setDynamicSuspendProgressionFactor(SynapsePath dynamicSuspendProgressionFactor) { + + this.dynamicSuspendProgressionFactor = dynamicSuspendProgressionFactor; + } + + public boolean isSuspendProgressionFactorDynamic() { + + return dynamicSuspendProgressionFactor != null; + } + + public float evaluateDynamicSuspendProgressionFactor(MessageContext messageContext) { + + float result = suspendProgressionFactor; + try { + String stringValue = dynamicSuspendProgressionFactor.stringValueOf(messageContext); + if (stringValue != null) { + result = Float.parseFloat(stringValue); + } + } catch (NumberFormatException e) { + log.warn("Error while evaluating dynamic endpoint timeout expression."); + } + return result; + } + + public float getResolvedSuspendProgressionFactor(MessageContext messageContext) { + if (isSuspendProgressionFactorDynamic()) { + return evaluateDynamicSuspendProgressionFactor(messageContext); + } + return suspendProgressionFactor; + } + public long getSuspendMaximumDuration() { return suspendMaximumDuration; } From 26ff0b4cabd6c40d3545fe2ead68ed7ee4be917a Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Fri, 4 Oct 2024 11:45:14 +0530 Subject: [PATCH 04/27] add expression for retries on timeout before suspend without set timeout state properties change --- .../endpoints/EndpointDefinitionFactory.java | 13 ++++++- .../EndpointDefinitionSerializer.java | 8 +++- .../synapse/endpoints/EndpointContext.java | 12 +++--- .../synapse/endpoints/EndpointDefinition.java | 39 +++++++++++++++++++ 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java index c96983a3aa..411153140f 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java @@ -219,11 +219,20 @@ 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)) { + String expressionStr = trimmedRetriesBeforeSuspend.substring(1, trimmedRetriesBeforeSuspend.length() - 1); + SynapseXPath expressionXPath = new SynapseXPath(expressionStr); + definition.setDynamicRetriesOnTimeoutBeforeSuspend(expressionXPath); + } 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"); } } diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java index 1983c2d470..1a8c87d067 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java @@ -204,11 +204,15 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, markAsTimedout.addChild(errorCodes); } - if (endpointDefinition.getRetriesOnTimeoutBeforeSuspend() > 0) { + if (endpointDefinition.getRetriesOnTimeoutBeforeSuspend() > 0 || endpointDefinition.isRetriesOnTimeoutBeforeSuspendDynamic()) { OMElement retries = fac.createOMElement( org.apache.synapse.config.xml.XMLConfigConstants.RETRIES_BEFORE_SUSPENSION, SynapseConstants.SYNAPSE_OMNAMESPACE); - retries.setText(Long.toString(endpointDefinition.getRetriesOnTimeoutBeforeSuspend())); + if (endpointDefinition.isRetriesOnTimeoutBeforeSuspendDynamic()) { + retries.setText('{' + endpointDefinition.getDynamicRetriesOnTimeoutBeforeSuspend().getExpression() + '}'); + } else { + retries.setText(Long.toString(endpointDefinition.getRetriesOnTimeoutBeforeSuspend())); + } markAsTimedout.addChild(retries); } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java index 02c8c15f4b..f6e3c123ce 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java @@ -214,7 +214,7 @@ private void setState(int state, MessageContext messageContext) { switch (state) { case ST_ACTIVE: { Replicator.setAndReplicateState(REMAINING_RETRIES_KEY, - definition.getRetriesOnTimeoutBeforeSuspend(), cfgCtx); + definition.getResolvedRetriesOnTimeoutBeforeSuspend(messageContext), cfgCtx); Replicator.setAndReplicateState(LAST_SUSPEND_DURATION_KEY, null, cfgCtx); Replicator.setAndReplicateState(REMAINING_RETRIES_KEY, maximumRetryLimit, cfgCtx); if (maximumRecursiveRetryLimit != -1) { @@ -226,7 +226,7 @@ private void setState(int state, MessageContext messageContext) { Integer retries = (Integer) cfgCtx.getPropertyNonReplicable(REMAINING_RETRIES_KEY); if (retries == null) { - retries = definition.getRetriesOnTimeoutBeforeSuspend(); + retries = definition.getResolvedRetriesOnTimeoutBeforeSuspend(messageContext); } if (retries <= 0) { @@ -258,7 +258,7 @@ private void setState(int state, MessageContext messageContext) { // mark as in maintenence, and reset all other information Replicator.setAndReplicateState(REMAINING_RETRIES_KEY, definition == null ? -1 : - definition.getRetriesOnTimeoutBeforeSuspend(), cfgCtx); + definition.getResolvedRetriesOnTimeoutBeforeSuspend(messageContext), cfgCtx); Replicator.setAndReplicateState(LAST_SUSPEND_DURATION_KEY, null, cfgCtx); Replicator.setAndReplicateState(REMAINING_RETRIES_KEY, maximumRetryLimit, cfgCtx); if (maximumRecursiveRetryLimit != -1) { @@ -280,7 +280,7 @@ private void setState(int state, MessageContext messageContext) { if (definition == null) return; switch (state) { case ST_ACTIVE: { - localRemainingRetries = definition.getRetriesOnTimeoutBeforeSuspend(); + localRemainingRetries = definition.getResolvedRetriesOnTimeoutBeforeSuspend(messageContext); localLastSuspendDuration = -1; maximumRemainingRetries = maximumRetryLimit; if (maximumRecursiveRetryLimit != -1) { @@ -291,7 +291,7 @@ private void setState(int state, MessageContext messageContext) { case ST_TIMEOUT: { int retries = localRemainingRetries; if (retries == -1) { - retries = definition.getRetriesOnTimeoutBeforeSuspend(); + retries = definition.getResolvedRetriesOnTimeoutBeforeSuspend(messageContext); } if (retries <= 0) { @@ -320,7 +320,7 @@ private void setState(int state, MessageContext messageContext) { case ST_OFF: { // mark as in maintenence, and reset all other information localRemainingRetries = definition == null ? - -1 : definition.getRetriesOnTimeoutBeforeSuspend(); + -1 : definition.getResolvedRetriesOnTimeoutBeforeSuspend(messageContext); localLastSuspendDuration = -1; maximumRemainingRetries = maximumRetryLimit; if (maximumRecursiveRetryLimit != -1) { diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java index f884b992e2..ca18f9ac7b 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java @@ -178,6 +178,8 @@ public class EndpointDefinition implements AspectConfigurable { /** No of retries to attempt on timeout, before an endpoint is makred inactive */ private int retriesOnTimeoutBeforeSuspend = 0; + /** The expression to evaluate dynamic retries on timeout before suspend */ + private SynapsePath dynamicRetriesOnTimeoutBeforeSuspend = null; /** The delay between retries for a timeout out endpoint */ private int retryDurationOnTimeout = 0; /** A list of error codes which puts the endpoint into timeout mode */ @@ -770,6 +772,43 @@ public void setRetriesOnTimeoutBeforeSuspend(int retriesOnTimeoutBeforeSuspend) this.retriesOnTimeoutBeforeSuspend = retriesOnTimeoutBeforeSuspend; } + public SynapsePath getDynamicRetriesOnTimeoutBeforeSuspend() { + + return dynamicRetriesOnTimeoutBeforeSuspend; + } + + public void setDynamicRetriesOnTimeoutBeforeSuspend( + SynapsePath dynamicRetriesOnTimeoutBeforeSuspend) { + + this.dynamicRetriesOnTimeoutBeforeSuspend = dynamicRetriesOnTimeoutBeforeSuspend; + } + + public boolean isRetriesOnTimeoutBeforeSuspendDynamic() { + + return dynamicRetriesOnTimeoutBeforeSuspend != null; + } + + public int evaluateDynamicRetriesOnTimeoutBeforeSuspend(MessageContext messageContext) { + + int result = retriesOnTimeoutBeforeSuspend; + try { + String stringValue = dynamicRetriesOnTimeoutBeforeSuspend.stringValueOf(messageContext); + if (stringValue != null) { + result = Integer.parseInt(stringValue); + } + } catch (NumberFormatException e) { + log.warn("Error while evaluating dynamic endpoint timeout expression."); + } + return result; + } + + public int getResolvedRetriesOnTimeoutBeforeSuspend(MessageContext messageContext) { + if (isRetriesOnTimeoutBeforeSuspendDynamic()) { + return evaluateDynamicRetriesOnTimeoutBeforeSuspend(messageContext); + } + return retriesOnTimeoutBeforeSuspend; + } + public int getRetryDurationOnTimeout() { return retryDurationOnTimeout; } From 64aab6c4b15c9d28aa78d0f32b4a178a8d500caa Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Fri, 4 Oct 2024 15:00:56 +0530 Subject: [PATCH 05/27] add expression for retry duration on timeout --- .../endpoints/EndpointDefinitionFactory.java | 13 ++++++- .../EndpointDefinitionSerializer.java | 10 +++-- .../synapse/endpoints/EndpointContext.java | 4 +- .../synapse/endpoints/EndpointDefinition.java | 38 +++++++++++++++++++ 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java index 411153140f..14430d6e48 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java @@ -241,11 +241,20 @@ 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)) { + String expressionStr = trimmedRetryDelay.substring(1, trimmedRetryDelay.length() - 1); + SynapseXPath expressionXPath = new SynapseXPath(expressionStr); + definition.setDynamicRetryDurationOnTimeout(expressionXPath); + } 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"); } } } diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java index 1a8c87d067..3db6db43f2 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java @@ -188,7 +188,7 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, element.addChild(suspendOnFailure); } - if (endpointDefinition.getRetryDurationOnTimeout() > 0 || + if (endpointDefinition.getRetryDurationOnTimeout() > 0 || endpointDefinition.isRetryDurationOnTimeoutDynamic() || !endpointDefinition.getTimeoutErrorCodes().isEmpty()) { OMElement markAsTimedout = fac.createOMElement( @@ -216,11 +216,15 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, markAsTimedout.addChild(retries); } - if (endpointDefinition.getRetryDurationOnTimeout() > 0) { + if (endpointDefinition.getRetryDurationOnTimeout() > 0 || endpointDefinition.isRetryDurationOnTimeoutDynamic()) { OMElement retryDelay = fac.createOMElement( org.apache.synapse.config.xml.XMLConfigConstants.RETRY_DELAY, SynapseConstants.SYNAPSE_OMNAMESPACE); - retryDelay.setText(Long.toString(endpointDefinition.getRetryDurationOnTimeout())); + if (endpointDefinition.isRetryDurationOnTimeoutDynamic()) { + retryDelay.setText('{' + endpointDefinition.getDynamicRetryDurationOnTimeout().getExpression() + '}'); + } else { + retryDelay.setText(Long.toString(endpointDefinition.getRetryDurationOnTimeout())); + } markAsTimedout.addChild(retryDelay); } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java index f6e3c123ce..93cc211f60 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java @@ -240,7 +240,7 @@ private void setState(int state, MessageContext messageContext) { Replicator.setAndReplicateState( REMAINING_RETRIES_KEY, (retries - 1), cfgCtx); long nextRetry = System.currentTimeMillis() - + definition.getRetryDurationOnTimeout(); + + definition.getResolvedRetryDurationOnTimeout(messageContext); Replicator.setAndReplicateState(NEXT_RETRY_TIME_KEY, nextRetry, cfgCtx); log.warn("Endpoint : " + endpointName + printEndpointAddress() + @@ -304,7 +304,7 @@ private void setState(int state, MessageContext messageContext) { } else { localRemainingRetries = retries - 1; localNextRetryTime = - System.currentTimeMillis() + definition.getRetryDurationOnTimeout(); + System.currentTimeMillis() + definition.getResolvedRetryDurationOnTimeout(messageContext); log.warn("Endpoint : " + endpointName + printEndpointAddress() + " is marked as TIMEOUT and " + "will be retried : " + localRemainingRetries + " more time/s " + diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java index ca18f9ac7b..7ce64d44e5 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java @@ -182,6 +182,8 @@ public class EndpointDefinition implements AspectConfigurable { private SynapsePath dynamicRetriesOnTimeoutBeforeSuspend = null; /** The delay between retries for a timeout out endpoint */ private int retryDurationOnTimeout = 0; + /** The expression to evaluate dynamic retry duration on timeout */ + private SynapsePath dynamicRetryDurationOnTimeout = null; /** A list of error codes which puts the endpoint into timeout mode */ private final List timeoutErrorCodes = new ArrayList(); @@ -817,6 +819,42 @@ public void setRetryDurationOnTimeout(int retryDurationOnTimeout) { this.retryDurationOnTimeout = retryDurationOnTimeout; } + public SynapsePath getDynamicRetryDurationOnTimeout() { + + return dynamicRetryDurationOnTimeout; + } + + public void setDynamicRetryDurationOnTimeout(SynapsePath dynamicRetryDurationOnTimeout) { + + this.dynamicRetryDurationOnTimeout = dynamicRetryDurationOnTimeout; + } + + public boolean isRetryDurationOnTimeoutDynamic() { + + return dynamicRetryDurationOnTimeout != null; + } + + public int evaluateDynamicRetryDurationOnTimeout(MessageContext messageContext) { + + int result = retryDurationOnTimeout; + try { + String stringValue = dynamicRetryDurationOnTimeout.stringValueOf(messageContext); + if (stringValue != null) { + result = Integer.parseInt(stringValue); + } + } catch (NumberFormatException e) { + log.warn("Error while evaluating dynamic endpoint timeout expression."); + } + return result; + } + + public int getResolvedRetryDurationOnTimeout(MessageContext messageContext) { + if (isRetryDurationOnTimeoutDynamic()) { + return evaluateDynamicRetryDurationOnTimeout(messageContext); + } + return retryDurationOnTimeout; + } + public List getSuspendErrorCodes() { return suspendErrorCodes; } From 1ba5ad253d762ad03cf29da6309eb5e67896b0ef Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Sat, 5 Oct 2024 18:15:50 +0530 Subject: [PATCH 06/27] add tests to check added expressions --- .../dynamic/DynamicEndpointTest.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java b/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java index cbd641290c..36fa58fa0b 100644 --- a/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java +++ b/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java @@ -38,4 +38,63 @@ public void testContextProperties() throws Exception { endpoint.getDefinition().evaluateDynamicEndpointTimeout(synCtx)); } + public void testContextPropertiesForInitialSuspendDuration() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:initialSuspendDuration"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicInitialSuspendDuration(xpath); + MessageContext synCtx = new TestMessageContext(); + synCtx.setProperty("initialSuspendDuration", "90000"); + assertEquals(endpoint.getDefinition().getResolvedInitialSuspendDuration(synCtx), 90000); + } + + public void testContextPropertiesForSuspendMaximumDuration() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:suspendMaximumDuration"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicSuspendMaximumDuration(xpath); + MessageContext synCtx = new TestMessageContext(); + synCtx.setProperty("suspendMaximumDuration", "90000"); + assertEquals(endpoint.getDefinition().getResolvedSuspendMaximumDuration(synCtx), 90000); + } + + public void testContextPropertiesForSuspendProgressionFactor() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:suspendProgressionFactor"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicSuspendProgressionFactor(xpath); + MessageContext synCtx = new TestMessageContext(); + synCtx.setProperty("suspendProgressionFactor", "2"); + assertEquals(endpoint.getDefinition().getResolvedSuspendProgressionFactor(synCtx), 2.0f); + } + + public void testContextPropertiesForRetriesOnTimeoutBeforeSuspend() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:retriesOnTimeoutBeforeSuspend"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicRetriesOnTimeoutBeforeSuspend(xpath); + MessageContext synCtx = new TestMessageContext(); + synCtx.setProperty("retriesOnTimeoutBeforeSuspend", "3"); + assertEquals(endpoint.getDefinition().getResolvedRetriesOnTimeoutBeforeSuspend(synCtx), 3); + } + + public void testContextPropertiesForRetryDurationOnTimeout() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:retryDurationOnTimeout"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicRetryDurationOnTimeout(xpath); + MessageContext synCtx = new TestMessageContext(); + synCtx.setProperty("retryDurationOnTimeout", "90000"); + assertEquals(endpoint.getDefinition().getResolvedRetryDurationOnTimeout(synCtx), 90000); + } } From ea622f3d538680191ded172d8c1796ca3d278022 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Sun, 6 Oct 2024 00:21:20 +0530 Subject: [PATCH 07/27] add expression for timeout action --- .../endpoints/EndpointDefinitionFactory.java | 24 +++++++--- .../EndpointDefinitionSerializer.java | 18 ++++--- .../core/axis2/Axis2FlexibleMEPClient.java | 22 ++++----- .../synapse/endpoints/AbstractEndpoint.java | 2 +- .../apache/synapse/endpoints/EPConstants.java | 3 ++ .../synapse/endpoints/EndpointDefinition.java | 47 +++++++++++++++++++ .../synapse/endpoints/HttpEndpointTest.java | 14 ++++++ .../dynamic/DynamicEndpointTest.java | 27 +++++++++++ 8 files changed, 130 insertions(+), 27 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java index 14430d6e48..db02638b45 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java @@ -180,14 +180,24 @@ public EndpointDefinition createDefinition(OMElement elem) { 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)) { + 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"); + } } 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"); + } } } } diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java index 3db6db43f2..9ae09b0597 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java @@ -102,7 +102,7 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, element.addChild(sec); } - if (endpointDefinition.getTimeoutAction() != SynapseConstants.NONE || + if (endpointDefinition.getTimeoutAction() != SynapseConstants.NONE || endpointDefinition.getDynamicTimeoutAction() != null || endpointDefinition.getTimeoutDuration() > 0 || endpointDefinition.isDynamicTimeoutEndpoint()) { OMElement timeout = fac.createOMElement( @@ -120,13 +120,17 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, timeout.addChild(duration); } - if (endpointDefinition.getTimeoutAction() != SynapseConstants.NONE) { + if (endpointDefinition.getTimeoutAction() != SynapseConstants.NONE || endpointDefinition.getDynamicTimeoutAction() != null) { OMElement action = fac.createOMElement("responseAction", SynapseConstants.SYNAPSE_OMNAMESPACE); - if (endpointDefinition.getTimeoutAction() == SynapseConstants.DISCARD) { - action.setText("discard"); - } else if (endpointDefinition.getTimeoutAction() - == SynapseConstants.DISCARD_AND_FAULT) { - action.setText("fault"); + if (endpointDefinition.isTimeoutActionDynamic()) { + action.setText('{' + endpointDefinition.getDynamicTimeoutAction().getExpression() + '}'); + } else { + if (endpointDefinition.getTimeoutAction() == SynapseConstants.DISCARD) { + action.setText("discard"); + } else if (endpointDefinition.getTimeoutAction() + == SynapseConstants.DISCARD_AND_FAULT) { + action.setText("fault"); + } } timeout.addChild(action); } diff --git a/modules/core/src/main/java/org/apache/synapse/core/axis2/Axis2FlexibleMEPClient.java b/modules/core/src/main/java/org/apache/synapse/core/axis2/Axis2FlexibleMEPClient.java index 03abfd2a4e..29d7000b04 100644 --- a/modules/core/src/main/java/org/apache/synapse/core/axis2/Axis2FlexibleMEPClient.java +++ b/modules/core/src/main/java/org/apache/synapse/core/axis2/Axis2FlexibleMEPClient.java @@ -579,27 +579,25 @@ public static void send( if (endpoint != null) { // set the timeout time and the timeout action to the callback, so that the // TimeoutHandler can detect timed out callbacks and take appropriate action. + long endpointTimeout; if (!endpoint.isDynamicTimeoutEndpoint()) { - long endpointTimeout = endpoint.getEffectiveTimeout(); - callback.setTimeout(endpointTimeout); - callback.setTimeOutAction(endpoint.getTimeoutAction()); - callback.setTimeoutType(endpoint.getEndpointTimeoutType()); + endpointTimeout = endpoint.getEffectiveTimeout(); if (log.isDebugEnabled()) { log.debug("Setting Timeout for endpoint : " + - getEndpointLogMessage(synapseOutMessageContext, axisOutMsgCtx) + - " to static timeout value : " + endpointTimeout); + getEndpointLogMessage(synapseOutMessageContext, axisOutMsgCtx) + + " to static timeout value : " + endpointTimeout); } } else { - long endpointTimeout = endpoint.evaluateDynamicEndpointTimeout(synapseOutMessageContext); - callback.setTimeout(endpointTimeout); - callback.setTimeOutAction(endpoint.getTimeoutAction()); - callback.setTimeoutType(endpoint.getEndpointTimeoutType()); + endpointTimeout = endpoint.evaluateDynamicEndpointTimeout(synapseOutMessageContext); if (log.isDebugEnabled()) { log.debug("Setting Timeout for endpoint : " + - getEndpointLogMessage(synapseOutMessageContext, axisOutMsgCtx) + - " to dynamic timeout value : " + endpointTimeout); + getEndpointLogMessage(synapseOutMessageContext, axisOutMsgCtx) + + " to dynamic timeout value : " + endpointTimeout); } } + callback.setTimeout(endpointTimeout); + callback.setTimeOutAction(endpoint.getResolvedTimeoutAction(synapseOutMessageContext)); + callback.setTimeoutType(endpoint.getEndpointTimeoutType()); } else { long globalTimeout = synapseOutMessageContext.getEnvironment().getGlobalTimeout(); callback.setTimeout(globalTimeout); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java index 2ba5b72fc7..0546d3b63d 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java @@ -575,7 +575,7 @@ protected boolean isSuspendFault(MessageContext synCtx) { */ public void onFault(MessageContext synCtx) { EndpointDefinition endpointDefinition = getDefinition(); - if (endpointDefinition != null && endpointDefinition.getTimeoutAction() == SynapseConstants.DISCARD) { + if (endpointDefinition != null && endpointDefinition.getResolvedTimeoutAction(synCtx) == SynapseConstants.DISCARD) { log.info("Ignoring fault handlers since the timeout action is set to DISCARD"); } else { logSetter(); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EPConstants.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EPConstants.java index 3abd5ab034..422b1eb33b 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EPConstants.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EPConstants.java @@ -25,4 +25,7 @@ public class EPConstants { public static final int SUPER_TENANT_ID = -1234; public static final String TENANT_INFO_ID = "tenant.info.id"; public static final String LOCAL_TRANSPORT_IDENTIFIER = "local://"; + + public static final String DISCARD = "discard"; + public static final String FAULT = "fault"; } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java index 7ce64d44e5..d2135e183d 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java @@ -158,6 +158,8 @@ public class EndpointDefinition implements AspectConfigurable { * action to perform when a timeout occurs (NONE | DISCARD | DISCARD_AND_FAULT) * */ private int timeoutAction = SynapseConstants.NONE; + /** The expression to evaluate dynamic timeout action */ + private SynapsePath dynamicTimeoutAction = null; /** The initial suspend duration when an endpoint is marked inactive */ private long initialSuspendDuration = -1; @@ -586,6 +588,51 @@ public void setTimeoutAction(int timeoutAction) { this.timeoutAction = timeoutAction; } + public SynapsePath getDynamicTimeoutAction() { + + return dynamicTimeoutAction; + } + + public void setDynamicTimeoutAction(SynapsePath dynamicTimeoutAction) { + + this.dynamicTimeoutAction = dynamicTimeoutAction; + } + + public boolean isTimeoutActionDynamic() { + + return dynamicTimeoutAction != null; + } + + public int evaluateDynamicTimeoutAction(MessageContext synCtx) { + + int result = timeoutAction; + try { + String timeoutActionStr = dynamicTimeoutAction.stringValueOf(synCtx); + if (timeoutActionStr != null) { + if (EPConstants.DISCARD.equalsIgnoreCase(timeoutActionStr)) { + result = SynapseConstants.DISCARD; + } else if (EPConstants.FAULT.equalsIgnoreCase(timeoutActionStr)) { + result = SynapseConstants.DISCARD_AND_FAULT; + } else { + log.warn("Error while evaluating dynamic endpoint timeout action."); + } + } else { + log.warn("Error while evaluating dynamic endpoint timeout action."); + } + } catch (NumberFormatException e) { + log.warn("Error while evaluating dynamic endpoint timeout action."); + } + return result; + } + + public int getResolvedTimeoutAction(MessageContext messageContext) { + + if (isTimeoutActionDynamic()) { + return evaluateDynamicTimeoutAction(messageContext); + } + return timeoutAction; + } + public String getFormat() { return format; } diff --git a/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java b/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java index 4134977bfc..16481a08d9 100644 --- a/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java +++ b/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java @@ -153,6 +153,20 @@ public void testQueryParamsWithLegacyEncoding() throws AxisFault, XMLStreamExcep } + @Test + public void testSetDynamicTimeoutAction() throws XMLStreamException, AxisFault { + + HTTPEndpointFactory httpEndpointFactory = new HTTPEndpointFactory(); + OMElement omElement = AXIOMUtil.stringToOM( + "12{$ctx:timeoutAction}"); + EndpointDefinition ep = httpEndpointFactory.createEndpointDefinition(omElement); + HTTPEndpoint httpEndpoint = new HTTPEndpoint(); + httpEndpoint.setDefinition(ep); + MessageContext messageContext = createMessageContext(); + messageContext.setProperty("timeoutAction", "discard"); + Assert.assertEquals(httpEndpoint.getDefinition().getResolvedTimeoutAction(messageContext), SynapseConstants.DISCARD); + } + @Test public void testSetSuspendOnFailureInitialDuration() throws XMLStreamException, AxisFault { diff --git a/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java b/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java index 36fa58fa0b..4d454b3834 100644 --- a/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java +++ b/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java @@ -18,6 +18,7 @@ import junit.framework.TestCase; import org.apache.synapse.MessageContext; +import org.apache.synapse.SynapseConstants; import org.apache.synapse.TestMessageContext; import org.apache.synapse.endpoints.AbstractEndpoint; import org.apache.synapse.endpoints.AddressEndpoint; @@ -97,4 +98,30 @@ public void testContextPropertiesForRetryDurationOnTimeout() throws Exception { synCtx.setProperty("retryDurationOnTimeout", "90000"); assertEquals(endpoint.getDefinition().getResolvedRetryDurationOnTimeout(synCtx), 90000); } + + public void testContextPropertiesForTimeoutActionFault() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:timeoutAction"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicTimeoutAction(xpath); + MessageContext synCtx = new TestMessageContext(); + synCtx.setProperty("timeoutAction", "fault"); + assertEquals(endpoint.getDefinition().getResolvedTimeoutAction(synCtx), SynapseConstants.DISCARD_AND_FAULT); + } + + public void testContextPropertiesForTimeoutActionDiscard() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:timeoutAction"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicTimeoutAction(xpath); + MessageContext synCtx = new TestMessageContext(); + synCtx.setProperty("timeoutAction", "discard"); + assertEquals(endpoint.getDefinition().getResolvedTimeoutAction(synCtx), SynapseConstants.DISCARD); + } + + } From 9e371abffdd25bef88bfda61943e36bb98f90b93 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Sun, 6 Oct 2024 00:21:49 +0530 Subject: [PATCH 08/27] fix expression check pattern --- .../synapse/config/xml/endpoints/EndpointDefinitionFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java index db02638b45..69d628f145 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java @@ -424,7 +424,7 @@ public EndpointDefinition createDefinition(OMElement elem) { } private boolean isExpression(String expressionStr) { - Pattern pattern = Pattern.compile("\\{.*\\}}"); + Pattern pattern = Pattern.compile("\\{.*\\}"); if (pattern.matcher(expressionStr).matches()) { return true; } From 71786231883bbe0289145e9978a1c24e6bbef001 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Mon, 7 Oct 2024 14:42:36 +0530 Subject: [PATCH 09/27] add expression for suspend error codes without setSuspendStateProperties --- .../endpoints/EndpointDefinitionFactory.java | 26 ++++++++---- .../EndpointDefinitionSerializer.java | 12 ++++-- .../synapse/endpoints/AbstractEndpoint.java | 6 +-- .../synapse/endpoints/EndpointDefinition.java | 42 +++++++++++++++++++ 4 files changed, 71 insertions(+), 15 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java index 69d628f145..425b8cbddb 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java @@ -295,15 +295,25 @@ 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(); + String trimmedSuspendCodes = suspendCodes.getText().trim(); + if (isExpression(trimmedSuspendCodes)) { 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 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"); + } + } 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); + } } } } diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java index 9ae09b0597..c9f8db7861 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java @@ -137,18 +137,22 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, } if (endpointDefinition.getInitialSuspendDuration() != -1 || endpointDefinition.isInitialSuspendDurationDynamic() || - !endpointDefinition.getSuspendErrorCodes().isEmpty()) { + !endpointDefinition.getSuspendErrorCodes().isEmpty() || endpointDefinition.isSuspendErrorCodesDynamic()) { OMElement suspendOnFailure = fac.createOMElement( org.apache.synapse.config.xml.XMLConfigConstants.SUSPEND_ON_FAILURE, SynapseConstants.SYNAPSE_OMNAMESPACE); - if (!endpointDefinition.getSuspendErrorCodes().isEmpty()) { + if (!endpointDefinition.getSuspendErrorCodes().isEmpty() || endpointDefinition.isSuspendErrorCodesDynamic()) { OMElement errorCodes = fac.createOMElement( org.apache.synapse.config.xml.XMLConfigConstants.ERROR_CODES, SynapseConstants.SYNAPSE_OMNAMESPACE); - errorCodes.setText(endpointDefinition.getSuspendErrorCodes(). - toString().replaceAll("[\\[\\] ]", "")); + if (endpointDefinition.isSuspendErrorCodesDynamic()) { + errorCodes.setText('{' + endpointDefinition.getDynamicSuspendErrorCodes().getExpression() + '}'); + } else { + errorCodes.setText(endpointDefinition.getSuspendErrorCodes(). + toString().replaceAll("[\\[\\] ]", "")); + } suspendOnFailure.addChild(errorCodes); } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java index 0546d3b63d..8f8c3dad90 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java @@ -542,7 +542,7 @@ protected boolean isRetry(MessageContext synCtx) { protected boolean isSuspendFault(MessageContext synCtx) { Integer errorCode = (Integer) synCtx.getProperty(SynapseConstants.ERROR_CODE); if (errorCode != null) { - if (definition.getSuspendErrorCodes().isEmpty()) { + if (definition.getResolvedSuspendErrorCodes(synCtx).isEmpty()) { // if suspend codes are not defined, any error will be fatal for the endpoint if (log.isDebugEnabled()) { log.debug(this.toString() + " encountered a fatal error : " + errorCode); @@ -550,10 +550,10 @@ protected boolean isSuspendFault(MessageContext synCtx) { return true; } else { - if (definition.getSuspendErrorCodes().contains(errorCode)) { + if (definition.getResolvedSuspendErrorCodes(synCtx).contains(errorCode)) { if (log.isDebugEnabled()) { log.debug("Encountered a suspend error : " + errorCode + - " defined suspend codes are : " + definition.getSuspendErrorCodes()); + " defined suspend codes are : " + definition.getResolvedSuspendErrorCodes(synCtx)); } return true; } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java index d2135e183d..6c68a0fd3b 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java @@ -177,6 +177,8 @@ public class EndpointDefinition implements AspectConfigurable { private SynapsePath dynamicSuspendMaximumDuration = null; /** A list of error codes, which directly puts an endpoint into suspend mode */ private final List suspendErrorCodes = new ArrayList(); + /** The expression to evaluate dynamic suspend error codes */ + private SynapsePath dynamicSuspendErrorCodes = null; /** No of retries to attempt on timeout, before an endpoint is makred inactive */ private int retriesOnTimeoutBeforeSuspend = 0; @@ -930,6 +932,46 @@ public void addSuspendErrorCode(int code) { suspendErrorCodes.add(code); } + public SynapsePath getDynamicSuspendErrorCodes() { + + return dynamicSuspendErrorCodes; + } + + public void setDynamicSuspendErrorCodes(SynapsePath dynamicSuspendErrorCodes) { + + this.dynamicSuspendErrorCodes = dynamicSuspendErrorCodes; + } + + public boolean isSuspendErrorCodesDynamic() { + + return dynamicSuspendErrorCodes != null; + } + + public List evaluateDynamicSuspendErrorCodes(MessageContext messageContext) { + + List result = suspendErrorCodes; + try { + String stringValue = dynamicSuspendErrorCodes.stringValueOf(messageContext); + if (stringValue != null) { + String[] errorCodes = stringValue.split(","); + result = new ArrayList(); + for (String errorCode : errorCodes) { + result.add(Integer.parseInt(errorCode.trim())); + } + } + } catch (NumberFormatException e) { + log.warn("Error while evaluating dynamic endpoint timeout expression."); + } + return result; + } + + public List getResolvedSuspendErrorCodes(MessageContext messageContext) { + if (isSuspendErrorCodesDynamic()) { + return evaluateDynamicSuspendErrorCodes(messageContext); + } + return suspendErrorCodes; + } + public void addTimeoutErrorCode(int code) { timeoutErrorCodes.add(code); } From 67e5592c47c3bb1a65c4a2b80c1cc1ed1fb15fc7 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Mon, 7 Oct 2024 15:21:14 +0530 Subject: [PATCH 10/27] add expression for timeout error codes without setTimeoutStateProperties --- .../endpoints/EndpointDefinitionFactory.java | 25 +++++++---- .../EndpointDefinitionSerializer.java | 12 ++++-- .../synapse/endpoints/AbstractEndpoint.java | 6 +-- .../synapse/endpoints/EndpointDefinition.java | 42 +++++++++++++++++++ 4 files changed, 71 insertions(+), 14 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java index 425b8cbddb..1ccf127916 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java @@ -212,14 +212,25 @@ 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(); + String trimmedTimeoutCodes = timeoutCodes.getText().trim(); + if (isExpression(trimmedTimeoutCodes)) { 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 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"); + } + } else { + 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); + } } } } diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java index c9f8db7861..1b41e6e280 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java @@ -197,18 +197,22 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, } if (endpointDefinition.getRetryDurationOnTimeout() > 0 || endpointDefinition.isRetryDurationOnTimeoutDynamic() || - !endpointDefinition.getTimeoutErrorCodes().isEmpty()) { + !endpointDefinition.getTimeoutErrorCodes().isEmpty() || endpointDefinition.isTimeoutErrorCodesDynamic()) { OMElement markAsTimedout = fac.createOMElement( org.apache.synapse.config.xml.XMLConfigConstants.MARK_FOR_SUSPENSION, SynapseConstants.SYNAPSE_OMNAMESPACE); - if (!endpointDefinition.getTimeoutErrorCodes().isEmpty()) { + if (!endpointDefinition.getTimeoutErrorCodes().isEmpty() || endpointDefinition.isTimeoutErrorCodesDynamic()) { OMElement errorCodes = fac.createOMElement( org.apache.synapse.config.xml.XMLConfigConstants.ERROR_CODES, SynapseConstants.SYNAPSE_OMNAMESPACE); - errorCodes.setText(endpointDefinition.getTimeoutErrorCodes(). - toString().replaceAll("[\\[\\] ]", "")); + if (endpointDefinition.isTimeoutErrorCodesDynamic()) { + errorCodes.setText('{' + endpointDefinition.getDynamicTimeoutErrorCodes().getExpression() + '}'); + } else { + errorCodes.setText(endpointDefinition.getTimeoutErrorCodes(). + toString().replaceAll("[\\[\\] ]", "")); + } markAsTimedout.addChild(errorCodes); } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java index 8f8c3dad90..a19a901c76 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java @@ -468,7 +468,7 @@ protected boolean isTimeout(MessageContext synCtx) { } } if (errorCode != null) { - if (definition.getTimeoutErrorCodes().isEmpty()) { + if (definition.getResolvedTimeoutErrorCodes(synCtx).isEmpty()) { // if timeout codes are not defined, assume only HTTP timeout and connection close boolean isTimeout = SynapseConstants.NHTTP_CONNECTION_TIMEOUT == errorCode; boolean isClosed = SynapseConstants.NHTTP_CONNECTION_CLOSED == errorCode; @@ -482,11 +482,11 @@ protected boolean isTimeout(MessageContext synCtx) { return true; } } else { - if (definition.getTimeoutErrorCodes().contains(errorCode)) { + if (definition.getResolvedTimeoutErrorCodes(synCtx).contains(errorCode)) { if (log.isDebugEnabled()) { log.debug("Encountered a mark for suspension error : " + errorCode + " defined " + "error codes are : " - + definition.getTimeoutErrorCodes()); + + definition.getResolvedTimeoutErrorCodes(synCtx)); } return true; } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java index 6c68a0fd3b..6687b2c0fd 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java @@ -190,6 +190,8 @@ public class EndpointDefinition implements AspectConfigurable { private SynapsePath dynamicRetryDurationOnTimeout = null; /** A list of error codes which puts the endpoint into timeout mode */ private final List timeoutErrorCodes = new ArrayList(); + /** The expression to evaluate dynamic timeout error codes */ + private SynapsePath dynamicTimeoutErrorCodes = null; private AspectConfiguration aspectConfiguration; @@ -976,6 +978,46 @@ public void addTimeoutErrorCode(int code) { timeoutErrorCodes.add(code); } + public SynapsePath getDynamicTimeoutErrorCodes() { + + return dynamicTimeoutErrorCodes; + } + + public void setDynamicTimeoutErrorCodes(SynapsePath dynamicTimeoutErrorCodes) { + + this.dynamicTimeoutErrorCodes = dynamicTimeoutErrorCodes; + } + + public boolean isTimeoutErrorCodesDynamic() { + + return dynamicTimeoutErrorCodes != null; + } + + public List evaluateDynamicTimeoutErrorCodes(MessageContext messageContext) { + + List result = timeoutErrorCodes; + try { + String stringValue = dynamicTimeoutErrorCodes.stringValueOf(messageContext); + if (stringValue != null) { + String[] errorCodes = stringValue.split(","); + result = new ArrayList(); + for (String errorCode : errorCodes) { + result.add(Integer.parseInt(errorCode.trim())); + } + } + } catch (NumberFormatException e) { + log.warn("Error while evaluating dynamic endpoint timeout expression."); + } + return result; + } + + public List getResolvedTimeoutErrorCodes(MessageContext messageContext) { + if (isTimeoutErrorCodesDynamic()) { + return evaluateDynamicTimeoutErrorCodes(messageContext); + } + return timeoutErrorCodes; + } + public void addRetryDisabledErrorCode(int code) { retryDisabledErrorCodes.add(code); } From 65fd4d258dc39084ee154414d4a8a916a66f65b2 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Mon, 7 Oct 2024 17:37:29 +0530 Subject: [PATCH 11/27] add tests for error codes --- .../synapse/endpoints/HttpEndpointTest.java | 56 ++++++++++++++++ .../dynamic/DynamicEndpointTest.java | 65 +++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java b/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java index 16481a08d9..d99d6ee521 100644 --- a/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java +++ b/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java @@ -37,11 +37,15 @@ import org.apache.synapse.core.SynapseEnvironment; import org.apache.synapse.core.axis2.Axis2MessageContext; import org.apache.synapse.core.axis2.Axis2SynapseEnvironment; +import org.json.HTTP; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; +import java.util.ArrayList; +import java.util.List; + import javax.xml.stream.XMLStreamException; /** @@ -178,6 +182,58 @@ public void testSetSuspendOnFailureInitialDuration() throws XMLStreamException, Assert.assertEquals(ep1.getInitialSuspendDuration(), 1000); } + @Test + public void testSetSuspendErrorCodes() throws XMLStreamException, AxisFault { + + HTTPEndpointFactory httpEndpointFactory = new HTTPEndpointFactory(); + OMElement omElement = AXIOMUtil.stringToOM( + "\n" + + " \n" + + " {$ctx:suspendErrorCodes}\n" + + " -1\n" + + " 1\n" + + " \n" + + " \n" + + " 0\n" + + " \n" + + " "); + EndpointDefinition ep = httpEndpointFactory.createEndpointDefinition(omElement); + HTTPEndpoint httpEndpoint = new HTTPEndpoint(); + httpEndpoint.setDefinition(ep); + MessageContext messageContext = createMessageContext(); + messageContext.setProperty("suspendErrorCodes", "101503,101504"); + + List actualSuspendErrorCodes = new ArrayList<>(); + actualSuspendErrorCodes.add(101503); + actualSuspendErrorCodes.add(101504); + List expectedSuspendErrorCodes = httpEndpoint.getDefinition().getResolvedSuspendErrorCodes(messageContext); + Assert.assertTrue(expectedSuspendErrorCodes.equals(actualSuspendErrorCodes)); + } + + @Test + public void testSetTimeoutErrorCodes() throws XMLStreamException, AxisFault { + + HTTPEndpointFactory httpEndpointFactory = new HTTPEndpointFactory(); + OMElement omElement = AXIOMUtil.stringToOM( + "\n" + + " \n" + + " {$ctx:timeoutErrorCodes}\n" + + " 10\n" + + " \n" + + " "); + EndpointDefinition ep = httpEndpointFactory.createEndpointDefinition(omElement); + HTTPEndpoint httpEndpoint = new HTTPEndpoint(); + httpEndpoint.setDefinition(ep); + MessageContext messageContext = createMessageContext(); + messageContext.setProperty("timeoutErrorCodes", "101503,101504"); + + List actualTimeoutErrorCodes = new ArrayList<>(); + actualTimeoutErrorCodes.add(101503); + actualTimeoutErrorCodes.add(101504); + List expectedTimeoutErrorCodes = httpEndpoint.getDefinition().getResolvedTimeoutErrorCodes(messageContext); + Assert.assertTrue(expectedTimeoutErrorCodes.equals(actualTimeoutErrorCodes)); + } + /** * Create a mock SynapseEnvironment object * diff --git a/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java b/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java index 4d454b3834..6271e4dc01 100644 --- a/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java +++ b/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java @@ -25,6 +25,9 @@ import org.apache.synapse.endpoints.EndpointDefinition; import org.apache.synapse.util.xpath.SynapseXPath; +import java.util.ArrayList; +import java.util.List; + public class DynamicEndpointTest extends TestCase { public void testContextProperties() throws Exception { @@ -123,5 +126,67 @@ public void testContextPropertiesForTimeoutActionDiscard() throws Exception { assertEquals(endpoint.getDefinition().getResolvedTimeoutAction(synCtx), SynapseConstants.DISCARD); } + public void testContextPropertiesForSuspendErrorCodes() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:suspendErrorCodes"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicSuspendErrorCodes(xpath); + MessageContext synCtx = new TestMessageContext(); + synCtx.setProperty("suspendErrorCodes", "101503,101504"); + + List actualSuspendErrorCodes = new ArrayList<>(); + actualSuspendErrorCodes.add(101503); + actualSuspendErrorCodes.add(101504); + List expectedSuspendErrorCodes = endpoint.getDefinition().getResolvedSuspendErrorCodes(synCtx); + assertTrue(expectedSuspendErrorCodes.equals(actualSuspendErrorCodes)); + } + + public void testContextPropertiesForEmptySuspendErrorCodes() throws Exception { + SynapseXPath xpath = new SynapseXPath("$ctx:suspendErrorCodes"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicSuspendErrorCodes(xpath); + MessageContext synCtx = new TestMessageContext(); + synCtx.setProperty("suspendErrorCodes", ""); + + List actualSuspendErrorCodes = new ArrayList<>(); + List expectedSuspendErrorCodes = endpoint.getDefinition().getResolvedSuspendErrorCodes(synCtx); + assertTrue(expectedSuspendErrorCodes.equals(actualSuspendErrorCodes)); + } + + public void testContextPropertiesForTimeoutErrorCodes() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:timeoutErrorCodes"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicTimeoutErrorCodes(xpath); + MessageContext synCtx = new TestMessageContext(); + synCtx.setProperty("timeoutErrorCodes", "101503,101504"); + + List actualTimeoutErrorCodes = new ArrayList<>(); + actualTimeoutErrorCodes.add(101503); + actualTimeoutErrorCodes.add(101504); + List expectedTimeoutErrorCodes = endpoint.getDefinition().getResolvedTimeoutErrorCodes(synCtx); + assertTrue(expectedTimeoutErrorCodes.equals(actualTimeoutErrorCodes)); + } + + public void testContextPropertiesForEmptyTimeoutErrorCodes() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:timeoutErrorCodes"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicTimeoutErrorCodes(xpath); + MessageContext synCtx = new TestMessageContext(); + synCtx.setProperty("timeoutErrorCodes", ""); + + List actualTimeoutErrorCodes = new ArrayList<>(); + List expectedTimeoutErrorCodes = endpoint.getDefinition().getResolvedTimeoutErrorCodes(synCtx); + assertTrue(expectedTimeoutErrorCodes.equals(actualTimeoutErrorCodes)); + } } From e19e5d409eb3bdb49d09e4e084b4adbad6e822a1 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Tue, 8 Oct 2024 14:48:29 +0530 Subject: [PATCH 12/27] parse message context for createJsonRepresentation --- .../management/helpers/SpanTagger.java | 2 +- .../synapse/endpoints/AbstractEndpoint.java | 29 +++++++++---------- .../synapse/endpoints/AddressEndpoint.java | 4 +-- .../synapse/endpoints/ClassEndpoint.java | 2 +- .../synapse/endpoints/DefaultEndpoint.java | 4 +-- .../apache/synapse/endpoints/Endpoint.java | 2 +- .../synapse/endpoints/FailoverEndpoint.java | 2 +- .../synapse/endpoints/HTTPEndpoint.java | 5 ++-- .../synapse/endpoints/IndirectEndpoint.java | 2 +- .../endpoints/LoadbalanceEndpoint.java | 2 +- .../endpoints/RecipientListEndpoint.java | 2 +- .../synapse/endpoints/ResolvingEndpoint.java | 2 +- .../synapse/endpoints/TemplateEndpoint.java | 2 +- .../synapse/endpoints/WSDLEndpoint.java | 4 +-- .../xml/endpoints/CustomClassEndpoint.java | 2 +- 15 files changed, 33 insertions(+), 33 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/management/helpers/SpanTagger.java b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/management/helpers/SpanTagger.java index 1e1fe707fa..4cf0dd5461 100644 --- a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/management/helpers/SpanTagger.java +++ b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/management/helpers/SpanTagger.java @@ -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, diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java index a19a901c76..bb89bad0a4 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java @@ -947,35 +947,35 @@ public void setComponentStatisticsId(ArtifactHolder holder) { /** * Set advanced properties of the endpoint to json object. */ - protected void setAdvancedProperties() { + protected void setAdvancedProperties(MessageContext messageContext) { JSONObject advancedProps = new JSONObject(); endpointJson.put("advanced", advancedProps); - setSuspendStateProperties(getDefinition(), advancedProps); - setTimeoutStateProperties(getDefinition(), advancedProps); + setSuspendStateProperties(getDefinition(), advancedProps, messageContext); + setTimeoutStateProperties(getDefinition(), advancedProps, messageContext); } /** * Set time-out state properties of the endpoint to json object. */ - private void setTimeoutStateProperties(EndpointDefinition definition, JSONObject advancedProps) { + private void setTimeoutStateProperties(EndpointDefinition definition, JSONObject advancedProps, MessageContext messageContext) { JSONObject timeoutStateProps = new JSONObject(); advancedProps.put("timeoutState", timeoutStateProps); - timeoutStateProps.put("errorCodes", definition.getTimeoutErrorCodes()); - timeoutStateProps.put("reties", definition.getRetriesOnTimeoutBeforeSuspend()); + timeoutStateProps.put("errorCodes", definition.getResolvedTimeoutErrorCodes(messageContext)); + timeoutStateProps.put("reties", definition.getResolvedRetriesOnTimeoutBeforeSuspend(messageContext)); } /** * Set suspend state properties of the endpoint to json object. */ - private void setSuspendStateProperties(EndpointDefinition definition, JSONObject advancedProps) { + private void setSuspendStateProperties(EndpointDefinition definition, JSONObject advancedProps, MessageContext messageContext) { JSONObject suspendStatePros = new JSONObject(); advancedProps.put("suspendState", suspendStatePros); - suspendStatePros.put("errorCodes", definition.getSuspendErrorCodes()); - suspendStatePros.put("maxDuration", definition.getSuspendMaximumDuration()); - suspendStatePros.put("initialDuration", definition.getInitialSuspendDuration()); + suspendStatePros.put("errorCodes", definition.getResolvedSuspendErrorCodes(messageContext)); + suspendStatePros.put("maxDuration", definition.getResolvedSuspendMaximumDuration(messageContext)); + suspendStatePros.put("initialDuration", definition.getResolvedInitialSuspendDuration(messageContext)); } protected JSONArray getEndpointChildrenAsJson(List children) { @@ -983,21 +983,20 @@ protected JSONArray getEndpointChildrenAsJson(List children) { if (children != null && children.size() != 0) { for (Endpoint child : children) { - childrenJsonList.put(child.getJsonRepresentation()); + childrenJsonList.put(child.getJsonRepresentation(null)); } } return childrenJsonList; } @Override - public JSONObject getJsonRepresentation() { + public JSONObject getJsonRepresentation(MessageContext messageContext) { if (endpointJson == null) { - createJsonRepresentation(); + createJsonRepresentation(messageContext); } return endpointJson; } - protected abstract void createJsonRepresentation(); - + protected abstract void createJsonRepresentation(MessageContext synCtx); } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/AddressEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/AddressEndpoint.java index 2808ce7f7f..9eeaffc594 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/AddressEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/AddressEndpoint.java @@ -58,12 +58,12 @@ public void onSuccess() { } } - protected void createJsonRepresentation() { + protected void createJsonRepresentation(MessageContext messageContext) { endpointJson = new JSONObject(); endpointJson.put("name", getName()); endpointJson.put("type", "Address Endpoint"); endpointJson.put("address", getDefinition().getAddress()); - setAdvancedProperties(); + setAdvancedProperties(messageContext); } public void send(MessageContext synCtx) { diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/ClassEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/ClassEndpoint.java index 5f610213de..7387116f6d 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/ClassEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/ClassEndpoint.java @@ -83,7 +83,7 @@ public void send(MessageContext synCtx) { } @Override - protected void createJsonRepresentation() { + protected void createJsonRepresentation(MessageContext messageContext) { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); endpointJson.put(TYPE_JSON_ATT, "Class Endpoint"); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/DefaultEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/DefaultEndpoint.java index a4fd4e2e91..48000cf39b 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/DefaultEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/DefaultEndpoint.java @@ -66,12 +66,12 @@ public void onSuccess() { } @Override - protected void createJsonRepresentation() { + protected void createJsonRepresentation(MessageContext messageContext) { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); endpointJson.put(TYPE_JSON_ATT, "Default Endpoint"); endpointJson.put(CHILDREN_JSON_ATT, getEndpointChildrenAsJson(getChildren())); - setAdvancedProperties(); + setAdvancedProperties(messageContext); } public void send(MessageContext synCtx) { diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/Endpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/Endpoint.java index fc5ef86c8b..31144946d6 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/Endpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/Endpoint.java @@ -156,6 +156,6 @@ public interface Endpoint extends ManagedLifecycle, SynapseArtifact, Nameable { * * @return JSONObject */ - public JSONObject getJsonRepresentation(); + public JSONObject getJsonRepresentation(MessageContext messageContext); } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/FailoverEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/FailoverEndpoint.java index bd086e5132..f24c2309e4 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/FailoverEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/FailoverEndpoint.java @@ -294,7 +294,7 @@ public boolean readyToSend() { return false; } - protected void createJsonRepresentation() { + protected void createJsonRepresentation(MessageContext messageContext) { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); endpointJson.put(TYPE_JSON_ATT, "Failover Endpoint"); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/HTTPEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/HTTPEndpoint.java index ce53631fee..cc7410569d 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/HTTPEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/HTTPEndpoint.java @@ -87,14 +87,15 @@ public void onSuccess() { } } - protected void createJsonRepresentation() { + @Override + protected void createJsonRepresentation(MessageContext messageContext) { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); endpointJson.put(TYPE_JSON_ATT, "HTTP Endpoint"); endpointJson.put("method", getHttpMethod()); endpointJson.put("uriTemplate", getUriTemplate().expand()); endpointJson.put("errorHandler", getErrorHandler()); - setAdvancedProperties(); + setAdvancedProperties(messageContext); } public void send(MessageContext synCtx) { diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/IndirectEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/IndirectEndpoint.java index fb7c459b27..0d509c98ec 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/IndirectEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/IndirectEndpoint.java @@ -61,7 +61,7 @@ public void send(MessageContext synCtx) { } @Override - protected void createJsonRepresentation() { + protected void createJsonRepresentation(MessageContext messageContext) { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); endpointJson.put(TYPE_JSON_ATT, "Indirect Endpoint"); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/LoadbalanceEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/LoadbalanceEndpoint.java index aae31bb0f1..ab64a2172d 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/LoadbalanceEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/LoadbalanceEndpoint.java @@ -151,7 +151,7 @@ public boolean isInitialized() { return loadBalanceEPInitialized; } - protected void createJsonRepresentation() { + protected void createJsonRepresentation(MessageContext messageContext) { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/RecipientListEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/RecipientListEndpoint.java index 966185f4ea..a5f0685099 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/RecipientListEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/RecipientListEndpoint.java @@ -97,7 +97,7 @@ public void destroy() { } @Override - protected void createJsonRepresentation() { + protected void createJsonRepresentation(MessageContext messageContext) { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/ResolvingEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/ResolvingEndpoint.java index 861c0bb067..618a0c5c5e 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/ResolvingEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/ResolvingEndpoint.java @@ -61,7 +61,7 @@ public void send(MessageContext synCtx) { } @Override - protected void createJsonRepresentation() { + protected void createJsonRepresentation(MessageContext messageContext) { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); endpointJson.put(TYPE_JSON_ATT, "Resolving Endpoint"); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java index f24d50814c..47fc6820c8 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java @@ -63,7 +63,7 @@ public void send(MessageContext synCtx) { } @Override - protected void createJsonRepresentation() { + protected void createJsonRepresentation(MessageContext messageContext) { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/WSDLEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/WSDLEndpoint.java index dc8975cdf3..cf7c865ced 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/WSDLEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/WSDLEndpoint.java @@ -85,7 +85,7 @@ public void onSuccess() { } @Override - protected void createJsonRepresentation() { + protected void createJsonRepresentation(MessageContext messageContext) { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); @@ -93,7 +93,7 @@ protected void createJsonRepresentation() { endpointJson.put("wsdlUri", getWsdlURI()); endpointJson.put("serviceName", getServiceName()); endpointJson.put("portName", getPortName()); - setAdvancedProperties(); + setAdvancedProperties(messageContext); } public void send(MessageContext synCtx) { diff --git a/modules/core/src/test/java/org/apache/synapse/config/xml/endpoints/CustomClassEndpoint.java b/modules/core/src/test/java/org/apache/synapse/config/xml/endpoints/CustomClassEndpoint.java index 730b44664f..8605f85417 100644 --- a/modules/core/src/test/java/org/apache/synapse/config/xml/endpoints/CustomClassEndpoint.java +++ b/modules/core/src/test/java/org/apache/synapse/config/xml/endpoints/CustomClassEndpoint.java @@ -20,7 +20,7 @@ public void onSuccess() { } @Override - protected void createJsonRepresentation() { + protected void createJsonRepresentation(MessageContext messageContext) { //TODO } From 81b9899020b9edaa12b56b04a187c0913a22d76f Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Mon, 25 Nov 2024 14:37:26 +0530 Subject: [PATCH 13/27] handle timeout action value "never" --- .../apache/synapse/endpoints/EPConstants.java | 1 + .../synapse/endpoints/EndpointDefinition.java | 2 ++ .../dynamic/DynamicEndpointTest.java | 23 +++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EPConstants.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EPConstants.java index 422b1eb33b..5f3896682f 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EPConstants.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EPConstants.java @@ -28,4 +28,5 @@ public class EPConstants { public static final String DISCARD = "discard"; public static final String FAULT = "fault"; + public static final String NEVER = "never"; } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java index 6687b2c0fd..c28364ac32 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java @@ -617,6 +617,8 @@ public int evaluateDynamicTimeoutAction(MessageContext synCtx) { result = SynapseConstants.DISCARD; } else if (EPConstants.FAULT.equalsIgnoreCase(timeoutActionStr)) { result = SynapseConstants.DISCARD_AND_FAULT; + } else if (EPConstants.NEVER.equalsIgnoreCase(timeoutActionStr)) { + result = SynapseConstants.NONE; } else { log.warn("Error while evaluating dynamic endpoint timeout action."); } diff --git a/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java b/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java index 6271e4dc01..bb0fcb8a4a 100644 --- a/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java +++ b/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java @@ -126,6 +126,29 @@ public void testContextPropertiesForTimeoutActionDiscard() throws Exception { assertEquals(endpoint.getDefinition().getResolvedTimeoutAction(synCtx), SynapseConstants.DISCARD); } + public void testContextPropertiesForNoTimeoutAction() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:timeoutAction"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicTimeoutAction(xpath); + MessageContext synCtx = new TestMessageContext(); + assertEquals(endpoint.getDefinition().getResolvedTimeoutAction(synCtx), SynapseConstants.NONE); + } + + public void testContextPropertiesForTimeoutActionNever() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:timeoutAction"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicTimeoutAction(xpath); + MessageContext synCtx = new TestMessageContext(); + synCtx.setProperty("timeoutAction", "never"); + assertEquals(endpoint.getDefinition().getResolvedTimeoutAction(synCtx), SynapseConstants.NONE); + } + public void testContextPropertiesForSuspendErrorCodes() throws Exception { SynapseXPath xpath = new SynapseXPath("$ctx:suspendErrorCodes"); From e27203b83bbf5501b89c757fbe6e2baac1833640 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Mon, 25 Nov 2024 14:45:56 +0530 Subject: [PATCH 14/27] remove log warns where unnecessary --- .../org/apache/synapse/endpoints/EndpointDefinition.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java index c28364ac32..255f94e484 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java @@ -243,8 +243,6 @@ public long evaluateDynamicEndpointTimeout(MessageContext synCtx) { if (stringValue != null) { timeoutMilliSeconds = Long.parseLong(stringValue.trim()); } else { - log.warn("Error while evaluating dynamic endpoint timeout expression." + - "Synapse global timeout is taken as effective timeout."); timeoutMilliSeconds = effectiveTimeout; } } catch (NumberFormatException e) { @@ -619,11 +617,7 @@ public int evaluateDynamicTimeoutAction(MessageContext synCtx) { result = SynapseConstants.DISCARD_AND_FAULT; } else if (EPConstants.NEVER.equalsIgnoreCase(timeoutActionStr)) { result = SynapseConstants.NONE; - } else { - log.warn("Error while evaluating dynamic endpoint timeout action."); } - } else { - log.warn("Error while evaluating dynamic endpoint timeout action."); } } catch (NumberFormatException e) { log.warn("Error while evaluating dynamic endpoint timeout action."); @@ -707,8 +701,6 @@ public long evaluateDynamicInitialSuspendDuration(MessageContext synCtx) { String stringValue = dynamicInitialSuspendDuration.stringValueOf(synCtx); if (stringValue != null) { result = Long.parseLong(stringValue.trim()); - } else { - log.warn("Error while evaluating dynamic initial suspend duration."); } } catch (NumberFormatException e) { log.warn("Error while evaluating dynamic initial suspend duration."); From 2a5cb16b2e82d0902f25da44893edd0964e48b7d Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Mon, 25 Nov 2024 15:01:59 +0530 Subject: [PATCH 15/27] add new tests handling value not defined scenarios --- .../dynamic/DynamicEndpointTest.java | 145 ++++++++++++++---- 1 file changed, 112 insertions(+), 33 deletions(-) diff --git a/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java b/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java index bb0fcb8a4a..be5c4a0949 100644 --- a/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java +++ b/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java @@ -54,6 +54,17 @@ public void testContextPropertiesForInitialSuspendDuration() throws Exception { assertEquals(endpoint.getDefinition().getResolvedInitialSuspendDuration(synCtx), 90000); } + public void testContextPropertiesForNoInitialSuspendDuration() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:initialSuspendDuration"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicInitialSuspendDuration(xpath); + MessageContext synCtx = new TestMessageContext(); + assertEquals(-1, endpoint.getDefinition().getResolvedInitialSuspendDuration(synCtx)); + } + public void testContextPropertiesForSuspendMaximumDuration() throws Exception { SynapseXPath xpath = new SynapseXPath("$ctx:suspendMaximumDuration"); @@ -66,6 +77,17 @@ public void testContextPropertiesForSuspendMaximumDuration() throws Exception { assertEquals(endpoint.getDefinition().getResolvedSuspendMaximumDuration(synCtx), 90000); } + public void testContextPropertiesForNoSuspendMaximumDuration() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:suspendMaximumDuration"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicSuspendMaximumDuration(xpath); + MessageContext synCtx = new TestMessageContext(); + assertEquals(Long.MAX_VALUE, endpoint.getDefinition().getResolvedSuspendMaximumDuration(synCtx)); + } + public void testContextPropertiesForSuspendProgressionFactor() throws Exception { SynapseXPath xpath = new SynapseXPath("$ctx:suspendProgressionFactor"); @@ -78,6 +100,17 @@ public void testContextPropertiesForSuspendProgressionFactor() throws Exception assertEquals(endpoint.getDefinition().getResolvedSuspendProgressionFactor(synCtx), 2.0f); } + public void testContextPropertiesForNoSuspendProgressionFactor() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:suspendProgressionFactor"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicSuspendProgressionFactor(xpath); + MessageContext synCtx = new TestMessageContext(); + assertEquals(1.0f, endpoint.getDefinition().getResolvedSuspendProgressionFactor(synCtx)); + } + public void testContextPropertiesForRetriesOnTimeoutBeforeSuspend() throws Exception { SynapseXPath xpath = new SynapseXPath("$ctx:retriesOnTimeoutBeforeSuspend"); @@ -90,6 +123,17 @@ public void testContextPropertiesForRetriesOnTimeoutBeforeSuspend() throws Excep assertEquals(endpoint.getDefinition().getResolvedRetriesOnTimeoutBeforeSuspend(synCtx), 3); } + public void testContextPropertiesForNoRetriesOnTimeoutBeforeSuspend() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:retriesOnTimeoutBeforeSuspend"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicRetriesOnTimeoutBeforeSuspend(xpath); + MessageContext synCtx = new TestMessageContext(); + assertEquals(0, endpoint.getDefinition().getResolvedRetriesOnTimeoutBeforeSuspend(synCtx)); + } + public void testContextPropertiesForRetryDurationOnTimeout() throws Exception { SynapseXPath xpath = new SynapseXPath("$ctx:retryDurationOnTimeout"); @@ -102,6 +146,17 @@ public void testContextPropertiesForRetryDurationOnTimeout() throws Exception { assertEquals(endpoint.getDefinition().getResolvedRetryDurationOnTimeout(synCtx), 90000); } + public void testContextPropertiesForNoRetryDurationOnTimeout() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:retryDurationOnTimeout"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicRetryDurationOnTimeout(xpath); + MessageContext synCtx = new TestMessageContext(); + assertEquals(0, endpoint.getDefinition().getResolvedRetryDurationOnTimeout(synCtx)); + } + public void testContextPropertiesForTimeoutActionFault() throws Exception { SynapseXPath xpath = new SynapseXPath("$ctx:timeoutAction"); @@ -168,48 +223,72 @@ public void testContextPropertiesForSuspendErrorCodes() throws Exception { public void testContextPropertiesForEmptySuspendErrorCodes() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:suspendErrorCodes"); - AbstractEndpoint endpoint = new AddressEndpoint(); - EndpointDefinition definition = new EndpointDefinition(); - endpoint.setDefinition(definition); - definition.setDynamicSuspendErrorCodes(xpath); - MessageContext synCtx = new TestMessageContext(); - synCtx.setProperty("suspendErrorCodes", ""); + SynapseXPath xpath = new SynapseXPath("$ctx:suspendErrorCodes"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicSuspendErrorCodes(xpath); + MessageContext synCtx = new TestMessageContext(); + synCtx.setProperty("suspendErrorCodes", ""); + + List actualSuspendErrorCodes = new ArrayList<>(); + List expectedSuspendErrorCodes = endpoint.getDefinition().getResolvedSuspendErrorCodes(synCtx); + assertTrue(expectedSuspendErrorCodes.equals(actualSuspendErrorCodes)); + } + + public void testContextPropertiesForNoSuspendErrorCodes() throws Exception { - List actualSuspendErrorCodes = new ArrayList<>(); - List expectedSuspendErrorCodes = endpoint.getDefinition().getResolvedSuspendErrorCodes(synCtx); - assertTrue(expectedSuspendErrorCodes.equals(actualSuspendErrorCodes)); + SynapseXPath xpath = new SynapseXPath("$ctx:suspendErrorCodes"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicSuspendErrorCodes(xpath); + MessageContext synCtx = new TestMessageContext(); + List expectedSuspendErrorCodes = endpoint.getDefinition().getResolvedSuspendErrorCodes(synCtx); + assertTrue(expectedSuspendErrorCodes.isEmpty()); } public void testContextPropertiesForTimeoutErrorCodes() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:timeoutErrorCodes"); - AbstractEndpoint endpoint = new AddressEndpoint(); - EndpointDefinition definition = new EndpointDefinition(); - endpoint.setDefinition(definition); - definition.setDynamicTimeoutErrorCodes(xpath); - MessageContext synCtx = new TestMessageContext(); - synCtx.setProperty("timeoutErrorCodes", "101503,101504"); + SynapseXPath xpath = new SynapseXPath("$ctx:timeoutErrorCodes"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicTimeoutErrorCodes(xpath); + MessageContext synCtx = new TestMessageContext(); + synCtx.setProperty("timeoutErrorCodes", "101503,101504"); - List actualTimeoutErrorCodes = new ArrayList<>(); - actualTimeoutErrorCodes.add(101503); - actualTimeoutErrorCodes.add(101504); - List expectedTimeoutErrorCodes = endpoint.getDefinition().getResolvedTimeoutErrorCodes(synCtx); - assertTrue(expectedTimeoutErrorCodes.equals(actualTimeoutErrorCodes)); + List actualTimeoutErrorCodes = new ArrayList<>(); + actualTimeoutErrorCodes.add(101503); + actualTimeoutErrorCodes.add(101504); + List expectedTimeoutErrorCodes = endpoint.getDefinition().getResolvedTimeoutErrorCodes(synCtx); + assertTrue(expectedTimeoutErrorCodes.equals(actualTimeoutErrorCodes)); } public void testContextPropertiesForEmptyTimeoutErrorCodes() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:timeoutErrorCodes"); - AbstractEndpoint endpoint = new AddressEndpoint(); - EndpointDefinition definition = new EndpointDefinition(); - endpoint.setDefinition(definition); - definition.setDynamicTimeoutErrorCodes(xpath); - MessageContext synCtx = new TestMessageContext(); - synCtx.setProperty("timeoutErrorCodes", ""); - - List actualTimeoutErrorCodes = new ArrayList<>(); - List expectedTimeoutErrorCodes = endpoint.getDefinition().getResolvedTimeoutErrorCodes(synCtx); - assertTrue(expectedTimeoutErrorCodes.equals(actualTimeoutErrorCodes)); + SynapseXPath xpath = new SynapseXPath("$ctx:timeoutErrorCodes"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicTimeoutErrorCodes(xpath); + MessageContext synCtx = new TestMessageContext(); + synCtx.setProperty("timeoutErrorCodes", ""); + + List actualTimeoutErrorCodes = new ArrayList<>(); + List expectedTimeoutErrorCodes = endpoint.getDefinition().getResolvedTimeoutErrorCodes(synCtx); + assertTrue(expectedTimeoutErrorCodes.equals(actualTimeoutErrorCodes)); + } + + public void testContextPropertiesForNoTimeoutErrorCodes() throws Exception { + + SynapseXPath xpath = new SynapseXPath("$ctx:timeoutErrorCodes"); + AbstractEndpoint endpoint = new AddressEndpoint(); + EndpointDefinition definition = new EndpointDefinition(); + endpoint.setDefinition(definition); + definition.setDynamicTimeoutErrorCodes(xpath); + MessageContext synCtx = new TestMessageContext(); + List expectedTimeoutErrorCodes = endpoint.getDefinition().getResolvedTimeoutErrorCodes(synCtx); + assertTrue(expectedTimeoutErrorCodes.isEmpty()); } } From 1b00df2202e6df726a1cd07b822b104ea0a05952 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Tue, 26 Nov 2024 10:33:17 +0530 Subject: [PATCH 16/27] remove handling timeout action value "never" --- .../org/apache/synapse/endpoints/EPConstants.java | 1 - .../apache/synapse/endpoints/EndpointDefinition.java | 2 -- .../endpoints/dynamic/DynamicEndpointTest.java | 12 ------------ 3 files changed, 15 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EPConstants.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EPConstants.java index 5f3896682f..422b1eb33b 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EPConstants.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EPConstants.java @@ -28,5 +28,4 @@ public class EPConstants { public static final String DISCARD = "discard"; public static final String FAULT = "fault"; - public static final String NEVER = "never"; } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java index 255f94e484..68c6014abf 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java @@ -615,8 +615,6 @@ public int evaluateDynamicTimeoutAction(MessageContext synCtx) { result = SynapseConstants.DISCARD; } else if (EPConstants.FAULT.equalsIgnoreCase(timeoutActionStr)) { result = SynapseConstants.DISCARD_AND_FAULT; - } else if (EPConstants.NEVER.equalsIgnoreCase(timeoutActionStr)) { - result = SynapseConstants.NONE; } } } catch (NumberFormatException e) { diff --git a/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java b/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java index be5c4a0949..bdbc88e5e3 100644 --- a/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java +++ b/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java @@ -192,18 +192,6 @@ public void testContextPropertiesForNoTimeoutAction() throws Exception { assertEquals(endpoint.getDefinition().getResolvedTimeoutAction(synCtx), SynapseConstants.NONE); } - public void testContextPropertiesForTimeoutActionNever() throws Exception { - - SynapseXPath xpath = new SynapseXPath("$ctx:timeoutAction"); - AbstractEndpoint endpoint = new AddressEndpoint(); - EndpointDefinition definition = new EndpointDefinition(); - endpoint.setDefinition(definition); - definition.setDynamicTimeoutAction(xpath); - MessageContext synCtx = new TestMessageContext(); - synCtx.setProperty("timeoutAction", "never"); - assertEquals(endpoint.getDefinition().getResolvedTimeoutAction(synCtx), SynapseConstants.NONE); - } - public void testContextPropertiesForSuspendErrorCodes() throws Exception { SynapseXPath xpath = new SynapseXPath("$ctx:suspendErrorCodes"); From 2df1a58c94471c972a308d7c144be4f42f5da89e Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Tue, 26 Nov 2024 19:47:12 +0530 Subject: [PATCH 17/27] add synapse expression support --- .../endpoints/EndpointDefinitionFactory.java | 65 +++-------- .../synapse/endpoints/EndpointDefinition.java | 101 +++++++++++------- .../dynamic/DynamicEndpointTest.java | 87 ++++++++------- 3 files changed, 122 insertions(+), 131 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java index 1ccf127916..eee1c45fa7 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java @@ -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; @@ -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")); @@ -159,11 +161,8 @@ 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); @@ -171,8 +170,6 @@ public EndpointDefinition createDefinition(OMElement elem) { } 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"); } } } @@ -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); @@ -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()) { @@ -242,9 +227,7 @@ 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)); @@ -252,8 +235,6 @@ public EndpointDefinition createDefinition(OMElement elem) { } 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"); } } @@ -264,9 +245,7 @@ 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)); @@ -274,8 +253,6 @@ public EndpointDefinition createDefinition(OMElement elem) { } 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"); } } } @@ -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()) { @@ -336,9 +307,7 @@ 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)); @@ -346,8 +315,6 @@ public EndpointDefinition createDefinition(OMElement elem) { } 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"); } } @@ -358,9 +325,7 @@ 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)); @@ -368,8 +333,6 @@ public EndpointDefinition createDefinition(OMElement elem) { } 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"); } } @@ -380,9 +343,7 @@ 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())); @@ -390,8 +351,6 @@ public EndpointDefinition createDefinition(OMElement elem) { } 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"); } } } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java index 68c6014abf..ab72d5c995 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java @@ -28,6 +28,7 @@ import org.apache.synapse.aspects.AspectConfiguration; import org.apache.synapse.config.SynapseConfigUtils; import org.apache.synapse.config.xml.SynapsePath; +import org.apache.synapse.mediators.Value; import java.util.ArrayList; import java.util.List; @@ -134,7 +135,7 @@ public class EndpointDefinition implements AspectConfigurable { /** * The expression to evaluate dynamic timeout. */ - private SynapsePath dynamicTimeout = null; + private Value dynamicTimeout = null; /** * Whether endpoint state replication should be disabled or not (only valid in clustered setups) @@ -159,39 +160,39 @@ public class EndpointDefinition implements AspectConfigurable { */ private int timeoutAction = SynapseConstants.NONE; /** The expression to evaluate dynamic timeout action */ - private SynapsePath dynamicTimeoutAction = null; + private Value dynamicTimeoutAction = null; /** The initial suspend duration when an endpoint is marked inactive */ private long initialSuspendDuration = -1; /** * The expression to evaluate dynamic initial suspend duration. */ - private SynapsePath dynamicInitialSuspendDuration = null; + private Value dynamicInitialSuspendDuration = null; /** The suspend duration ratio for the next duration - this is the geometric series multipler */ private float suspendProgressionFactor = 1; /** The expression to evaluate dynamic suspend progression factor */ - private SynapsePath dynamicSuspendProgressionFactor = null; + private Value dynamicSuspendProgressionFactor = null; /** This is the maximum duration for which a node will be suspended */ private long suspendMaximumDuration = Long.MAX_VALUE; /** The expression to maximum duration for which a node will be suspended */ - private SynapsePath dynamicSuspendMaximumDuration = null; + private Value dynamicSuspendMaximumDuration = null; /** A list of error codes, which directly puts an endpoint into suspend mode */ private final List suspendErrorCodes = new ArrayList(); /** The expression to evaluate dynamic suspend error codes */ - private SynapsePath dynamicSuspendErrorCodes = null; + private Value dynamicSuspendErrorCodes = null; /** No of retries to attempt on timeout, before an endpoint is makred inactive */ private int retriesOnTimeoutBeforeSuspend = 0; /** The expression to evaluate dynamic retries on timeout before suspend */ - private SynapsePath dynamicRetriesOnTimeoutBeforeSuspend = null; + private Value dynamicRetriesOnTimeoutBeforeSuspend = null; /** The delay between retries for a timeout out endpoint */ private int retryDurationOnTimeout = 0; /** The expression to evaluate dynamic retry duration on timeout */ - private SynapsePath dynamicRetryDurationOnTimeout = null; + private Value dynamicRetryDurationOnTimeout = null; /** A list of error codes which puts the endpoint into timeout mode */ private final List timeoutErrorCodes = new ArrayList(); /** The expression to evaluate dynamic timeout error codes */ - private SynapsePath dynamicTimeoutErrorCodes = null; + private Value dynamicTimeoutErrorCodes = null; private AspectConfiguration aspectConfiguration; @@ -220,12 +221,15 @@ public EndpointDefinition() { } } - public void setDynamicTimeoutExpression(SynapsePath expression) { + public void setDynamicTimeoutExpression(Value expression) { this.dynamicTimeout = expression; } public SynapsePath getDynamicTimeoutExpression() { - return this.dynamicTimeout; + if (dynamicTimeout == null) { + return null; + } + return this.dynamicTimeout.getExpression(); } public boolean isDynamicTimeoutEndpoint() { @@ -239,7 +243,7 @@ public boolean isDynamicTimeoutEndpoint() { public long evaluateDynamicEndpointTimeout(MessageContext synCtx) { long timeoutMilliSeconds; try { - String stringValue = dynamicTimeout.stringValueOf(synCtx); + String stringValue = dynamicTimeout.evaluateValue(synCtx); if (stringValue != null) { timeoutMilliSeconds = Long.parseLong(stringValue.trim()); } else { @@ -592,10 +596,13 @@ public void setTimeoutAction(int timeoutAction) { public SynapsePath getDynamicTimeoutAction() { - return dynamicTimeoutAction; + if (dynamicTimeoutAction == null) { + return null; + } + return dynamicTimeoutAction.getExpression(); } - public void setDynamicTimeoutAction(SynapsePath dynamicTimeoutAction) { + public void setDynamicTimeoutAction(Value dynamicTimeoutAction) { this.dynamicTimeoutAction = dynamicTimeoutAction; } @@ -609,7 +616,7 @@ public int evaluateDynamicTimeoutAction(MessageContext synCtx) { int result = timeoutAction; try { - String timeoutActionStr = dynamicTimeoutAction.stringValueOf(synCtx); + String timeoutActionStr = dynamicTimeoutAction.evaluateValue(synCtx); if (timeoutActionStr != null) { if (EPConstants.DISCARD.equalsIgnoreCase(timeoutActionStr)) { result = SynapseConstants.DISCARD; @@ -677,10 +684,13 @@ public void setInitialSuspendDuration(long initialSuspendDuration) { public SynapsePath getDynamicInitialSuspendDuration() { - return dynamicInitialSuspendDuration; + if (dynamicInitialSuspendDuration == null) { + return null; + } + return dynamicInitialSuspendDuration.getExpression(); } - public void setDynamicInitialSuspendDuration(SynapsePath dynamicInitialSuspendDuration) { + public void setDynamicInitialSuspendDuration(Value dynamicInitialSuspendDuration) { this.dynamicInitialSuspendDuration = dynamicInitialSuspendDuration; } @@ -696,7 +706,7 @@ public boolean isInitialSuspendDurationDynamic() { public long evaluateDynamicInitialSuspendDuration(MessageContext synCtx) { long result = initialSuspendDuration; try { - String stringValue = dynamicInitialSuspendDuration.stringValueOf(synCtx); + String stringValue = dynamicInitialSuspendDuration.evaluateValue(synCtx); if (stringValue != null) { result = Long.parseLong(stringValue.trim()); } @@ -731,10 +741,13 @@ public void setSuspendProgressionFactor(float suspendProgressionFactor) { public SynapsePath getDynamicSuspendProgressionFactor() { - return dynamicSuspendProgressionFactor; + if (dynamicSuspendProgressionFactor == null) { + return null; + } + return dynamicSuspendProgressionFactor.getExpression(); } - public void setDynamicSuspendProgressionFactor(SynapsePath dynamicSuspendProgressionFactor) { + public void setDynamicSuspendProgressionFactor(Value dynamicSuspendProgressionFactor) { this.dynamicSuspendProgressionFactor = dynamicSuspendProgressionFactor; } @@ -748,7 +761,7 @@ public float evaluateDynamicSuspendProgressionFactor(MessageContext messageConte float result = suspendProgressionFactor; try { - String stringValue = dynamicSuspendProgressionFactor.stringValueOf(messageContext); + String stringValue = dynamicSuspendProgressionFactor.evaluateValue(messageContext); if (stringValue != null) { result = Float.parseFloat(stringValue); } @@ -775,10 +788,13 @@ public void setSuspendMaximumDuration(long suspendMaximumDuration) { public SynapsePath getDynamicSuspendMaximumDuration() { - return dynamicSuspendMaximumDuration; + if (dynamicSuspendMaximumDuration == null) { + return null; + } + return dynamicSuspendMaximumDuration.getExpression(); } - public void setDynamicSuspendMaximumDuration(SynapsePath dynamicSuspendMaximumDuration) { + public void setDynamicSuspendMaximumDuration(Value dynamicSuspendMaximumDuration) { this.dynamicSuspendMaximumDuration = dynamicSuspendMaximumDuration; } @@ -792,7 +808,7 @@ public long evaluateDynamicSuspendMaximumDuration(MessageContext messageContext) long result = suspendMaximumDuration; try { - String stringValue = dynamicSuspendMaximumDuration.stringValueOf(messageContext); + String stringValue = dynamicSuspendMaximumDuration.evaluateValue(messageContext); if (stringValue != null) { result = Long.parseLong(stringValue); } @@ -819,11 +835,13 @@ public void setRetriesOnTimeoutBeforeSuspend(int retriesOnTimeoutBeforeSuspend) public SynapsePath getDynamicRetriesOnTimeoutBeforeSuspend() { - return dynamicRetriesOnTimeoutBeforeSuspend; + if (dynamicRetriesOnTimeoutBeforeSuspend == null) { + return null; + } + return dynamicRetriesOnTimeoutBeforeSuspend.getExpression(); } - public void setDynamicRetriesOnTimeoutBeforeSuspend( - SynapsePath dynamicRetriesOnTimeoutBeforeSuspend) { + public void setDynamicRetriesOnTimeoutBeforeSuspend(Value dynamicRetriesOnTimeoutBeforeSuspend) { this.dynamicRetriesOnTimeoutBeforeSuspend = dynamicRetriesOnTimeoutBeforeSuspend; } @@ -837,7 +855,7 @@ public int evaluateDynamicRetriesOnTimeoutBeforeSuspend(MessageContext messageCo int result = retriesOnTimeoutBeforeSuspend; try { - String stringValue = dynamicRetriesOnTimeoutBeforeSuspend.stringValueOf(messageContext); + String stringValue = dynamicRetriesOnTimeoutBeforeSuspend.evaluateValue(messageContext); if (stringValue != null) { result = Integer.parseInt(stringValue); } @@ -864,10 +882,13 @@ public void setRetryDurationOnTimeout(int retryDurationOnTimeout) { public SynapsePath getDynamicRetryDurationOnTimeout() { - return dynamicRetryDurationOnTimeout; + if (dynamicRetryDurationOnTimeout == null) { + return null; + } + return dynamicRetryDurationOnTimeout.getExpression(); } - public void setDynamicRetryDurationOnTimeout(SynapsePath dynamicRetryDurationOnTimeout) { + public void setDynamicRetryDurationOnTimeout(Value dynamicRetryDurationOnTimeout) { this.dynamicRetryDurationOnTimeout = dynamicRetryDurationOnTimeout; } @@ -881,7 +902,7 @@ public int evaluateDynamicRetryDurationOnTimeout(MessageContext messageContext) int result = retryDurationOnTimeout; try { - String stringValue = dynamicRetryDurationOnTimeout.stringValueOf(messageContext); + String stringValue = dynamicRetryDurationOnTimeout.evaluateValue(messageContext); if (stringValue != null) { result = Integer.parseInt(stringValue); } @@ -928,10 +949,13 @@ public void addSuspendErrorCode(int code) { public SynapsePath getDynamicSuspendErrorCodes() { - return dynamicSuspendErrorCodes; + if (dynamicSuspendErrorCodes == null) { + return null; + } + return dynamicSuspendErrorCodes.getExpression(); } - public void setDynamicSuspendErrorCodes(SynapsePath dynamicSuspendErrorCodes) { + public void setDynamicSuspendErrorCodes(Value dynamicSuspendErrorCodes) { this.dynamicSuspendErrorCodes = dynamicSuspendErrorCodes; } @@ -945,7 +969,7 @@ public List evaluateDynamicSuspendErrorCodes(MessageContext messageCont List result = suspendErrorCodes; try { - String stringValue = dynamicSuspendErrorCodes.stringValueOf(messageContext); + String stringValue = dynamicSuspendErrorCodes.evaluateValue(messageContext); if (stringValue != null) { String[] errorCodes = stringValue.split(","); result = new ArrayList(); @@ -972,10 +996,13 @@ public void addTimeoutErrorCode(int code) { public SynapsePath getDynamicTimeoutErrorCodes() { - return dynamicTimeoutErrorCodes; + if (dynamicTimeoutErrorCodes == null) { + return null; + } + return dynamicTimeoutErrorCodes.getExpression(); } - public void setDynamicTimeoutErrorCodes(SynapsePath dynamicTimeoutErrorCodes) { + public void setDynamicTimeoutErrorCodes(Value dynamicTimeoutErrorCodes) { this.dynamicTimeoutErrorCodes = dynamicTimeoutErrorCodes; } @@ -989,7 +1016,7 @@ public List evaluateDynamicTimeoutErrorCodes(MessageContext messageCont List result = timeoutErrorCodes; try { - String stringValue = dynamicTimeoutErrorCodes.stringValueOf(messageContext); + String stringValue = dynamicTimeoutErrorCodes.evaluateValue(messageContext); if (stringValue != null) { String[] errorCodes = stringValue.split(","); result = new ArrayList(); diff --git a/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java b/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java index bdbc88e5e3..a2e4b101c0 100644 --- a/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java +++ b/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java @@ -17,9 +17,12 @@ package org.apache.synapse.endpoints.dynamic; import junit.framework.TestCase; +import org.apache.axiom.om.OMElement; +import org.apache.axiom.om.util.AXIOMUtil; import org.apache.synapse.MessageContext; import org.apache.synapse.SynapseConstants; import org.apache.synapse.TestMessageContext; +import org.apache.synapse.config.xml.ValueFactory; import org.apache.synapse.endpoints.AbstractEndpoint; import org.apache.synapse.endpoints.AddressEndpoint; import org.apache.synapse.endpoints.EndpointDefinition; @@ -31,47 +34,48 @@ public class DynamicEndpointTest extends TestCase { public void testContextProperties() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:timeout"); + + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:timeout}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicTimeoutExpression(xpath); + definition.setDynamicTimeoutExpression(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("timeout", "90000"); - assertEquals(Long.valueOf((String) xpath.evaluate(synCtx)).longValue(), + assertEquals(90000, endpoint.getDefinition().evaluateDynamicEndpointTimeout(synCtx)); } public void testContextPropertiesForInitialSuspendDuration() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:initialSuspendDuration"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:suspendInitialDuration}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicInitialSuspendDuration(xpath); + definition.setDynamicInitialSuspendDuration(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); - synCtx.setProperty("initialSuspendDuration", "90000"); + synCtx.setProperty("suspendInitialDuration", "90000"); assertEquals(endpoint.getDefinition().getResolvedInitialSuspendDuration(synCtx), 90000); } public void testContextPropertiesForNoInitialSuspendDuration() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:initialSuspendDuration"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:suspendInitialDuration}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicInitialSuspendDuration(xpath); + definition.setDynamicInitialSuspendDuration(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); assertEquals(-1, endpoint.getDefinition().getResolvedInitialSuspendDuration(synCtx)); } public void testContextPropertiesForSuspendMaximumDuration() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:suspendMaximumDuration"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:suspendMaximumDuration}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicSuspendMaximumDuration(xpath); + definition.setDynamicSuspendMaximumDuration(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("suspendMaximumDuration", "90000"); assertEquals(endpoint.getDefinition().getResolvedSuspendMaximumDuration(synCtx), 90000); @@ -79,22 +83,22 @@ public void testContextPropertiesForSuspendMaximumDuration() throws Exception { public void testContextPropertiesForNoSuspendMaximumDuration() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:suspendMaximumDuration"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:suspendMaximumDuration}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicSuspendMaximumDuration(xpath); + definition.setDynamicSuspendMaximumDuration(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); assertEquals(Long.MAX_VALUE, endpoint.getDefinition().getResolvedSuspendMaximumDuration(synCtx)); } public void testContextPropertiesForSuspendProgressionFactor() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:suspendProgressionFactor"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:suspendProgressionFactor}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicSuspendProgressionFactor(xpath); + definition.setDynamicSuspendProgressionFactor(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("suspendProgressionFactor", "2"); assertEquals(endpoint.getDefinition().getResolvedSuspendProgressionFactor(synCtx), 2.0f); @@ -102,22 +106,22 @@ public void testContextPropertiesForSuspendProgressionFactor() throws Exception public void testContextPropertiesForNoSuspendProgressionFactor() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:suspendProgressionFactor"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:suspendProgressionFactor}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicSuspendProgressionFactor(xpath); + definition.setDynamicSuspendProgressionFactor(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); assertEquals(1.0f, endpoint.getDefinition().getResolvedSuspendProgressionFactor(synCtx)); } public void testContextPropertiesForRetriesOnTimeoutBeforeSuspend() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:retriesOnTimeoutBeforeSuspend"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:retriesOnTimeoutBeforeSuspend}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicRetriesOnTimeoutBeforeSuspend(xpath); + definition.setDynamicRetriesOnTimeoutBeforeSuspend(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("retriesOnTimeoutBeforeSuspend", "3"); assertEquals(endpoint.getDefinition().getResolvedRetriesOnTimeoutBeforeSuspend(synCtx), 3); @@ -125,22 +129,23 @@ public void testContextPropertiesForRetriesOnTimeoutBeforeSuspend() throws Excep public void testContextPropertiesForNoRetriesOnTimeoutBeforeSuspend() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:retriesOnTimeoutBeforeSuspend"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:retriesOnTimeoutBeforeSuspend}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicRetriesOnTimeoutBeforeSuspend(xpath); + definition.setDynamicRetriesOnTimeoutBeforeSuspend(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); assertEquals(0, endpoint.getDefinition().getResolvedRetriesOnTimeoutBeforeSuspend(synCtx)); } public void testContextPropertiesForRetryDurationOnTimeout() throws Exception { + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:retryDurationOnTimeout}"); SynapseXPath xpath = new SynapseXPath("$ctx:retryDurationOnTimeout"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicRetryDurationOnTimeout(xpath); + definition.setDynamicRetryDurationOnTimeout(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("retryDurationOnTimeout", "90000"); assertEquals(endpoint.getDefinition().getResolvedRetryDurationOnTimeout(synCtx), 90000); @@ -148,22 +153,22 @@ public void testContextPropertiesForRetryDurationOnTimeout() throws Exception { public void testContextPropertiesForNoRetryDurationOnTimeout() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:retryDurationOnTimeout"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:retryDurationOnTimeout}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicRetryDurationOnTimeout(xpath); + definition.setDynamicRetryDurationOnTimeout(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); assertEquals(0, endpoint.getDefinition().getResolvedRetryDurationOnTimeout(synCtx)); } public void testContextPropertiesForTimeoutActionFault() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:timeoutAction"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:timeoutAction}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicTimeoutAction(xpath); + definition.setDynamicTimeoutAction(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("timeoutAction", "fault"); assertEquals(endpoint.getDefinition().getResolvedTimeoutAction(synCtx), SynapseConstants.DISCARD_AND_FAULT); @@ -171,11 +176,11 @@ public void testContextPropertiesForTimeoutActionFault() throws Exception { public void testContextPropertiesForTimeoutActionDiscard() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:timeoutAction"); + OMElement omElement = AXIOMUtil.stringToOM("{get-property('timeoutAction')}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicTimeoutAction(xpath); + definition.setDynamicTimeoutAction(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("timeoutAction", "discard"); assertEquals(endpoint.getDefinition().getResolvedTimeoutAction(synCtx), SynapseConstants.DISCARD); @@ -183,22 +188,22 @@ public void testContextPropertiesForTimeoutActionDiscard() throws Exception { public void testContextPropertiesForNoTimeoutAction() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:timeoutAction"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:timeoutAction}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicTimeoutAction(xpath); + definition.setDynamicTimeoutAction(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); assertEquals(endpoint.getDefinition().getResolvedTimeoutAction(synCtx), SynapseConstants.NONE); } public void testContextPropertiesForSuspendErrorCodes() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:suspendErrorCodes"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:suspendErrorCodes}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicSuspendErrorCodes(xpath); + definition.setDynamicSuspendErrorCodes(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("suspendErrorCodes", "101503,101504"); @@ -211,11 +216,11 @@ public void testContextPropertiesForSuspendErrorCodes() throws Exception { public void testContextPropertiesForEmptySuspendErrorCodes() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:suspendErrorCodes"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:suspendErrorCodes}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicSuspendErrorCodes(xpath); + definition.setDynamicSuspendErrorCodes(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("suspendErrorCodes", ""); @@ -226,11 +231,11 @@ public void testContextPropertiesForEmptySuspendErrorCodes() throws Exception { public void testContextPropertiesForNoSuspendErrorCodes() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:suspendErrorCodes"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:suspendErrorCodes}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicSuspendErrorCodes(xpath); + definition.setDynamicSuspendErrorCodes(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); List expectedSuspendErrorCodes = endpoint.getDefinition().getResolvedSuspendErrorCodes(synCtx); assertTrue(expectedSuspendErrorCodes.isEmpty()); @@ -238,11 +243,11 @@ public void testContextPropertiesForNoSuspendErrorCodes() throws Exception { public void testContextPropertiesForTimeoutErrorCodes() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:timeoutErrorCodes"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:timeoutErrorCodes}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicTimeoutErrorCodes(xpath); + definition.setDynamicTimeoutErrorCodes(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("timeoutErrorCodes", "101503,101504"); @@ -255,11 +260,11 @@ public void testContextPropertiesForTimeoutErrorCodes() throws Exception { public void testContextPropertiesForEmptyTimeoutErrorCodes() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:timeoutErrorCodes"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:timeoutErrorCodes}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicTimeoutErrorCodes(xpath); + definition.setDynamicTimeoutErrorCodes(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("timeoutErrorCodes", ""); @@ -270,11 +275,11 @@ public void testContextPropertiesForEmptyTimeoutErrorCodes() throws Exception { public void testContextPropertiesForNoTimeoutErrorCodes() throws Exception { - SynapseXPath xpath = new SynapseXPath("$ctx:timeoutErrorCodes"); + OMElement omElement = AXIOMUtil.stringToOM("{$ctx:timeoutErrorCodes}"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicTimeoutErrorCodes(xpath); + definition.setDynamicTimeoutErrorCodes(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); List expectedTimeoutErrorCodes = endpoint.getDefinition().getResolvedTimeoutErrorCodes(synCtx); assertTrue(expectedTimeoutErrorCodes.isEmpty()); From 3c782b1d5a08f67f160dc570121ba238a530a473 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Thu, 28 Nov 2024 15:16:49 +0530 Subject: [PATCH 18/27] Revert "parse message context for createJsonRepresentation" This reverts commit e19e5d409eb3bdb49d09e4e084b4adbad6e822a1. --- .../management/helpers/SpanTagger.java | 2 +- .../synapse/endpoints/AbstractEndpoint.java | 29 ++++++++++--------- .../synapse/endpoints/AddressEndpoint.java | 4 +-- .../synapse/endpoints/ClassEndpoint.java | 2 +- .../synapse/endpoints/DefaultEndpoint.java | 4 +-- .../apache/synapse/endpoints/Endpoint.java | 2 +- .../synapse/endpoints/FailoverEndpoint.java | 2 +- .../synapse/endpoints/HTTPEndpoint.java | 5 ++-- .../synapse/endpoints/IndirectEndpoint.java | 2 +- .../endpoints/LoadbalanceEndpoint.java | 2 +- .../endpoints/RecipientListEndpoint.java | 2 +- .../synapse/endpoints/ResolvingEndpoint.java | 2 +- .../synapse/endpoints/TemplateEndpoint.java | 2 +- .../synapse/endpoints/WSDLEndpoint.java | 4 +-- .../xml/endpoints/CustomClassEndpoint.java | 2 +- 15 files changed, 33 insertions(+), 33 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/management/helpers/SpanTagger.java b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/management/helpers/SpanTagger.java index 4cf0dd5461..1e1fe707fa 100644 --- a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/management/helpers/SpanTagger.java +++ b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/management/helpers/SpanTagger.java @@ -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(synCtx))); + String.valueOf(openStatisticsLog.getEndpoint().getJsonRepresentation())); } if (synCtx.getProperty(CorrelationConstants.CORRELATION_ID) != null) { span.setAttribute(TelemetryConstants.CORRELATION_ID_ATTRIBUTE_KEY, diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java index bb89bad0a4..a19a901c76 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java @@ -947,35 +947,35 @@ public void setComponentStatisticsId(ArtifactHolder holder) { /** * Set advanced properties of the endpoint to json object. */ - protected void setAdvancedProperties(MessageContext messageContext) { + protected void setAdvancedProperties() { JSONObject advancedProps = new JSONObject(); endpointJson.put("advanced", advancedProps); - setSuspendStateProperties(getDefinition(), advancedProps, messageContext); - setTimeoutStateProperties(getDefinition(), advancedProps, messageContext); + setSuspendStateProperties(getDefinition(), advancedProps); + setTimeoutStateProperties(getDefinition(), advancedProps); } /** * Set time-out state properties of the endpoint to json object. */ - private void setTimeoutStateProperties(EndpointDefinition definition, JSONObject advancedProps, MessageContext messageContext) { + private void setTimeoutStateProperties(EndpointDefinition definition, JSONObject advancedProps) { JSONObject timeoutStateProps = new JSONObject(); advancedProps.put("timeoutState", timeoutStateProps); - timeoutStateProps.put("errorCodes", definition.getResolvedTimeoutErrorCodes(messageContext)); - timeoutStateProps.put("reties", definition.getResolvedRetriesOnTimeoutBeforeSuspend(messageContext)); + timeoutStateProps.put("errorCodes", definition.getTimeoutErrorCodes()); + timeoutStateProps.put("reties", definition.getRetriesOnTimeoutBeforeSuspend()); } /** * Set suspend state properties of the endpoint to json object. */ - private void setSuspendStateProperties(EndpointDefinition definition, JSONObject advancedProps, MessageContext messageContext) { + private void setSuspendStateProperties(EndpointDefinition definition, JSONObject advancedProps) { JSONObject suspendStatePros = new JSONObject(); advancedProps.put("suspendState", suspendStatePros); - suspendStatePros.put("errorCodes", definition.getResolvedSuspendErrorCodes(messageContext)); - suspendStatePros.put("maxDuration", definition.getResolvedSuspendMaximumDuration(messageContext)); - suspendStatePros.put("initialDuration", definition.getResolvedInitialSuspendDuration(messageContext)); + suspendStatePros.put("errorCodes", definition.getSuspendErrorCodes()); + suspendStatePros.put("maxDuration", definition.getSuspendMaximumDuration()); + suspendStatePros.put("initialDuration", definition.getInitialSuspendDuration()); } protected JSONArray getEndpointChildrenAsJson(List children) { @@ -983,20 +983,21 @@ protected JSONArray getEndpointChildrenAsJson(List children) { if (children != null && children.size() != 0) { for (Endpoint child : children) { - childrenJsonList.put(child.getJsonRepresentation(null)); + childrenJsonList.put(child.getJsonRepresentation()); } } return childrenJsonList; } @Override - public JSONObject getJsonRepresentation(MessageContext messageContext) { + public JSONObject getJsonRepresentation() { if (endpointJson == null) { - createJsonRepresentation(messageContext); + createJsonRepresentation(); } return endpointJson; } - protected abstract void createJsonRepresentation(MessageContext synCtx); + protected abstract void createJsonRepresentation(); + } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/AddressEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/AddressEndpoint.java index 9eeaffc594..2808ce7f7f 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/AddressEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/AddressEndpoint.java @@ -58,12 +58,12 @@ public void onSuccess() { } } - protected void createJsonRepresentation(MessageContext messageContext) { + protected void createJsonRepresentation() { endpointJson = new JSONObject(); endpointJson.put("name", getName()); endpointJson.put("type", "Address Endpoint"); endpointJson.put("address", getDefinition().getAddress()); - setAdvancedProperties(messageContext); + setAdvancedProperties(); } public void send(MessageContext synCtx) { diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/ClassEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/ClassEndpoint.java index 7387116f6d..5f610213de 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/ClassEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/ClassEndpoint.java @@ -83,7 +83,7 @@ public void send(MessageContext synCtx) { } @Override - protected void createJsonRepresentation(MessageContext messageContext) { + protected void createJsonRepresentation() { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); endpointJson.put(TYPE_JSON_ATT, "Class Endpoint"); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/DefaultEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/DefaultEndpoint.java index 48000cf39b..a4fd4e2e91 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/DefaultEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/DefaultEndpoint.java @@ -66,12 +66,12 @@ public void onSuccess() { } @Override - protected void createJsonRepresentation(MessageContext messageContext) { + protected void createJsonRepresentation() { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); endpointJson.put(TYPE_JSON_ATT, "Default Endpoint"); endpointJson.put(CHILDREN_JSON_ATT, getEndpointChildrenAsJson(getChildren())); - setAdvancedProperties(messageContext); + setAdvancedProperties(); } public void send(MessageContext synCtx) { diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/Endpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/Endpoint.java index 31144946d6..fc5ef86c8b 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/Endpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/Endpoint.java @@ -156,6 +156,6 @@ public interface Endpoint extends ManagedLifecycle, SynapseArtifact, Nameable { * * @return JSONObject */ - public JSONObject getJsonRepresentation(MessageContext messageContext); + public JSONObject getJsonRepresentation(); } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/FailoverEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/FailoverEndpoint.java index f24c2309e4..bd086e5132 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/FailoverEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/FailoverEndpoint.java @@ -294,7 +294,7 @@ public boolean readyToSend() { return false; } - protected void createJsonRepresentation(MessageContext messageContext) { + protected void createJsonRepresentation() { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); endpointJson.put(TYPE_JSON_ATT, "Failover Endpoint"); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/HTTPEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/HTTPEndpoint.java index cc7410569d..ce53631fee 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/HTTPEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/HTTPEndpoint.java @@ -87,15 +87,14 @@ public void onSuccess() { } } - @Override - protected void createJsonRepresentation(MessageContext messageContext) { + protected void createJsonRepresentation() { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); endpointJson.put(TYPE_JSON_ATT, "HTTP Endpoint"); endpointJson.put("method", getHttpMethod()); endpointJson.put("uriTemplate", getUriTemplate().expand()); endpointJson.put("errorHandler", getErrorHandler()); - setAdvancedProperties(messageContext); + setAdvancedProperties(); } public void send(MessageContext synCtx) { diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/IndirectEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/IndirectEndpoint.java index 0d509c98ec..fb7c459b27 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/IndirectEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/IndirectEndpoint.java @@ -61,7 +61,7 @@ public void send(MessageContext synCtx) { } @Override - protected void createJsonRepresentation(MessageContext messageContext) { + protected void createJsonRepresentation() { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); endpointJson.put(TYPE_JSON_ATT, "Indirect Endpoint"); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/LoadbalanceEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/LoadbalanceEndpoint.java index ab64a2172d..aae31bb0f1 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/LoadbalanceEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/LoadbalanceEndpoint.java @@ -151,7 +151,7 @@ public boolean isInitialized() { return loadBalanceEPInitialized; } - protected void createJsonRepresentation(MessageContext messageContext) { + protected void createJsonRepresentation() { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/RecipientListEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/RecipientListEndpoint.java index a5f0685099..966185f4ea 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/RecipientListEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/RecipientListEndpoint.java @@ -97,7 +97,7 @@ public void destroy() { } @Override - protected void createJsonRepresentation(MessageContext messageContext) { + protected void createJsonRepresentation() { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/ResolvingEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/ResolvingEndpoint.java index 618a0c5c5e..861c0bb067 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/ResolvingEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/ResolvingEndpoint.java @@ -61,7 +61,7 @@ public void send(MessageContext synCtx) { } @Override - protected void createJsonRepresentation(MessageContext messageContext) { + protected void createJsonRepresentation() { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); endpointJson.put(TYPE_JSON_ATT, "Resolving Endpoint"); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java index 47fc6820c8..f24d50814c 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java @@ -63,7 +63,7 @@ public void send(MessageContext synCtx) { } @Override - protected void createJsonRepresentation(MessageContext messageContext) { + protected void createJsonRepresentation() { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/WSDLEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/WSDLEndpoint.java index cf7c865ced..dc8975cdf3 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/WSDLEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/WSDLEndpoint.java @@ -85,7 +85,7 @@ public void onSuccess() { } @Override - protected void createJsonRepresentation(MessageContext messageContext) { + protected void createJsonRepresentation() { endpointJson = new JSONObject(); endpointJson.put(NAME_JSON_ATT, getName()); @@ -93,7 +93,7 @@ protected void createJsonRepresentation(MessageContext messageContext) { endpointJson.put("wsdlUri", getWsdlURI()); endpointJson.put("serviceName", getServiceName()); endpointJson.put("portName", getPortName()); - setAdvancedProperties(messageContext); + setAdvancedProperties(); } public void send(MessageContext synCtx) { diff --git a/modules/core/src/test/java/org/apache/synapse/config/xml/endpoints/CustomClassEndpoint.java b/modules/core/src/test/java/org/apache/synapse/config/xml/endpoints/CustomClassEndpoint.java index 8605f85417..730b44664f 100644 --- a/modules/core/src/test/java/org/apache/synapse/config/xml/endpoints/CustomClassEndpoint.java +++ b/modules/core/src/test/java/org/apache/synapse/config/xml/endpoints/CustomClassEndpoint.java @@ -20,7 +20,7 @@ public void onSuccess() { } @Override - protected void createJsonRepresentation(MessageContext messageContext) { + protected void createJsonRepresentation() { //TODO } From 4a9905465179e5a899b7af2fb7f52a3469366eec Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Thu, 28 Nov 2024 15:15:03 +0530 Subject: [PATCH 19/27] introduce value to handle endpoint definition attributes --- .../endpoints/EndpointDefinitionFactory.java | 111 ++--- .../EndpointDefinitionSerializer.java | 71 +-- .../endpoints/DynamicLoadbalanceEndpoint.java | 3 +- .../synapse/endpoints/EndpointDefinition.java | 433 +++++++----------- .../synapse/endpoints/HttpEndpointTest.java | 4 +- .../dynamic/DynamicEndpointTest.java | 42 +- 6 files changed, 260 insertions(+), 404 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java index eee1c45fa7..b3685893c8 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java @@ -29,6 +29,7 @@ import org.apache.synapse.config.xml.ValueFactory; import org.apache.synapse.config.xml.XMLConfigConstants; import org.apache.synapse.endpoints.EndpointDefinition; +import org.apache.synapse.mediators.Value; import org.apache.synapse.util.xpath.SynapseXPath; import org.jaxen.JaxenException; @@ -161,12 +162,11 @@ public EndpointDefinition createDefinition(OMElement elem) { String d = duration.getText(); if (d != null) { try { - if (isExpression(d)) { - definition.setDynamicTimeoutExpression(valueFactory.createTextValue(duration)); - } else { - long timeoutMilliSeconds = Long.parseLong(d.trim()); - definition.setTimeoutDuration(timeoutMilliSeconds); + Value timeoutDurationValue = valueFactory.createTextValue(duration); + if (timeoutDurationValue.getKeyValue() != null) { + Long.parseLong(timeoutDurationValue.getKeyValue()); } + definition.setTimeoutDuration(valueFactory.createTextValue(duration)); } catch (NumberFormatException e) { handleException("Endpoint timeout duration expected as a " + "number but was not a number"); @@ -177,19 +177,12 @@ public EndpointDefinition createDefinition(OMElement elem) { OMElement action = timeout.getFirstChildWithName( new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "responseAction")); if (action != null && action.getText() != null) { - String actionString = action.getText().trim(); - if (isExpression(actionString)) { - definition.setDynamicTimeoutAction(valueFactory.createTextValue(action)); - } else { - 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"); - } + Value timeoutActionValue = valueFactory.createTextValue(action); + + if (timeoutActionValue.getKeyValue() != null && !timeoutActionValue.getKeyValue().equals("discard") && !timeoutActionValue.getKeyValue().equals("fault")) { + handleException("Invalid timeout action, action : " + timeoutActionValue.getKeyValue() + " is not supported"); } + definition.setTimeoutAction(timeoutActionValue); } } @@ -203,21 +196,20 @@ public EndpointDefinition createDefinition(OMElement elem) { SynapseConstants.SYNAPSE_NAMESPACE, XMLConfigConstants.ERROR_CODES)); if (timeoutCodes != null && timeoutCodes.getText() != null) { - String trimmedTimeoutCodes = timeoutCodes.getText().trim(); - if (isExpression(trimmedTimeoutCodes)) { - definition.setDynamicTimeoutErrorCodes(valueFactory.createTextValue(timeoutCodes)); - } else { + Value timeoutErrorCodesValue = valueFactory.createTextValue(timeoutCodes); + if (timeoutErrorCodesValue.getKeyValue() != null) { StringTokenizer st = new StringTokenizer(timeoutCodes.getText().trim(), ", "); while (st.hasMoreTokens()) { String s = st.nextToken(); try { - definition.addTimeoutErrorCode(Integer.parseInt(s)); + Integer.parseInt(s); } catch (NumberFormatException e) { handleException("The timeout error codes should be specified " + "as valid numbers separated by commas : " + timeoutCodes.getText(), e); } } } + definition.setTimeoutErrorCodes(timeoutErrorCodesValue); } OMElement retriesBeforeSuspend = markAsTimedOut.getFirstChildWithName(new QName( @@ -225,13 +217,11 @@ public EndpointDefinition createDefinition(OMElement elem) { XMLConfigConstants.RETRIES_BEFORE_SUSPENSION)); if (retriesBeforeSuspend != null && retriesBeforeSuspend.getText() != null) { try { - String trimmedRetriesBeforeSuspend = retriesBeforeSuspend.getText().trim(); - if (isExpression(trimmedRetriesBeforeSuspend)) { - definition.setDynamicRetriesOnTimeoutBeforeSuspend(valueFactory.createTextValue(retriesBeforeSuspend)); - } else { - definition.setRetriesOnTimeoutBeforeSuspend( - Integer.parseInt(trimmedRetriesBeforeSuspend)); + Value retriesBeforeSuspendValue = valueFactory.createTextValue(retriesBeforeSuspend); + if (retriesBeforeSuspendValue.getKeyValue() != null) { + Integer.parseInt(retriesBeforeSuspendValue.getKeyValue()); } + definition.setRetriesOnTimeoutBeforeSuspend(retriesBeforeSuspendValue); } catch (NumberFormatException e) { handleException("The retries before suspend [for timeouts] should be " + "specified as a valid number : " + retriesBeforeSuspend.getText(), e); @@ -243,13 +233,11 @@ public EndpointDefinition createDefinition(OMElement elem) { XMLConfigConstants.RETRY_DELAY)); if (retryDelay != null && retryDelay.getText() != null) { try { - String trimmedRetryDelay = retryDelay.getText().trim(); - if (isExpression(trimmedRetryDelay)) { - definition.setDynamicRetryDurationOnTimeout(valueFactory.createTextValue(retryDelay)); - } else { - definition.setRetryDurationOnTimeout( - Integer.parseInt(trimmedRetryDelay)); + Value retryDelayValue = valueFactory.createTextValue(retryDelay); + if (retryDelayValue.getKeyValue() != null) { + Integer.parseInt(retryDelayValue.getKeyValue()); } + definition.setRetryDurationOnTimeout(valueFactory.createTextValue(retryDelay)); } catch (NumberFormatException e) { handleException("The retry delay for timeouts should be specified " + "as a valid number : " + retryDelay.getText(), e); @@ -264,9 +252,9 @@ public EndpointDefinition createDefinition(OMElement elem) { log.warn("Configuration uses deprecated style for endpoint 'suspendDurationOnFailure'"); try { - definition.setInitialSuspendDuration( - 1000 * Long.parseLong(suspendDurationOnFailure.getText().trim())); - definition.setSuspendProgressionFactor((float) 1.0); + long suspendDurationValue = 1000 * Long.parseLong(suspendDurationOnFailure.getText().trim()); + definition.setInitialSuspendDuration(new Value(String.valueOf(suspendDurationValue))); + definition.setSuspendProgressionFactor(new Value(String.valueOf((float) 1.0))); } catch (NumberFormatException e) { handleException("The initial suspend duration should be specified " + "as a valid number : " + suspendDurationOnFailure.getText(), e); @@ -283,21 +271,20 @@ public EndpointDefinition createDefinition(OMElement elem) { SynapseConstants.SYNAPSE_NAMESPACE, XMLConfigConstants.ERROR_CODES)); if (suspendCodes != null && suspendCodes.getText() != null) { - String trimmedSuspendCodes = suspendCodes.getText().trim(); - if (isExpression(trimmedSuspendCodes)) { - definition.setDynamicSuspendErrorCodes(valueFactory.createTextValue(suspendCodes)); - } else { - StringTokenizer st = new StringTokenizer(trimmedSuspendCodes, ", "); + Value suspendErrorCodesValue = valueFactory.createTextValue(suspendCodes); + if (suspendErrorCodesValue.getKeyValue() != null) { + StringTokenizer st = new StringTokenizer(suspendCodes.getText().trim(), ", "); while (st.hasMoreTokens()) { String s = st.nextToken(); try { - definition.addSuspendErrorCode(Integer.parseInt(s)); + Integer.parseInt(s); } catch (NumberFormatException e) { handleException("The suspend error codes should be specified " + "as valid numbers separated by commas : " + suspendCodes.getText(), e); } } } + definition.setSuspendErrorCodes(valueFactory.createTextValue(suspendCodes)); } OMElement initialDuration = suspendOnFailure.getFirstChildWithName(new QName( @@ -305,13 +292,11 @@ public EndpointDefinition createDefinition(OMElement elem) { XMLConfigConstants.SUSPEND_INITIAL_DURATION)); if (initialDuration != null && initialDuration.getText() != null) { try { - String initialDurationTrimmed = initialDuration.getText().trim(); - if (isExpression(initialDurationTrimmed)) { - definition.setDynamicInitialSuspendDuration(valueFactory.createTextValue(initialDuration)); - } else { - definition.setInitialSuspendDuration( - Integer.parseInt(initialDurationTrimmed)); + Value initialSuspendDurationValue = valueFactory.createTextValue(initialDuration); + if (initialSuspendDurationValue.getKeyValue() != null) { + Integer.parseInt(initialSuspendDurationValue.getKeyValue()); } + definition.setInitialSuspendDuration(initialSuspendDurationValue); } catch (NumberFormatException e) { handleException("The initial suspend duration should be specified " + "as a valid number : " + initialDuration.getText(), e); @@ -323,13 +308,11 @@ public EndpointDefinition createDefinition(OMElement elem) { XMLConfigConstants.SUSPEND_PROGRESSION_FACTOR)); if (progressionFactor != null && progressionFactor.getText() != null) { try { - String trimmedProgressionFactor = progressionFactor.getText().trim(); - if (isExpression(trimmedProgressionFactor)) { - definition.setDynamicSuspendProgressionFactor(valueFactory.createTextValue(progressionFactor)); - } else { - definition.setSuspendProgressionFactor( - Float.parseFloat(trimmedProgressionFactor)); + Value progressionFactorValue = valueFactory.createTextValue(progressionFactor); + if (progressionFactorValue.getKeyValue() != null) { + Float.parseFloat(progressionFactorValue.getKeyValue()); } + definition.setSuspendProgressionFactor(progressionFactorValue); } catch (NumberFormatException e) { handleException("The suspend duration progression factor should be specified " + "as a valid float : " + progressionFactor.getText(), e); @@ -341,13 +324,11 @@ public EndpointDefinition createDefinition(OMElement elem) { XMLConfigConstants.SUSPEND_MAXIMUM_DURATION)); if (maximumDuration != null && maximumDuration.getText() != null) { try { - String trimmedMaximumDuration = maximumDuration.getText().trim(); - if (isExpression(trimmedMaximumDuration)) { - definition.setDynamicSuspendMaximumDuration(valueFactory.createTextValue(maximumDuration)); - } else { - definition.setSuspendMaximumDuration( - Long.parseLong(maximumDuration.getText().trim())); + Value suspendMaximumDurationValue = valueFactory.createTextValue(maximumDuration); + if (suspendMaximumDurationValue.getKeyValue() != null) { + Long.parseLong(suspendMaximumDurationValue.getKeyValue()); } + definition.setSuspendMaximumDuration(suspendMaximumDurationValue); } catch (NumberFormatException e) { handleException("The maximum suspend duration should be specified " + "as a valid number : " + maximumDuration.getText(), e); @@ -403,14 +384,6 @@ public EndpointDefinition createDefinition(OMElement elem) { return definition; } - private boolean isExpression(String expressionStr) { - Pattern pattern = Pattern.compile("\\{.*\\}"); - if (pattern.matcher(expressionStr).matches()) { - return true; - } - return false; - } - protected static void handleException(String msg) { log.error(msg); throw new SynapseException(msg); diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java index 1b41e6e280..b3a7390e59 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java @@ -102,33 +102,32 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, element.addChild(sec); } - if (endpointDefinition.getTimeoutAction() != SynapseConstants.NONE || endpointDefinition.getDynamicTimeoutAction() != null || - endpointDefinition.getTimeoutDuration() > 0 || endpointDefinition.isDynamicTimeoutEndpoint()) { + if (!endpointDefinition.getTimeoutAction().equals("none") || endpointDefinition.isTimeoutActionDynamic() || + isStringPositiveNumber(endpointDefinition.getTimeoutDuration()) || endpointDefinition.isDynamicTimeoutEndpoint()) { OMElement timeout = fac.createOMElement( "timeout", SynapseConstants.SYNAPSE_OMNAMESPACE); element.addChild(timeout); - if (endpointDefinition.getTimeoutDuration() > 0 || endpointDefinition.isDynamicTimeoutEndpoint()) { + if (isStringPositiveNumber(endpointDefinition.getTimeoutDuration()) || endpointDefinition.isDynamicTimeoutEndpoint()) { OMElement duration = fac.createOMElement( "duration", SynapseConstants.SYNAPSE_OMNAMESPACE); if (!endpointDefinition.isDynamicTimeoutEndpoint()) { - duration.setText(Long.toString(endpointDefinition.getTimeoutDuration())); + duration.setText(endpointDefinition.getTimeoutDuration()); } else { - duration.setText('{' + endpointDefinition.getDynamicTimeoutExpression().getExpression() + '}'); + duration.setText('{' + endpointDefinition.getTimeoutDuration() + '}'); } timeout.addChild(duration); } - if (endpointDefinition.getTimeoutAction() != SynapseConstants.NONE || endpointDefinition.getDynamicTimeoutAction() != null) { + if (!endpointDefinition.getTimeoutAction().equals("none") || endpointDefinition.isTimeoutActionDynamic()) { OMElement action = fac.createOMElement("responseAction", SynapseConstants.SYNAPSE_OMNAMESPACE); if (endpointDefinition.isTimeoutActionDynamic()) { - action.setText('{' + endpointDefinition.getDynamicTimeoutAction().getExpression() + '}'); + action.setText('{' + endpointDefinition.getTimeoutAction() + '}'); } else { - if (endpointDefinition.getTimeoutAction() == SynapseConstants.DISCARD) { + if (endpointDefinition.getTimeoutAction().equals("discard")) { action.setText("discard"); - } else if (endpointDefinition.getTimeoutAction() - == SynapseConstants.DISCARD_AND_FAULT) { + } else if (endpointDefinition.getTimeoutAction().equals("fault")) { action.setText("fault"); } } @@ -136,7 +135,7 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, } } - if (endpointDefinition.getInitialSuspendDuration() != -1 || endpointDefinition.isInitialSuspendDurationDynamic() || + if (!endpointDefinition.getInitialSuspendDuration().equals(String.valueOf(-1)) || endpointDefinition.isInitialSuspendDurationDynamic() || !endpointDefinition.getSuspendErrorCodes().isEmpty() || endpointDefinition.isSuspendErrorCodesDynamic()) { OMElement suspendOnFailure = fac.createOMElement( @@ -148,7 +147,7 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, org.apache.synapse.config.xml.XMLConfigConstants.ERROR_CODES, SynapseConstants.SYNAPSE_OMNAMESPACE); if (endpointDefinition.isSuspendErrorCodesDynamic()) { - errorCodes.setText('{' + endpointDefinition.getDynamicSuspendErrorCodes().getExpression() + '}'); + errorCodes.setText('{' + endpointDefinition.getSuspendErrorCodes() + '}'); } else { errorCodes.setText(endpointDefinition.getSuspendErrorCodes(). toString().replaceAll("[\\[\\] ]", "")); @@ -156,39 +155,39 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, suspendOnFailure.addChild(errorCodes); } - if (endpointDefinition.getInitialSuspendDuration() != -1 || endpointDefinition.isInitialSuspendDurationDynamic()) { + if (!endpointDefinition.getInitialSuspendDuration().equals(String.valueOf(-1)) || endpointDefinition.isInitialSuspendDurationDynamic()) { OMElement initialDuration = fac.createOMElement( org.apache.synapse.config.xml.XMLConfigConstants.SUSPEND_INITIAL_DURATION, SynapseConstants.SYNAPSE_OMNAMESPACE); if (!endpointDefinition.isInitialSuspendDurationDynamic()){ - initialDuration.setText(Long.toString(endpointDefinition.getInitialSuspendDuration())); + initialDuration.setText(endpointDefinition.getInitialSuspendDuration()); } else { - initialDuration.setText('{' + endpointDefinition.getDynamicInitialSuspendDuration().getExpression() + '}'); + initialDuration.setText('{' + endpointDefinition.getInitialSuspendDuration() + '}'); } suspendOnFailure.addChild(initialDuration); } - if (endpointDefinition.getSuspendProgressionFactor() != -1 || endpointDefinition.isSuspendProgressionFactorDynamic()) { + if (!endpointDefinition.getSuspendProgressionFactor().equals(String.valueOf(-1)) || endpointDefinition.isSuspendProgressionFactorDynamic()) { OMElement progressionFactor = fac.createOMElement( org.apache.synapse.config.xml.XMLConfigConstants.SUSPEND_PROGRESSION_FACTOR, SynapseConstants.SYNAPSE_OMNAMESPACE); if (endpointDefinition.isSuspendProgressionFactorDynamic()) { - progressionFactor.setText('{' + endpointDefinition.getDynamicSuspendProgressionFactor().getExpression() + '}'); + progressionFactor.setText('{' + endpointDefinition.getSuspendProgressionFactor() + '}'); } else { - progressionFactor.setText(Float.toString(endpointDefinition.getSuspendProgressionFactor())); + progressionFactor.setText(endpointDefinition.getSuspendProgressionFactor()); } suspendOnFailure.addChild(progressionFactor); } - if ((endpointDefinition.getSuspendMaximumDuration() != -1 && - endpointDefinition.getSuspendMaximumDuration() != Long.MAX_VALUE) || endpointDefinition.isSuspendMaximumDurationDynamic()) { + if ((!endpointDefinition.getSuspendMaximumDuration().equals(String.valueOf(-1)) && + !endpointDefinition.getSuspendMaximumDuration().equals(String.valueOf(Long.MAX_VALUE))) || endpointDefinition.isSuspendMaximumDurationDynamic()) { OMElement suspendMaximum = fac.createOMElement( org.apache.synapse.config.xml.XMLConfigConstants.SUSPEND_MAXIMUM_DURATION, SynapseConstants.SYNAPSE_OMNAMESPACE); if (endpointDefinition.isSuspendMaximumDurationDynamic()) { - suspendMaximum.setText('{' + endpointDefinition.getDynamicSuspendMaximumDuration().getExpression() + '}'); + suspendMaximum.setText('{' + endpointDefinition.getSuspendMaximumDuration() + '}'); } else { - suspendMaximum.setText(Long.toString(endpointDefinition.getSuspendMaximumDuration())); + suspendMaximum.setText(endpointDefinition.getSuspendMaximumDuration()); } suspendOnFailure.addChild(suspendMaximum); } @@ -196,7 +195,7 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, element.addChild(suspendOnFailure); } - if (endpointDefinition.getRetryDurationOnTimeout() > 0 || endpointDefinition.isRetryDurationOnTimeoutDynamic() || + if (isStringPositiveNumber(endpointDefinition.getRetryDurationOnTimeout()) || endpointDefinition.isRetryDurationOnTimeoutDynamic() || !endpointDefinition.getTimeoutErrorCodes().isEmpty() || endpointDefinition.isTimeoutErrorCodesDynamic()) { OMElement markAsTimedout = fac.createOMElement( @@ -208,7 +207,7 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, org.apache.synapse.config.xml.XMLConfigConstants.ERROR_CODES, SynapseConstants.SYNAPSE_OMNAMESPACE); if (endpointDefinition.isTimeoutErrorCodesDynamic()) { - errorCodes.setText('{' + endpointDefinition.getDynamicTimeoutErrorCodes().getExpression() + '}'); + errorCodes.setText('{' + endpointDefinition.getTimeoutErrorCodes() + '}'); } else { errorCodes.setText(endpointDefinition.getTimeoutErrorCodes(). toString().replaceAll("[\\[\\] ]", "")); @@ -216,26 +215,26 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, markAsTimedout.addChild(errorCodes); } - if (endpointDefinition.getRetriesOnTimeoutBeforeSuspend() > 0 || endpointDefinition.isRetriesOnTimeoutBeforeSuspendDynamic()) { + if (isStringPositiveNumber(endpointDefinition.getRetriesOnTimeoutBeforeSuspend()) || endpointDefinition.isRetriesOnTimeoutBeforeSuspendDynamic()) { OMElement retries = fac.createOMElement( org.apache.synapse.config.xml.XMLConfigConstants.RETRIES_BEFORE_SUSPENSION, SynapseConstants.SYNAPSE_OMNAMESPACE); if (endpointDefinition.isRetriesOnTimeoutBeforeSuspendDynamic()) { - retries.setText('{' + endpointDefinition.getDynamicRetriesOnTimeoutBeforeSuspend().getExpression() + '}'); + retries.setText('{' + endpointDefinition.getRetriesOnTimeoutBeforeSuspend() + '}'); } else { - retries.setText(Long.toString(endpointDefinition.getRetriesOnTimeoutBeforeSuspend())); + retries.setText(endpointDefinition.getRetriesOnTimeoutBeforeSuspend()); } markAsTimedout.addChild(retries); } - if (endpointDefinition.getRetryDurationOnTimeout() > 0 || endpointDefinition.isRetryDurationOnTimeoutDynamic()) { + if (isStringPositiveNumber(endpointDefinition.getRetryDurationOnTimeout()) || endpointDefinition.isRetryDurationOnTimeoutDynamic()) { OMElement retryDelay = fac.createOMElement( org.apache.synapse.config.xml.XMLConfigConstants.RETRY_DELAY, SynapseConstants.SYNAPSE_OMNAMESPACE); if (endpointDefinition.isRetryDurationOnTimeoutDynamic()) { - retryDelay.setText('{' + endpointDefinition.getDynamicRetryDurationOnTimeout().getExpression() + '}'); + retryDelay.setText('{' + endpointDefinition.getRetryDurationOnTimeout() + '}'); } else { - retryDelay.setText(Long.toString(endpointDefinition.getRetryDurationOnTimeout())); + retryDelay.setText(endpointDefinition.getRetryDurationOnTimeout()); } markAsTimedout.addChild(retryDelay); } @@ -263,4 +262,16 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, element.addChild(retryConfig); } } + + private boolean isStringPositiveNumber(String input) { + if (input == null || input.isEmpty()) { + return false; + } + try { + float number = Float.parseFloat(input); + return number > 0; + } catch (NumberFormatException e) { + return false; + } + } } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/DynamicLoadbalanceEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/DynamicLoadbalanceEndpoint.java index df16a85f48..13ecce36aa 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/DynamicLoadbalanceEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/DynamicLoadbalanceEndpoint.java @@ -40,6 +40,7 @@ import org.apache.synapse.endpoints.dispatch.HttpSessionDispatcher; import org.apache.synapse.endpoints.dispatch.SALSessions; import org.apache.synapse.endpoints.dispatch.SessionInformation; +import org.apache.synapse.mediators.Value; import org.apache.synapse.transport.nhttp.NhttpConstants; import java.net.MalformedURLException; @@ -456,7 +457,7 @@ private Endpoint getEndpoint(EndpointReference to, Member member, MessageContext endpoint.setName("DLB:" + member.getHostName() + ":" + member.getPort() + ":" + UUID.randomUUID()); EndpointDefinition definition = new EndpointDefinition(); - definition.setSuspendMaximumDuration(10000); + definition.setSuspendMaximumDuration(new Value("10000")); definition.setReplicationDisabled(true); definition.setAddress(to.getAddress()); endpoint.setDefinition(definition); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java index ab72d5c995..4dbbea3b29 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java @@ -132,11 +132,6 @@ public class EndpointDefinition implements AspectConfigurable { */ private String charSetEncoding; - /** - * The expression to evaluate dynamic timeout. - */ - private Value dynamicTimeout = null; - /** * Whether endpoint state replication should be disabled or not (only valid in clustered setups) */ @@ -148,7 +143,8 @@ public class EndpointDefinition implements AspectConfigurable { * not set any timeout configuration, default timeout action is set to NONE, which won't do * anything for timeouts. */ - private long timeoutDuration = 0; + private final long defaultTimeoutDuration = 0; + private Value timeoutDuration = new Value(String.valueOf(defaultTimeoutDuration)); /** * Effective timeout interval for the endpoint @@ -158,41 +154,36 @@ public class EndpointDefinition implements AspectConfigurable { /** * action to perform when a timeout occurs (NONE | DISCARD | DISCARD_AND_FAULT) * */ - private int timeoutAction = SynapseConstants.NONE; - /** The expression to evaluate dynamic timeout action */ - private Value dynamicTimeoutAction = null; + private final int defaultTimeoutAction = SynapseConstants.NONE; + private Value timeoutAction = new Value("none"); /** The initial suspend duration when an endpoint is marked inactive */ - private long initialSuspendDuration = -1; + private final long defaultInitialSuspendDuration = -1; + private Value initialSuspendDuration = new Value(String.valueOf(defaultInitialSuspendDuration)); + /** - * The expression to evaluate dynamic initial suspend duration. + * The suspend duration ratio for the next duration - this is the geometric series multipler */ - private Value dynamicInitialSuspendDuration = null; - /** The suspend duration ratio for the next duration - this is the geometric series multipler */ - private float suspendProgressionFactor = 1; - /** The expression to evaluate dynamic suspend progression factor */ - private Value dynamicSuspendProgressionFactor = null; + private final float defaultSuspendProgressionFactor = 1; + private Value suspendProgressionFactor = new Value(String.valueOf(defaultSuspendProgressionFactor)); + /** This is the maximum duration for which a node will be suspended */ - private long suspendMaximumDuration = Long.MAX_VALUE; - /** The expression to maximum duration for which a node will be suspended */ - private Value dynamicSuspendMaximumDuration = null; + private final long defaultSuspendMaximumDuration = Long.MAX_VALUE; + private Value suspendMaximumDuration = new Value(String.valueOf(defaultSuspendMaximumDuration)); + /** A list of error codes, which directly puts an endpoint into suspend mode */ - private final List suspendErrorCodes = new ArrayList(); - /** The expression to evaluate dynamic suspend error codes */ - private Value dynamicSuspendErrorCodes = null; + private Value suspendErrorCodes = new Value(""); /** No of retries to attempt on timeout, before an endpoint is makred inactive */ - private int retriesOnTimeoutBeforeSuspend = 0; - /** The expression to evaluate dynamic retries on timeout before suspend */ - private Value dynamicRetriesOnTimeoutBeforeSuspend = null; + private final int defaultRetriesOnTimeOutBeforeSuspend = 0; + private Value retriesOnTimeoutBeforeSuspend = new Value(String.valueOf(defaultRetriesOnTimeOutBeforeSuspend)); + /** The delay between retries for a timeout out endpoint */ - private int retryDurationOnTimeout = 0; - /** The expression to evaluate dynamic retry duration on timeout */ - private Value dynamicRetryDurationOnTimeout = null; + private final int defaultRetryDurationOnTimeout = 0; + private Value retryDurationOnTimeout = new Value(String.valueOf(defaultRetryDurationOnTimeout)); + /** A list of error codes which puts the endpoint into timeout mode */ - private final List timeoutErrorCodes = new ArrayList(); - /** The expression to evaluate dynamic timeout error codes */ - private Value dynamicTimeoutErrorCodes = null; + private Value timeoutErrorCodes = new Value(""); private AspectConfiguration aspectConfiguration; @@ -221,29 +212,16 @@ public EndpointDefinition() { } } - public void setDynamicTimeoutExpression(Value expression) { - this.dynamicTimeout = expression; - } - - public SynapsePath getDynamicTimeoutExpression() { - if (dynamicTimeout == null) { - return null; - } - return this.dynamicTimeout.getExpression(); - } - public boolean isDynamicTimeoutEndpoint() { - if (this.dynamicTimeout != null) { - return true; - } else { - return false; - } + + return timeoutDuration.getExpression() != null; } public long evaluateDynamicEndpointTimeout(MessageContext synCtx) { + long timeoutMilliSeconds; try { - String stringValue = dynamicTimeout.evaluateValue(synCtx); + String stringValue = timeoutDuration.evaluateValue(synCtx); if (stringValue != null) { timeoutMilliSeconds = Long.parseLong(stringValue.trim()); } else { @@ -559,8 +537,12 @@ public void setUseSwa(boolean useSwa) { this.useSwa = useSwa; } - public long getTimeoutDuration() { - return timeoutDuration; + public String getTimeoutDuration() { + + if (timeoutDuration.getKeyValue() != null) { + return timeoutDuration.getKeyValue(); + } + return timeoutDuration.getExpression().getExpression(); } /** @@ -580,64 +562,49 @@ public long getEffectiveTimeout() { * * @param timeoutDuration a duration in milliseconds */ - public void setTimeoutDuration(long timeoutDuration) { + public void setTimeoutDuration(Value timeoutDuration) { + this.timeoutDuration = timeoutDuration; - this.effectiveTimeout = timeoutDuration; + if (timeoutDuration.getKeyValue() != null) { + this.effectiveTimeout = Long.parseLong(timeoutDuration.getKeyValue()); + } this.endpointTimeoutType = SynapseConstants.ENDPOINT_TIMEOUT_TYPE.ENDPOINT_TIMEOUT; } - public int getTimeoutAction() { - return timeoutAction; - } + public String getTimeoutAction() { - public void setTimeoutAction(int timeoutAction) { - this.timeoutAction = timeoutAction; - } - - public SynapsePath getDynamicTimeoutAction() { - - if (dynamicTimeoutAction == null) { - return null; + if (timeoutAction.getKeyValue() != null) { + return timeoutAction.getKeyValue(); } - return dynamicTimeoutAction.getExpression(); + return timeoutAction.getExpression().getExpression(); } - public void setDynamicTimeoutAction(Value dynamicTimeoutAction) { + public void setTimeoutAction(Value timeoutAction) { - this.dynamicTimeoutAction = dynamicTimeoutAction; + this.timeoutAction = timeoutAction; } public boolean isTimeoutActionDynamic() { - return dynamicTimeoutAction != null; + return timeoutAction.getExpression() != null; } - public int evaluateDynamicTimeoutAction(MessageContext synCtx) { + public int getResolvedTimeoutAction(MessageContext synCtx) { - int result = timeoutAction; - try { - String timeoutActionStr = dynamicTimeoutAction.evaluateValue(synCtx); - if (timeoutActionStr != null) { - if (EPConstants.DISCARD.equalsIgnoreCase(timeoutActionStr)) { - result = SynapseConstants.DISCARD; - } else if (EPConstants.FAULT.equalsIgnoreCase(timeoutActionStr)) { - result = SynapseConstants.DISCARD_AND_FAULT; - } + int result = defaultTimeoutAction; + String timeoutActionStr = timeoutAction.evaluateValue(synCtx); + if (timeoutActionStr != null) { + if (EPConstants.DISCARD.equalsIgnoreCase(timeoutActionStr)) { + result = SynapseConstants.DISCARD; + } else if (EPConstants.FAULT.equalsIgnoreCase(timeoutActionStr)) { + result = SynapseConstants.DISCARD_AND_FAULT; + } else { + log.warn("Invalid timeout action, action : '" + timeoutActionStr + "' is not supported."); } - } catch (NumberFormatException e) { - log.warn("Error while evaluating dynamic endpoint timeout action."); } return result; } - public int getResolvedTimeoutAction(MessageContext messageContext) { - - if (isTimeoutActionDynamic()) { - return evaluateDynamicTimeoutAction(messageContext); - } - return timeoutAction; - } - public String getFormat() { return format; } @@ -669,8 +636,12 @@ public void setCharSetEncoding(String charSetEncoding) { * * @return suspendOnFailDuration */ - public long getInitialSuspendDuration() { - return initialSuspendDuration; + public String getInitialSuspendDuration() { + + if (initialSuspendDuration.getKeyValue() != null) { + return initialSuspendDuration.getKeyValue(); + } + return initialSuspendDuration.getExpression().getExpression(); } /** @@ -678,253 +649,182 @@ public long getInitialSuspendDuration() { * * @param initialSuspendDuration a duration in milliseconds */ - public void setInitialSuspendDuration(long initialSuspendDuration) { + public void setInitialSuspendDuration(Value initialSuspendDuration) { + this.initialSuspendDuration = initialSuspendDuration; } - public SynapsePath getDynamicInitialSuspendDuration() { + public boolean isInitialSuspendDurationDynamic() { - if (dynamicInitialSuspendDuration == null) { - return null; - } - return dynamicInitialSuspendDuration.getExpression(); + return initialSuspendDuration.getExpression() != null; } - public void setDynamicInitialSuspendDuration(Value dynamicInitialSuspendDuration) { - - this.dynamicInitialSuspendDuration = dynamicInitialSuspendDuration; - } + public long getResolvedInitialSuspendDuration(MessageContext synCtx) { - public boolean isInitialSuspendDurationDynamic() { - if (dynamicInitialSuspendDuration != null) { - return true; - } else { - return false; - } - } - - public long evaluateDynamicInitialSuspendDuration(MessageContext synCtx) { - long result = initialSuspendDuration; + long result = defaultInitialSuspendDuration; + String stringValue = ""; try { - String stringValue = dynamicInitialSuspendDuration.evaluateValue(synCtx); + stringValue = initialSuspendDuration.evaluateValue(synCtx); if (stringValue != null) { result = Long.parseLong(stringValue.trim()); } } catch (NumberFormatException e) { - log.warn("Error while evaluating dynamic initial suspend duration."); + log.warn("Error while evaluating initial suspend duration. The resolved value '" + stringValue + "' should be a valid number. Hence the default value '" + defaultInitialSuspendDuration + "' is used."); } return result; } - public long getResolvedInitialSuspendDuration(MessageContext messageContext) { - if (isInitialSuspendDurationDynamic()) { - return evaluateDynamicInitialSuspendDuration(messageContext); - } - return initialSuspendDuration; - } - -// public int getTraceState() { -// return traceState; -// } -// -// public void setTraceState(int traceState) { -// this.traceState = traceState; -// } - - public float getSuspendProgressionFactor() { - return suspendProgressionFactor; - } - - public void setSuspendProgressionFactor(float suspendProgressionFactor) { - this.suspendProgressionFactor = suspendProgressionFactor; - } - - public SynapsePath getDynamicSuspendProgressionFactor() { + public String getSuspendProgressionFactor() { - if (dynamicSuspendProgressionFactor == null) { - return null; + if (suspendProgressionFactor.getKeyValue() != null) { + return suspendProgressionFactor.getKeyValue(); } - return dynamicSuspendProgressionFactor.getExpression(); + return suspendProgressionFactor.getExpression().getExpression(); } - public void setDynamicSuspendProgressionFactor(Value dynamicSuspendProgressionFactor) { + public void setSuspendProgressionFactor(Value suspendProgressionFactor) { - this.dynamicSuspendProgressionFactor = dynamicSuspendProgressionFactor; + this.suspendProgressionFactor = suspendProgressionFactor; } public boolean isSuspendProgressionFactorDynamic() { - return dynamicSuspendProgressionFactor != null; + return suspendProgressionFactor.getExpression() != null; } - public float evaluateDynamicSuspendProgressionFactor(MessageContext messageContext) { + public float getResolvedSuspendProgressionFactor(MessageContext messageContext) { - float result = suspendProgressionFactor; + float result = defaultSuspendProgressionFactor; + String stringValue = ""; try { - String stringValue = dynamicSuspendProgressionFactor.evaluateValue(messageContext); + stringValue = suspendProgressionFactor.evaluateValue(messageContext); if (stringValue != null) { result = Float.parseFloat(stringValue); } } catch (NumberFormatException e) { - log.warn("Error while evaluating dynamic endpoint timeout expression."); + log.warn("Error while evaluating suspend duration progression factor. The resolved value '" + stringValue + "' should be a valid float. Hence the default value '" + defaultSuspendProgressionFactor + "' is used."); } return result; } - public float getResolvedSuspendProgressionFactor(MessageContext messageContext) { - if (isSuspendProgressionFactorDynamic()) { - return evaluateDynamicSuspendProgressionFactor(messageContext); - } - return suspendProgressionFactor; - } - - public long getSuspendMaximumDuration() { - return suspendMaximumDuration; - } + public String getSuspendMaximumDuration() { - public void setSuspendMaximumDuration(long suspendMaximumDuration) { - this.suspendMaximumDuration = suspendMaximumDuration; - } - - public SynapsePath getDynamicSuspendMaximumDuration() { - - if (dynamicSuspendMaximumDuration == null) { - return null; + if (suspendMaximumDuration.getKeyValue() != null) { + return suspendMaximumDuration.getKeyValue(); } - return dynamicSuspendMaximumDuration.getExpression(); + return suspendMaximumDuration.getExpression().getExpression(); } - public void setDynamicSuspendMaximumDuration(Value dynamicSuspendMaximumDuration) { + public void setSuspendMaximumDuration(Value suspendMaximumDuration) { - this.dynamicSuspendMaximumDuration = dynamicSuspendMaximumDuration; + this.suspendMaximumDuration = suspendMaximumDuration; } public boolean isSuspendMaximumDurationDynamic() { - return dynamicSuspendMaximumDuration != null; + return suspendMaximumDuration.getExpression() != null; } - public long evaluateDynamicSuspendMaximumDuration(MessageContext messageContext) { + public long getResolvedSuspendMaximumDuration(MessageContext messageContext) { - long result = suspendMaximumDuration; + long result = defaultSuspendMaximumDuration; + String stringValue = ""; try { - String stringValue = dynamicSuspendMaximumDuration.evaluateValue(messageContext); + stringValue = suspendMaximumDuration.evaluateValue(messageContext); if (stringValue != null) { result = Long.parseLong(stringValue); } } catch (NumberFormatException e) { - log.warn("Error while evaluating dynamic endpoint timeout expression."); + log.warn("Error while evaluating suspend maximum duration. The resolved value '" + stringValue + "' should be a valid number. Hence the default value '" + defaultSuspendMaximumDuration + "' is used."); } return result; } - public long getResolvedSuspendMaximumDuration(MessageContext messageContext) { - if (isSuspendMaximumDurationDynamic()) { - return evaluateDynamicSuspendMaximumDuration(messageContext); - } - return suspendMaximumDuration; - } - - public int getRetriesOnTimeoutBeforeSuspend() { - return retriesOnTimeoutBeforeSuspend; - } - - public void setRetriesOnTimeoutBeforeSuspend(int retriesOnTimeoutBeforeSuspend) { - this.retriesOnTimeoutBeforeSuspend = retriesOnTimeoutBeforeSuspend; - } - - public SynapsePath getDynamicRetriesOnTimeoutBeforeSuspend() { + public String getRetriesOnTimeoutBeforeSuspend() { - if (dynamicRetriesOnTimeoutBeforeSuspend == null) { - return null; + if (retriesOnTimeoutBeforeSuspend.getKeyValue() != null) { + return retriesOnTimeoutBeforeSuspend.getKeyValue(); } - return dynamicRetriesOnTimeoutBeforeSuspend.getExpression(); + return retriesOnTimeoutBeforeSuspend.getExpression().getExpression(); } - public void setDynamicRetriesOnTimeoutBeforeSuspend(Value dynamicRetriesOnTimeoutBeforeSuspend) { + public void setRetriesOnTimeoutBeforeSuspend(Value retriesOnTimeoutBeforeSuspend) { - this.dynamicRetriesOnTimeoutBeforeSuspend = dynamicRetriesOnTimeoutBeforeSuspend; + this.retriesOnTimeoutBeforeSuspend = retriesOnTimeoutBeforeSuspend; } public boolean isRetriesOnTimeoutBeforeSuspendDynamic() { - return dynamicRetriesOnTimeoutBeforeSuspend != null; + return retriesOnTimeoutBeforeSuspend.getExpression() != null; } - public int evaluateDynamicRetriesOnTimeoutBeforeSuspend(MessageContext messageContext) { + public int getResolvedRetriesOnTimeoutBeforeSuspend(MessageContext messageContext) { - int result = retriesOnTimeoutBeforeSuspend; + int result = defaultRetriesOnTimeOutBeforeSuspend; + String stringValue = ""; try { - String stringValue = dynamicRetriesOnTimeoutBeforeSuspend.evaluateValue(messageContext); + stringValue = retriesOnTimeoutBeforeSuspend.evaluateValue(messageContext); if (stringValue != null) { result = Integer.parseInt(stringValue); } } catch (NumberFormatException e) { - log.warn("Error while evaluating dynamic endpoint timeout expression."); + log.warn("Error while evaluating retries before suspend [for timeouts]. The resolved value '" + stringValue + "' should be a valid number. Hence the default value '" + defaultRetriesOnTimeOutBeforeSuspend + "' is used."); } return result; } - public int getResolvedRetriesOnTimeoutBeforeSuspend(MessageContext messageContext) { - if (isRetriesOnTimeoutBeforeSuspendDynamic()) { - return evaluateDynamicRetriesOnTimeoutBeforeSuspend(messageContext); - } - return retriesOnTimeoutBeforeSuspend; - } + public String getRetryDurationOnTimeout() { - public int getRetryDurationOnTimeout() { - return retryDurationOnTimeout; - } - - public void setRetryDurationOnTimeout(int retryDurationOnTimeout) { - this.retryDurationOnTimeout = retryDurationOnTimeout; - } - - public SynapsePath getDynamicRetryDurationOnTimeout() { - - if (dynamicRetryDurationOnTimeout == null) { - return null; + if (retryDurationOnTimeout.getKeyValue() != null) { + return retryDurationOnTimeout.getKeyValue(); } - return dynamicRetryDurationOnTimeout.getExpression(); + return retryDurationOnTimeout.getExpression().getExpression(); } - public void setDynamicRetryDurationOnTimeout(Value dynamicRetryDurationOnTimeout) { + public void setRetryDurationOnTimeout(Value retryDurationOnTimeout) { - this.dynamicRetryDurationOnTimeout = dynamicRetryDurationOnTimeout; + this.retryDurationOnTimeout = retryDurationOnTimeout; } public boolean isRetryDurationOnTimeoutDynamic() { - return dynamicRetryDurationOnTimeout != null; + return retryDurationOnTimeout.getExpression() != null; } - public int evaluateDynamicRetryDurationOnTimeout(MessageContext messageContext) { + public int getResolvedRetryDurationOnTimeout(MessageContext messageContext) { - int result = retryDurationOnTimeout; + int result = defaultRetryDurationOnTimeout; + String stringValue = ""; try { - String stringValue = dynamicRetryDurationOnTimeout.evaluateValue(messageContext); + stringValue = retryDurationOnTimeout.evaluateValue(messageContext); if (stringValue != null) { result = Integer.parseInt(stringValue); } } catch (NumberFormatException e) { - log.warn("Error while evaluating dynamic endpoint timeout expression."); + log.warn("Error while evaluating retry delay for timeouts. The resolved value '" + stringValue + "' should be a valid number. Hence the default value '" + defaultRetryDurationOnTimeout + "' is used."); } return result; } - public int getResolvedRetryDurationOnTimeout(MessageContext messageContext) { - if (isRetryDurationOnTimeoutDynamic()) { - return evaluateDynamicRetryDurationOnTimeout(messageContext); + public String getSuspendErrorCodes() { + + if (suspendErrorCodes.getKeyValue() != null) { + return suspendErrorCodes.getKeyValue(); } - return retryDurationOnTimeout; + return suspendErrorCodes.getExpression().getExpression(); } - public List getSuspendErrorCodes() { - return suspendErrorCodes; + public boolean isSuspendErrorCodesDynamic() { + + return suspendErrorCodes.getExpression() != null; } - public List getTimeoutErrorCodes() { - return timeoutErrorCodes; + public String getTimeoutErrorCodes() { + + if (timeoutErrorCodes.getKeyValue() != null) { + return timeoutErrorCodes.getKeyValue(); + } + return timeoutErrorCodes.getExpression().getExpression(); } public List getRetryDisabledErrorCodes() { @@ -943,33 +843,24 @@ public void setReplicationDisabled(boolean replicationDisabled) { this.replicationDisabled = replicationDisabled; } - public void addSuspendErrorCode(int code) { - suspendErrorCodes.add(code); - } + public void setSuspendErrorCodes(Value suspendErrorCodes) { - public SynapsePath getDynamicSuspendErrorCodes() { - - if (dynamicSuspendErrorCodes == null) { - return null; - } - return dynamicSuspendErrorCodes.getExpression(); - } - - public void setDynamicSuspendErrorCodes(Value dynamicSuspendErrorCodes) { - - this.dynamicSuspendErrorCodes = dynamicSuspendErrorCodes; + this.suspendErrorCodes = suspendErrorCodes; } - public boolean isSuspendErrorCodesDynamic() { + public void addSuspendErrorCode(int code) { - return dynamicSuspendErrorCodes != null; + if (suspendErrorCodes.getKeyValue() != null) { + suspendErrorCodes = new Value(suspendErrorCodes.getKeyValue() + "," + code); + } } - public List evaluateDynamicSuspendErrorCodes(MessageContext messageContext) { + public List getResolvedSuspendErrorCodes(MessageContext messageContext) { - List result = suspendErrorCodes; + List result = new ArrayList<>(); + String stringValue = ""; try { - String stringValue = dynamicSuspendErrorCodes.evaluateValue(messageContext); + stringValue = suspendErrorCodes.evaluateValue(messageContext); if (stringValue != null) { String[] errorCodes = stringValue.split(","); result = new ArrayList(); @@ -978,45 +869,34 @@ public List evaluateDynamicSuspendErrorCodes(MessageContext messageCont } } } catch (NumberFormatException e) { - log.warn("Error while evaluating dynamic endpoint timeout expression."); + log.warn("Error while evaluating suspend error codes. The resolved value '" + stringValue + "' should be valid numbers separated by commas."); } return result; } - public List getResolvedSuspendErrorCodes(MessageContext messageContext) { - if (isSuspendErrorCodesDynamic()) { - return evaluateDynamicSuspendErrorCodes(messageContext); - } - return suspendErrorCodes; - } + public void setTimeoutErrorCodes(Value timeoutErrorCodes) { - public void addTimeoutErrorCode(int code) { - timeoutErrorCodes.add(code); + this.timeoutErrorCodes = timeoutErrorCodes; } - public SynapsePath getDynamicTimeoutErrorCodes() { + public void addTimeoutErrorCode(int code) { - if (dynamicTimeoutErrorCodes == null) { - return null; + if (timeoutErrorCodes.getKeyValue() != null) { + timeoutErrorCodes = new Value(timeoutErrorCodes.getKeyValue() + "," + code); } - return dynamicTimeoutErrorCodes.getExpression(); - } - - public void setDynamicTimeoutErrorCodes(Value dynamicTimeoutErrorCodes) { - - this.dynamicTimeoutErrorCodes = dynamicTimeoutErrorCodes; } public boolean isTimeoutErrorCodesDynamic() { - return dynamicTimeoutErrorCodes != null; + return timeoutErrorCodes.getExpression() != null; } - public List evaluateDynamicTimeoutErrorCodes(MessageContext messageContext) { + public List getResolvedTimeoutErrorCodes(MessageContext messageContext) { - List result = timeoutErrorCodes; + List result = new ArrayList<>(); + String stringValue = ""; try { - String stringValue = dynamicTimeoutErrorCodes.evaluateValue(messageContext); + stringValue = timeoutErrorCodes.evaluateValue(messageContext); if (stringValue != null) { String[] errorCodes = stringValue.split(","); result = new ArrayList(); @@ -1025,18 +905,11 @@ public List evaluateDynamicTimeoutErrorCodes(MessageContext messageCont } } } catch (NumberFormatException e) { - log.warn("Error while evaluating dynamic endpoint timeout expression."); + log.warn("Error while evaluating timeout error codes. The resolved value '" + stringValue + "' should be valid numbers separated by commas."); } return result; } - public List getResolvedTimeoutErrorCodes(MessageContext messageContext) { - if (isTimeoutErrorCodesDynamic()) { - return evaluateDynamicTimeoutErrorCodes(messageContext); - } - return timeoutErrorCodes; - } - public void addRetryDisabledErrorCode(int code) { retryDisabledErrorCodes.add(code); } diff --git a/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java b/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java index d99d6ee521..6b2edb80b5 100644 --- a/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java +++ b/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java @@ -178,8 +178,8 @@ public void testSetSuspendOnFailureInitialDuration() throws XMLStreamException, OMElement omElement = AXIOMUtil.stringToOM( "10100010"); EndpointDefinition ep1 = httpEndpointFactory.createEndpointDefinition(omElement); - Assert.assertEquals(ep1.getRetriesOnTimeoutBeforeSuspend(), 0); - Assert.assertEquals(ep1.getInitialSuspendDuration(), 1000); + Assert.assertEquals(ep1.getRetriesOnTimeoutBeforeSuspend(), "0"); + Assert.assertEquals(ep1.getInitialSuspendDuration(), "1000"); } @Test diff --git a/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java b/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java index a2e4b101c0..0010f054a5 100644 --- a/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java +++ b/modules/core/src/test/java/org/apache/synapse/endpoints/dynamic/DynamicEndpointTest.java @@ -26,7 +26,6 @@ import org.apache.synapse.endpoints.AbstractEndpoint; import org.apache.synapse.endpoints.AddressEndpoint; import org.apache.synapse.endpoints.EndpointDefinition; -import org.apache.synapse.util.xpath.SynapseXPath; import java.util.ArrayList; import java.util.List; @@ -39,7 +38,7 @@ public void testContextProperties() throws Exception { AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicTimeoutExpression(new ValueFactory().createTextValue(omElement)); + definition.setTimeoutDuration(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("timeout", "90000"); assertEquals(90000, @@ -52,7 +51,7 @@ public void testContextPropertiesForInitialSuspendDuration() throws Exception { AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicInitialSuspendDuration(new ValueFactory().createTextValue(omElement)); + definition.setInitialSuspendDuration(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("suspendInitialDuration", "90000"); assertEquals(endpoint.getDefinition().getResolvedInitialSuspendDuration(synCtx), 90000); @@ -64,7 +63,7 @@ public void testContextPropertiesForNoInitialSuspendDuration() throws Exception AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicInitialSuspendDuration(new ValueFactory().createTextValue(omElement)); + definition.setInitialSuspendDuration(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); assertEquals(-1, endpoint.getDefinition().getResolvedInitialSuspendDuration(synCtx)); } @@ -75,7 +74,7 @@ public void testContextPropertiesForSuspendMaximumDuration() throws Exception { AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicSuspendMaximumDuration(new ValueFactory().createTextValue(omElement)); + definition.setSuspendMaximumDuration(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("suspendMaximumDuration", "90000"); assertEquals(endpoint.getDefinition().getResolvedSuspendMaximumDuration(synCtx), 90000); @@ -87,7 +86,7 @@ public void testContextPropertiesForNoSuspendMaximumDuration() throws Exception AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicSuspendMaximumDuration(new ValueFactory().createTextValue(omElement)); + definition.setSuspendMaximumDuration(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); assertEquals(Long.MAX_VALUE, endpoint.getDefinition().getResolvedSuspendMaximumDuration(synCtx)); } @@ -98,7 +97,7 @@ public void testContextPropertiesForSuspendProgressionFactor() throws Exception AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicSuspendProgressionFactor(new ValueFactory().createTextValue(omElement)); + definition.setSuspendProgressionFactor(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("suspendProgressionFactor", "2"); assertEquals(endpoint.getDefinition().getResolvedSuspendProgressionFactor(synCtx), 2.0f); @@ -110,7 +109,7 @@ public void testContextPropertiesForNoSuspendProgressionFactor() throws Exceptio AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicSuspendProgressionFactor(new ValueFactory().createTextValue(omElement)); + definition.setSuspendProgressionFactor(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); assertEquals(1.0f, endpoint.getDefinition().getResolvedSuspendProgressionFactor(synCtx)); } @@ -121,7 +120,7 @@ public void testContextPropertiesForRetriesOnTimeoutBeforeSuspend() throws Excep AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicRetriesOnTimeoutBeforeSuspend(new ValueFactory().createTextValue(omElement)); + definition.setRetriesOnTimeoutBeforeSuspend(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("retriesOnTimeoutBeforeSuspend", "3"); assertEquals(endpoint.getDefinition().getResolvedRetriesOnTimeoutBeforeSuspend(synCtx), 3); @@ -133,7 +132,7 @@ public void testContextPropertiesForNoRetriesOnTimeoutBeforeSuspend() throws Exc AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicRetriesOnTimeoutBeforeSuspend(new ValueFactory().createTextValue(omElement)); + definition.setRetriesOnTimeoutBeforeSuspend(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); assertEquals(0, endpoint.getDefinition().getResolvedRetriesOnTimeoutBeforeSuspend(synCtx)); } @@ -141,11 +140,10 @@ public void testContextPropertiesForNoRetriesOnTimeoutBeforeSuspend() throws Exc public void testContextPropertiesForRetryDurationOnTimeout() throws Exception { OMElement omElement = AXIOMUtil.stringToOM("{$ctx:retryDurationOnTimeout}"); - SynapseXPath xpath = new SynapseXPath("$ctx:retryDurationOnTimeout"); AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicRetryDurationOnTimeout(new ValueFactory().createTextValue(omElement)); + definition.setRetryDurationOnTimeout(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("retryDurationOnTimeout", "90000"); assertEquals(endpoint.getDefinition().getResolvedRetryDurationOnTimeout(synCtx), 90000); @@ -157,7 +155,7 @@ public void testContextPropertiesForNoRetryDurationOnTimeout() throws Exception AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicRetryDurationOnTimeout(new ValueFactory().createTextValue(omElement)); + definition.setRetryDurationOnTimeout(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); assertEquals(0, endpoint.getDefinition().getResolvedRetryDurationOnTimeout(synCtx)); } @@ -168,7 +166,7 @@ public void testContextPropertiesForTimeoutActionFault() throws Exception { AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicTimeoutAction(new ValueFactory().createTextValue(omElement)); + definition.setTimeoutAction(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("timeoutAction", "fault"); assertEquals(endpoint.getDefinition().getResolvedTimeoutAction(synCtx), SynapseConstants.DISCARD_AND_FAULT); @@ -180,7 +178,7 @@ public void testContextPropertiesForTimeoutActionDiscard() throws Exception { AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicTimeoutAction(new ValueFactory().createTextValue(omElement)); + definition.setTimeoutAction(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("timeoutAction", "discard"); assertEquals(endpoint.getDefinition().getResolvedTimeoutAction(synCtx), SynapseConstants.DISCARD); @@ -192,7 +190,7 @@ public void testContextPropertiesForNoTimeoutAction() throws Exception { AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicTimeoutAction(new ValueFactory().createTextValue(omElement)); + definition.setTimeoutAction(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); assertEquals(endpoint.getDefinition().getResolvedTimeoutAction(synCtx), SynapseConstants.NONE); } @@ -203,7 +201,7 @@ public void testContextPropertiesForSuspendErrorCodes() throws Exception { AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicSuspendErrorCodes(new ValueFactory().createTextValue(omElement)); + definition.setSuspendErrorCodes(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("suspendErrorCodes", "101503,101504"); @@ -220,7 +218,7 @@ public void testContextPropertiesForEmptySuspendErrorCodes() throws Exception { AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicSuspendErrorCodes(new ValueFactory().createTextValue(omElement)); + definition.setSuspendErrorCodes(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("suspendErrorCodes", ""); @@ -235,7 +233,7 @@ public void testContextPropertiesForNoSuspendErrorCodes() throws Exception { AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicSuspendErrorCodes(new ValueFactory().createTextValue(omElement)); + definition.setSuspendErrorCodes(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); List expectedSuspendErrorCodes = endpoint.getDefinition().getResolvedSuspendErrorCodes(synCtx); assertTrue(expectedSuspendErrorCodes.isEmpty()); @@ -247,7 +245,7 @@ public void testContextPropertiesForTimeoutErrorCodes() throws Exception { AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicTimeoutErrorCodes(new ValueFactory().createTextValue(omElement)); + definition.setTimeoutErrorCodes(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("timeoutErrorCodes", "101503,101504"); @@ -264,7 +262,7 @@ public void testContextPropertiesForEmptyTimeoutErrorCodes() throws Exception { AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicTimeoutErrorCodes(new ValueFactory().createTextValue(omElement)); + definition.setTimeoutErrorCodes(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("timeoutErrorCodes", ""); @@ -279,7 +277,7 @@ public void testContextPropertiesForNoTimeoutErrorCodes() throws Exception { AbstractEndpoint endpoint = new AddressEndpoint(); EndpointDefinition definition = new EndpointDefinition(); endpoint.setDefinition(definition); - definition.setDynamicTimeoutErrorCodes(new ValueFactory().createTextValue(omElement)); + definition.setTimeoutErrorCodes(new ValueFactory().createTextValue(omElement)); MessageContext synCtx = new TestMessageContext(); List expectedTimeoutErrorCodes = endpoint.getDefinition().getResolvedTimeoutErrorCodes(synCtx); assertTrue(expectedTimeoutErrorCodes.isEmpty()); From 4383df850448275fdc4bc92c544d0fd0a16e8ff9 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Fri, 6 Dec 2024 14:23:13 +0530 Subject: [PATCH 20/27] add 'never' as default value of timeoutAction --- .../config/xml/endpoints/EndpointDefinitionFactory.java | 9 +++++++-- .../java/org/apache/synapse/endpoints/EPConstants.java | 2 ++ .../org/apache/synapse/endpoints/EndpointDefinition.java | 8 ++++---- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java index b3685893c8..0b55db5f9e 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java @@ -28,6 +28,7 @@ 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.EPConstants; import org.apache.synapse.endpoints.EndpointDefinition; import org.apache.synapse.mediators.Value; import org.apache.synapse.util.xpath.SynapseXPath; @@ -179,8 +180,12 @@ public EndpointDefinition createDefinition(OMElement elem) { if (action != null && action.getText() != null) { Value timeoutActionValue = valueFactory.createTextValue(action); - if (timeoutActionValue.getKeyValue() != null && !timeoutActionValue.getKeyValue().equals("discard") && !timeoutActionValue.getKeyValue().equals("fault")) { - handleException("Invalid timeout action, action : " + timeoutActionValue.getKeyValue() + " is not supported"); + if (timeoutActionValue.getKeyValue() != null && + !timeoutActionValue.getKeyValue().equalsIgnoreCase(EPConstants.NEVER) && + !timeoutActionValue.getKeyValue().equalsIgnoreCase(EPConstants.DISCARD) && + !timeoutActionValue.getKeyValue().equalsIgnoreCase(EPConstants.FAULT)) { + handleException("Invalid timeout action, action : " + timeoutActionValue.getKeyValue() + + " is not supported"); } definition.setTimeoutAction(timeoutActionValue); } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EPConstants.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EPConstants.java index 422b1eb33b..b4db4a4b7a 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EPConstants.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EPConstants.java @@ -28,4 +28,6 @@ public class EPConstants { public static final String DISCARD = "discard"; public static final String FAULT = "fault"; + public static final String NEVER = "never"; + public static final String EMPTRY_STRING = ""; } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java index 4dbbea3b29..2c6e0edce6 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java @@ -155,7 +155,7 @@ public class EndpointDefinition implements AspectConfigurable { * action to perform when a timeout occurs (NONE | DISCARD | DISCARD_AND_FAULT) * */ private final int defaultTimeoutAction = SynapseConstants.NONE; - private Value timeoutAction = new Value("none"); + private Value timeoutAction = new Value(EPConstants.NEVER); /** The initial suspend duration when an endpoint is marked inactive */ private final long defaultInitialSuspendDuration = -1; @@ -172,7 +172,7 @@ public class EndpointDefinition implements AspectConfigurable { private Value suspendMaximumDuration = new Value(String.valueOf(defaultSuspendMaximumDuration)); /** A list of error codes, which directly puts an endpoint into suspend mode */ - private Value suspendErrorCodes = new Value(""); + private Value suspendErrorCodes = new Value(EPConstants.EMPTRY_STRING); /** No of retries to attempt on timeout, before an endpoint is makred inactive */ private final int defaultRetriesOnTimeOutBeforeSuspend = 0; @@ -183,7 +183,7 @@ public class EndpointDefinition implements AspectConfigurable { private Value retryDurationOnTimeout = new Value(String.valueOf(defaultRetryDurationOnTimeout)); /** A list of error codes which puts the endpoint into timeout mode */ - private Value timeoutErrorCodes = new Value(""); + private Value timeoutErrorCodes = new Value(EPConstants.EMPTRY_STRING); private AspectConfiguration aspectConfiguration; @@ -593,7 +593,7 @@ public int getResolvedTimeoutAction(MessageContext synCtx) { int result = defaultTimeoutAction; String timeoutActionStr = timeoutAction.evaluateValue(synCtx); - if (timeoutActionStr != null) { + if (timeoutActionStr != null && !timeoutActionStr.trim().equalsIgnoreCase(EPConstants.NEVER)) { if (EPConstants.DISCARD.equalsIgnoreCase(timeoutActionStr)) { result = SynapseConstants.DISCARD; } else if (EPConstants.FAULT.equalsIgnoreCase(timeoutActionStr)) { From 80a2d642f76703fca252830c47d608f146725208 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Fri, 6 Dec 2024 15:34:50 +0530 Subject: [PATCH 21/27] change delimiter ", " to "," --- .../config/xml/endpoints/EndpointDefinitionFactory.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java index 0b55db5f9e..59ed6ff85d 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java @@ -203,7 +203,7 @@ public EndpointDefinition createDefinition(OMElement elem) { if (timeoutCodes != null && timeoutCodes.getText() != null) { Value timeoutErrorCodesValue = valueFactory.createTextValue(timeoutCodes); if (timeoutErrorCodesValue.getKeyValue() != null) { - StringTokenizer st = new StringTokenizer(timeoutCodes.getText().trim(), ", "); + StringTokenizer st = new StringTokenizer(timeoutCodes.getText().trim(), ","); while (st.hasMoreTokens()) { String s = st.nextToken(); try { @@ -278,7 +278,7 @@ public EndpointDefinition createDefinition(OMElement elem) { if (suspendCodes != null && suspendCodes.getText() != null) { Value suspendErrorCodesValue = valueFactory.createTextValue(suspendCodes); if (suspendErrorCodesValue.getKeyValue() != null) { - StringTokenizer st = new StringTokenizer(suspendCodes.getText().trim(), ", "); + StringTokenizer st = new StringTokenizer(suspendCodes.getText().trim(), ","); while (st.hasMoreTokens()) { String s = st.nextToken(); try { @@ -351,7 +351,7 @@ public EndpointDefinition createDefinition(OMElement elem) { if (retryDisabledErrorCodes != null && retryDisabledErrorCodes.getText() != null) { StringTokenizer st = new StringTokenizer( - retryDisabledErrorCodes.getText().trim(), ", "); + retryDisabledErrorCodes.getText().trim(), ","); while (st.hasMoreTokens()) { String s = st.nextToken(); try { From b1367aa3079c5d8c83bb366f23b862ac7f2c97a1 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Sat, 7 Dec 2024 12:04:10 +0530 Subject: [PATCH 22/27] add tests to check endpoint definition --- .../synapse/endpoints/HttpEndpointTest.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java b/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java index 6b2edb80b5..c2a9cf06f9 100644 --- a/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java +++ b/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java @@ -182,6 +182,27 @@ public void testSetSuspendOnFailureInitialDuration() throws XMLStreamException, Assert.assertEquals(ep1.getInitialSuspendDuration(), "1000"); } + @Test + public void testDefaultErrorHandlingValues() throws XMLStreamException, AxisFault { + + HTTPEndpointFactory httpEndpointFactory = new HTTPEndpointFactory(); + OMElement omElement = AXIOMUtil.stringToOM( + "\n" + + " "); + EndpointDefinition ep = httpEndpointFactory.createEndpointDefinition(omElement); + HTTPEndpoint httpEndpoint = new HTTPEndpoint(); + httpEndpoint.setDefinition(ep); + MessageContext messageContext = createMessageContext(); + Assert.assertEquals("Default timeout action should be 100", 100, httpEndpoint.getDefinition().getResolvedTimeoutAction(messageContext)); + Assert.assertEquals("Default initial suspend duration should be -1", -1, httpEndpoint.getDefinition().getResolvedInitialSuspendDuration(messageContext)); + Assert.assertEquals("Default maximum suspend duration should be 0", Long.MAX_VALUE, httpEndpoint.getDefinition().getResolvedSuspendMaximumDuration(messageContext)); + Assert.assertEquals("Default suspend progression factor should be 1", 1f, httpEndpoint.getDefinition().getResolvedSuspendProgressionFactor(messageContext), 0.0f); + Assert.assertEquals("Default suspend error codes should be empty", new ArrayList<>(), httpEndpoint.getDefinition().getResolvedSuspendErrorCodes(messageContext)); + Assert.assertEquals("Default timeout error codes should be empty", new ArrayList<>(), httpEndpoint.getDefinition().getResolvedTimeoutErrorCodes(messageContext)); + Assert.assertEquals("Default retries on timeout before suspend should be 0", 0, httpEndpoint.getDefinition().getResolvedRetriesOnTimeoutBeforeSuspend(messageContext)); + Assert.assertEquals("Default retry duration on timeout should be 0", 0, httpEndpoint.getDefinition().getResolvedRetryDurationOnTimeout(messageContext)); + } + @Test public void testSetSuspendErrorCodes() throws XMLStreamException, AxisFault { From 9d29be998f46dbeb9d453bc36e2b30e09c267292 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Sun, 8 Dec 2024 22:48:25 +0530 Subject: [PATCH 23/27] address reviewed changes --- .../EndpointDefinitionSerializer.java | 26 ++++---- .../synapse/endpoints/EndpointDefinition.java | 59 +++++++++++-------- 2 files changed, 50 insertions(+), 35 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java index b3a7390e59..fa54066a6f 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionSerializer.java @@ -25,6 +25,7 @@ import org.apache.synapse.SynapseConstants; import org.apache.synapse.aspects.statistics.StatisticsConfigurable; import org.apache.synapse.config.xml.XMLConfigConstants; +import org.apache.synapse.endpoints.EPConstants; import org.apache.synapse.endpoints.EndpointDefinition; public class EndpointDefinitionSerializer { @@ -102,14 +103,14 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, element.addChild(sec); } - if (!endpointDefinition.getTimeoutAction().equals("none") || endpointDefinition.isTimeoutActionDynamic() || - isStringPositiveNumber(endpointDefinition.getTimeoutDuration()) || endpointDefinition.isDynamicTimeoutEndpoint()) { + if (!endpointDefinition.getTimeoutAction().equals(EPConstants.NEVER) || endpointDefinition.isTimeoutActionDynamic() || + isPositiveNumber(endpointDefinition.getTimeoutDuration()) || endpointDefinition.isDynamicTimeoutEndpoint()) { OMElement timeout = fac.createOMElement( "timeout", SynapseConstants.SYNAPSE_OMNAMESPACE); element.addChild(timeout); - if (isStringPositiveNumber(endpointDefinition.getTimeoutDuration()) || endpointDefinition.isDynamicTimeoutEndpoint()) { + if (isPositiveNumber(endpointDefinition.getTimeoutDuration()) || endpointDefinition.isDynamicTimeoutEndpoint()) { OMElement duration = fac.createOMElement( "duration", SynapseConstants.SYNAPSE_OMNAMESPACE); if (!endpointDefinition.isDynamicTimeoutEndpoint()) { @@ -120,15 +121,16 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, timeout.addChild(duration); } - if (!endpointDefinition.getTimeoutAction().equals("none") || endpointDefinition.isTimeoutActionDynamic()) { + if (!endpointDefinition.getTimeoutAction().equals( + EPConstants.NEVER) || endpointDefinition.isTimeoutActionDynamic()) { OMElement action = fac.createOMElement("responseAction", SynapseConstants.SYNAPSE_OMNAMESPACE); if (endpointDefinition.isTimeoutActionDynamic()) { action.setText('{' + endpointDefinition.getTimeoutAction() + '}'); } else { - if (endpointDefinition.getTimeoutAction().equals("discard")) { - action.setText("discard"); - } else if (endpointDefinition.getTimeoutAction().equals("fault")) { - action.setText("fault"); + if (endpointDefinition.getTimeoutAction().equals(EPConstants.DISCARD)) { + action.setText(EPConstants.DISCARD); + } else if (endpointDefinition.getTimeoutAction().equals(EPConstants.FAULT)) { + action.setText(EPConstants.FAULT); } } timeout.addChild(action); @@ -195,7 +197,7 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, element.addChild(suspendOnFailure); } - if (isStringPositiveNumber(endpointDefinition.getRetryDurationOnTimeout()) || endpointDefinition.isRetryDurationOnTimeoutDynamic() || + if (isPositiveNumber(endpointDefinition.getRetryDurationOnTimeout()) || endpointDefinition.isRetryDurationOnTimeoutDynamic() || !endpointDefinition.getTimeoutErrorCodes().isEmpty() || endpointDefinition.isTimeoutErrorCodesDynamic()) { OMElement markAsTimedout = fac.createOMElement( @@ -215,7 +217,7 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, markAsTimedout.addChild(errorCodes); } - if (isStringPositiveNumber(endpointDefinition.getRetriesOnTimeoutBeforeSuspend()) || endpointDefinition.isRetriesOnTimeoutBeforeSuspendDynamic()) { + if (isPositiveNumber(endpointDefinition.getRetriesOnTimeoutBeforeSuspend()) || endpointDefinition.isRetriesOnTimeoutBeforeSuspendDynamic()) { OMElement retries = fac.createOMElement( org.apache.synapse.config.xml.XMLConfigConstants.RETRIES_BEFORE_SUSPENSION, SynapseConstants.SYNAPSE_OMNAMESPACE); @@ -227,7 +229,7 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, markAsTimedout.addChild(retries); } - if (isStringPositiveNumber(endpointDefinition.getRetryDurationOnTimeout()) || endpointDefinition.isRetryDurationOnTimeoutDynamic()) { + if (isPositiveNumber(endpointDefinition.getRetryDurationOnTimeout()) || endpointDefinition.isRetryDurationOnTimeoutDynamic()) { OMElement retryDelay = fac.createOMElement( org.apache.synapse.config.xml.XMLConfigConstants.RETRY_DELAY, SynapseConstants.SYNAPSE_OMNAMESPACE); @@ -263,7 +265,7 @@ public void serializeEndpointDefinition(EndpointDefinition endpointDefinition, } } - private boolean isStringPositiveNumber(String input) { + private boolean isPositiveNumber(String input) { if (input == null || input.isEmpty()) { return false; } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java index 2c6e0edce6..bdfa3f8bd0 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java @@ -19,6 +19,7 @@ package org.apache.synapse.endpoints; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.synapse.MessageContext; @@ -592,8 +593,8 @@ public boolean isTimeoutActionDynamic() { public int getResolvedTimeoutAction(MessageContext synCtx) { int result = defaultTimeoutAction; - String timeoutActionStr = timeoutAction.evaluateValue(synCtx); - if (timeoutActionStr != null && !timeoutActionStr.trim().equalsIgnoreCase(EPConstants.NEVER)) { + String timeoutActionStr = (synCtx != null) ? timeoutAction.evaluateValue(synCtx) : timeoutAction.getKeyValue(); + if (StringUtils.isNotEmpty(timeoutActionStr) && !timeoutActionStr.trim().equalsIgnoreCase(EPConstants.NEVER)) { if (EPConstants.DISCARD.equalsIgnoreCase(timeoutActionStr)) { result = SynapseConstants.DISCARD; } else if (EPConstants.FAULT.equalsIgnoreCase(timeoutActionStr)) { @@ -664,12 +665,14 @@ public long getResolvedInitialSuspendDuration(MessageContext synCtx) { long result = defaultInitialSuspendDuration; String stringValue = ""; try { - stringValue = initialSuspendDuration.evaluateValue(synCtx); - if (stringValue != null) { + stringValue = (synCtx != null) ? initialSuspendDuration.evaluateValue(synCtx) : initialSuspendDuration.getKeyValue(); + if (StringUtils.isNotEmpty(stringValue)) { result = Long.parseLong(stringValue.trim()); } } catch (NumberFormatException e) { - log.warn("Error while evaluating initial suspend duration. The resolved value '" + stringValue + "' should be a valid number. Hence the default value '" + defaultInitialSuspendDuration + "' is used."); + log.warn("Error while evaluating initial suspend duration. The resolved value '" + + stringValue + "' should be a valid number. Hence the default value '" + + defaultInitialSuspendDuration + "' is used."); } return result; } @@ -697,12 +700,14 @@ public float getResolvedSuspendProgressionFactor(MessageContext messageContext) float result = defaultSuspendProgressionFactor; String stringValue = ""; try { - stringValue = suspendProgressionFactor.evaluateValue(messageContext); - if (stringValue != null) { + stringValue = (messageContext != null) ? suspendProgressionFactor.evaluateValue(messageContext) : suspendProgressionFactor.getKeyValue(); + if (StringUtils.isNotEmpty(stringValue)) { result = Float.parseFloat(stringValue); } } catch (NumberFormatException e) { - log.warn("Error while evaluating suspend duration progression factor. The resolved value '" + stringValue + "' should be a valid float. Hence the default value '" + defaultSuspendProgressionFactor + "' is used."); + log.warn("Error while evaluating suspend duration progression factor. The resolved value '" + + stringValue + "' should be a valid float. Hence the default value '" + + defaultSuspendProgressionFactor + "' is used."); } return result; } @@ -730,12 +735,14 @@ public long getResolvedSuspendMaximumDuration(MessageContext messageContext) { long result = defaultSuspendMaximumDuration; String stringValue = ""; try { - stringValue = suspendMaximumDuration.evaluateValue(messageContext); - if (stringValue != null) { + stringValue = (messageContext != null) ? suspendMaximumDuration.evaluateValue(messageContext) : suspendMaximumDuration.getKeyValue(); + if (StringUtils.isNotEmpty(stringValue)) { result = Long.parseLong(stringValue); } } catch (NumberFormatException e) { - log.warn("Error while evaluating suspend maximum duration. The resolved value '" + stringValue + "' should be a valid number. Hence the default value '" + defaultSuspendMaximumDuration + "' is used."); + log.warn("Error while evaluating suspend maximum duration. The resolved value '" + + stringValue + "' should be a valid number. Hence the default value '" + + defaultSuspendMaximumDuration + "' is used."); } return result; } @@ -763,12 +770,14 @@ public int getResolvedRetriesOnTimeoutBeforeSuspend(MessageContext messageContex int result = defaultRetriesOnTimeOutBeforeSuspend; String stringValue = ""; try { - stringValue = retriesOnTimeoutBeforeSuspend.evaluateValue(messageContext); - if (stringValue != null) { + stringValue = (messageContext != null) ? retriesOnTimeoutBeforeSuspend.evaluateValue(messageContext) : retriesOnTimeoutBeforeSuspend.getKeyValue(); + if (StringUtils.isNotEmpty(stringValue)) { result = Integer.parseInt(stringValue); } } catch (NumberFormatException e) { - log.warn("Error while evaluating retries before suspend [for timeouts]. The resolved value '" + stringValue + "' should be a valid number. Hence the default value '" + defaultRetriesOnTimeOutBeforeSuspend + "' is used."); + log.warn("Error while evaluating retries before suspend [for timeouts]. " + + "The resolved value '" + stringValue + "' should be a valid number. Hence the default value '" + + defaultRetriesOnTimeOutBeforeSuspend + "' is used."); } return result; } @@ -796,12 +805,14 @@ public int getResolvedRetryDurationOnTimeout(MessageContext messageContext) { int result = defaultRetryDurationOnTimeout; String stringValue = ""; try { - stringValue = retryDurationOnTimeout.evaluateValue(messageContext); - if (stringValue != null) { + stringValue = (messageContext != null) ? retryDurationOnTimeout.evaluateValue(messageContext) : retryDurationOnTimeout.getKeyValue(); + if (StringUtils.isNotEmpty(stringValue)) { result = Integer.parseInt(stringValue); } } catch (NumberFormatException e) { - log.warn("Error while evaluating retry delay for timeouts. The resolved value '" + stringValue + "' should be a valid number. Hence the default value '" + defaultRetryDurationOnTimeout + "' is used."); + log.warn("Error while evaluating retry delay for timeouts. The resolved value '" + + stringValue + "' should be a valid number. Hence the default value '" + + defaultRetryDurationOnTimeout + "' is used."); } return result; } @@ -860,8 +871,8 @@ public List getResolvedSuspendErrorCodes(MessageContext messageContext) List result = new ArrayList<>(); String stringValue = ""; try { - stringValue = suspendErrorCodes.evaluateValue(messageContext); - if (stringValue != null) { + stringValue = (messageContext != null) ? suspendErrorCodes.evaluateValue(messageContext) : suspendErrorCodes.getKeyValue(); + if (StringUtils.isNotEmpty(stringValue)) { String[] errorCodes = stringValue.split(","); result = new ArrayList(); for (String errorCode : errorCodes) { @@ -869,7 +880,8 @@ public List getResolvedSuspendErrorCodes(MessageContext messageContext) } } } catch (NumberFormatException e) { - log.warn("Error while evaluating suspend error codes. The resolved value '" + stringValue + "' should be valid numbers separated by commas."); + log.warn("Error while evaluating suspend error codes. The resolved value '" + + stringValue + "' should be valid numbers separated by commas."); } return result; } @@ -896,8 +908,8 @@ public List getResolvedTimeoutErrorCodes(MessageContext messageContext) List result = new ArrayList<>(); String stringValue = ""; try { - stringValue = timeoutErrorCodes.evaluateValue(messageContext); - if (stringValue != null) { + stringValue = (messageContext != null) ? timeoutErrorCodes.evaluateValue(messageContext) : timeoutErrorCodes.getKeyValue(); + if (StringUtils.isNotEmpty(stringValue)) { String[] errorCodes = stringValue.split(","); result = new ArrayList(); for (String errorCode : errorCodes) { @@ -905,7 +917,8 @@ public List getResolvedTimeoutErrorCodes(MessageContext messageContext) } } } catch (NumberFormatException e) { - log.warn("Error while evaluating timeout error codes. The resolved value '" + stringValue + "' should be valid numbers separated by commas."); + log.warn("Error while evaluating timeout error codes. The resolved value '" + + stringValue + "' should be valid numbers separated by commas."); } return result; } From c88988f08c607a0d6b05a61eef4bdc3710f82206 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Sun, 8 Dec 2024 22:48:36 +0530 Subject: [PATCH 24/27] add serializer tests --- .../endpoints/HTTPEndpointSerializerTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/modules/core/src/test/java/org/apache/synapse/config/xml/endpoints/HTTPEndpointSerializerTest.java b/modules/core/src/test/java/org/apache/synapse/config/xml/endpoints/HTTPEndpointSerializerTest.java index 5747924368..08fbc0c266 100644 --- a/modules/core/src/test/java/org/apache/synapse/config/xml/endpoints/HTTPEndpointSerializerTest.java +++ b/modules/core/src/test/java/org/apache/synapse/config/xml/endpoints/HTTPEndpointSerializerTest.java @@ -33,4 +33,78 @@ public void test() throws Exception { OMElement serializedResponse = HTTPEndpointSerializer.getElementFromEndpoint(endpoint); assertTrue("Endpoint not serialized!", compare(serializedResponse, inputElement)); } + + public void testCreateEndpointDefinitionWithErrorHandling() throws Exception { + + String inputXml = "" + + "" + + "" + + "1000" + + "discard" + + "" + + "" + + "403" + + "1" + + "" + + "" + + ""; + OMElement inputElement = AXIOMUtil.stringToOM(inputXml); + HTTPEndpoint endpoint = (HTTPEndpoint) HTTPEndpointFactory.getEndpointFromElement(inputElement, true, null); + OMElement serializedResponse = HTTPEndpointSerializer.getElementFromEndpoint(endpoint); + assertTrue("Endpoint definition with error handling not serialized!", compare(serializedResponse, inputElement)); + } + + public void testCreateEndpointDefinitionWithCompleteErrorHandling() throws Exception { + + String inputXml = "" + + "" + + "" + + "200" + + "discard" + + "" + + "" + + "403" + + "10" + + "10" + + "2" + + "" + + "" + + "304"+ + "10" + + "5" + + "" + + "" + + ""; + OMElement inputElement = AXIOMUtil.stringToOM(inputXml); + HTTPEndpoint endpoint = (HTTPEndpoint) HTTPEndpointFactory.getEndpointFromElement(inputElement, true, null); + OMElement serializedResponse = HTTPEndpointSerializer.getElementFromEndpoint(endpoint); + assertTrue("Endpoint definition with complete error handling not serialized!", compare(serializedResponse, inputElement)); + } + + public void testCreateEndpointDefinitionWithDynamicErrorHandling() throws Exception { + + String inputXml = "" + + "" + + "" + + "{get-property('timeoutDuration')}" + + "{get-property('timeoutAction')}" + + "" + + "" + + "{get-property('suspendErrorCodes')}" + + "{get-property('suspendInitialDuration')}" + + "{get-property('suspendMaximumDuration')}" + + "{get-property('suspendProgressionFactor')}" + + "" + + "" + + "{get-property('retryErrorCodes')}"+ + "{get-property('retryCount')}" + + "{get-property('retryDelay')}" + + "" + + "" + + ""; + OMElement inputElement = AXIOMUtil.stringToOM(inputXml); + HTTPEndpoint endpoint = (HTTPEndpoint) HTTPEndpointFactory.getEndpointFromElement(inputElement, true, null); + OMElement serializedResponse = HTTPEndpointSerializer.getElementFromEndpoint(endpoint); + assertTrue("Endpoint definition with dynamic error handling not serialized!", compare(serializedResponse, inputElement)); + } } \ No newline at end of file From 68cf96db27925a4d664d8534c708df3e03166602 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Mon, 9 Dec 2024 23:05:33 +0530 Subject: [PATCH 25/27] add method overload for onSuccess, onFault, onTimeout --- .../core/axis2/SynapseCallbackReceiver.java | 4 ++-- .../synapse/endpoints/AbstractEndpoint.java | 3 +++ .../apache/synapse/endpoints/Endpoint.java | 2 ++ .../synapse/endpoints/EndpointContext.java | 21 +++++++++++-------- .../synapse/endpoints/HTTPEndpoint.java | 6 +++--- .../senders/blocking/BlockingMsgSender.java | 2 +- 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/core/axis2/SynapseCallbackReceiver.java b/modules/core/src/main/java/org/apache/synapse/core/axis2/SynapseCallbackReceiver.java index bb9579db7b..1e4574fa29 100644 --- a/modules/core/src/main/java/org/apache/synapse/core/axis2/SynapseCallbackReceiver.java +++ b/modules/core/src/main/java/org/apache/synapse/core/axis2/SynapseCallbackReceiver.java @@ -596,14 +596,14 @@ private void handleMessage(String messageID ,MessageContext response, } return; } else { - successfulEndpoint.onSuccess(); + successfulEndpoint.onSuccess(synapseOutMsgCtx); if(failOver) { popFailOverEPFromFaultStack(synapseOutMsgCtx); } } } else if(successfulEndpoint != null) { - successfulEndpoint.onSuccess(); + successfulEndpoint.onSuccess(synapseOutMsgCtx); if(failOver) { popFailOverEPFromFaultStack(synapseOutMsgCtx); } diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java index a19a901c76..d2491a2d5d 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/AbstractEndpoint.java @@ -591,6 +591,9 @@ public void onSuccess() { // do nothing } + public void onSuccess(MessageContext messageContext) { + // do nothing + } /** * Should this mediator perform tracing? True if its explicitly asked to diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/Endpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/Endpoint.java index fc5ef86c8b..141ac0ba73 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/Endpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/Endpoint.java @@ -71,6 +71,8 @@ public interface Endpoint extends ManagedLifecycle, SynapseArtifact, Nameable { */ public void onSuccess(); + public void onSuccess(MessageContext synCtx); + /** * Returns true to indicate that the endpoint is ready to service requests * @return true if endpoint is ready to service requests diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java index 93cc211f60..a274d85628 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java @@ -337,6 +337,10 @@ private void setState(int state, MessageContext messageContext) { * Endpoint has processed a message successfully */ public void onSuccess() { + onSuccess(null); + } + + public void onSuccess(MessageContext messageContext) { if (isClustered) { Integer state = (Integer) cfgCtx.getPropertyNonReplicable(STATE_KEY); @@ -344,14 +348,14 @@ public void onSuccess() { log.info("Endpoint : " + endpointName + printEndpointAddress() + " currently " + getStateAsString() + " will now be marked active since it processed its last message"); - setState(ST_ACTIVE); + setState(ST_ACTIVE, messageContext); } } else { if (localState != ST_ACTIVE && localState != ST_OFF) { log.info("Endpoint : " + endpointName + printEndpointAddress() + " currently " + getStateAsString() + " will now be marked active since it processed its last message"); - setState(ST_ACTIVE); + setState(ST_ACTIVE, messageContext); } } } @@ -360,14 +364,9 @@ public void onSuccess() { * Endpoint failed processing a message */ public void onFault() { - log.warn("Endpoint : " + endpointName + printEndpointAddress() + - " will be marked SUSPENDED as it failed"); - setState(ST_SUSPENDED); + onFault(null); } - /** - * Endpoint failed processing a message - */ public void onFault(MessageContext messageContext) { log.warn("Endpoint : " + endpointName + printEndpointAddress() + " will be marked SUSPENDED as it failed"); @@ -378,11 +377,15 @@ public void onFault(MessageContext messageContext) { * Endpoint timeout processing a message */ public void onTimeout() { + onTimeout(null); + } + + public void onTimeout(MessageContext messageContext) { if (log.isDebugEnabled()) { log.debug("Endpoint : " + endpointName + printEndpointAddress() + " will be marked for " + "SUSPENSION due to the occurrence of one of the configured errors"); } - setState(ST_TIMEOUT); + setState(ST_TIMEOUT, messageContext); } /** diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/HTTPEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/HTTPEndpoint.java index ce53631fee..ff3e1d8b5c 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/HTTPEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/HTTPEndpoint.java @@ -70,7 +70,7 @@ public void onFault(MessageContext synCtx) { } else { // is this really a fault or a timeout/connection close etc? if (isTimeout(synCtx)) { - getContext().onTimeout(); + getContext().onTimeout(synCtx); } else if (isSuspendFault(synCtx)) { getContext().onFault(synCtx); } @@ -81,9 +81,9 @@ public void onFault(MessageContext synCtx) { super.onFault(synCtx); } - public void onSuccess() { + public void onSuccess(MessageContext synCtx) { if (getContext() != null) { - getContext().onSuccess(); + getContext().onSuccess(synCtx); } } diff --git a/modules/core/src/main/java/org/apache/synapse/message/senders/blocking/BlockingMsgSender.java b/modules/core/src/main/java/org/apache/synapse/message/senders/blocking/BlockingMsgSender.java index 82e5df42d0..a098a3708d 100644 --- a/modules/core/src/main/java/org/apache/synapse/message/senders/blocking/BlockingMsgSender.java +++ b/modules/core/src/main/java/org/apache/synapse/message/senders/blocking/BlockingMsgSender.java @@ -466,7 +466,7 @@ public void send(EndpointDefinition endpointDefinition, MessageContext synapseIn if (faultStack.peek() instanceof AbstractEndpoint) { successfulEndpoint = (AbstractEndpoint) faultStack.pop(); - successfulEndpoint.onSuccess(); + successfulEndpoint.onSuccess(synapseInMsgCtx); } if (successfulEndpoint instanceof OAuthConfiguredHTTPEndpoint) { From 213b9ec722a61850ff0094e733ac6e13d60da194 Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Mon, 9 Dec 2024 23:44:43 +0530 Subject: [PATCH 26/27] update print endpoint address --- .../synapse/config/SynapseConfigUtils.java | 32 +++++++++++++++-- .../synapse/endpoints/EndpointContext.java | 35 ++++++++++++------- .../synapse/endpoints/EndpointDefinition.java | 32 +++++------------ 3 files changed, 61 insertions(+), 38 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java b/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java index f1c9aaa59e..ead80377b0 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java +++ b/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java @@ -32,6 +32,7 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.synapse.MessageContext; import org.apache.synapse.SynapseConstants; import org.apache.synapse.SynapseException; import org.apache.synapse.aspects.AspectConfiguration; @@ -53,12 +54,10 @@ import org.xml.sax.InputSource; import javax.activation.DataHandler; -import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import javax.xml.stream.XMLInputFactory; @@ -86,7 +85,8 @@ import java.util.List; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; - +import java.util.regex.Matcher; +import java.util.regex.Pattern; @SuppressWarnings({"UnusedDeclaration"}) public class SynapseConfigUtils { @@ -895,6 +895,32 @@ public static boolean isFailSafeEnabled(String componentName) { return false; } + /** + * Replaces occurrences of a pattern in the input string with corresponding property values from the + * message context. + * + * @param input the input string containing patterns to be replaced + * @param pattern the pattern to be matched in the input string + * @param messageContext the message context containing property values + * @return the resulting string with patterns replaced by property values + */ + public static String replacePatternWithProperties(String input, Pattern pattern, MessageContext messageContext) { + Matcher matcher = pattern.matcher(input); + StringBuilder result = new StringBuilder(); + + int s = 0; + while (matcher.find()) { + String propertyName = matcher.group(1); + Object propertyValue = messageContext.getProperty(propertyName); + if (propertyValue != null) { + result.append(input.substring(s, matcher.start())); + result.append(propertyValue.toString()); + s = matcher.end(); + } + } + result.append(input.substring(s)); + return result.toString(); + } public static SynapseConfiguration getSynapseConfiguration(String tenantDomain){ return lastRegisteredSynapseConfigurationMap.get(tenantDomain); diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java index a274d85628..b2bcb8a1ea 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointContext.java @@ -230,7 +230,7 @@ private void setState(int state, MessageContext messageContext) { } if (retries <= 0) { - log.info("Endpoint : " + endpointName + printEndpointAddress() + + log.info("Endpoint : " + endpointName + printEndpointAddress(messageContext) + " has been marked for SUSPENSION," + " but no further retries remain. Thus it will be SUSPENDED."); @@ -243,7 +243,7 @@ private void setState(int state, MessageContext messageContext) { + definition.getResolvedRetryDurationOnTimeout(messageContext); Replicator.setAndReplicateState(NEXT_RETRY_TIME_KEY, nextRetry, cfgCtx); - log.warn("Endpoint : " + endpointName + printEndpointAddress() + + log.warn("Endpoint : " + endpointName + printEndpointAddress(messageContext) + " is marked as TIMEOUT and " + "will be retried : " + (retries - 1) + " more time/s after : " + new Date(nextRetry) + " until its marked SUSPENDED for failure"); @@ -295,7 +295,7 @@ private void setState(int state, MessageContext messageContext) { } if (retries <= 0) { - log.info("Endpoint : " + endpointName + printEndpointAddress() + log.info("Endpoint : " + endpointName + printEndpointAddress(messageContext) + " has been marked for SUSPENSION, " + "but no further retries remain. Thus it will be SUSPENDED."); @@ -305,7 +305,7 @@ private void setState(int state, MessageContext messageContext) { localRemainingRetries = retries - 1; localNextRetryTime = System.currentTimeMillis() + definition.getResolvedRetryDurationOnTimeout(messageContext); - log.warn("Endpoint : " + endpointName + printEndpointAddress() + log.warn("Endpoint : " + endpointName + printEndpointAddress(messageContext) + " is marked as TIMEOUT and " + "will be retried : " + localRemainingRetries + " more time/s " + "after : " + new Date(localNextRetryTime) @@ -345,14 +345,14 @@ public void onSuccess(MessageContext messageContext) { Integer state = (Integer) cfgCtx.getPropertyNonReplicable(STATE_KEY); if ((state != null) && ((state != ST_ACTIVE) && (state != ST_OFF))) { - log.info("Endpoint : " + endpointName + printEndpointAddress() + log.info("Endpoint : " + endpointName + printEndpointAddress(messageContext) + " currently " + getStateAsString() + " will now be marked active since it processed its last message"); setState(ST_ACTIVE, messageContext); } } else { if (localState != ST_ACTIVE && localState != ST_OFF) { - log.info("Endpoint : " + endpointName + printEndpointAddress() + log.info("Endpoint : " + endpointName + printEndpointAddress(messageContext) + " currently " + getStateAsString() + " will now be marked active since it processed its last message"); setState(ST_ACTIVE, messageContext); @@ -368,7 +368,7 @@ public void onFault() { } public void onFault(MessageContext messageContext) { - log.warn("Endpoint : " + endpointName + printEndpointAddress() + + log.warn("Endpoint : " + endpointName + printEndpointAddress(messageContext) + " will be marked SUSPENDED as it failed"); setState(ST_SUSPENDED, messageContext); } @@ -382,7 +382,7 @@ public void onTimeout() { public void onTimeout(MessageContext messageContext) { if (log.isDebugEnabled()) { - log.debug("Endpoint : " + endpointName + printEndpointAddress() + " will be marked for " + + log.debug("Endpoint : " + endpointName + printEndpointAddress(messageContext) + " will be marked for " + "SUSPENSION due to the occurrence of one of the configured errors"); } setState(ST_TIMEOUT, messageContext); @@ -426,7 +426,7 @@ private void computeNextRetryTimeForSuspended(MessageContext messageContext) { localNextRetryTime = nextRetryTime; } - log.warn("Suspending endpoint : " + endpointName + printEndpointAddress() + + log.warn("Suspending endpoint : " + endpointName + printEndpointAddress(messageContext) + (notYetSuspended ? " -" : " - last suspend duration was : " + lastSuspendDuration + "ms and") + " current suspend duration is : " + nextSuspendDuration + "ms - " + @@ -616,11 +616,22 @@ public String toString() { } private String printEndpointAddress() { + return printEndpointAddress(null); + } + + private String printEndpointAddress(MessageContext messageContext) { if(this.definition != null && this.definition.getAddress() != null) { - return " with address " + MessageHelper.maskURLPassword(this.definition.getAddress()); - } else { - return " "; + String address = ""; + if (messageContext != null) { + address = this.definition.getAddress(messageContext); + } else { + address = this.definition.getAddress(); + } + if (address != null) { + return " with address " + MessageHelper.maskURLPassword(address); + } } + return " "; } /** diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java index bdfa3f8bd0..1dad6b2eda 100755 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/EndpointDefinition.java @@ -33,7 +33,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -266,32 +265,19 @@ public String getAddress(MessageContext messageContext) { if (dynamicUrl != null && !dynamicUrl.isEmpty()) { addressString = dynamicUrl; } - boolean matches = false; - int s = 0; - Pattern pattern = Pattern.compile("\\$\\{.*?\\}"); - StringBuffer computedAddress = new StringBuffer(); - - Matcher matcher = pattern.matcher(addressString); - while (matcher.find()) { - - - Object property = messageContext.getProperty( - addressString.substring(matcher.start() + 2, matcher.end() - 1)); - if (property != null) { - computedAddress.append(addressString.substring(s, matcher.start())); - computedAddress.append(property.toString()); - s = matcher.end(); - matches = true; - } + Pattern oldPattern = Pattern.compile("\\$\\{(.*?)\\}"); + String oldProcessedAddress = SynapseConfigUtils.replacePatternWithProperties(addressString, oldPattern, messageContext); + if (!oldProcessedAddress.equals(addressString)) { + return oldProcessedAddress; } - if (!matches) { - return addressString; - } else { - computedAddress.append(addressString.substring(s, addressString.length())); - return computedAddress.toString(); + Pattern newPattern = Pattern.compile("\\{\\+?([^}]+)\\}"); + String newProcessedAddress = SynapseConfigUtils.replacePatternWithProperties(addressString, newPattern, messageContext); + if (!newProcessedAddress.equals(addressString)) { + return newProcessedAddress; } + return addressString; } /** From e79f395ea8e0be499073b3be572ee54ed8b9afad Mon Sep 17 00:00:00 2001 From: chathurangaj Date: Tue, 10 Dec 2024 20:42:05 +0530 Subject: [PATCH 27/27] address reviewed changes --- .../endpoints/EndpointDefinitionFactory.java | 6 +- .../synapse/endpoints/HttpEndpointTest.java | 66 ++++++++++++++----- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java index 59ed6ff85d..c4f36618ed 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/EndpointDefinitionFactory.java @@ -167,7 +167,7 @@ public EndpointDefinition createDefinition(OMElement elem) { if (timeoutDurationValue.getKeyValue() != null) { Long.parseLong(timeoutDurationValue.getKeyValue()); } - definition.setTimeoutDuration(valueFactory.createTextValue(duration)); + definition.setTimeoutDuration(timeoutDurationValue); } catch (NumberFormatException e) { handleException("Endpoint timeout duration expected as a " + "number but was not a number"); @@ -242,7 +242,7 @@ public EndpointDefinition createDefinition(OMElement elem) { if (retryDelayValue.getKeyValue() != null) { Integer.parseInt(retryDelayValue.getKeyValue()); } - definition.setRetryDurationOnTimeout(valueFactory.createTextValue(retryDelay)); + definition.setRetryDurationOnTimeout(retryDelayValue); } catch (NumberFormatException e) { handleException("The retry delay for timeouts should be specified " + "as a valid number : " + retryDelay.getText(), e); @@ -289,7 +289,7 @@ public EndpointDefinition createDefinition(OMElement elem) { } } } - definition.setSuspendErrorCodes(valueFactory.createTextValue(suspendCodes)); + definition.setSuspendErrorCodes(suspendErrorCodesValue); } OMElement initialDuration = suspendOnFailure.getFirstChildWithName(new QName( diff --git a/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java b/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java index c2a9cf06f9..8bbf2ce205 100644 --- a/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java +++ b/modules/core/src/test/java/org/apache/synapse/endpoints/HttpEndpointTest.java @@ -162,13 +162,21 @@ public void testSetDynamicTimeoutAction() throws XMLStreamException, AxisFault { HTTPEndpointFactory httpEndpointFactory = new HTTPEndpointFactory(); OMElement omElement = AXIOMUtil.stringToOM( - "12{$ctx:timeoutAction}"); + "" + + "" + + "" + + "12" + + "{$ctx:timeoutAction}" + + "" + + ""); EndpointDefinition ep = httpEndpointFactory.createEndpointDefinition(omElement); HTTPEndpoint httpEndpoint = new HTTPEndpoint(); httpEndpoint.setDefinition(ep); MessageContext messageContext = createMessageContext(); messageContext.setProperty("timeoutAction", "discard"); - Assert.assertEquals(httpEndpoint.getDefinition().getResolvedTimeoutAction(messageContext), SynapseConstants.DISCARD); + Assert.assertEquals( + httpEndpoint.getDefinition().getResolvedTimeoutAction(messageContext), SynapseConstants.DISCARD); } @Test @@ -176,7 +184,19 @@ public void testSetSuspendOnFailureInitialDuration() throws XMLStreamException, HTTPEndpointFactory httpEndpointFactory = new HTTPEndpointFactory(); OMElement omElement = AXIOMUtil.stringToOM( - "10100010"); + "" + + "" + + "10" + + "" + + "" + + "1000" + + "1" + + "" + + "" + + "0" + + "" + + ""); EndpointDefinition ep1 = httpEndpointFactory.createEndpointDefinition(omElement); Assert.assertEquals(ep1.getRetriesOnTimeoutBeforeSuspend(), "0"); Assert.assertEquals(ep1.getInitialSuspendDuration(), "1000"); @@ -187,20 +207,28 @@ public void testDefaultErrorHandlingValues() throws XMLStreamException, AxisFaul HTTPEndpointFactory httpEndpointFactory = new HTTPEndpointFactory(); OMElement omElement = AXIOMUtil.stringToOM( - "\n" + - " "); + ""); EndpointDefinition ep = httpEndpointFactory.createEndpointDefinition(omElement); HTTPEndpoint httpEndpoint = new HTTPEndpoint(); httpEndpoint.setDefinition(ep); MessageContext messageContext = createMessageContext(); - Assert.assertEquals("Default timeout action should be 100", 100, httpEndpoint.getDefinition().getResolvedTimeoutAction(messageContext)); - Assert.assertEquals("Default initial suspend duration should be -1", -1, httpEndpoint.getDefinition().getResolvedInitialSuspendDuration(messageContext)); - Assert.assertEquals("Default maximum suspend duration should be 0", Long.MAX_VALUE, httpEndpoint.getDefinition().getResolvedSuspendMaximumDuration(messageContext)); - Assert.assertEquals("Default suspend progression factor should be 1", 1f, httpEndpoint.getDefinition().getResolvedSuspendProgressionFactor(messageContext), 0.0f); - Assert.assertEquals("Default suspend error codes should be empty", new ArrayList<>(), httpEndpoint.getDefinition().getResolvedSuspendErrorCodes(messageContext)); - Assert.assertEquals("Default timeout error codes should be empty", new ArrayList<>(), httpEndpoint.getDefinition().getResolvedTimeoutErrorCodes(messageContext)); - Assert.assertEquals("Default retries on timeout before suspend should be 0", 0, httpEndpoint.getDefinition().getResolvedRetriesOnTimeoutBeforeSuspend(messageContext)); - Assert.assertEquals("Default retry duration on timeout should be 0", 0, httpEndpoint.getDefinition().getResolvedRetryDurationOnTimeout(messageContext)); + Assert.assertEquals("Default timeout action should be 100", 100, + httpEndpoint.getDefinition().getResolvedTimeoutAction(messageContext)); + Assert.assertEquals("Default initial suspend duration should be -1", -1, + httpEndpoint.getDefinition().getResolvedInitialSuspendDuration(messageContext)); + Assert.assertEquals("Default maximum suspend duration should be 0", Long.MAX_VALUE, + httpEndpoint.getDefinition().getResolvedSuspendMaximumDuration(messageContext)); + Assert.assertEquals("Default suspend progression factor should be 1", 1f, + httpEndpoint.getDefinition().getResolvedSuspendProgressionFactor(messageContext), 0.0f); + Assert.assertEquals("Default suspend error codes should be empty", new ArrayList<>(), + httpEndpoint.getDefinition().getResolvedSuspendErrorCodes(messageContext)); + Assert.assertEquals("Default timeout error codes should be empty", new ArrayList<>(), + httpEndpoint.getDefinition().getResolvedTimeoutErrorCodes(messageContext)); + Assert.assertEquals("Default retries on timeout before suspend should be 0", 0, + httpEndpoint.getDefinition().getResolvedRetriesOnTimeoutBeforeSuspend(messageContext)); + Assert.assertEquals("Default retry duration on timeout should be 0", 0, + httpEndpoint.getDefinition().getResolvedRetryDurationOnTimeout(messageContext)); } @Test @@ -208,7 +236,8 @@ public void testSetSuspendErrorCodes() throws XMLStreamException, AxisFault { HTTPEndpointFactory httpEndpointFactory = new HTTPEndpointFactory(); OMElement omElement = AXIOMUtil.stringToOM( - "\n" + + "\n" + " \n" + " {$ctx:suspendErrorCodes}\n" + " -1\n" + @@ -227,7 +256,8 @@ public void testSetSuspendErrorCodes() throws XMLStreamException, AxisFault { List actualSuspendErrorCodes = new ArrayList<>(); actualSuspendErrorCodes.add(101503); actualSuspendErrorCodes.add(101504); - List expectedSuspendErrorCodes = httpEndpoint.getDefinition().getResolvedSuspendErrorCodes(messageContext); + List expectedSuspendErrorCodes = + httpEndpoint.getDefinition().getResolvedSuspendErrorCodes(messageContext); Assert.assertTrue(expectedSuspendErrorCodes.equals(actualSuspendErrorCodes)); } @@ -236,7 +266,8 @@ public void testSetTimeoutErrorCodes() throws XMLStreamException, AxisFault { HTTPEndpointFactory httpEndpointFactory = new HTTPEndpointFactory(); OMElement omElement = AXIOMUtil.stringToOM( - "\n" + + "\n" + " \n" + " {$ctx:timeoutErrorCodes}\n" + " 10\n" + @@ -251,7 +282,8 @@ public void testSetTimeoutErrorCodes() throws XMLStreamException, AxisFault { List actualTimeoutErrorCodes = new ArrayList<>(); actualTimeoutErrorCodes.add(101503); actualTimeoutErrorCodes.add(101504); - List expectedTimeoutErrorCodes = httpEndpoint.getDefinition().getResolvedTimeoutErrorCodes(messageContext); + List expectedTimeoutErrorCodes = + httpEndpoint.getDefinition().getResolvedTimeoutErrorCodes(messageContext); Assert.assertTrue(expectedTimeoutErrorCodes.equals(actualTimeoutErrorCodes)); }