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

Java: remove URL #41

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Changes from all commits
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
@@ -1,16 +1,13 @@
package com.scylladb.alternator;

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.net.URISyntaxException;
import java.net.MalformedURLException;
import java.net.HttpURLConnection;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
Expand Down Expand Up @@ -81,18 +78,16 @@ public URI nextAsURI() {
try {
return new URI(alternatorScheme, null, nextNode(), alternatorPort, "", null, null);
} catch (URISyntaxException e) {
// Can't happen with the empty path and other nulls we used above...
logger.log(Level.WARNING, "nextAsURI", e);
return null;
}
}

public URL nextAsURL(String file) {
public URI nextAsURI(String file) {
try {
return new URL(alternatorScheme, nextNode(), alternatorPort, file);
} catch (MalformedURLException e) {
// Can only happen if alternatorScheme is an unknown one.
logger.log(Level.WARNING, "nextAsURL", e);
return new URI(alternatorScheme, "", nextNode(), alternatorPort, file, "", "");
} catch (URISyntaxException e) {
logger.log(Level.WARNING, "nextAsURI", e);
return null;
}
}
Expand All @@ -106,11 +101,11 @@ private static String streamToString(java.io.InputStream is) {

private void updateLiveNodes() {
List<String> newHosts = new ArrayList<>();
URL url = nextAsURL("/localnodes");
URI uri = nextAsURI("/localnodes");
try {
// Note that despite this being called HttpURLConnection, it actually
// supports HTTPS as well.
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection();

Choose a reason for hiding this comment

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

note: Now if nextAsUri is provided with invalid scheme for URL we'll get MalformedURLException here instead. It's handled by catch but just wanted to make sure that's intended.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for pointing this out, i will address it in next PRs

conn.setRequestMethod("GET");
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
Expand All @@ -126,7 +121,7 @@ private void updateLiveNodes() {
}
}
} catch (IOException e) {
logger.log(Level.FINE, "Request failed: " + url, e);
logger.log(Level.FINE, "Request failed: " + uri, e);
}
if (!newHosts.isEmpty()) {
liveNodes.set(newHosts);
Expand Down