Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

W-17286858 | failover brokerUrl format #630

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.jms.ConnectionFactory;

Expand Down Expand Up @@ -136,11 +134,7 @@ private String setPropertiesInURL(String brokerURL, String factoryClass,
ActiveMQConnectionFactoryUtil.isVerifyHostnameValidVersion(getActiveMqClientVersion(factoryClass));
boolean isFailOverURl = brokerURL.contains("failover");
if (isFailOverURl && verifyHostNameCheck) {
String failoverUrl = brokerURL.substring("failover:(".length(), brokerURL.length() - 1);
String addedVerifyHostName = Arrays.stream(failoverUrl.split(","))
.map(element -> element + "?" + VERIFY_HOSTNAME + "=" + factoryConfiguration.getVerifyHostName())
.collect(Collectors.joining(","));
brokerURL = "failover:(" + addedVerifyHostName + ")";
brokerURL = ActiveMQConnectionFactoryUtil.brokerUrlFormat(brokerURL, factoryConfiguration.getVerifyHostName());
}
URI brokerURI = createURI(brokerURL);
Map<String, String> map = (brokerURI.getQuery() != null) ? URISupport.parseQuery(brokerURI.getQuery()) : new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
*/
package org.mule.extensions.jms.internal.util;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class ActiveMQConnectionFactoryUtil {

private static final int MAJOR_VERSION_THRESHOLD = 5;
private static final int MINOR_VERSION_THRESHOLD = 15;
private static final int PATCH_VERSION_THRESHOLD = 6;
private static final String REX_PATTER = "(\\d+)\\.(\\d+)\\.(\\d+)";
private static final String HOSTS_MATCHER = "\\((.*?)\\)" ;
private static final String VERIFY_HOSTNAME = "socket.verifyHostName";

/**
* Utility that helps us determine if the activeMQ client version is lower than the limit version to configure the VerifyHostName parameter
Expand Down Expand Up @@ -43,4 +47,25 @@ public static boolean isVerifyHostnameValidVersion(String activeMqClientVersion)
}
return patchVersion >= PATCH_VERSION_THRESHOLD;
}

public static String brokerUrlFormat(String brokerURL, boolean verifyHostName) {
acurci-at-mulesoft marked this conversation as resolved.
Show resolved Hide resolved
int parametersIndex = brokerURL.indexOf("?");
String queryParameters = "";
if (parametersIndex != -1) {
queryParameters = brokerURL.substring(parametersIndex);
}
Pattern pattern = Pattern.compile(HOSTS_MATCHER);
Matcher matcher = pattern.matcher(brokerURL);
if (matcher.find()) {
String failoverUrl = matcher.group(1);
String addedVerifyHostName = Arrays.stream(failoverUrl.split(","))
.map(element -> element + "?" + VERIFY_HOSTNAME + "=" + verifyHostName)
.collect(Collectors.joining(","));
brokerURL = "failover:(" + addedVerifyHostName + ")";
}
if (!queryParameters.isEmpty()) {
brokerURL += queryParameters;
}
return brokerURL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import org.junit.Test;
import org.mule.extensions.jms.internal.util.ActiveMQConnectionFactoryUtil;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Expand All @@ -18,6 +20,23 @@ public void testVersionValid() {
assertFalse(ActiveMQConnectionFactoryUtil.isVerifyHostnameValidVersion("5.14.5"));
assertFalse(ActiveMQConnectionFactoryUtil.isVerifyHostnameValidVersion("4.10.5"));
assertTrue(ActiveMQConnectionFactoryUtil.isVerifyHostnameValidVersion("5.15.6"));
assertTrue(ActiveMQConnectionFactoryUtil.isVerifyHostnameValidVersion("5.18.3"));
assertTrue(ActiveMQConnectionFactoryUtil.isVerifyHostnameValidVersion("6.0.0"));
}

tomastoloza marked this conversation as resolved.
Show resolved Hide resolved
@Test
public void testUrlFormatWithQueryParameters() {
assertEquals(null,
"failover:(ssl://localhost:61616?socket.verifyHostName=false,ssl://localhost:61616?socket.verifyHostName=false)?maxReconnectAttempts=5&useExponentialBackOff=false&randomize=false&jms.prefetchPolicy.all=5",
ActiveMQConnectionFactoryUtil.brokerUrlFormat(
"failover:(ssl://localhost:61616,ssl://localhost:61616)?maxReconnectAttempts=5&useExponentialBackOff=false&randomize=false&jms.prefetchPolicy.all=5",
false));
}

@Test
public void testUrlFormatWithoutQueryParameters() {
assertEquals(null,
"failover:(ssl://localhost:61616?socket.verifyHostName=false,ssl://localhost:61616?socket.verifyHostName=false)",
ActiveMQConnectionFactoryUtil.brokerUrlFormat("failover:(ssl://localhost:61616,ssl://localhost:61616)", false));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouln't we add a test to verify the behavior when the verifyHostName parameter is true?
What would it happen if I tests the following scenarios?:

  • ActiveMQConnectionFactoryUtil.brokerUrlFormat(brokerURL, true)
  • ActiveMQConnectionFactoryUtil.brokerUrlFormat("", true)
  • ActiveMQConnectionFactoryUtil.brokerUrlFormat(null, true)
  • "failover:(ssl://localhost:61616?socket.verifyHostName=false,ssl://localhost:61616)?maxReconnectAttempts=5"
  • "failover:( ssl://localhost:61616, ssl://localhost:61616 )?maxReconnectAttempts=5"
  • "failover:(ssl://localhost:61616)?maxReconnectAttempts=5"
  • "failover:( invalidURL )?maxReconnectAttempts=5"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}