Skip to content

Commit

Permalink
update print endpoint address
Browse files Browse the repository at this point in the history
  • Loading branch information
chathuranga-jayanath-99 committed Dec 10, 2024
1 parent 68cf96d commit 213b9ec
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand All @@ -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");
Expand Down Expand Up @@ -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.");

Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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 - " +
Expand Down Expand Up @@ -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 " ";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 213b9ec

Please sign in to comment.