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

feat: use an enhanced CypherAPI to refactor it #2143

Merged
merged 16 commits into from
Mar 15, 2023
Merged
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 @@ -30,6 +30,7 @@

@ThreadSafe
public final class CypherManager {
lynnbond marked this conversation as resolved.
Show resolved Hide resolved

private final String configurationFile;
private YAMLConfiguration configuration;

Expand All @@ -49,30 +50,28 @@ public CypherClient getClient(String userName, String password) {
E.checkArgument(password != null && !password.isEmpty(),
"The password parameter can't be null or empty");

//TODO: Need to cache the client and make it hold the connection.
// TODO: Need to cache the client and make it hold the connection.
return new CypherClient(userName, password, this::cloneConfig);
}

public CypherClient getClient(String token) {
E.checkArgument(token != null && !token.isEmpty(),
"The token parameter can't be null or empty");

//TODO: Need to cache the client and make it hold the connection.
// TODO: Need to cache the client and make it hold the connection.
return new CypherClient(token, this::cloneConfig);
}

private Configuration cloneConfig() {
if (this.configuration == null) {
this.configuration = loadYaml(this.configurationFile);
}

return (Configuration) this.configuration.clone();
}

private static YAMLConfiguration loadYaml(String configurationFile) {
File yamlFile = getConfigFile(configurationFile);
YAMLConfiguration yaml;

try {
Reader reader = new FileReader(yamlFile);
yaml = new YAMLConfiguration();
Expand All @@ -81,27 +80,22 @@ private static YAMLConfiguration loadYaml(String configurationFile) {
throw new RuntimeException(String.format("Failed to load configuration file," +
" the file at '%s'.", configurationFile), e);
}

return yaml;
}

private static File getConfigFile(String configurationFile) {
final File systemFile = new File(configurationFile);

File systemFile = new File(configurationFile);
if (!systemFile.exists()) {
final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
final URL resource = currentClassLoader.getResource(configurationFile);
ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
URL resource = currentClassLoader.getResource(configurationFile);
assert resource != null;
final File resourceFile = new File(resource.getFile());

File resourceFile = new File(resource.getFile());
if (!resourceFile.exists()) {
throw new IllegalArgumentException(String.format("Configuration file at '%s' does" +
" not exist", configurationFile));
}
return resourceFile;

}

return systemFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public static void initAdminUserIfNeeded(String confFile) throws Exception {
}

private class TokenSaslAuthenticator implements SaslNegotiator {

private static final byte NUL = 0;
lynnbond marked this conversation as resolved.
Show resolved Hide resolved
private String username;
private String password;
Expand Down Expand Up @@ -261,16 +262,15 @@ private void decode(byte[] bytes) throws AuthenticationException {
int end = bytes.length;

for (int i = bytes.length - 1; i >= 0; i--) {
if (bytes[i] == NUL) {
if (this.password == null) {
password = new String(Arrays.copyOfRange(bytes, i + 1, end),
StandardCharsets.UTF_8);
} else if (this.username == null) {
username = new String(Arrays.copyOfRange(bytes, i + 1, end),
StandardCharsets.UTF_8);
}
end = i;
if (bytes[i] != NUL) continue;
lynnbond marked this conversation as resolved.
Show resolved Hide resolved
if (this.password == null) {
password = new String(Arrays.copyOfRange(bytes, i + 1, end),
StandardCharsets.UTF_8);
} else if (this.username == null) {
username = new String(Arrays.copyOfRange(bytes, i + 1, end),
StandardCharsets.UTF_8);
}
end = i;
}

if (this.username == null) {
Expand Down