Skip to content

Commit

Permalink
Merge pull request apache#44 from rafael-telles/implement-case-insens…
Browse files Browse the repository at this point in the history
…itive

Implement case insensitive for the property keys
  • Loading branch information
jvictorhuguenin authored Aug 3, 2022
2 parents 1da5ba8 + 6eb3cfa commit 220e844
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public ArrowFlightConnection connect(final String url, final Properties info)
this,
factory,
url,
properties,
lowerCasePropertyKeys(properties),
new RootAllocator(Long.MAX_VALUE));
} catch (final FlightRuntimeException e) {
throw new SQLException("Failed to connect.", e);
Expand Down Expand Up @@ -251,4 +251,10 @@ private Map<Object, Object> getUrlsArgs(String url)

return resultMap;
}

private Properties lowerCasePropertyKeys(final Properties properties) {
final Properties resultProperty = new Properties();
properties.forEach((k, v) -> resultProperty.put(k.toString().toLowerCase(), v));
return resultProperty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,13 @@ public enum ArrowFlightConnectionProperty implements ConnectionProperty {
*/
public Object get(final Properties properties) {
Preconditions.checkNotNull(properties, "Properties cannot be null.");
Preconditions.checkState(
properties.containsKey(camelName) || !required,
format("Required property not provided: <%s>.", this));
return properties.getOrDefault(camelName, defaultValue);
Object value = properties.get(camelName.toLowerCase());
if (required) {
Preconditions.checkNotNull(value, format("Required property not provided: <%s>.", this));
return value;
} else {
return value != null ? value : defaultValue;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ public class UrlParser {
*/
public static Map<String, String> parse(String url, String separator) {
Map<String, String> resultMap = new HashMap<>();
String[] keyValues = url.split(separator);
if (url != null) {
String[] keyValues = url.split(separator);

for (String keyValue : keyValues) {
int separatorKey = keyValue.indexOf("="); // Find the first equal sign to split key and value.
String key = keyValue.substring(0, separatorKey);
String value = "";
if (!keyValue.endsWith("=")) { // Avoid crashes for empty values.
value = keyValue.substring(separatorKey + 1);
for (String keyValue : keyValues) {
int separatorKey = keyValue.indexOf("="); // Find the first equal sign to split key and value.
if (separatorKey != -1) { // Avoid crashes when not finding an equal sign in the property value.
String key = keyValue.substring(0, separatorKey);
String value = keyValue.substring(separatorKey + 1);
resultMap.put(key, value);
}
}
resultMap.put(key, value);
}

return resultMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Collection;
import java.util.Map;

import java.util.Properties;
import org.apache.arrow.driver.jdbc.authentication.UserPasswordAuthentication;
import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty;
import org.apache.arrow.driver.jdbc.utils.MockFlightSqlProducer;
Expand Down Expand Up @@ -116,7 +117,38 @@ public void testShouldConnectWhenProvidedWithValidUrl() throws Exception {
dataSource.getConfig().getHost() + ":" +
dataSource.getConfig().getPort() + "?" +
"useEncryption=false",
dataSource.getProperties(dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()))) {
dataSource.getProperties(dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()))) {
assert connection.isValid(300);
}
}

@Test
public void testConnectWithInsensitiveCasePropertyKeys() throws Exception {
// Get the Arrow Flight JDBC driver by providing a URL with insensitive case property keys.
final Driver driver = new ArrowFlightJdbcDriver();

try (Connection connection =
driver.connect("jdbc:arrow-flight://" +
dataSource.getConfig().getHost() + ":" +
dataSource.getConfig().getPort() + "?" +
"UseEncryptiOn=false",
dataSource.getProperties(dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()))) {
assert connection.isValid(300);
}
}

@Test
public void testConnectWithInsensitiveCasePropertyKeys2() throws Exception {
// Get the Arrow Flight JDBC driver by providing a property object with insensitive case keys.
final Driver driver = new ArrowFlightJdbcDriver();
Properties properties =
dataSource.getProperties(dataSource.getConfig().getUser(), dataSource.getConfig().getPassword());
properties.put("UseEncryptiOn", "false");

try (Connection connection =
driver.connect("jdbc:arrow-flight://" +
dataSource.getConfig().getHost() + ":" +
dataSource.getConfig().getPort(), properties)) {
assert connection.isValid(300);
}
}
Expand Down

0 comments on commit 220e844

Please sign in to comment.